@@ -141,4 +141,82 @@ final class PropertyExecutorTests: XCTestCase {
141141 XCTAssertTrue ( mocks. client. hasExecutedRequestOfType ( OSRequestUpdateProperties . self) )
142142 XCTAssertTrue ( invalidatedCallbackWasCalled)
143143 }
144+
145+ func testUpdateRequests_Retry_OnTokenUpdate( ) {
146+
147+ /* Setup */
148+ let mocks = Mocks ( )
149+ mocks. setAuthRequired ( true )
150+ OneSignalUserManagerImpl . sharedInstance. operationRepo. paused = true
151+
152+ let user = mocks. setUserManagerInternalUser ( externalId: userA_EUID, onesignalId: userA_OSID)
153+ user. identityModel. jwtBearerToken = userA_InvalidJwtToken
154+
155+ // We need to use the user manager's executor because the onJWTUpdated callback won't fire on the mock executor
156+ let executor = OneSignalUserManagerImpl . sharedInstance. propertyExecutor!
157+
158+ let tags = [ " testUserA " : " true " ]
159+ MockUserRequests . setUnauthorizedUpdatePropertiesFailureResponses ( with: mocks. client, tags: tags)
160+ executor. enqueueDelta ( OSDelta ( name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: user. identityModel. modelId, model: OSPropertiesModel ( changeNotifier: OSEventProducer ( ) ) , property: " tags " , value: tags) )
161+
162+ var invalidatedCallbackWasCalled = false
163+ OneSignalUserManagerImpl . sharedInstance. User. onJwtInvalidated { event in
164+ invalidatedCallbackWasCalled = true
165+ MockUserRequests . setAddTagsResponse ( with: mocks. client, tags: tags)
166+ OneSignalUserManagerImpl . sharedInstance. updateUserJwt ( externalId: userA_EUID, token: userA_ValidJwtToken)
167+ }
168+
169+ /* When */
170+ executor. processDeltaQueue ( inBackground: false )
171+ OneSignalCoreMocks . waitForBackgroundThreads ( seconds: 0.5 )
172+
173+ /* Then */
174+ XCTAssertTrue ( mocks. client. hasExecutedRequestOfType ( OSRequestUpdateProperties . self) )
175+ XCTAssertTrue ( invalidatedCallbackWasCalled)
176+ XCTAssertEqual ( mocks. client. networkRequestCount, 2 )
177+ }
178+
179+ func testUpdateRequests_RetryRequests_OnTokenUpdate_ForOnlyUpdatedUser( ) {
180+ /* Setup */
181+ let mocks = Mocks ( )
182+
183+ mocks. setAuthRequired ( true )
184+
185+ let userA = mocks. setUserManagerInternalUser ( externalId: userA_EUID, onesignalId: userA_OSID)
186+ userA. identityModel. jwtBearerToken = userA_InvalidJwtToken
187+
188+ let userB = mocks. setUserManagerInternalUser ( externalId: userB_EUID, onesignalId: userB_OSID)
189+ userB. identityModel. jwtBearerToken = userA_InvalidJwtToken
190+ // We need to use the user manager's executor because the onJWTUpdated callback won't fire on the mock executor
191+ let executor = OneSignalUserManagerImpl . sharedInstance. propertyExecutor!
192+
193+ let tags = [ " testUserA " : " true " ]
194+ MockUserRequests . setUnauthorizedUpdatePropertiesFailureResponses ( with: mocks. client, tags: tags)
195+
196+ executor. enqueueDelta ( OSDelta ( name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: userA. identityModel. modelId, model: OSPropertiesModel ( changeNotifier: OSEventProducer ( ) ) , property: " tags " , value: tags) )
197+ executor. enqueueDelta ( OSDelta ( name: OS_UPDATE_PROPERTIES_DELTA, identityModelId: userB. identityModel. modelId, model: OSPropertiesModel ( changeNotifier: OSEventProducer ( ) ) , property: " tags " , value: tags) )
198+
199+ var invalidatedCallbackWasCalled = false
200+ OneSignalUserManagerImpl . sharedInstance. User. onJwtInvalidated { event in
201+ invalidatedCallbackWasCalled = true
202+ }
203+
204+ /* When */
205+ executor. processDeltaQueue ( inBackground: false )
206+ OneSignalCoreMocks . waitForBackgroundThreads ( seconds: 0.5 )
207+
208+ MockUserRequests . setAddTagsResponse ( with: mocks. client, tags: tags)
209+ OneSignalUserManagerImpl . sharedInstance. updateUserJwt ( externalId: userB_EUID, token: userB_ValidJwtToken)
210+
211+ OneSignalCoreMocks . waitForBackgroundThreads ( seconds: 0.5 )
212+
213+ /* Then */
214+ // The executor should execute this request since identity verification is required and the token was set
215+ XCTAssertTrue ( mocks. client. hasExecutedRequestOfType ( OSRequestUpdateProperties . self) )
216+ XCTAssertTrue ( invalidatedCallbackWasCalled)
217+ let updateRequests = mocks. client. executedRequests. filter { request in
218+ request. isKind ( of: OSRequestUpdateProperties . self)
219+ }
220+ XCTAssertEqual ( updateRequests. count, 3 )
221+ }
144222}
0 commit comments