| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505 |
- /*
- * Copyright 2019 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import <XCTest/XCTest.h>
- #import <OCMock/OCMock.h>
- #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
- #import "FBLPromise+Testing.h"
- #import "FBLPromise+Then.h"
- #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallationsErrorUtil+Tests.h"
- #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallationsItem+Tests.h"
- #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsErrorUtil.h"
- #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsHTTPError.h"
- #import "FirebaseInstallations/Source/Library/IIDMigration/FIRInstallationsIIDStore.h"
- #import "FirebaseInstallations/Source/Library/IIDMigration/FIRInstallationsIIDTokenStore.h"
- #import "FirebaseInstallations/Source/Library/InstallationsAPI/FIRInstallationsAPIService.h"
- #import "FirebaseInstallations/Source/Library/InstallationsIDController/FIRInstallationsBackoffController.h"
- #import "FirebaseInstallations/Source/Library/InstallationsIDController/FIRInstallationsIDController.h"
- #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStore.h"
- #import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FIRInstallations.h"
- #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredAuthToken.h"
- @interface FIRInstallationsIDController (Tests)
- - (instancetype)initWithGoogleAppID:(NSString *)appID
- appName:(NSString *)appName
- installationsStore:(FIRInstallationsStore *)installationsStore
- APIService:(FIRInstallationsAPIService *)APIService
- IIDStore:(FIRInstallationsIIDStore *)IIDStore
- IIDTokenStore:(FIRInstallationsIIDTokenStore *)IIDTokenStore
- backoffController:
- (id<FIRInstallationsBackoffControllerProtocol>)backoffController;
- @end
- @interface FIRInstallationsIDControllerTests : XCTestCase
- @property(nonatomic) FIRInstallationsIDController *controller;
- @property(nonatomic) id mockInstallationsStore;
- @property(nonatomic) id mockAPIService;
- @property(nonatomic) id mockIIDStore;
- @property(nonatomic) id mockIIDTokenStore;
- @property(nonatomic) id mockBackoffController;
- @property(nonatomic) NSString *appID;
- @property(nonatomic) NSString *appName;
- @end
- @implementation FIRInstallationsIDControllerTests
- - (void)setUp {
- [self setUpWithAppName:kFIRDefaultAppName];
- }
- - (void)setUpWithAppName:(NSString *)appName {
- self.appID = @"appID";
- self.appName = appName;
- self.mockInstallationsStore = OCMStrictClassMock([FIRInstallationsStore class]);
- self.mockAPIService = OCMStrictClassMock([FIRInstallationsAPIService class]);
- self.mockIIDStore = OCMStrictClassMock([FIRInstallationsIIDStore class]);
- self.mockIIDTokenStore = OCMStrictClassMock([FIRInstallationsIIDTokenStore class]);
- self.mockBackoffController =
- OCMProtocolMock(@protocol(FIRInstallationsBackoffControllerProtocol));
- self.controller =
- [[FIRInstallationsIDController alloc] initWithGoogleAppID:self.appID
- appName:self.appName
- installationsStore:self.mockInstallationsStore
- APIService:self.mockAPIService
- IIDStore:self.mockIIDStore
- IIDTokenStore:self.mockIIDTokenStore
- backoffController:self.mockBackoffController];
- }
- - (void)tearDown {
- self.controller = nil;
- self.mockIIDStore = nil;
- self.mockAPIService = nil;
- self.mockInstallationsStore = nil;
- self.appID = nil;
- self.appName = nil;
- }
- #pragma mark - Initialization
- - (void)testInitWhenProjectIDSetThenItIsPassedToAPIService {
- FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{
- kFIRAPIKey : @"api-key",
- kFIRProjectID : @"project-id",
- kFIRGoogleAppID : @"app-id",
- kFIRGCMSenderID : @"sender-id"
- }];
- FIRApp *app = [[FIRApp alloc] initInstanceWithName:@"app-name" options:options];
- OCMExpect([self.mockAPIService alloc]).andReturn(self.mockAPIService);
- OCMExpect([self.mockAPIService initWithAPIKey:app.options.APIKey
- projectID:app.options.projectID
- heartbeatLogger:app.heartbeatLogger])
- .andReturn(self.mockAPIService);
- FIRInstallationsIDController *controller = [[FIRInstallationsIDController alloc] initWithApp:app];
- XCTAssertNotNil(controller);
- OCMVerifyAll(self.mockAPIService);
- }
- #pragma mark - Get Installation
- - (void)testGetInstallationItem_WhenFIDExists_ThenItIsReturned {
- FIRInstallationsItem *storedInstallations =
- [FIRInstallationsItem createRegisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallations]);
- // Don't expect FIRInstallationIDDidChangeNotification to be sent.
- XCTestExpectation *notificationExpectation =
- [self installationIDDidChangeNotificationExpectation];
- notificationExpectation.inverted = YES;
- FBLPromise<FIRInstallationsItem *> *promise = [self.controller getInstallationItem];
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(promise.error);
- XCTAssertEqual(promise.value, storedInstallations);
- OCMVerifyAll(self.mockInstallationsStore);
- [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
- }
- - (void)testGetInstallationItem_WhenNoFIDAndNoIID_ThenFIDIsCreatedAndRegistered {
- [self expectInstallationsStoreGetInstallationNotFound];
- [self expectStoredIIDNotFound];
- [self assertGetInstallation_NewFIDCreatedAndRegistered];
- }
- - (void)testGetInstallationItem_WhenFIDCorruptedAndNoIID_ThenFIDIsCreatedAndRegistered {
- [self expectInstallationsStoreGetInstallationCorruptedFID];
- [self expectStoredIIDNotFound];
- [self assertGetInstallation_NewFIDCreatedAndRegistered];
- }
- - (void)testGetInstallationItem_WhenThereIsIIDAndNoFIDNotDefaultApp_ThenIIDIsNotUsedAsFID {
- // 0. Configure controller with not default app.
- NSString *appName = @"appName";
- [self setUpWithAppName:appName];
- // 1. Stub store get installation.
- [self expectInstallationsStoreGetInstallationNotFound];
- // 2. Don't expect IIDStore to be checked for existing IID (not default app).
- OCMReject([self.mockIIDStore existingIID]);
- // 3. Stub store save installation.
- __block FIRInstallationsItem *createdInstallation;
- OCMExpect([self.mockInstallationsStore
- saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- [self assertValidCreatedInstallation:obj];
- createdInstallation = obj;
- return YES;
- }]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 4. Stub API register installation.
- // 4.1. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 4.2. Verify installation to be registered.
- id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- [self assertValidCreatedInstallation:obj];
- return YES;
- }];
- // 4.3. Expect for `registerInstallation` to be called.
- FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
- .andReturn(registerPromise);
- // 4.4. Expect backoff success.
- [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
- // 5. Call get installation and check.
- FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
- [self.controller getInstallationItem];
- // 5.1. Wait for the stored item to be read and saved.
- OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
- // 5.2. Wait for `registerInstallation` to be called.
- OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
- // 5.3. Expect for the registered installation to be saved.
- FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
- createRegisteredInstallationItemWithAppID:createdInstallation.appID
- appName:createdInstallation.firebaseAppName];
- OCMExpect([self.mockInstallationsStore
- saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- XCTAssertEqual(registeredInstallation, obj);
- return YES;
- }]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 5.5. Resolve `registerPromise` to simulate finished registration.
- [registerPromise fulfill:registeredInstallation];
- // 5.4. Wait for the task to complete.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(getInstallationPromise.error);
- // We expect the initially created installation to be returned - must not wait for registration to
- // complete here.
- XCTAssertEqual(getInstallationPromise.value, createdInstallation);
- // 5.5. Verify registered installation was saved.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)testGetInstallationItem_WhenThereIsIIDAndNoFID_ThenIIDIsRegisteredAsFID {
- // 1. Stub store get installation.
- [self expectInstallationsStoreGetInstallationNotFound];
- // 2. Expect IIDStore to be checked for existing IID.
- NSString *existingIID = @"existing-iid";
- OCMExpect([self.mockIIDStore existingIID]).andReturn([FBLPromise resolvedWith:existingIID]);
- // 3. Expect IID checkin store to be requested for checkin data.
- NSString *existingIIDDefaultToken = @"existing-iid-token";
- OCMExpect([self.mockIIDTokenStore existingIIDDefaultToken])
- .andReturn([FBLPromise resolvedWith:existingIIDDefaultToken]);
- // 3. Stub store save installation.
- __block FIRInstallationsItem *createdInstallation;
- OCMExpect([self.mockInstallationsStore
- saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- [self assertValidCreatedInstallation:obj];
- XCTAssertEqualObjects(existingIID, obj.firebaseInstallationID);
- XCTAssertEqualObjects(obj.IIDDefaultToken, existingIIDDefaultToken);
- createdInstallation = obj;
- return YES;
- }]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 4. Stub API register installation.
- // 4.1. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 4.2. Verify installation to be registered.
- id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- [self assertValidCreatedInstallation:obj];
- XCTAssertEqualObjects(existingIID, obj.firebaseInstallationID);
- return YES;
- }];
- // 4.3. Expect for `registerInstallation` to be called.
- FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
- .andReturn(registerPromise);
- // 4.4. Expect backoff success.
- [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
- // 5. Call get installation and check.
- FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
- [self.controller getInstallationItem];
- // 5.1. Wait for the stored item to be read and saved.
- OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
- // 5.2. Wait for `registerInstallation` to be called.
- OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
- // 5.3. Expect for the registered installation to be saved.
- FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
- createRegisteredInstallationItemWithAppID:createdInstallation.appID
- appName:createdInstallation.firebaseAppName];
- OCMExpect([self.mockInstallationsStore
- saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- XCTAssertEqual(registeredInstallation, obj);
- return YES;
- }]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 5.5. Resolve `registerPromise` to simulate finished registration.
- [registerPromise fulfill:registeredInstallation];
- // 5.4. Wait for the task to complete.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(getInstallationPromise.error);
- // We expect the initially created installation to be returned - must not wait for registration to
- // complete here.
- XCTAssertEqual(getInstallationPromise.value, createdInstallation);
- // 5.5. Verify registered installation was saved.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockIIDTokenStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)testGetInstallationItem_WhenCalledWhileRegistering_DoesNotWaitForAPIResponse {
- // 1. Expect the installation to be requested from the store only once.
- FIRInstallationsItem *storedInstallation1 =
- [FIRInstallationsItem createUnregisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation1]);
- // 2. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 3. Expect registration API request to be sent.
- FBLPromise<FIRInstallationsItem *> *pendingAPIPromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockAPIService registerInstallation:storedInstallation1])
- .andReturn(pendingAPIPromise);
- // 4. Expect backoff recoverable error for HTTP 429.
- [self expectBackoffEvent:FIRInstallationsBackoffEventRecoverableFailure];
- // 5. Request and wait for 1st FID.
- FBLPromise<FIRInstallationsItem *> *getInstallationPromise1 =
- [self.controller getInstallationItem];
- XCTestExpectation *getInstallationsExpectation1 =
- [self expectationWithDescription:@"getInstallationsExpectation1"];
- getInstallationPromise1.then(^id(FIRInstallationsItem *installation) {
- [getInstallationsExpectation1 fulfill];
- return nil;
- });
- [self waitForExpectations:@[ getInstallationsExpectation1 ] timeout:0.5];
- // 6. Request FID 2nd time.
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation1]);
- FBLPromise<FIRInstallationsItem *> *getInstallationPromise2 =
- [self.controller getInstallationItem];
- XCTestExpectation *getInstallationsExpectation2 =
- [self expectationWithDescription:@"getInstallationsExpectation2"];
- getInstallationPromise2.then(^id(FIRInstallationsItem *installation) {
- [getInstallationsExpectation2 fulfill];
- return nil;
- });
- [self waitForExpectations:@[ getInstallationsExpectation2 ] timeout:0.5];
- // 7. Resolve API promise.
- [pendingAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:429]];
- // 8. Check
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)testGetInstallationItem_WhenCalledSeveralTimesWaitingForStore_OnlyOneOperationIsPerformed {
- // 1. Expect the installation to be requested from the store only once.
- FIRInstallationsItem *storedInstallation1 =
- [FIRInstallationsItem createRegisteredInstallationItem];
- FBLPromise<FIRInstallationsItem *> *pendingStorePromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn(pendingStorePromise);
- // 2. Request installation n times
- NSInteger requestCount = 10;
- NSMutableArray *installationPromises = [NSMutableArray arrayWithCapacity:requestCount];
- for (NSInteger i = 0; i < requestCount; i++) {
- [installationPromises addObject:[self.controller getInstallationItem]];
- }
- // 3. Resolve store promise.
- [pendingStorePromise fulfill:storedInstallation1];
- // 4. Wait for operation to be completed and check.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- for (FBLPromise<FIRInstallationsItem *> *installationPromise in installationPromises) {
- XCTAssertNil(installationPromise.error);
- XCTAssertEqual(installationPromise.value, storedInstallation1);
- }
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- // 5. Check that a new request is performed once previous finished.
- FIRInstallationsItem *storedInstallation2 =
- [FIRInstallationsItem createRegisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation2]);
- FBLPromise<FIRInstallationsItem *> *installationPromise = [self.controller getInstallationItem];
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(installationPromise.error);
- XCTAssertEqual(installationPromise.value, storedInstallation2);
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- }
- - (void)testGetInstallationItem_WhenCalledSeveralTimesWaitingForAPI_OnlyOneAPIRequestIsSent {
- // 1. Expect a single API request.
- // 1.1. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 1.2. Expect the request itself.
- FBLPromise<FIRInstallationsItem *> *registerAPIPromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]]).andReturn(registerAPIPromise);
- // 2. Request FID multiple times.
- NSInteger requestCount = 10;
- for (NSInteger i = 0; i < requestCount; i++) {
- XCTestExpectation *getFIDExpectation = [self
- expectationWithDescription:[NSString stringWithFormat:@"getFIDExpectation%ld", (long)i]];
- // 2.1. Expect stored FID to be requested.
- FIRInstallationsItem *storedInstallation =
- [FIRInstallationsItem createUnregisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation]);
- // 2.2. Expect the FID to be returned.
- FBLPromise<FIRInstallationsItem *> *getFIDPromise = [self.controller getInstallationItem];
- [getFIDPromise then:^id _Nullable(FIRInstallationsItem *_Nullable value) {
- XCTAssertNotNil(value);
- XCTAssertEqualObjects(value.firebaseInstallationID,
- storedInstallation.firebaseInstallationID);
- [getFIDExpectation fulfill];
- return nil;
- }];
- [self waitForExpectations:@[ getFIDExpectation ] timeout:0.5];
- }
- // 3. Expect backoff unrecoverable failure.
- [self expectBackoffEvent:FIRInstallationsBackoffEventUnrecoverableFailure];
- // 4. Finish API request.
- [registerAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:400]];
- // 5. Verify mocks
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)testGetInstallationItem_WhenRegistrationError_ThenBackoffEventIsLogged {
- [self expectBackoffEvent:FIRInstallationsBackoffEventUnrecoverableFailure
- forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:400]];
- [self expectBackoffEvent:FIRInstallationsBackoffEventUnrecoverableFailure
- forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:403]];
- [self expectBackoffEvent:FIRInstallationsBackoffEventRecoverableFailure
- forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:429]];
- [self expectBackoffEvent:FIRInstallationsBackoffEventRecoverableFailure
- forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:500]];
- [self expectBackoffEvent:FIRInstallationsBackoffEventRecoverableFailure
- forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:503]];
- // An arbitrary unknown server response.
- [self expectBackoffEvent:FIRInstallationsBackoffEventRecoverableFailure
- forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:444]];
- // A connection error.
- [self expectBackoffEvent:kNoBackoffEvents
- forRegisterFIDAPIError:[FIRInstallationsErrorUtil
- networkErrorWithError:[NSError errorWithDomain:@"tests"
- code:-1
- userInfo:nil]]];
- // An unknown error.
- [self expectBackoffEvent:kNoBackoffEvents
- forRegisterFIDAPIError:[NSError errorWithDomain:@"tests" code:-1 userInfo:nil]];
- }
- - (void)testGetInstallationItem_WhenNextRequestIsNotAllowed {
- // 1. Stub store get installation.
- [self expectInstallationsStoreGetInstallationNotFound];
- // 2. Stub store save installation.
- __block FIRInstallationsItem *createdInstallation;
- OCMExpect([self.mockInstallationsStore
- saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- [self assertValidCreatedInstallation:obj];
- createdInstallation = obj;
- return YES;
- }]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 3. Expect IIDStore to be checked for existing IID.
- [self expectStoredIIDNotFound];
- // 4. Stub API register installation.
- // 4.1. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:NO];
- // 4.2. Don't expect for `registerInstallation` to be called.
- OCMReject([self.mockAPIService registerInstallation:[OCMArg any]]);
- // 4.3. Don't expect backoff updates.
- [self rejectBackoffEvent];
- // 5. Call get installation and check.
- FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
- [self.controller getInstallationItem];
- // 5.1. Wait for the stored item to be read and saved.
- OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
- // 5.2. Wait for `registerInstallation` to be called.
- OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
- // 5.3. Don't Expect for the registered installation to be saved.
- OCMReject([self.mockInstallationsStore saveInstallation:[OCMArg any]]);
- // 5.4. Wait for the task to complete.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(getInstallationPromise.error);
- // We expect the initially created installation to be returned - must not wait for registration to
- // complete here.
- XCTAssertEqual(getInstallationPromise.value, createdInstallation);
- // 5.5. Verify registered installation was saved.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- #pragma mark - Get Auth Token
- - (void)testGetAuthToken_WhenValidInstallationExists_ThenItIsReturned {
- // 1. Expect installation to be requested from the store.
- FIRInstallationsItem *storedInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation]);
- // 2. Request auth token.
- FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
- // 3. Wait for the promise to resolve.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- // 4. Check.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- XCTAssertNil(promise.error);
- XCTAssertNotNil(promise.value);
- XCTAssertEqualObjects(promise.value.authToken.token, storedInstallation.authToken.token);
- XCTAssertEqualObjects(promise.value.authToken.expirationDate,
- storedInstallation.authToken.expirationDate);
- }
- - (void)testGetAuthToken_WhenValidInstallationWithExpiredTokenExists_ThenTokenRequested {
- // 1.1. Expect installation to be requested from the store.
- FIRInstallationsItem *storedInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- storedInstallation.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 - 1];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation]);
- // 1.2. Auth Token refresh.
- FIRInstallationsItem *responseInstallation =
- [self expectAuthTokenRefreshForInstallation:storedInstallation];
- // 2. Request auth token.
- FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
- // 3. Wait for the promise to resolve.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- // 4. Check.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockBackoffController);
- XCTAssertNil(promise.error);
- XCTAssertNotNil(promise.value);
- XCTAssertEqualObjects(promise.value.authToken.token, responseInstallation.authToken.token);
- XCTAssertEqualObjects(promise.value.authToken.expirationDate,
- responseInstallation.authToken.expirationDate);
- }
- - (void)testGetAuthTokenForcingRefresh_WhenValidInstallationExists_ThenTokenRequested {
- // 1.1. Expect installation to be requested from the store.
- FIRInstallationsItem *storedInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation]);
- // 1.2. Auth Token refresh.
- FIRInstallationsItem *responseInstallation =
- [self expectAuthTokenRefreshForInstallation:storedInstallation];
- // 2. Request auth token.
- FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
- // 3. Wait for the promise to resolve.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- // 4. Check.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockBackoffController);
- XCTAssertNil(promise.error);
- XCTAssertNotNil(promise.value);
- XCTAssertEqualObjects(promise.value.authToken.token, responseInstallation.authToken.token);
- XCTAssertEqualObjects(promise.value.authToken.expirationDate,
- responseInstallation.authToken.expirationDate);
- }
- - (void)testGetAuthToken_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
- // 1. Expect installation to be requested from the store.
- FIRInstallationsItem *storedInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- FBLPromise *storagePendingPromise = [FBLPromise pendingPromise];
- // Expect the installation to be requested only once.
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn(storagePendingPromise);
- // 2. Request auth token n times.
- NSInteger requestCount = 10;
- NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
- for (NSInteger i = 0; i < requestCount; i++) {
- [authTokenPromises addObject:[self.controller getAuthTokenForcingRefresh:NO]];
- }
- // 3. Finish the storage request.
- [storagePendingPromise fulfill:storedInstallation];
- // 4. Wait for the promise to resolve.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- // 5. Check.
- OCMVerifyAll(self.mockInstallationsStore);
- for (FBLPromise<FIRInstallationsItem *> *authPromise in authTokenPromises) {
- XCTAssertNil(authPromise.error);
- XCTAssertNotNil(authPromise.value);
- XCTAssertEqualObjects(authPromise.value.authToken.token, storedInstallation.authToken.token);
- XCTAssertEqualObjects(authPromise.value.authToken.expirationDate,
- storedInstallation.authToken.expirationDate);
- }
- }
- - (void)testGetAuthTokenForceRefresh_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
- // 1.1. Expect installation to be requested from the store.
- FIRInstallationsItem *storedInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation]);
- // 1.2. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 1.3. Expect API request.
- FIRInstallationsItem *responseInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- responseInstallation.authToken.token =
- [responseInstallation.authToken.token stringByAppendingString:@"_new"];
- FBLPromise *pendingAPIPromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
- .andReturn(pendingAPIPromise);
- // 1.4. Expect backoff success.
- [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
- // 1.5. Expect new token to be stored.
- OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 2. Request auth token n times.
- NSInteger requestCount = 10;
- NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
- for (NSInteger i = 0; i < requestCount; i++) {
- [authTokenPromises addObject:[self.controller getAuthTokenForcingRefresh:YES]];
- }
- // 3. Finish the API request.
- [pendingAPIPromise fulfill:responseInstallation];
- // 4. Wait for the promise to resolve.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- // 5. Check.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockBackoffController);
- for (FBLPromise<FIRInstallationsItem *> *authPromise in authTokenPromises) {
- XCTAssertNil(authPromise.error);
- XCTAssertNotNil(authPromise.value);
- XCTAssertEqualObjects(authPromise.value.authToken.token, responseInstallation.authToken.token);
- XCTAssertEqualObjects(authPromise.value.authToken.expirationDate,
- responseInstallation.authToken.expirationDate);
- }
- }
- - (void)testGetAuthToken_WhenAPIResponse404_ThenFISResetAndReregistered {
- NSTimeInterval timeout = 0.5;
- // 1.1. Expect installation to be requested from the store.
- FIRInstallationsItem *storedInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- [self expectInstallationStoreToBeRequestedAndReturnInstallation:storedInstallation];
- // 1.2. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 1.3. Expect API request.
- FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
- .andReturn(rejectedAPIPromise);
- // 1.4. We don't expect a backoff event corresponding to the 404 sever response as the error is
- // auto-recovered by the controller.
- // 2. Request auth token.
- FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
- // 3. Wait for refresh token request.
- OCMVerifyAllWithDelay(self.mockAPIService, timeout);
- // 4.1. Expect Installation to be requested before deletion.
- [self expectInstallationStoreToBeRequestedAndReturnInstallation:storedInstallation];
- // 4. Expect for FIS to be deleted locally.
- NSArray<XCTestExpectation *> *deleteExpectations =
- [self expectInstallationToBeDeletedLocally:storedInstallation];
- // 6. Expect a new installation to be created and registered.
- // 6.1. Expect to request FIS from storage.
- [self expectInstallationsStoreGetInstallationNotFound];
- // 6.2. Expect stored IID not found.
- [self expectStoredIIDNotFound];
- // 6.3. Expect new Installation to be stored.
- __block FIRInstallationsItem *createdInstallation;
- OCMExpect([self.mockInstallationsStore
- saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- [self assertValidCreatedInstallation:obj];
- createdInstallation = obj;
- return YES;
- }]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 6.4 Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 6.5. Expect registration API request to be sent.
- FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]]).andReturn(registerPromise);
- // 6.6. Expect backoff success.
- [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
- // 6.7. Reject API request promise with 404.
- NSError *error404 = [FIRInstallationsErrorUtil APIErrorWithHTTPCode:404];
- [rejectedAPIPromise reject:error404];
- // 6.8. Wait local FIS to be deleted.
- [self waitForExpectations:deleteExpectations timeout:timeout];
- // 6.9 Wait for the new Installation to be stored.
- OCMVerifyAllWithDelay(self.mockInstallationsStore, timeout);
- // 6.10. Wait for registration API request to be sent.
- OCMVerifyAllWithDelay(self.mockAPIService, timeout);
- // 6.11. Expect for the registered installation to be saved.
- FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
- createRegisteredInstallationItemWithAppID:createdInstallation.appID
- appName:createdInstallation.firebaseAppName];
- OCMExpect([self.mockInstallationsStore
- saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- XCTAssertEqual(registeredInstallation, obj);
- return YES;
- }]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 6.12. Fulfill the registration API request promise.
- [registerPromise fulfill:registeredInstallation];
- // 7. Wait for promises.
- XCTAssert(FBLWaitForPromisesWithTimeout(timeout));
- // 8. Check.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockBackoffController);
- XCTAssertNil(promise.error);
- XCTAssertNotNil(promise.value);
- XCTAssertNotEqualObjects(promise.value.firebaseInstallationID,
- storedInstallation.firebaseInstallationID);
- XCTAssertEqualObjects(promise.value, registeredInstallation);
- }
- - (void)testGetAuthToken_WhenNextRequestIsNotAllowed {
- // 1.1. Expect installation to be requested from the store.
- FIRInstallationsItem *storedInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- storedInstallation.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 - 1];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation]);
- // 1.2 Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:NO];
- // 1.3. Don't expect API request.
- OCMReject([self.mockAPIService refreshAuthTokenForInstallation:[OCMArg any]]);
- // 1.4. Don't expect new token to be stored.
- OCMReject([self.mockInstallationsStore saveInstallation:[OCMArg any]]);
- // 1.5. Don't expect backoff events.
- [self rejectBackoffEvent];
- // 2. Request auth token.
- FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
- // 3. Wait for the promise to resolve.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- // 4. Check.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockBackoffController);
- XCTAssertNil(promise.value);
- XCTAssertNotNil(promise.error);
- XCTAssertEqualObjects(promise.error, [FIRInstallationsErrorUtil backoffIntervalWaitError]);
- }
- #pragma mark - FID Deletion
- - (void)testDeleteRegisteredInstallation {
- // 1. Expect installation to be requested from the store.
- FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
- appName:installation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:installation]);
- // 2. Expect API request to delete installation.
- OCMExpect([self.mockAPIService deleteInstallation:installation])
- .andReturn([FBLPromise resolvedWith:installation]);
- // 2.1. Don't expect backoff calls for the delete API method.
- OCMReject([self.mockBackoffController isNextRequestAllowed]);
- [self rejectBackoffEvent];
- // 3.1. Expect the installation to be removed from the storage.
- OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
- appName:installation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 3.2. Expect IID to be deleted, because it is default app.
- OCMExpect([self.mockIIDStore deleteExistingIID])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
- XCTestExpectation *notificationExpectation =
- [self installationIDDidChangeNotificationExpectation];
- // 5. Call delete installation.
- FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
- // 6. Wait for operations to complete and check.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(promise.error);
- XCTAssertTrue(promise.isFulfilled);
- [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)testDeleteUnregisteredInstallation {
- // 1. Expect installation to be requested from the store.
- FIRInstallationsItem *installation = [FIRInstallationsItem createUnregisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
- appName:installation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:installation]);
- // 2. Don't expect backoff calls for the delete API method.
- OCMReject([self.mockBackoffController isNextRequestAllowed]);
- [self rejectBackoffEvent];
- // 3. Don't expect API request to delete installation.
- OCMReject([self.mockAPIService deleteInstallation:[OCMArg any]]);
- // 4.1. Expect the installation to be removed from the storage.
- OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
- appName:installation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 4.2. Expect IID to be deleted, because it is default app.
- OCMExpect([self.mockIIDStore deleteExistingIID])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 5. Expect FIRInstallationIDDidChangeNotification to be sent.
- XCTestExpectation *notificationExpectation =
- [self installationIDDidChangeNotificationExpectation];
- // 6. Call delete installation.
- FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
- // 7. Wait for operations to complete and check.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(promise.error);
- XCTAssertTrue(promise.isFulfilled);
- [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)testDeleteRegisteredInstallation_WhenAPIRequestFails_ThenFailsAndInstallationIsNotRemoved {
- // 1. Expect installation to be requested from the store.
- FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
- appName:installation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:installation]);
- // 2. Expect API request to delete installation.
- FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
- NSError *error500 =
- [FIRInstallationsErrorUtil APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError];
- [rejectedAPIPromise reject:error500];
- OCMExpect([self.mockAPIService deleteInstallation:installation]).andReturn(rejectedAPIPromise);
- // 2.1. Don't expect backoff calls for the delete API method.
- OCMReject([self.mockBackoffController isNextRequestAllowed]);
- [self rejectBackoffEvent];
- // 3.1. Don't expect the installation to be removed from the storage.
- OCMReject([self.mockInstallationsStore removeInstallationForAppID:[OCMArg any]
- appName:[OCMArg any]]);
- // 3.2. Don't expect IID to be deleted.
- OCMReject([self.mockIIDStore deleteExistingIID]);
- // 4. Don't expect FIRInstallationIDDidChangeNotification to be sent.
- XCTestExpectation *notificationExpectation =
- [self installationIDDidChangeNotificationExpectation];
- notificationExpectation.inverted = YES;
- // 5. Call delete installation.
- FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
- // 6. Wait for operations to complete and check.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertEqualObjects(promise.error, error500);
- XCTAssertTrue(promise.isRejected);
- [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)testDeleteRegisteredInstallation_WhenAPIFailsWithNotFound_ThenInstallationIsRemoved {
- // 1. Expect installation to be requested from the store.
- FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
- appName:installation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:installation]);
- // 2. Expect API request to delete installation.
- FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
- [rejectedAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:404]];
- OCMExpect([self.mockAPIService deleteInstallation:installation]).andReturn(rejectedAPIPromise);
- // 2.1. Don't expect backoff calls for the delete API method.
- OCMReject([self.mockBackoffController isNextRequestAllowed]);
- [self rejectBackoffEvent];
- // 3. Expect the installation to be removed from the storage.
- OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
- appName:installation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 3.2. Expect IID to be deleted, because it is default app.
- OCMExpect([self.mockIIDStore deleteExistingIID])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
- XCTestExpectation *notificationExpectation =
- [self installationIDDidChangeNotificationExpectation];
- // 5. Call delete installation.
- FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
- // 6. Wait for operations to complete and check.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(promise.error);
- XCTAssertTrue(promise.isFulfilled);
- [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)testDeleteInstallation_WhenThereIsOngoingAuthTokenRequest_ThenUsesItsResult {
- // 1. Stub mocks for auth token request.
- // 1.1. Expect installation to be requested from the store.
- FIRInstallationsItem *storedInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation]);
- // 1.2 Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 1.3. Expect API request.
- FIRInstallationsItem *responseInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- responseInstallation.authToken.token =
- [responseInstallation.authToken.token stringByAppendingString:@"_new"];
- FBLPromise *pendingAuthTokenAPIPromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
- .andReturn(pendingAuthTokenAPIPromise);
- // 2. Send auth token request.
- [self.controller getAuthTokenForcingRefresh:YES];
- OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
- OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
- // 3. Delete installation.
- // 3.1. Don't expect installation to be requested from the store.
- OCMReject([self.mockInstallationsStore installationForAppID:[OCMArg any] appName:[OCMArg any]]);
- // 3.2. Expect API request to delete the UPDATED installation.
- OCMExpect([self.mockAPIService deleteInstallation:responseInstallation])
- .andReturn([FBLPromise resolvedWith:responseInstallation]);
- // 3.3. Expect the UPDATED installation to be removed from the storage.
- OCMExpect([self.mockInstallationsStore
- removeInstallationForAppID:responseInstallation.appID
- appName:responseInstallation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 3.4. Expect IID to be deleted, because it is default app.
- OCMExpect([self.mockIIDStore deleteExistingIID])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 3.4. Call delete installation.
- FBLPromise<NSNull *> *deletePromise = [self.controller deleteInstallation];
- // 4. Fulfill auth token promise to proceed.
- // 4.1. Expect new token to be stored on API response.
- OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- [pendingAuthTokenAPIPromise fulfill:responseInstallation];
- // 5. Wait for operations to complete and check the result.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(deletePromise.error);
- XCTAssertTrue(deletePromise.isFulfilled);
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)testDeleteInstallation_WhenNotDefaultApp_ThenIIDIsNotDeleted {
- // 0. Configure controller for not default app.
- NSString *appName = @"appName";
- [self setUpWithAppName:appName];
- // 1. Expect installation to be requested from the store.
- FIRInstallationsItem *installation =
- [FIRInstallationsItem createRegisteredInstallationItemWithAppID:self.appID appName:appName];
- OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
- appName:installation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:installation]);
- // 2. Expect API request to delete installation.
- OCMExpect([self.mockAPIService deleteInstallation:installation])
- .andReturn([FBLPromise resolvedWith:installation]);
- // 2.1. Don't expect backoff calls for the delete API method.
- OCMReject([self.mockBackoffController isNextRequestAllowed]);
- [self rejectBackoffEvent];
- // 3.1. Expect the installation to be removed from the storage.
- OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
- appName:installation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 3.2. Don't expect IID to be deleted, because it is not a default app.
- OCMReject([self.mockIIDStore deleteExistingIID]);
- // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
- XCTestExpectation *notificationExpectation =
- [self installationIDDidChangeNotificationExpectation];
- // 5. Call delete installation.
- FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
- // 6. Wait for operations to complete and check.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(promise.error);
- XCTAssertTrue(promise.isFulfilled);
- [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (NSArray<XCTestExpectation *> *)expectInstallationToBeDeletedLocally:
- (FIRInstallationsItem *)installation {
- // 3.1. Expect the installation to be removed from the storage.
- OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
- appName:installation.firebaseAppName])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 3.2. Expect IID to be deleted, because it is default app.
- OCMExpect([self.mockIIDStore deleteExistingIID])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
- XCTestExpectation *notificationExpectation =
- [self installationIDDidChangeNotificationExpectation];
- return @[ notificationExpectation ];
- }
- // TODO: Test a single delete installation request at a time.
- #pragma mark - Notifications
- - (void)testFIDDidChangeNotificationIsSentWhenFIDCreated {
- // 1. Stub - no installation.
- // 1.2. FID store.
- [self expectInstallationsStoreGetInstallationNotFound];
- OCMStub([self.mockInstallationsStore saveInstallation:[OCMArg any]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 1.3. IID store.
- [self expectStoredIIDNotFound];
- // 1.4. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 1.5. API Service.
- OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]])
- .andReturn([FBLPromise resolvedWith:[FIRInstallationsItem createRegisteredInstallationItem]]);
- // 1.6. Expect backoff success.
- [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
- // 2. Expect FIRInstallationIDDidChangeNotification to be sent.
- XCTestExpectation *notificationExpectation =
- [self installationIDDidChangeNotificationExpectation];
- // 3. Request FID.
- FBLPromise *promise = [self.controller getInstallationItem];
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- // 4. Check.
- XCTAssertNil(promise.error);
- XCTAssertNotNil(promise.value);
- [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)testRegisterInstallation_WhenServerRespondsWithDifferentFID_ThenFIDDidChangeNotification {
- // 1.1. Expect installation to be requested from the store.
- FIRInstallationsItem *storedInstallation =
- [FIRInstallationsItem createUnregisteredInstallationItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation]);
- // 1.2. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 1.3. Expect register FID to be called.
- FIRInstallationsItem *receivedInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- receivedInstallation.firebaseInstallationID =
- [storedInstallation.firebaseInstallationID stringByAppendingString:@"_new"];
- OCMExpect([self.mockAPIService registerInstallation:storedInstallation])
- .andReturn([FBLPromise resolvedWith:receivedInstallation]);
- // 1.4. Expect backoff success.
- [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
- // 1.5. Expect the received installation to be stored.
- OCMExpect([self.mockInstallationsStore saveInstallation:receivedInstallation])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 2. Expect FIRInstallationIDDidChangeNotification to be sent.
- XCTestExpectation *notificationExpectation =
- [self installationIDDidChangeNotificationExpectation];
- // 3. Request Auth Token.
- FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- // 4. Check.
- XCTAssertNil(promise.error);
- XCTAssertNotNil(promise.value);
- XCTAssertEqualObjects(promise.value.firebaseInstallationID,
- receivedInstallation.firebaseInstallationID);
- [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockAPIService);
- OCMVerifyAll(self.mockBackoffController);
- }
- #pragma mark - Helpers
- - (void)expectInstallationsStoreGetInstallationNotFound {
- NSError *notFoundError =
- [FIRInstallationsErrorUtil installationItemNotFoundForAppID:self.appID appName:self.appName];
- FBLPromise *installationNotFoundPromise = [FBLPromise pendingPromise];
- [installationNotFoundPromise reject:notFoundError];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn(installationNotFoundPromise);
- }
- - (void)expectInstallationsStoreGetInstallationCorruptedFID {
- FIRInstallationsItem *storedInstallations = [FIRInstallationsItem createCorruptedItem];
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallations]);
- }
- - (void)expectStoredIIDNotFound {
- FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
- [rejectedPromise reject:[FIRInstallationsErrorUtil keychainErrorWithFunction:@"" status:-1]];
- OCMExpect([self.mockIIDStore existingIID]).andReturn(rejectedPromise);
- OCMExpect([self.mockIIDTokenStore existingIIDDefaultToken]).andReturn(rejectedPromise);
- }
- - (void)assertValidCreatedInstallation:(FIRInstallationsItem *)installation {
- XCTAssertEqualObjects([installation class], [FIRInstallationsItem class]);
- XCTAssertEqualObjects(installation.appID, self.appID);
- XCTAssertEqualObjects(installation.firebaseAppName, self.appName);
- XCTAssertEqual(installation.registrationStatus, FIRInstallationStatusUnregistered);
- XCTAssertNotNil(installation.firebaseInstallationID);
- }
- - (XCTestExpectation *)installationIDDidChangeNotificationExpectation {
- XCTestExpectation *notificationExpectation = [self
- expectationForNotification:FIRInstallationIDDidChangeNotification
- object:nil
- handler:^BOOL(NSNotification *_Nonnull notification) {
- XCTAssertEqualObjects(
- notification
- .userInfo[kFIRInstallationIDDidChangeNotificationAppNameKey],
- self.appName);
- return YES;
- }];
- return notificationExpectation;
- }
- - (void)expectInstallationStoreToBeRequestedAndReturnInstallation:
- (FIRInstallationsItem *)storedInstallation {
- OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
- .andReturn([FBLPromise resolvedWith:storedInstallation]);
- }
- - (FIRInstallationsItem *)expectAuthTokenRefreshForInstallation:
- (FIRInstallationsItem *)installation {
- FIRInstallationsItem *responseInstallation =
- [FIRInstallationsItem createRegisteredInstallationItem];
- responseInstallation.authToken.token =
- [responseInstallation.authToken.token stringByAppendingString:@"_new"];
- // 1. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 2. Expect API request.
- OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:installation])
- .andReturn([FBLPromise resolvedWith:responseInstallation]);
- // 3. Expect new token to be stored.
- OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 4. Expect backoff success.
- [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
- return responseInstallation;
- }
- - (void)expectIsNextRequestAllowedWithResult:(BOOL)isNextRequestAllowed {
- OCMExpect([self.mockBackoffController isNextRequestAllowed]).andReturn(isNextRequestAllowed);
- }
- - (void)expectBackoffEvent:(FIRInstallationsBackoffEvent)event {
- OCMExpect([self.mockBackoffController registerEvent:event]);
- }
- - (void)rejectBackoffEvent {
- OCMReject([self.mockBackoffController registerEvent:FIRInstallationsBackoffEventSuccess]);
- OCMReject(
- [self.mockBackoffController registerEvent:FIRInstallationsBackoffEventRecoverableFailure]);
- OCMReject(
- [self.mockBackoffController registerEvent:FIRInstallationsBackoffEventUnrecoverableFailure]);
- }
- static const NSInteger kNoBackoffEvents = -1;
- - (void)expectBackoffEvent:(FIRInstallationsBackoffEvent)event
- forRegisterFIDAPIError:(NSError *)error {
- // 1. Stub store get installation.
- [self expectInstallationsStoreGetInstallationNotFound];
- // 2. Expect IIDStore to be checked for existing IID.
- [self expectStoredIIDNotFound];
- // 3. Stub store save installation.
- __block FIRInstallationsItem *createdInstallation;
- OCMExpect([self.mockInstallationsStore
- saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- [self assertValidCreatedInstallation:obj];
- createdInstallation = obj;
- return YES;
- }]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 4. Stub API register installation.
- // 4.1. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 4.2. Verify installation to be registered.
- id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- [self assertValidCreatedInstallation:obj];
- XCTAssertEqual(obj.firebaseInstallationID.length, 22);
- return YES;
- }];
- // 4.3. Expect for `registerInstallation` to be called.
- FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
- .andReturn(registerPromise);
- // 4.4. Expect backoff event.
- if (event == kNoBackoffEvents) {
- [self rejectBackoffEvent];
- } else {
- [self expectBackoffEvent:event];
- }
- // 5. Call get installation and check.
- FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
- [self.controller getInstallationItem];
- // 5.1. Wait for the stored item to be read and saved.
- OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
- // 5.2. Wait for `registerInstallation` to be called.
- OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
- // 5.3. Resolve `registerPromise` to simulate finished registration.
- [registerPromise reject:error];
- // 5.4. Wait for the task to complete.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- // Get installation returns a value no matter what.
- XCTAssertNil(getInstallationPromise.error);
- // We expect the initially created installation to be returned - must not wait for registration to
- // complete here.
- XCTAssertEqual(getInstallationPromise.value, createdInstallation);
- // 5.5. Verify registered installation was saved.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- - (void)assertGetInstallation_NewFIDCreatedAndRegistered {
- // 2. Stub store save installation.
- __block FIRInstallationsItem *createdInstallation;
- OCMExpect([self.mockInstallationsStore
- saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- [self assertValidCreatedInstallation:obj];
- createdInstallation = obj;
- return YES;
- }]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 3. Stub API register installation.
- // 3.1. Expect backoff controller to be requested.
- [self expectIsNextRequestAllowedWithResult:YES];
- // 3.2. Verify installation to be registered.
- id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- [self assertValidCreatedInstallation:obj];
- XCTAssertEqual(obj.firebaseInstallationID.length, 22);
- return YES;
- }];
- // 3.3. Expect for `registerInstallation` to be called.
- FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
- OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
- .andReturn(registerPromise);
- // 3.4. Expect backoff success.
- [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
- // 5. Call get installation and check.
- FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
- [self.controller getInstallationItem];
- // 5.1. Wait for the stored item to be read and saved.
- OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
- // 5.2. Wait for `registerInstallation` to be called.
- OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
- // 5.3. Expect for the registered installation to be saved.
- FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
- createRegisteredInstallationItemWithAppID:createdInstallation.appID
- appName:createdInstallation.firebaseAppName];
- OCMExpect([self.mockInstallationsStore
- saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
- XCTAssertEqual(registeredInstallation, obj);
- return YES;
- }]])
- .andReturn([FBLPromise resolvedWith:[NSNull null]]);
- // 5.5. Resolve `registerPromise` to simulate finished registration.
- [registerPromise fulfill:registeredInstallation];
- // 5.4. Wait for the task to complete.
- XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
- XCTAssertNil(getInstallationPromise.error);
- // We expect the initially created installation to be returned - must not wait for registration to
- // complete here.
- XCTAssertEqual(getInstallationPromise.value, createdInstallation);
- // 5.5. Verify registered installation was saved.
- OCMVerifyAll(self.mockInstallationsStore);
- OCMVerifyAll(self.mockIIDStore);
- OCMVerifyAll(self.mockBackoffController);
- }
- @end
|