Skip to content

Commit 3789979

Browse files
committed
add tests
1 parent a8eb5db commit 3789979

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
3CC063E02B6D7F2A002BB07F /* OneSignalUserMocks.h in Headers */ = {isa = PBXBuildFile; fileRef = 3CC063DF2B6D7F2A002BB07F /* OneSignalUserMocks.h */; settings = {ATTRIBUTES = (Public, ); }; };
186186
3CC063E62B6D7F96002BB07F /* OneSignalUserMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CC063E52B6D7F96002BB07F /* OneSignalUserMocks.swift */; };
187187
3CC063EE2B6D7FE8002BB07F /* OneSignalUserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CC063ED2B6D7FE8002BB07F /* OneSignalUserTests.swift */; };
188+
B91A66287DEA4026A4DC5952 /* OSIdentityModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C1399651D1A401EB888DA77 /* OSIdentityModelTests.swift */; };
188189
3CC063EF2B6D7FE8002BB07F /* OneSignalUser.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DE69E19B282ED8060090BB3D /* OneSignalUser.framework */; };
189190
3CC890352C5BF9A7002CB4CC /* UserConcurrencyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3CC890342C5BF9A7002CB4CC /* UserConcurrencyTests.swift */; };
190191
3CC9A6342AFA1FDE008F68FD /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3CC9A6332AFA1FDD008F68FD /* PrivacyInfo.xcprivacy */; };
@@ -1439,6 +1440,7 @@
14391440
3CC063E52B6D7F96002BB07F /* OneSignalUserMocks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OneSignalUserMocks.swift; sourceTree = "<group>"; };
14401441
3CC063EB2B6D7FE8002BB07F /* OneSignalUserTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OneSignalUserTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
14411442
3CC063ED2B6D7FE8002BB07F /* OneSignalUserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OneSignalUserTests.swift; sourceTree = "<group>"; };
1443+
6C1399651D1A401EB888DA77 /* OSIdentityModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSIdentityModelTests.swift; sourceTree = "<group>"; };
14421444
3CC890342C5BF9A7002CB4CC /* UserConcurrencyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserConcurrencyTests.swift; sourceTree = "<group>"; };
14431445
3CC9A6332AFA1FDD008F68FD /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
14441446
3CC9A6352AFA26E7008F68FD /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
@@ -2422,6 +2424,7 @@
24222424
3CDE664A2BFC2A55006DA114 /* OneSignalUserTests-Bridging-Header.h */,
24232425
3CF11E3E2C6D61AC002856F5 /* Executors */,
24242426
3CC063ED2B6D7FE8002BB07F /* OneSignalUserTests.swift */,
2427+
6C1399651D1A401EB888DA77 /* OSIdentityModelTests.swift */,
24252428
3CC890342C5BF9A7002CB4CC /* UserConcurrencyTests.swift */,
24262429
3CB331672F281679000E1801 /* CustomEventsIntegrationTests.swift */,
24272430
3C67F7792BEB2B710085A0F0 /* SwitchUserIntegrationTests.swift */,
@@ -4539,6 +4542,7 @@
45394542
DE3568F22C8911EA00AF447C /* IdentityExecutorTests.swift in Sources */,
45404543
3C67F77A2BEB2B710085A0F0 /* SwitchUserIntegrationTests.swift in Sources */,
45414544
3CC063EE2B6D7FE8002BB07F /* OneSignalUserTests.swift in Sources */,
4545+
B91A66287DEA4026A4DC5952 /* OSIdentityModelTests.swift in Sources */,
45424546
3CC890352C5BF9A7002CB4CC /* UserConcurrencyTests.swift in Sources */,
45434547
DE3568F02C89067400AF447C /* SubscriptionsExecutorTests.swift in Sources */,
45444548
3CB3316A2F281692000E1801 /* OSCustomEventsExecutorTests.swift in Sources */,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Modified MIT License
3+
4+
Copyright 2026 OneSignal
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
1. The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
2. All copies of substantial portions of the Software may only be used in connection
17+
with services provided by OneSignal.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
*/
27+
28+
import XCTest
29+
import OneSignalCore
30+
@testable import OneSignalOSCore
31+
@testable import OneSignalUser
32+
33+
/// Tests for the two new JWT APIs added to `OSIdentityModel`:
34+
/// - `getValidJwt()` snapshots and returns the bearer token only when it is
35+
/// non-nil, non-empty, and not the `OS_JWT_TOKEN_INVALID` sentinel.
36+
/// - `invalidateJwtBearerToken()` performs an atomic compare-and-set to
37+
/// `OS_JWT_TOKEN_INVALID`, returning `true` only on the transition.
38+
final class OSIdentityModelTests: XCTestCase {
39+
40+
private func makeModel(token: String? = nil) -> OSIdentityModel {
41+
let model = OSIdentityModel(aliases: [:], changeNotifier: OSEventProducer())
42+
model.jwtBearerToken = token
43+
return model
44+
}
45+
46+
// MARK: - getValidJwt()
47+
48+
func testGetValidJwt_returnsNil_whenTokenIsNil() {
49+
XCTAssertNil(makeModel(token: nil).getValidJwt())
50+
}
51+
52+
func testGetValidJwt_returnsNil_whenTokenIsEmptyString() {
53+
XCTAssertNil(makeModel(token: "").getValidJwt())
54+
}
55+
56+
func testGetValidJwt_returnsNil_whenTokenIsInvalidSentinel() {
57+
XCTAssertNil(makeModel(token: OS_JWT_TOKEN_INVALID).getValidJwt())
58+
}
59+
60+
func testGetValidJwt_returnsToken_whenTokenIsValid() {
61+
let token = "eyJhbGciOiJFUzI1NiJ9.payload.sig"
62+
XCTAssertEqual(makeModel(token: token).getValidJwt(), token)
63+
}
64+
65+
// MARK: - invalidateJwtBearerToken()
66+
67+
func testInvalidate_returnsTrueOnFirstTransition_andSetsInvalidSentinel() {
68+
let model = makeModel(token: "valid-token")
69+
70+
XCTAssertTrue(model.invalidateJwtBearerToken())
71+
XCTAssertEqual(model.jwtBearerToken, OS_JWT_TOKEN_INVALID)
72+
}
73+
74+
func testInvalidate_returnsFalseWhenAlreadyInvalid() {
75+
let model = makeModel(token: "valid-token")
76+
_ = model.invalidateJwtBearerToken()
77+
78+
XCTAssertFalse(model.invalidateJwtBearerToken())
79+
XCTAssertEqual(model.jwtBearerToken, OS_JWT_TOKEN_INVALID)
80+
}
81+
82+
func testInvalidate_returnsTrueWhenStartingFromNil() {
83+
// Defensive: nil → INVALID is still a real transition, the model lands
84+
// on the sentinel and the caller can fire fireJwtExpired once.
85+
let model = makeModel(token: nil)
86+
87+
XCTAssertTrue(model.invalidateJwtBearerToken())
88+
XCTAssertEqual(model.jwtBearerToken, OS_JWT_TOKEN_INVALID)
89+
}
90+
}

0 commit comments

Comments
 (0)