@@ -553,6 +553,89 @@ - (void)testGetAuthTokenForceRefresh_WhenCalledSeveralTimes_OnlyOneOperationIsPe
553
553
}
554
554
}
555
555
556
+ - (void )testGetAuthToken_WhenAPIResponse401_ThenFISResetAndReregistered {
557
+ NSTimeInterval timeout = 0.5 ;
558
+
559
+ // 1.1. Expect installation to be requested from the store.
560
+ FIRInstallationsItem *storedInstallation =
561
+ [FIRInstallationsItem createRegisteredInstallationItem ];
562
+ [self expectInstallationStoreToBeRequestedAndReturnInstallation: storedInstallation];
563
+
564
+ // 1.2. Expect API request.
565
+ FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise ];
566
+ OCMExpect ([self .mockAPIService refreshAuthTokenForInstallation: storedInstallation])
567
+ .andReturn (rejectedAPIPromise);
568
+
569
+ // 2. Request auth token.
570
+ FBLPromise<FIRInstallationsItem *> *promise = [self .controller getAuthTokenForcingRefresh: YES ];
571
+
572
+ // 3. Wait for refresh token request.
573
+ OCMVerifyAllWithDelay (self.mockAPIService , timeout);
574
+
575
+ // 4.1. Expect Installation to be requested before deletion.
576
+ [self expectInstallationStoreToBeRequestedAndReturnInstallation: storedInstallation];
577
+ // 4. Expect for FIS to be deleted locally.
578
+ NSArray <XCTestExpectation *> *deleteExpectations =
579
+ [self expectInstallationToBeDeletedLocally: storedInstallation];
580
+
581
+ // 6. Expect a new installation to be created and registered.
582
+ // 6.1. Expect to request FIS from storage.
583
+ [self expectInstallationsStoreGetInstallationNotFound ];
584
+ // 6.2. Expect stored IID not found.
585
+ [self expectStoredIIDNotFound ];
586
+ // 6.3. Expect new Installation to be stored.
587
+ __block FIRInstallationsItem *createdInstallation;
588
+ OCMExpect ([self .mockInstallationsStore
589
+ saveInstallation: [OCMArg checkWithBlock: ^BOOL (FIRInstallationsItem *obj) {
590
+ [self assertValidCreatedInstallation: obj];
591
+
592
+ createdInstallation = obj;
593
+ return YES ;
594
+ }]])
595
+ .andReturn ([FBLPromise resolvedWith: [NSNull null ]]);
596
+ // 6.4. Expect registration API request to be sent.
597
+ FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise ];
598
+ OCMExpect ([self .mockAPIService registerInstallation: [OCMArg any ]]).andReturn (registerPromise);
599
+
600
+ // 6.5. Reject API request promise with 401.
601
+ NSError *error401 = [FIRInstallationsErrorUtil APIErrorWithHTTPCode: 404 ];
602
+ [rejectedAPIPromise reject: error401];
603
+ // 6.6. Wait local FIS to be deleted.
604
+ [self waitForExpectations: deleteExpectations timeout: timeout];
605
+
606
+ // 6.7 Wait for the new Installation to be stored.
607
+ OCMVerifyAllWithDelay (self.mockInstallationsStore , timeout);
608
+ // 6.8. Wait for registration API request to be sent.
609
+ OCMVerifyAllWithDelay (self.mockAPIService , timeout);
610
+ // 6.9. Expect for the registered installation to be saved.
611
+ FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
612
+ createRegisteredInstallationItemWithAppID: createdInstallation.appID
613
+ appName: createdInstallation.firebaseAppName];
614
+
615
+ OCMExpect ([self .mockInstallationsStore
616
+ saveInstallation: [OCMArg checkWithBlock: ^BOOL (FIRInstallationsItem *obj) {
617
+ XCTAssertEqual (registeredInstallation, obj);
618
+ return YES ;
619
+ }]])
620
+ .andReturn ([FBLPromise resolvedWith: [NSNull null ]]);
621
+ // 6.9. Fulfill the registration API request promise.
622
+ [registerPromise fulfill: registeredInstallation];
623
+
624
+ // 7. Wait for promises.
625
+ XCTAssert (FBLWaitForPromisesWithTimeout (timeout));
626
+
627
+ // 8. Check.
628
+ OCMVerifyAll (self.mockInstallationsStore );
629
+ OCMVerifyAll (self.mockAPIService );
630
+
631
+ XCTAssertNil (promise.error );
632
+ XCTAssertNotNil (promise.value );
633
+
634
+ XCTAssertNotEqualObjects (promise.value .firebaseInstallationID ,
635
+ storedInstallation.firebaseInstallationID );
636
+ XCTAssertEqualObjects (promise.value , registeredInstallation);
637
+ }
638
+
556
639
#pragma mark - FID Deletion
557
640
558
641
- (void )testDeleteRegisteredInstallation {
@@ -642,7 +725,7 @@ - (void)testDeleteRegisteredInstallation_WhenAPIRequestFails_ThenFailsAndInstall
642
725
// 2. Expect API request to delete installation.
643
726
FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise ];
644
727
NSError *error500 =
645
- [FIRInstallationsErrorUtil APIErrorWithHTTPCode: kFIRInstallationsAPIInternalErrorHTTPCode ];
728
+ [FIRInstallationsErrorUtil APIErrorWithHTTPCode: FIRInstallationsHTTPCodesServerInternalError ];
646
729
[rejectedAPIPromise reject: error500];
647
730
OCMExpect ([self .mockAPIService deleteInstallation: installation]).andReturn (rejectedAPIPromise);
648
731
@@ -814,6 +897,24 @@ - (void)testDeleteInstallation_WhenNotDefaultApp_ThenIIDIsNotDeleted {
814
897
OCMVerifyAll (self.mockIIDStore );
815
898
}
816
899
900
+ - (NSArray <XCTestExpectation *> *)expectInstallationToBeDeletedLocally :
901
+ (FIRInstallationsItem *)installation {
902
+ // 3.1. Expect the installation to be removed from the storage.
903
+ OCMExpect ([self .mockInstallationsStore removeInstallationForAppID: installation.appID
904
+ appName: installation.firebaseAppName])
905
+ .andReturn ([FBLPromise resolvedWith: [NSNull null ]]);
906
+
907
+ // 3.2. Expect IID to be deleted, because it is default app.
908
+ OCMExpect ([self .mockIIDStore deleteExistingIID ])
909
+ .andReturn ([FBLPromise resolvedWith: [NSNull null ]]);
910
+
911
+ // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
912
+ XCTestExpectation *notificationExpectation =
913
+ [self installationIDDidChangeNotificationExpectation ];
914
+
915
+ return @[ notificationExpectation ];
916
+ }
917
+
817
918
// TODO: Test a single delete installation request at a time.
818
919
819
920
#pragma mark - Notifications
@@ -1087,6 +1188,12 @@ - (void)expectInstallationsStoreGetInstallationNotFound {
1087
1188
.andReturn (installationNotFoundPromise);
1088
1189
}
1089
1190
1191
+ - (void )expectStoredIIDNotFound {
1192
+ FBLPromise *rejectedPromise = [FBLPromise pendingPromise ];
1193
+ [rejectedPromise reject: [FIRInstallationsErrorUtil keychainErrorWithFunction: @" " status: -1 ]];
1194
+ OCMExpect ([self .mockIIDStore existingIID ]).andReturn (rejectedPromise);
1195
+ }
1196
+
1090
1197
- (void )assertValidCreatedInstallation : (FIRInstallationsItem *)installation {
1091
1198
XCTAssertEqualObjects ([installation class ], [FIRInstallationsItem class ]);
1092
1199
XCTAssertEqualObjects (installation.appID , self.appID );
@@ -1138,4 +1245,10 @@ - (FIRInstallationsStoredRegistrationParameters *)otherRegistrationParameters {
1138
1245
projectID: projectID];
1139
1246
}
1140
1247
1248
+ - (void )expectInstallationStoreToBeRequestedAndReturnInstallation :
1249
+ (FIRInstallationsItem *)storedInstallation {
1250
+ OCMExpect ([self .mockInstallationsStore installationForAppID: self .appID appName: self .appName])
1251
+ .andReturn ([FBLPromise resolvedWith: storedInstallation]);
1252
+ }
1253
+
1141
1254
@end
0 commit comments