Skip to content

Commit af6f4e5

Browse files
FIS: Make sure refreshed auth token is stored to the storage. (#4146)
* FIRInstallationsIDControllerTests: test refreshed token is stored. * FIRInstallationsIDControllerTests: all required tests updated to expect refreshed token to be stored. * Tests: assert all FBLWaitForPromisesWithTimeout * FIRInstallationsIDController: saving refreshed token implemented. * FIRInstallationsIDController: duplicated code removed.
1 parent 341312b commit af6f4e5

File tree

4 files changed

+61
-41
lines changed

4 files changed

+61
-41
lines changed

FirebaseInstallations/Source/Library/InstallationsIDController/FIRInstallationsIDController.m

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,9 @@ - (FIRInstallationsItem *)createInstallationWithFID:(NSString *)FID
257257
}
258258
})
259259
.then(^id(FIRInstallationsItem *registeredInstallation) {
260-
// Expected successful result: @[FIRInstallationsItem *registeredInstallation, NSNull]
261-
return [FBLPromise all:@[
262-
registeredInstallation, [self.installationsStore saveInstallation:registeredInstallation]
263-
]];
260+
return [self saveInstallation:registeredInstallation];
264261
})
265-
.then(^FIRInstallationsItem *(NSArray *result) {
266-
FIRInstallationsItem *registeredInstallation = result.firstObject;
262+
.then(^FIRInstallationsItem *(FIRInstallationsItem *registeredInstallation) {
267263
// Server may respond with a different FID if the sent one cannot be accepted.
268264
if (![registeredInstallation.firebaseInstallationID
269265
isEqualToString:installation.firebaseInstallationID]) {
@@ -318,7 +314,7 @@ - (BOOL)doesRegistrationErrorRequireConfigChange:(NSError *)error {
318314
[registeredInstallation.authToken.expirationDate timeIntervalSinceDate:[NSDate date]] <
319315
kFIRInstallationsTokenExpirationThreshold;
320316
if (forceRefresh || isTokenExpiredOrExpiresSoon) {
321-
return [self.APIService refreshAuthTokenForInstallation:registeredInstallation];
317+
return [self refreshAuthTokenForInstallation:registeredInstallation];
322318
} else {
323319
return registeredInstallation;
324320
}
@@ -328,6 +324,14 @@ - (BOOL)doesRegistrationErrorRequireConfigChange:(NSError *)error {
328324
});
329325
}
330326

327+
- (FBLPromise<FIRInstallationsItem *> *)refreshAuthTokenForInstallation:
328+
(FIRInstallationsItem *)installation {
329+
return [[self.APIService refreshAuthTokenForInstallation:installation]
330+
then:^id _Nullable(FIRInstallationsItem *_Nullable refreshedInstallation) {
331+
return [self saveInstallation:refreshedInstallation];
332+
}];
333+
}
334+
331335
- (id)regenerateFIDOnRefreshTokenErrorIfNeeded:(NSError *)error {
332336
if (![error isKindOfClass:[FIRInstallationsHTTPError class]]) {
333337
// No recovery possible. Return the same error.

FirebaseInstallations/Source/Tests/Unit/FIRInstallationsAPIServiceTests.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ - (void)testRegisterInstallationSuccess {
132132
taskCompletion(successResponseData, [self responseWithStatusCode:201], nil);
133133

134134
// 6. Check result.
135-
FBLWaitForPromisesWithTimeout(0.5);
135+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
136136

137137
XCTAssertNil(promise.error);
138138
XCTAssertNotNil(promise.value);
@@ -254,7 +254,7 @@ - (void)testRefreshAuthTokenSuccess {
254254
taskCompletion(successResponseData, [self responseWithStatusCode:200], nil);
255255

256256
// 6. Check result.
257-
FBLWaitForPromisesWithTimeout(0.5);
257+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
258258

259259
XCTAssertNil(promise.error);
260260
XCTAssertNotNil(promise.value);
@@ -313,7 +313,7 @@ - (void)testRefreshAuthTokenAPIError {
313313
taskCompletion(errorResponseData, [self responseWithStatusCode:401], nil);
314314

315315
// 6. Check result.
316-
FBLWaitForPromisesWithTimeout(0.5);
316+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
317317

318318
XCTAssertTrue([FIRInstallationsErrorUtil isAPIError:promise.error withHTTPCode:401]);
319319
XCTAssertNil(promise.value);
@@ -426,7 +426,7 @@ - (void)testRefreshAuthTokenDataNil {
426426
taskCompletion(nil, [self responseWithStatusCode:200], nil);
427427

428428
// 6. Check result.
429-
FBLWaitForPromisesWithTimeout(0.5);
429+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
430430

431431
XCTAssertEqualObjects(promise.error.userInfo[NSLocalizedFailureReasonErrorKey],
432432
@"Failed to serialize JSON data.");
@@ -472,7 +472,7 @@ - (void)testDeleteInstallationSuccess {
472472
taskCompletion(successResponseData, [self responseWithStatusCode:200], nil);
473473

474474
// 6. Check result.
475-
FBLWaitForPromisesWithTimeout(0.5);
475+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
476476

477477
XCTAssertNil(promise.error);
478478
XCTAssertEqual(promise.value, installation);
@@ -516,7 +516,7 @@ - (void)testDeleteInstallationErrorNotFound {
516516
taskCompletion(nil, [self responseWithStatusCode:404], nil);
517517

518518
// 6. Check result.
519-
FBLWaitForPromisesWithTimeout(0.5);
519+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
520520

521521
XCTAssertTrue([FIRInstallationsErrorUtil isAPIError:promise.error withHTTPCode:404]);
522522
XCTAssertNil(promise.value);
@@ -575,7 +575,7 @@ - (void)testDeleteInstallation_WhenAPIError500_ThenRetriesOnce {
575575
nil);
576576

577577
// 7. Check result.
578-
FBLWaitForPromisesWithTimeout(0.5);
578+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
579579

580580
XCTAssertTrue([FIRInstallationsErrorUtil
581581
isAPIError:promise.error

FirebaseInstallations/Source/Tests/Unit/FIRInstallationsIDControllerTests.m

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ - (void)testGetAuthToken_WhenValidInstallationExists_ThenItIsReturned {
392392
FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
393393

394394
// 3. Wait for the promise to resolve.
395-
FBLWaitForPromisesWithTimeout(0.5);
395+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
396396

397397
// 4. Check.
398398
OCMVerifyAll(self.mockInstallationsStore);
@@ -414,19 +414,15 @@ - (void)testGetAuthToken_WhenValidInstallationWithExpiredTokenExists_ThenTokenRe
414414
OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
415415
.andReturn([FBLPromise resolvedWith:storedInstallation]);
416416

417-
// 1.2. Expect API request.
417+
// 1.2. Auth Token refresh.
418418
FIRInstallationsItem *responseInstallation =
419-
[FIRInstallationsItem createRegisteredInstallationItem];
420-
responseInstallation.authToken.token =
421-
[responseInstallation.authToken.token stringByAppendingString:@"_new"];
422-
OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
423-
.andReturn([FBLPromise resolvedWith:responseInstallation]);
419+
[self expectAuthTokenRefreshForInstallation:storedInstallation];
424420

425421
// 2. Request auth token.
426422
FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
427423

428424
// 3. Wait for the promise to resolve.
429-
FBLWaitForPromisesWithTimeout(0.5);
425+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
430426

431427
// 4. Check.
432428
OCMVerifyAll(self.mockInstallationsStore);
@@ -447,19 +443,15 @@ - (void)testGetAuthTokenForcingRefresh_WhenValidInstallationExists_ThenTokenRequ
447443
OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
448444
.andReturn([FBLPromise resolvedWith:storedInstallation]);
449445

450-
// 1.2. Expect API request.
446+
// 1.2. Auth Token refresh.
451447
FIRInstallationsItem *responseInstallation =
452-
[FIRInstallationsItem createRegisteredInstallationItem];
453-
responseInstallation.authToken.token =
454-
[responseInstallation.authToken.token stringByAppendingString:@"_new"];
455-
OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
456-
.andReturn([FBLPromise resolvedWith:responseInstallation]);
448+
[self expectAuthTokenRefreshForInstallation:storedInstallation];
457449

458450
// 2. Request auth token.
459451
FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
460452

461453
// 3. Wait for the promise to resolve.
462-
FBLWaitForPromisesWithTimeout(0.5);
454+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
463455

464456
// 4. Check.
465457
OCMVerifyAll(self.mockInstallationsStore);
@@ -494,7 +486,7 @@ - (void)testGetAuthToken_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
494486
[storagePendingPromise fulfill:storedInstallation];
495487

496488
// 4. Wait for the promise to resolve.
497-
FBLWaitForPromisesWithTimeout(0.5);
489+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
498490

499491
// 5. Check.
500492
OCMVerifyAll(self.mockInstallationsStore);
@@ -525,6 +517,10 @@ - (void)testGetAuthTokenForceRefresh_WhenCalledSeveralTimes_OnlyOneOperationIsPe
525517
OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
526518
.andReturn(pendingAPIPromise);
527519

520+
// 1.3. Expect new token to be stored.
521+
OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
522+
.andReturn([FBLPromise resolvedWith:[NSNull null]]);
523+
528524
// 2. Request auth token n times.
529525
NSInteger requestCount = 10;
530526
NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
@@ -536,7 +532,7 @@ - (void)testGetAuthTokenForceRefresh_WhenCalledSeveralTimes_OnlyOneOperationIsPe
536532
[pendingAPIPromise fulfill:responseInstallation];
537533

538534
// 4. Wait for the promise to resolve.
539-
FBLWaitForPromisesWithTimeout(0.5);
535+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
540536

541537
// 5. Check.
542538
OCMVerifyAll(self.mockInstallationsStore);
@@ -664,7 +660,7 @@ - (void)testDeleteRegisteredInstallation {
664660
FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
665661

666662
// 6. Wait for operations to complete and check.
667-
FBLWaitForPromisesWithTimeout(0.5);
663+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
668664

669665
XCTAssertNil(promise.error);
670666
XCTAssertTrue(promise.isFulfilled);
@@ -702,7 +698,7 @@ - (void)testDeleteUnregisteredInstallation {
702698
FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
703699

704700
// 6. Wait for operations to complete and check.
705-
FBLWaitForPromisesWithTimeout(0.5);
701+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
706702

707703
XCTAssertNil(promise.error);
708704
XCTAssertTrue(promise.isFulfilled);
@@ -742,7 +738,7 @@ - (void)testDeleteRegisteredInstallation_WhenAPIRequestFails_ThenFailsAndInstall
742738
FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
743739

744740
// 6. Wait for operations to complete and check.
745-
FBLWaitForPromisesWithTimeout(0.5);
741+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
746742

747743
XCTAssertEqualObjects(promise.error, error500);
748744
XCTAssertTrue(promise.isRejected);
@@ -781,7 +777,7 @@ - (void)testDeleteRegisteredInstallation_WhenAPIFailsWithNotFound_ThenInstallati
781777
FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
782778

783779
// 6. Wait for operations to complete and check.
784-
FBLWaitForPromisesWithTimeout(0.5);
780+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
785781

786782
XCTAssertNil(promise.error);
787783
XCTAssertTrue(promise.isFulfilled);
@@ -839,10 +835,14 @@ - (void)testDeleteInstallation_WhenThereIsOngoingAuthTokenRequest_ThenUsesItsRes
839835
FBLPromise<NSNull *> *deletePromise = [self.controller deleteInstallation];
840836

841837
// 4. Fulfill auth token promise to proceed.
838+
// 4.1. Expect new token to be stored on API response.
839+
OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
840+
.andReturn([FBLPromise resolvedWith:[NSNull null]]);
841+
842842
[pendingAuthTokenAPIPromise fulfill:responseInstallation];
843843

844844
// 5. Wait for operations to complete and check the result.
845-
FBLWaitForPromisesWithTimeout(0.5);
845+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
846846

847847
XCTAssertNil(deletePromise.error);
848848
XCTAssertTrue(deletePromise.isFulfilled);
@@ -884,7 +884,7 @@ - (void)testDeleteInstallation_WhenNotDefaultApp_ThenIIDIsNotDeleted {
884884
FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
885885

886886
// 6. Wait for operations to complete and check.
887-
FBLWaitForPromisesWithTimeout(0.5);
887+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
888888

889889
XCTAssertNil(promise.error);
890890
XCTAssertTrue(promise.isFulfilled);
@@ -938,7 +938,7 @@ - (void)testFIDDidChangeNotificationIsSentWhenFIDCreated {
938938

939939
// 3. Request FID.
940940
FBLPromise *promise = [self.controller getInstallationItem];
941-
FBLWaitForPromisesWithTimeout(0.5);
941+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
942942

943943
// 4. Check.
944944
XCTAssertNil(promise.error);
@@ -1035,4 +1035,20 @@ - (void)expectInstallationStoreToBeRequestedAndReturnInstallation:
10351035
.andReturn([FBLPromise resolvedWith:storedInstallation]);
10361036
}
10371037

1038+
- (FIRInstallationsItem *)expectAuthTokenRefreshForInstallation:
1039+
(FIRInstallationsItem *)installation {
1040+
FIRInstallationsItem *responseInstallation =
1041+
[FIRInstallationsItem createRegisteredInstallationItem];
1042+
responseInstallation.authToken.token =
1043+
[responseInstallation.authToken.token stringByAppendingString:@"_new"];
1044+
OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:installation])
1045+
.andReturn([FBLPromise resolvedWith:responseInstallation]);
1046+
1047+
// 1.3. Expect new token to be stored.
1048+
OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
1049+
.andReturn([FBLPromise resolvedWith:[NSNull null]]);
1050+
1051+
return responseInstallation;
1052+
}
1053+
10381054
@end

FirebaseInstallations/Source/Tests/Unit/FIRInstallationsIIDStoreTests.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ - (void)testExistingIIDSuccess {
6060

6161
FBLPromise<NSString *> *IIDPromise = [self.IIDStore existingIID];
6262

63-
FBLWaitForPromisesWithTimeout(0.5);
63+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
6464

6565
XCTAssertNil(IIDPromise.error);
6666
XCTAssertEqualObjects(IIDPromise.value, existingIID);
@@ -73,14 +73,14 @@ - (void)testDeleteExistingIID {
7373

7474
// 2. Delete IID.
7575
FBLPromise<NSNull *> *deletePromise = [self.IIDStore deleteExistingIID];
76-
FBLWaitForPromisesWithTimeout(0.5);
76+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
7777

7878
XCTAssertNil(deletePromise.error);
7979
XCTAssertTrue(deletePromise.isFulfilled);
8080

8181
// 3. Check there is no IID.
8282
FBLPromise<NSString *> *IIDPromise = [self.IIDStore existingIID];
83-
FBLWaitForPromisesWithTimeout(0.5);
83+
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
8484

8585
XCTAssertNotNil(IIDPromise.error);
8686
XCTAssertTrue(IIDPromise.isRejected);

0 commit comments

Comments
 (0)