Skip to content

Commit ed888cd

Browse files
committed
[tests] fix tests to build and pass, after changes
* Set most things to not require auth / JWT off.
1 parent a0baad8 commit ed888cd

File tree

7 files changed

+79
-26
lines changed

7 files changed

+79
-26
lines changed

iOS_SDK/OneSignalSDK/OneSignalOSCoreMocks/OSCoreMocks.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ import OneSignalCore
3131

3232
@objc
3333
public class OSCoreMocks: NSObject {
34-
public static func resetOperationRepo() {
35-
OSOperationRepo.sharedInstance.reset()
36-
}
34+
// TODO: Add mocks here
3735
}
3836

3937
extension OSOperationRepo {
4038
/**
4139
The Operation Repo needs to reset between tests until we dependency inject the Operation Repo,
4240
to prevent state from carrying over between tests.
4341
*/
44-
func reset() {
42+
public func reset() {
4543
deltaQueue.removeAll()
4644
executors.removeAll()
4745
deltasToExecutorMap.removeAll()

iOS_SDK/OneSignalSDK/OneSignalUserMocks/OneSignalUserMocks.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class OneSignalUserMocks: NSObject {
3636
// TODO: create mocked server responses to user requests
3737
@objc
3838
public static func reset() {
39-
OSCoreMocks.resetOperationRepo()
4039
OneSignalUserManagerImpl.sharedInstance.reset()
4140
}
4241
}
@@ -55,6 +54,7 @@ extension OneSignalUserManagerImpl {
5554
*/
5655
func reset() {
5756
identityModelRepo.reset()
57+
operationRepo.reset()
5858

5959
// Model store listeners unsubscribe to their models
6060
// User Manager start() will subscribe them

iOS_SDK/OneSignalSDK/OneSignalUserTests/Executors/UserExecutorTests.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ import OneSignalUserMocks
3737
private class Mocks {
3838
let client = MockOneSignalClient()
3939
let newRecordsState = MockNewRecordsState()
40+
let jwtConfig = OSUserJwtConfig()
4041
let userExecutor: OSUserExecutor
4142

4243
init() {
4344
OneSignalCoreImpl.setSharedClient(client)
44-
userExecutor = OSUserExecutor(newRecordsState: newRecordsState)
45+
userExecutor = OSUserExecutor(newRecordsState: newRecordsState, jwtConfig: jwtConfig)
46+
}
47+
48+
func setAuthRequired(_ required: Bool) {
49+
// Set User Manager's JWT to off, or it blocks requests in prepareForExecution
50+
OneSignalUserManagerImpl.sharedInstance.jwtConfig.isRequired = required
51+
jwtConfig.isRequired = required
4552
}
4653

4754
func createUserInstance(externalId: String) -> OSUserInternal {
@@ -75,6 +82,7 @@ final class UserExecutorTests: XCTestCase {
7582
func testCreateUser_withPushSubscription_addsToNewRecords() {
7683
/* Setup */
7784
let mocks = Mocks()
85+
mocks.setAuthRequired(false)
7886
MockUserRequests.setDefaultCreateUserResponses(with: mocks.client, externalId: userA_EUID, subscriptionId: "push-sub-id")
7987

8088
/* When */
@@ -89,6 +97,7 @@ final class UserExecutorTests: XCTestCase {
8997
func testCreateUser_withoutPushSubscription_doesNot_addToNewRecords() {
9098
/* Setup */
9199
let mocks = Mocks()
100+
mocks.setAuthRequired(false)
92101
MockUserRequests.setDefaultCreateUserResponses(with: mocks.client, externalId: userA_EUID)
93102

94103
/* When */
@@ -110,6 +119,7 @@ final class UserExecutorTests: XCTestCase {
110119
func testIdentifyUser_successfully_forcesAddToNewRecords() {
111120
/* Setup */
112121
let mocks = Mocks()
122+
mocks.setAuthRequired(false)
113123
MockUserRequests.setDefaultIdentifyUserResponses(with: mocks.client, externalId: userA_EUID, conflicted: false)
114124

115125
/* When */
@@ -135,6 +145,7 @@ final class UserExecutorTests: XCTestCase {
135145
func testIdentifyUserSuccessful_butUserHasChangedSince_doesNotAddToNewRecords() {
136146
/* Setup */
137147
let mocks = Mocks()
148+
mocks.setAuthRequired(false)
138149
MockUserRequests.setDefaultIdentifyUserResponses(with: mocks.client, externalId: userA_EUID, conflicted: false)
139150

140151
/* When */
@@ -156,6 +167,7 @@ final class UserExecutorTests: XCTestCase {
156167
func testIdentifyUser_withConflict_addsToNewRecords() {
157168
/* Setup */
158169
let mocks = Mocks()
170+
mocks.setAuthRequired(false)
159171
let user = mocks.setUserManagerInternalUser(externalId: userB_EUID)
160172

161173
let anonIdentityModel = OSIdentityModel(aliases: [OS_ONESIGNAL_ID: userA_OSID], changeNotifier: OSEventProducer())
@@ -177,6 +189,7 @@ final class UserExecutorTests: XCTestCase {
177189
func testIdentifyUserWithConflict_butUserHasChangedSince_doesNot_addToNewRecords() {
178190
/* Setup */
179191
let mocks = Mocks()
192+
mocks.setAuthRequired(false)
180193

181194
let user = mocks.setUserManagerInternalUser(externalId: "new-eid")
182195
let anonIdentityModel = OSIdentityModel(aliases: [OS_ONESIGNAL_ID: userA_OSID], changeNotifier: OSEventProducer())

iOS_SDK/OneSignalSDK/OneSignalUserTests/OneSignalUserObjcTests.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ - (void)testSendPurchases {
6161
};
6262
[arrayOfPurchases addObject:purchase2];
6363

64+
// Set JWT to off, before accessing the User Manager
65+
[OneSignalUserManagerImpl.sharedInstance setRequiresUserAuth:false];
6466
[OneSignalUserManagerImpl.sharedInstance sendPurchases:arrayOfPurchases];
6567

6668
// Run background threads

iOS_SDK/OneSignalSDK/OneSignalUserTests/OneSignalUserTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ final class OneSignalUserTests: XCTestCase {
7878
MockUserRequests.setDefaultCreateAnonUserResponses(with: client)
7979
OneSignalCoreImpl.setSharedClient(client)
8080

81+
// Set JWT to off, before accessing the User Manager
82+
OneSignalUserManagerImpl.sharedInstance.setRequiresUserAuth(false)
83+
8184
// Increase flush interval to allow all the updates to batch
82-
OSOperationRepo.sharedInstance.pollIntervalMilliseconds = 300
85+
OneSignalUserManagerImpl.sharedInstance.operationRepo.pollIntervalMilliseconds = 300
8386

8487
// Wait to let any pending flushes in the Operation Repo to run
8588
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.1)

iOS_SDK/OneSignalSDK/OneSignalUserTests/SwitchUserIntegrationTests.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ final class SwitchUserIntegrationTests: XCTestCase {
4040

4141
OneSignalCoreImpl.setSharedClient(client)
4242

43+
// Set JWT to off, before accessing the User Manager
44+
OneSignalUserManagerImpl.sharedInstance.setRequiresUserAuth(false)
45+
4346
/* When */
4447

4548
// 1. Login to user A and add tag
@@ -108,6 +111,9 @@ final class SwitchUserIntegrationTests: XCTestCase {
108111
// Returns mocked user data to test hydration
109112
MockUserRequests.setDefaultFetchUserResponseForHydration(with: client, externalId: userA_EUID)
110113

114+
// Set JWT to off, before accessing the User Manager
115+
OneSignalUserManagerImpl.sharedInstance.setRequiresUserAuth(false)
116+
111117
/* When */
112118

113119
// 1. Anonymous user
@@ -215,6 +221,9 @@ final class SwitchUserIntegrationTests: XCTestCase {
215221
MockUserRequests.setAddAliasesResponse(with: client, aliases: ["alias_b": "id_b"])
216222
MockUserRequests.setAddEmailResponse(with: client, email: "[email protected]")
217223

224+
// Set JWT to off, before accessing the User Manager
225+
OneSignalUserManagerImpl.sharedInstance.setRequiresUserAuth(false)
226+
218227
/* When */
219228

220229
// 1. Anonymous user starts
@@ -318,8 +327,12 @@ final class SwitchUserIntegrationTests: XCTestCase {
318327
let client = MockOneSignalClient()
319328
OneSignalCoreImpl.setSharedClient(client)
320329

330+
// Set JWT to off, before accessing the User Manager
331+
OneSignalUserManagerImpl.sharedInstance.setRequiresUserAuth(false)
332+
321333
// Increase flush interval to allow all the updates to batch
322-
OSOperationRepo.sharedInstance.pollIntervalMilliseconds = 300
334+
OneSignalUserManagerImpl.sharedInstance.operationRepo.pollIntervalMilliseconds = 300
335+
323336
// Wait to let any pending flushes in the Operation Repo to run
324337
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.3)
325338

iOS_SDK/OneSignalSDK/OneSignalUserTests/UserConcurrencyTests.swift

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ final class UserConcurrencyTests: XCTestCase {
5757
/* Setup */
5858
OneSignalCoreImpl.setSharedClient(MockOneSignalClient())
5959

60+
// Set JWT to off, before accessing the User Manager
61+
OneSignalUserManagerImpl.sharedInstance.setRequiresUserAuth(false)
62+
6063
/* When */
6164

6265
// 1. Enqueue 10 Deltas to the Operation Repo
@@ -68,7 +71,7 @@ final class UserConcurrencyTests: XCTestCase {
6871
for _ in 1...4 {
6972
DispatchQueue.global().async {
7073
print("🧪 flushDeltaQueue on thread \(Thread.current)")
71-
OSOperationRepo.sharedInstance.addFlushDeltaQueueToDispatchQueue()
74+
OneSignalUserManagerImpl.sharedInstance.operationRepo.addFlushDeltaQueueToDispatchQueue()
7275
}
7376
}
7477

@@ -82,6 +85,7 @@ final class UserConcurrencyTests: XCTestCase {
8285
This test reproduces a crash in the Subscription Executor.
8386
It is possible for two threads to modify and cache queues concurrently.
8487
*/
88+
// TODO: revisit this test once subscriptions are addressed for JWT
8589
func testSubscriptionExecutorConcurrency() throws {
8690
/* Setup */
8791

@@ -92,18 +96,20 @@ final class UserConcurrencyTests: XCTestCase {
9296
)
9397
OneSignalCoreImpl.setSharedClient(client)
9498

95-
let executor = OSSubscriptionOperationExecutor(newRecordsState: OSNewRecordsState())
96-
OSOperationRepo.sharedInstance.addExecutor(executor)
99+
let jwtConfig = OSUserJwtConfig()
100+
let executor = OSSubscriptionOperationExecutor(newRecordsState: OSNewRecordsState(), jwtConfig: jwtConfig)
101+
let operationRepo = OSOperationRepo(jwtConfig: jwtConfig)
102+
operationRepo.addExecutor(executor)
97103

98104
/* When */
99105

100106
DispatchQueue.concurrentPerform(iterations: 50) { _ in
101107
// 1. Enqueue Remove Subscription Deltas to the Operation Repo
102-
OSOperationRepo.sharedInstance.enqueueDelta(OSDelta(name: OS_REMOVE_SUBSCRIPTION_DELTA, identityModelId: UUID().uuidString, model: OSSubscriptionModel(type: .email, address: nil, subscriptionId: UUID().uuidString, reachable: true, isDisabled: false, changeNotifier: OSEventProducer()), property: "email", value: "email"))
103-
OSOperationRepo.sharedInstance.enqueueDelta(OSDelta(name: OS_REMOVE_SUBSCRIPTION_DELTA, identityModelId: UUID().uuidString, model: OSSubscriptionModel(type: .email, address: nil, subscriptionId: UUID().uuidString, reachable: true, isDisabled: false, changeNotifier: OSEventProducer()), property: "email", value: "email"))
108+
operationRepo.enqueueDelta(OSDelta(name: OS_REMOVE_SUBSCRIPTION_DELTA, identityModelId: UUID().uuidString, model: OSSubscriptionModel(type: .email, address: nil, subscriptionId: UUID().uuidString, reachable: true, isDisabled: false, changeNotifier: OSEventProducer()), property: "email", value: "email"))
109+
operationRepo.enqueueDelta(OSDelta(name: OS_REMOVE_SUBSCRIPTION_DELTA, identityModelId: UUID().uuidString, model: OSSubscriptionModel(type: .email, address: nil, subscriptionId: UUID().uuidString, reachable: true, isDisabled: false, changeNotifier: OSEventProducer()), property: "email", value: "email"))
104110

105111
// 2. Flush Operation Repo
106-
OSOperationRepo.sharedInstance.addFlushDeltaQueueToDispatchQueue()
112+
operationRepo.addFlushDeltaQueueToDispatchQueue()
107113

108114
// 3. Simulate updating the executor's request queue from a network response
109115
executor.executeDeleteSubscriptionRequest(OSRequestDeleteSubscription(subscriptionModel: OSSubscriptionModel(type: .email, address: nil, subscriptionId: UUID().uuidString, reachable: true, isDisabled: false, changeNotifier: OSEventProducer())), inBackground: false)
@@ -117,6 +123,8 @@ final class UserConcurrencyTests: XCTestCase {
117123
// Previously caused crash: signal SIGABRT - malloc: double free for ptr
118124
// Assert that every request SDK makes has a response set, and is handled
119125
XCTAssertTrue(client.allRequestsHandled)
126+
// Ensure the requests are actually made, future proofing
127+
XCTAssertEqual(client.executedRequests.count, 200)
120128
}
121129

122130
/**
@@ -131,18 +139,22 @@ final class UserConcurrencyTests: XCTestCase {
131139
OneSignalCoreImpl.setSharedClient(client)
132140
MockUserRequests.setAddAliasesResponse(with: client, aliases: aliases)
133141

134-
let executor = OSIdentityOperationExecutor(newRecordsState: OSNewRecordsState())
135-
OSOperationRepo.sharedInstance.addExecutor(executor)
142+
// Set User Manager's JWT to off, or it blocks requests
143+
OneSignalUserManagerImpl.sharedInstance.setRequiresUserAuth(false)
144+
145+
let executor = OSIdentityOperationExecutor(newRecordsState: OSNewRecordsState(), jwtConfig: OneSignalUserManagerImpl.sharedInstance.jwtConfig)
146+
let operationRepo = OSOperationRepo(jwtConfig: OneSignalUserManagerImpl.sharedInstance.jwtConfig)
147+
operationRepo.addExecutor(executor)
136148

137149
/* When */
138150

139151
DispatchQueue.concurrentPerform(iterations: 50) { _ in
140152
// 1. Enqueue Add Alias Deltas to the Operation Repo
141-
OSOperationRepo.sharedInstance.enqueueDelta(OSDelta(name: OS_ADD_ALIAS_DELTA, identityModelId: UUID().uuidString, model: OSIdentityModel(aliases: [OS_ONESIGNAL_ID: UUID().uuidString], changeNotifier: OSEventProducer()), property: "aliases", value: aliases))
142-
OSOperationRepo.sharedInstance.enqueueDelta(OSDelta(name: OS_ADD_ALIAS_DELTA, identityModelId: UUID().uuidString, model: OSIdentityModel(aliases: [OS_ONESIGNAL_ID: UUID().uuidString], changeNotifier: OSEventProducer()), property: "aliases", value: aliases))
153+
operationRepo.enqueueDelta(OSDelta(name: OS_ADD_ALIAS_DELTA, identityModelId: UUID().uuidString, model: OSIdentityModel(aliases: [OS_ONESIGNAL_ID: UUID().uuidString], changeNotifier: OSEventProducer()), property: "aliases", value: aliases))
154+
operationRepo.enqueueDelta(OSDelta(name: OS_ADD_ALIAS_DELTA, identityModelId: UUID().uuidString, model: OSIdentityModel(aliases: [OS_ONESIGNAL_ID: UUID().uuidString], changeNotifier: OSEventProducer()), property: "aliases", value: aliases))
143155

144156
// 2. Flush Operation Repo
145-
OSOperationRepo.sharedInstance.addFlushDeltaQueueToDispatchQueue()
157+
operationRepo.addFlushDeltaQueueToDispatchQueue()
146158

147159
// 3. Simulate updating the executor's request queue from a network response
148160
executor.executeAddAliasesRequest(OSRequestAddAliases(aliases: aliases, identityModel: OSIdentityModel(aliases: [OS_ONESIGNAL_ID: UUID().uuidString], changeNotifier: OSEventProducer())), inBackground: false)
@@ -156,6 +168,8 @@ final class UserConcurrencyTests: XCTestCase {
156168
// Previously caused crash: signal SIGABRT - malloc: double free for ptr
157169
// Assert that every request SDK makes has a response set, and is handled
158170
XCTAssertTrue(client.allRequestsHandled)
171+
// Ensure the requests are actually made, future proofing
172+
XCTAssertGreaterThanOrEqual(client.executedRequests.count, 150)
159173
}
160174

161175
/**
@@ -169,21 +183,24 @@ final class UserConcurrencyTests: XCTestCase {
169183
client.fireSuccessForAllRequests = true
170184
OneSignalCoreImpl.setSharedClient(client)
171185

186+
// Set JWT to off, before accessing the User Manager
187+
OneSignalUserManagerImpl.sharedInstance.setRequiresUserAuth(false)
188+
172189
let identityModel = OSIdentityModel(aliases: [OS_ONESIGNAL_ID: UUID().uuidString], changeNotifier: OSEventProducer())
173190
OneSignalUserManagerImpl.sharedInstance.addIdentityModelToRepo(identityModel)
174191

175-
let executor = OSPropertyOperationExecutor(newRecordsState: OSNewRecordsState())
176-
OSOperationRepo.sharedInstance.addExecutor(executor)
192+
let executor = OSPropertyOperationExecutor(newRecordsState: OSNewRecordsState(), jwtConfig: OneSignalUserManagerImpl.sharedInstance.jwtConfig)
193+
OneSignalUserManagerImpl.sharedInstance.operationRepo.addExecutor(executor)
177194

178195
/* When */
179196

180197
DispatchQueue.concurrentPerform(iterations: 50) { _ in
181198
// 1. Enqueue Deltas to the Operation Repo
182-
OSOperationRepo.sharedInstance.enqueueDelta(OSDelta(name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: identityModel.modelId, model: OSPropertiesModel(changeNotifier: OSEventProducer()), property: "language", value: UUID().uuidString))
183-
OSOperationRepo.sharedInstance.enqueueDelta(OSDelta(name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: identityModel.modelId, model: OSPropertiesModel(changeNotifier: OSEventProducer()), property: "language", value: UUID().uuidString))
199+
OneSignalUserManagerImpl.sharedInstance.operationRepo.enqueueDelta(OSDelta(name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: identityModel.modelId, model: OSPropertiesModel(changeNotifier: OSEventProducer()), property: "language", value: UUID().uuidString))
200+
OneSignalUserManagerImpl.sharedInstance.operationRepo.enqueueDelta(OSDelta(name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: identityModel.modelId, model: OSPropertiesModel(changeNotifier: OSEventProducer()), property: "language", value: UUID().uuidString))
184201

185202
// 2. Flush Operation Repo
186-
OSOperationRepo.sharedInstance.addFlushDeltaQueueToDispatchQueue()
203+
OneSignalUserManagerImpl.sharedInstance.operationRepo.addFlushDeltaQueueToDispatchQueue()
187204

188205
// 3. Simulate updating the executor's request queue from a network response
189206
executor.executeUpdatePropertiesRequest(OSRequestUpdateProperties(params: ["properties": ["language": UUID().uuidString], "refresh_device_metadata": false], identityModel: identityModel), inBackground: false)
@@ -194,6 +211,8 @@ final class UserConcurrencyTests: XCTestCase {
194211

195212
/* Then */
196213
// No crash
214+
// Ensure the requests are actually made, future proofing
215+
XCTAssertGreaterThanOrEqual(client.executedRequests.count, 75)
197216
}
198217

199218
/**
@@ -213,13 +232,16 @@ final class UserConcurrencyTests: XCTestCase {
213232
let identityModel1 = OSIdentityModel(aliases: [OS_ONESIGNAL_ID: UUID().uuidString], changeNotifier: OSEventProducer())
214233
let identityModel2 = OSIdentityModel(aliases: [OS_ONESIGNAL_ID: UUID().uuidString], changeNotifier: OSEventProducer())
215234

216-
let userExecutor = OSUserExecutor(newRecordsState: OSNewRecordsState())
235+
// Set User Manager's JWT to off, or it blocks requests
236+
OneSignalUserManagerImpl.sharedInstance.setRequiresUserAuth(false)
237+
238+
let userExecutor = OSUserExecutor(newRecordsState: OSNewRecordsState(), jwtConfig: OneSignalUserManagerImpl.sharedInstance.jwtConfig)
217239

218240
/* When */
219241

220242
DispatchQueue.concurrentPerform(iterations: 50) { _ in
221243
let identifyRequest = OSRequestIdentifyUser(aliasLabel: OS_EXTERNAL_ID, aliasId: UUID().uuidString, identityModelToIdentify: identityModel1, identityModelToUpdate: identityModel2)
222-
let fetchRequest = OSRequestFetchUser(identityModel: identityModel1, aliasLabel: OS_ONESIGNAL_ID, aliasId: UUID().uuidString, onNewSession: false)
244+
let fetchRequest = OSRequestFetchUser(identityModel: identityModel1, onesignalId: UUID().uuidString, onNewSession: false)
223245

224246
// Append and execute requests simultaneously
225247
userExecutor.appendToQueue(identifyRequest)
@@ -233,6 +255,8 @@ final class UserConcurrencyTests: XCTestCase {
233255

234256
/* Then */
235257
// No crash
258+
// Ensure the requests are actually made, future proofing
259+
XCTAssertGreaterThanOrEqual(client.executedRequests.count, 75)
236260
}
237261

238262
/**

0 commit comments

Comments
 (0)