FIRInstallationsIDControllerTests.m 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import <OCMock/OCMock.h>
  18. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  19. #import "FBLPromise+Testing.h"
  20. #import "FBLPromise+Then.h"
  21. #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallationsErrorUtil+Tests.h"
  22. #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallationsItem+Tests.h"
  23. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsErrorUtil.h"
  24. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsHTTPError.h"
  25. #import "FirebaseInstallations/Source/Library/IIDMigration/FIRInstallationsIIDStore.h"
  26. #import "FirebaseInstallations/Source/Library/IIDMigration/FIRInstallationsIIDTokenStore.h"
  27. #import "FirebaseInstallations/Source/Library/InstallationsAPI/FIRInstallationsAPIService.h"
  28. #import "FirebaseInstallations/Source/Library/InstallationsIDController/FIRInstallationsBackoffController.h"
  29. #import "FirebaseInstallations/Source/Library/InstallationsIDController/FIRInstallationsIDController.h"
  30. #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStore.h"
  31. #import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FIRInstallations.h"
  32. #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredAuthToken.h"
  33. @interface FIRInstallationsIDController (Tests)
  34. - (instancetype)initWithGoogleAppID:(NSString *)appID
  35. appName:(NSString *)appName
  36. installationsStore:(FIRInstallationsStore *)installationsStore
  37. APIService:(FIRInstallationsAPIService *)APIService
  38. IIDStore:(FIRInstallationsIIDStore *)IIDStore
  39. IIDTokenStore:(FIRInstallationsIIDTokenStore *)IIDTokenStore
  40. backoffController:
  41. (id<FIRInstallationsBackoffControllerProtocol>)backoffController;
  42. @end
  43. @interface FIRInstallationsIDControllerTests : XCTestCase
  44. @property(nonatomic) FIRInstallationsIDController *controller;
  45. @property(nonatomic) id mockInstallationsStore;
  46. @property(nonatomic) id mockAPIService;
  47. @property(nonatomic) id mockIIDStore;
  48. @property(nonatomic) id mockIIDTokenStore;
  49. @property(nonatomic) id mockBackoffController;
  50. @property(nonatomic) NSString *appID;
  51. @property(nonatomic) NSString *appName;
  52. @end
  53. @implementation FIRInstallationsIDControllerTests
  54. - (void)setUp {
  55. [self setUpWithAppName:kFIRDefaultAppName];
  56. }
  57. - (void)setUpWithAppName:(NSString *)appName {
  58. self.appID = @"appID";
  59. self.appName = appName;
  60. self.mockInstallationsStore = OCMStrictClassMock([FIRInstallationsStore class]);
  61. self.mockAPIService = OCMStrictClassMock([FIRInstallationsAPIService class]);
  62. self.mockIIDStore = OCMStrictClassMock([FIRInstallationsIIDStore class]);
  63. self.mockIIDTokenStore = OCMStrictClassMock([FIRInstallationsIIDTokenStore class]);
  64. self.mockBackoffController =
  65. OCMProtocolMock(@protocol(FIRInstallationsBackoffControllerProtocol));
  66. self.controller =
  67. [[FIRInstallationsIDController alloc] initWithGoogleAppID:self.appID
  68. appName:self.appName
  69. installationsStore:self.mockInstallationsStore
  70. APIService:self.mockAPIService
  71. IIDStore:self.mockIIDStore
  72. IIDTokenStore:self.mockIIDTokenStore
  73. backoffController:self.mockBackoffController];
  74. }
  75. - (void)tearDown {
  76. self.controller = nil;
  77. self.mockIIDStore = nil;
  78. self.mockAPIService = nil;
  79. self.mockInstallationsStore = nil;
  80. self.appID = nil;
  81. self.appName = nil;
  82. }
  83. #pragma mark - Initialization
  84. - (void)testInitWhenProjectIDSetThenItIsPassedToAPIService {
  85. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{
  86. kFIRAPIKey : @"api-key",
  87. kFIRProjectID : @"project-id",
  88. kFIRGoogleAppID : @"app-id",
  89. kFIRGCMSenderID : @"sender-id"
  90. }];
  91. FIRApp *app = [[FIRApp alloc] initInstanceWithName:@"app-name" options:options];
  92. OCMExpect([self.mockAPIService alloc]).andReturn(self.mockAPIService);
  93. OCMExpect([self.mockAPIService initWithAPIKey:app.options.APIKey
  94. projectID:app.options.projectID
  95. heartbeatLogger:app.heartbeatLogger])
  96. .andReturn(self.mockAPIService);
  97. FIRInstallationsIDController *controller = [[FIRInstallationsIDController alloc] initWithApp:app];
  98. XCTAssertNotNil(controller);
  99. OCMVerifyAll(self.mockAPIService);
  100. }
  101. #pragma mark - Get Installation
  102. - (void)testGetInstallationItem_WhenFIDExists_ThenItIsReturned {
  103. FIRInstallationsItem *storedInstallations =
  104. [FIRInstallationsItem createRegisteredInstallationItem];
  105. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  106. .andReturn([FBLPromise resolvedWith:storedInstallations]);
  107. // Don't expect FIRInstallationIDDidChangeNotification to be sent.
  108. XCTestExpectation *notificationExpectation =
  109. [self installationIDDidChangeNotificationExpectation];
  110. notificationExpectation.inverted = YES;
  111. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getInstallationItem];
  112. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  113. XCTAssertNil(promise.error);
  114. XCTAssertEqual(promise.value, storedInstallations);
  115. OCMVerifyAll(self.mockInstallationsStore);
  116. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  117. }
  118. - (void)testGetInstallationItem_WhenNoFIDAndNoIID_ThenFIDIsCreatedAndRegistered {
  119. [self expectInstallationsStoreGetInstallationNotFound];
  120. [self expectStoredIIDNotFound];
  121. [self assertGetInstallation_NewFIDCreatedAndRegistered];
  122. }
  123. - (void)testGetInstallationItem_WhenFIDCorruptedAndNoIID_ThenFIDIsCreatedAndRegistered {
  124. [self expectInstallationsStoreGetInstallationCorruptedFID];
  125. [self expectStoredIIDNotFound];
  126. [self assertGetInstallation_NewFIDCreatedAndRegistered];
  127. }
  128. - (void)testGetInstallationItem_WhenThereIsIIDAndNoFIDNotDefaultApp_ThenIIDIsNotUsedAsFID {
  129. // 0. Configure controller with not default app.
  130. NSString *appName = @"appName";
  131. [self setUpWithAppName:appName];
  132. // 1. Stub store get installation.
  133. [self expectInstallationsStoreGetInstallationNotFound];
  134. // 2. Don't expect IIDStore to be checked for existing IID (not default app).
  135. OCMReject([self.mockIIDStore existingIID]);
  136. // 3. Stub store save installation.
  137. __block FIRInstallationsItem *createdInstallation;
  138. OCMExpect([self.mockInstallationsStore
  139. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  140. [self assertValidCreatedInstallation:obj];
  141. createdInstallation = obj;
  142. return YES;
  143. }]])
  144. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  145. // 4. Stub API register installation.
  146. // 4.1. Expect backoff controller to be requested.
  147. [self expectIsNextRequestAllowedWithResult:YES];
  148. // 4.2. Verify installation to be registered.
  149. id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  150. [self assertValidCreatedInstallation:obj];
  151. return YES;
  152. }];
  153. // 4.3. Expect for `registerInstallation` to be called.
  154. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  155. OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
  156. .andReturn(registerPromise);
  157. // 4.4. Expect backoff success.
  158. [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
  159. // 5. Call get installation and check.
  160. FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
  161. [self.controller getInstallationItem];
  162. // 5.1. Wait for the stored item to be read and saved.
  163. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  164. // 5.2. Wait for `registerInstallation` to be called.
  165. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  166. // 5.3. Expect for the registered installation to be saved.
  167. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  168. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  169. appName:createdInstallation.firebaseAppName];
  170. OCMExpect([self.mockInstallationsStore
  171. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  172. XCTAssertEqual(registeredInstallation, obj);
  173. return YES;
  174. }]])
  175. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  176. // 5.5. Resolve `registerPromise` to simulate finished registration.
  177. [registerPromise fulfill:registeredInstallation];
  178. // 5.4. Wait for the task to complete.
  179. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  180. XCTAssertNil(getInstallationPromise.error);
  181. // We expect the initially created installation to be returned - must not wait for registration to
  182. // complete here.
  183. XCTAssertEqual(getInstallationPromise.value, createdInstallation);
  184. // 5.5. Verify registered installation was saved.
  185. OCMVerifyAll(self.mockInstallationsStore);
  186. OCMVerifyAll(self.mockIIDStore);
  187. OCMVerifyAll(self.mockBackoffController);
  188. }
  189. - (void)testGetInstallationItem_WhenThereIsIIDAndNoFID_ThenIIDIsRegisteredAsFID {
  190. // 1. Stub store get installation.
  191. [self expectInstallationsStoreGetInstallationNotFound];
  192. // 2. Expect IIDStore to be checked for existing IID.
  193. NSString *existingIID = @"existing-iid";
  194. OCMExpect([self.mockIIDStore existingIID]).andReturn([FBLPromise resolvedWith:existingIID]);
  195. // 3. Expect IID checkin store to be requested for checkin data.
  196. NSString *existingIIDDefaultToken = @"existing-iid-token";
  197. OCMExpect([self.mockIIDTokenStore existingIIDDefaultToken])
  198. .andReturn([FBLPromise resolvedWith:existingIIDDefaultToken]);
  199. // 3. Stub store save installation.
  200. __block FIRInstallationsItem *createdInstallation;
  201. OCMExpect([self.mockInstallationsStore
  202. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  203. [self assertValidCreatedInstallation:obj];
  204. XCTAssertEqualObjects(existingIID, obj.firebaseInstallationID);
  205. XCTAssertEqualObjects(obj.IIDDefaultToken, existingIIDDefaultToken);
  206. createdInstallation = obj;
  207. return YES;
  208. }]])
  209. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  210. // 4. Stub API register installation.
  211. // 4.1. Expect backoff controller to be requested.
  212. [self expectIsNextRequestAllowedWithResult:YES];
  213. // 4.2. Verify installation to be registered.
  214. id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  215. [self assertValidCreatedInstallation:obj];
  216. XCTAssertEqualObjects(existingIID, obj.firebaseInstallationID);
  217. return YES;
  218. }];
  219. // 4.3. Expect for `registerInstallation` to be called.
  220. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  221. OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
  222. .andReturn(registerPromise);
  223. // 4.4. Expect backoff success.
  224. [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
  225. // 5. Call get installation and check.
  226. FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
  227. [self.controller getInstallationItem];
  228. // 5.1. Wait for the stored item to be read and saved.
  229. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  230. // 5.2. Wait for `registerInstallation` to be called.
  231. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  232. // 5.3. Expect for the registered installation to be saved.
  233. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  234. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  235. appName:createdInstallation.firebaseAppName];
  236. OCMExpect([self.mockInstallationsStore
  237. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  238. XCTAssertEqual(registeredInstallation, obj);
  239. return YES;
  240. }]])
  241. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  242. // 5.5. Resolve `registerPromise` to simulate finished registration.
  243. [registerPromise fulfill:registeredInstallation];
  244. // 5.4. Wait for the task to complete.
  245. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  246. XCTAssertNil(getInstallationPromise.error);
  247. // We expect the initially created installation to be returned - must not wait for registration to
  248. // complete here.
  249. XCTAssertEqual(getInstallationPromise.value, createdInstallation);
  250. // 5.5. Verify registered installation was saved.
  251. OCMVerifyAll(self.mockInstallationsStore);
  252. OCMVerifyAll(self.mockIIDStore);
  253. OCMVerifyAll(self.mockIIDTokenStore);
  254. OCMVerifyAll(self.mockBackoffController);
  255. }
  256. - (void)testGetInstallationItem_WhenCalledWhileRegistering_DoesNotWaitForAPIResponse {
  257. // 1. Expect the installation to be requested from the store only once.
  258. FIRInstallationsItem *storedInstallation1 =
  259. [FIRInstallationsItem createUnregisteredInstallationItem];
  260. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  261. .andReturn([FBLPromise resolvedWith:storedInstallation1]);
  262. // 2. Expect backoff controller to be requested.
  263. [self expectIsNextRequestAllowedWithResult:YES];
  264. // 3. Expect registration API request to be sent.
  265. FBLPromise<FIRInstallationsItem *> *pendingAPIPromise = [FBLPromise pendingPromise];
  266. OCMExpect([self.mockAPIService registerInstallation:storedInstallation1])
  267. .andReturn(pendingAPIPromise);
  268. // 4. Expect backoff recoverable error for HTTP 429.
  269. [self expectBackoffEvent:FIRInstallationsBackoffEventRecoverableFailure];
  270. // 5. Request and wait for 1st FID.
  271. FBLPromise<FIRInstallationsItem *> *getInstallationPromise1 =
  272. [self.controller getInstallationItem];
  273. XCTestExpectation *getInstallationsExpectation1 =
  274. [self expectationWithDescription:@"getInstallationsExpectation1"];
  275. getInstallationPromise1.then(^id(FIRInstallationsItem *installation) {
  276. [getInstallationsExpectation1 fulfill];
  277. return nil;
  278. });
  279. [self waitForExpectations:@[ getInstallationsExpectation1 ] timeout:0.5];
  280. // 6. Request FID 2nd time.
  281. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  282. .andReturn([FBLPromise resolvedWith:storedInstallation1]);
  283. FBLPromise<FIRInstallationsItem *> *getInstallationPromise2 =
  284. [self.controller getInstallationItem];
  285. XCTestExpectation *getInstallationsExpectation2 =
  286. [self expectationWithDescription:@"getInstallationsExpectation2"];
  287. getInstallationPromise2.then(^id(FIRInstallationsItem *installation) {
  288. [getInstallationsExpectation2 fulfill];
  289. return nil;
  290. });
  291. [self waitForExpectations:@[ getInstallationsExpectation2 ] timeout:0.5];
  292. // 7. Resolve API promise.
  293. [pendingAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:429]];
  294. // 8. Check
  295. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  296. OCMVerifyAll(self.mockInstallationsStore);
  297. OCMVerifyAll(self.mockAPIService);
  298. OCMVerifyAll(self.mockBackoffController);
  299. }
  300. - (void)testGetInstallationItem_WhenCalledSeveralTimesWaitingForStore_OnlyOneOperationIsPerformed {
  301. // 1. Expect the installation to be requested from the store only once.
  302. FIRInstallationsItem *storedInstallation1 =
  303. [FIRInstallationsItem createRegisteredInstallationItem];
  304. FBLPromise<FIRInstallationsItem *> *pendingStorePromise = [FBLPromise pendingPromise];
  305. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  306. .andReturn(pendingStorePromise);
  307. // 2. Request installation n times
  308. NSInteger requestCount = 10;
  309. NSMutableArray *installationPromises = [NSMutableArray arrayWithCapacity:requestCount];
  310. for (NSInteger i = 0; i < requestCount; i++) {
  311. [installationPromises addObject:[self.controller getInstallationItem]];
  312. }
  313. // 3. Resolve store promise.
  314. [pendingStorePromise fulfill:storedInstallation1];
  315. // 4. Wait for operation to be completed and check.
  316. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  317. for (FBLPromise<FIRInstallationsItem *> *installationPromise in installationPromises) {
  318. XCTAssertNil(installationPromise.error);
  319. XCTAssertEqual(installationPromise.value, storedInstallation1);
  320. }
  321. OCMVerifyAll(self.mockInstallationsStore);
  322. OCMVerifyAll(self.mockAPIService);
  323. // 5. Check that a new request is performed once previous finished.
  324. FIRInstallationsItem *storedInstallation2 =
  325. [FIRInstallationsItem createRegisteredInstallationItem];
  326. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  327. .andReturn([FBLPromise resolvedWith:storedInstallation2]);
  328. FBLPromise<FIRInstallationsItem *> *installationPromise = [self.controller getInstallationItem];
  329. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  330. XCTAssertNil(installationPromise.error);
  331. XCTAssertEqual(installationPromise.value, storedInstallation2);
  332. OCMVerifyAll(self.mockInstallationsStore);
  333. OCMVerifyAll(self.mockAPIService);
  334. }
  335. - (void)testGetInstallationItem_WhenCalledSeveralTimesWaitingForAPI_OnlyOneAPIRequestIsSent {
  336. // 1. Expect a single API request.
  337. // 1.1. Expect backoff controller to be requested.
  338. [self expectIsNextRequestAllowedWithResult:YES];
  339. // 1.2. Expect the request itself.
  340. FBLPromise<FIRInstallationsItem *> *registerAPIPromise = [FBLPromise pendingPromise];
  341. OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]]).andReturn(registerAPIPromise);
  342. // 2. Request FID multiple times.
  343. NSInteger requestCount = 10;
  344. for (NSInteger i = 0; i < requestCount; i++) {
  345. XCTestExpectation *getFIDExpectation = [self
  346. expectationWithDescription:[NSString stringWithFormat:@"getFIDExpectation%ld", (long)i]];
  347. // 2.1. Expect stored FID to be requested.
  348. FIRInstallationsItem *storedInstallation =
  349. [FIRInstallationsItem createUnregisteredInstallationItem];
  350. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  351. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  352. // 2.2. Expect the FID to be returned.
  353. FBLPromise<FIRInstallationsItem *> *getFIDPromise = [self.controller getInstallationItem];
  354. [getFIDPromise then:^id _Nullable(FIRInstallationsItem *_Nullable value) {
  355. XCTAssertNotNil(value);
  356. XCTAssertEqualObjects(value.firebaseInstallationID,
  357. storedInstallation.firebaseInstallationID);
  358. [getFIDExpectation fulfill];
  359. return nil;
  360. }];
  361. [self waitForExpectations:@[ getFIDExpectation ] timeout:0.5];
  362. }
  363. // 3. Expect backoff unrecoverable failure.
  364. [self expectBackoffEvent:FIRInstallationsBackoffEventUnrecoverableFailure];
  365. // 4. Finish API request.
  366. [registerAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:400]];
  367. // 5. Verify mocks
  368. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  369. OCMVerifyAll(self.mockInstallationsStore);
  370. OCMVerifyAll(self.mockAPIService);
  371. OCMVerifyAll(self.mockBackoffController);
  372. }
  373. - (void)testGetInstallationItem_WhenRegistrationError_ThenBackoffEventIsLogged {
  374. [self expectBackoffEvent:FIRInstallationsBackoffEventUnrecoverableFailure
  375. forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:400]];
  376. [self expectBackoffEvent:FIRInstallationsBackoffEventUnrecoverableFailure
  377. forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:403]];
  378. [self expectBackoffEvent:FIRInstallationsBackoffEventRecoverableFailure
  379. forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:429]];
  380. [self expectBackoffEvent:FIRInstallationsBackoffEventRecoverableFailure
  381. forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:500]];
  382. [self expectBackoffEvent:FIRInstallationsBackoffEventRecoverableFailure
  383. forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:503]];
  384. // An arbitrary unknown server response.
  385. [self expectBackoffEvent:FIRInstallationsBackoffEventRecoverableFailure
  386. forRegisterFIDAPIError:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:444]];
  387. // A connection error.
  388. [self expectBackoffEvent:kNoBackoffEvents
  389. forRegisterFIDAPIError:[FIRInstallationsErrorUtil
  390. networkErrorWithError:[NSError errorWithDomain:@"tests"
  391. code:-1
  392. userInfo:nil]]];
  393. // An unknown error.
  394. [self expectBackoffEvent:kNoBackoffEvents
  395. forRegisterFIDAPIError:[NSError errorWithDomain:@"tests" code:-1 userInfo:nil]];
  396. }
  397. - (void)testGetInstallationItem_WhenNextRequestIsNotAllowed {
  398. // 1. Stub store get installation.
  399. [self expectInstallationsStoreGetInstallationNotFound];
  400. // 2. Stub store save installation.
  401. __block FIRInstallationsItem *createdInstallation;
  402. OCMExpect([self.mockInstallationsStore
  403. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  404. [self assertValidCreatedInstallation:obj];
  405. createdInstallation = obj;
  406. return YES;
  407. }]])
  408. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  409. // 3. Expect IIDStore to be checked for existing IID.
  410. [self expectStoredIIDNotFound];
  411. // 4. Stub API register installation.
  412. // 4.1. Expect backoff controller to be requested.
  413. [self expectIsNextRequestAllowedWithResult:NO];
  414. // 4.2. Don't expect for `registerInstallation` to be called.
  415. OCMReject([self.mockAPIService registerInstallation:[OCMArg any]]);
  416. // 4.3. Don't expect backoff updates.
  417. [self rejectBackoffEvent];
  418. // 5. Call get installation and check.
  419. FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
  420. [self.controller getInstallationItem];
  421. // 5.1. Wait for the stored item to be read and saved.
  422. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  423. // 5.2. Wait for `registerInstallation` to be called.
  424. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  425. // 5.3. Don't Expect for the registered installation to be saved.
  426. OCMReject([self.mockInstallationsStore saveInstallation:[OCMArg any]]);
  427. // 5.4. Wait for the task to complete.
  428. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  429. XCTAssertNil(getInstallationPromise.error);
  430. // We expect the initially created installation to be returned - must not wait for registration to
  431. // complete here.
  432. XCTAssertEqual(getInstallationPromise.value, createdInstallation);
  433. // 5.5. Verify registered installation was saved.
  434. OCMVerifyAll(self.mockInstallationsStore);
  435. OCMVerifyAll(self.mockIIDStore);
  436. OCMVerifyAll(self.mockBackoffController);
  437. }
  438. #pragma mark - Get Auth Token
  439. - (void)testGetAuthToken_WhenValidInstallationExists_ThenItIsReturned {
  440. // 1. Expect installation to be requested from the store.
  441. FIRInstallationsItem *storedInstallation =
  442. [FIRInstallationsItem createRegisteredInstallationItem];
  443. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  444. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  445. // 2. Request auth token.
  446. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  447. // 3. Wait for the promise to resolve.
  448. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  449. // 4. Check.
  450. OCMVerifyAll(self.mockInstallationsStore);
  451. OCMVerifyAll(self.mockAPIService);
  452. XCTAssertNil(promise.error);
  453. XCTAssertNotNil(promise.value);
  454. XCTAssertEqualObjects(promise.value.authToken.token, storedInstallation.authToken.token);
  455. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  456. storedInstallation.authToken.expirationDate);
  457. }
  458. - (void)testGetAuthToken_WhenValidInstallationWithExpiredTokenExists_ThenTokenRequested {
  459. // 1.1. Expect installation to be requested from the store.
  460. FIRInstallationsItem *storedInstallation =
  461. [FIRInstallationsItem createRegisteredInstallationItem];
  462. storedInstallation.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 - 1];
  463. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  464. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  465. // 1.2. Auth Token refresh.
  466. FIRInstallationsItem *responseInstallation =
  467. [self expectAuthTokenRefreshForInstallation:storedInstallation];
  468. // 2. Request auth token.
  469. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  470. // 3. Wait for the promise to resolve.
  471. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  472. // 4. Check.
  473. OCMVerifyAll(self.mockInstallationsStore);
  474. OCMVerifyAll(self.mockAPIService);
  475. OCMVerifyAll(self.mockBackoffController);
  476. XCTAssertNil(promise.error);
  477. XCTAssertNotNil(promise.value);
  478. XCTAssertEqualObjects(promise.value.authToken.token, responseInstallation.authToken.token);
  479. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  480. responseInstallation.authToken.expirationDate);
  481. }
  482. - (void)testGetAuthTokenForcingRefresh_WhenValidInstallationExists_ThenTokenRequested {
  483. // 1.1. Expect installation to be requested from the store.
  484. FIRInstallationsItem *storedInstallation =
  485. [FIRInstallationsItem createRegisteredInstallationItem];
  486. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  487. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  488. // 1.2. Auth Token refresh.
  489. FIRInstallationsItem *responseInstallation =
  490. [self expectAuthTokenRefreshForInstallation:storedInstallation];
  491. // 2. Request auth token.
  492. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
  493. // 3. Wait for the promise to resolve.
  494. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  495. // 4. Check.
  496. OCMVerifyAll(self.mockInstallationsStore);
  497. OCMVerifyAll(self.mockAPIService);
  498. OCMVerifyAll(self.mockBackoffController);
  499. XCTAssertNil(promise.error);
  500. XCTAssertNotNil(promise.value);
  501. XCTAssertEqualObjects(promise.value.authToken.token, responseInstallation.authToken.token);
  502. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  503. responseInstallation.authToken.expirationDate);
  504. }
  505. - (void)testGetAuthToken_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
  506. // 1. Expect installation to be requested from the store.
  507. FIRInstallationsItem *storedInstallation =
  508. [FIRInstallationsItem createRegisteredInstallationItem];
  509. FBLPromise *storagePendingPromise = [FBLPromise pendingPromise];
  510. // Expect the installation to be requested only once.
  511. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  512. .andReturn(storagePendingPromise);
  513. // 2. Request auth token n times.
  514. NSInteger requestCount = 10;
  515. NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
  516. for (NSInteger i = 0; i < requestCount; i++) {
  517. [authTokenPromises addObject:[self.controller getAuthTokenForcingRefresh:NO]];
  518. }
  519. // 3. Finish the storage request.
  520. [storagePendingPromise fulfill:storedInstallation];
  521. // 4. Wait for the promise to resolve.
  522. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  523. // 5. Check.
  524. OCMVerifyAll(self.mockInstallationsStore);
  525. for (FBLPromise<FIRInstallationsItem *> *authPromise in authTokenPromises) {
  526. XCTAssertNil(authPromise.error);
  527. XCTAssertNotNil(authPromise.value);
  528. XCTAssertEqualObjects(authPromise.value.authToken.token, storedInstallation.authToken.token);
  529. XCTAssertEqualObjects(authPromise.value.authToken.expirationDate,
  530. storedInstallation.authToken.expirationDate);
  531. }
  532. }
  533. - (void)testGetAuthTokenForceRefresh_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
  534. // 1.1. Expect installation to be requested from the store.
  535. FIRInstallationsItem *storedInstallation =
  536. [FIRInstallationsItem createRegisteredInstallationItem];
  537. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  538. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  539. // 1.2. Expect backoff controller to be requested.
  540. [self expectIsNextRequestAllowedWithResult:YES];
  541. // 1.3. Expect API request.
  542. FIRInstallationsItem *responseInstallation =
  543. [FIRInstallationsItem createRegisteredInstallationItem];
  544. responseInstallation.authToken.token =
  545. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  546. FBLPromise *pendingAPIPromise = [FBLPromise pendingPromise];
  547. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  548. .andReturn(pendingAPIPromise);
  549. // 1.4. Expect backoff success.
  550. [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
  551. // 1.5. Expect new token to be stored.
  552. OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
  553. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  554. // 2. Request auth token n times.
  555. NSInteger requestCount = 10;
  556. NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
  557. for (NSInteger i = 0; i < requestCount; i++) {
  558. [authTokenPromises addObject:[self.controller getAuthTokenForcingRefresh:YES]];
  559. }
  560. // 3. Finish the API request.
  561. [pendingAPIPromise fulfill:responseInstallation];
  562. // 4. Wait for the promise to resolve.
  563. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  564. // 5. Check.
  565. OCMVerifyAll(self.mockInstallationsStore);
  566. OCMVerifyAll(self.mockBackoffController);
  567. for (FBLPromise<FIRInstallationsItem *> *authPromise in authTokenPromises) {
  568. XCTAssertNil(authPromise.error);
  569. XCTAssertNotNil(authPromise.value);
  570. XCTAssertEqualObjects(authPromise.value.authToken.token, responseInstallation.authToken.token);
  571. XCTAssertEqualObjects(authPromise.value.authToken.expirationDate,
  572. responseInstallation.authToken.expirationDate);
  573. }
  574. }
  575. - (void)testGetAuthToken_WhenAPIResponse404_ThenFISResetAndReregistered {
  576. NSTimeInterval timeout = 0.5;
  577. // 1.1. Expect installation to be requested from the store.
  578. FIRInstallationsItem *storedInstallation =
  579. [FIRInstallationsItem createRegisteredInstallationItem];
  580. [self expectInstallationStoreToBeRequestedAndReturnInstallation:storedInstallation];
  581. // 1.2. Expect backoff controller to be requested.
  582. [self expectIsNextRequestAllowedWithResult:YES];
  583. // 1.3. Expect API request.
  584. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  585. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  586. .andReturn(rejectedAPIPromise);
  587. // 1.4. We don't expect a backoff event corresponding to the 404 sever response as the error is
  588. // auto-recovered by the controller.
  589. // 2. Request auth token.
  590. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
  591. // 3. Wait for refresh token request.
  592. OCMVerifyAllWithDelay(self.mockAPIService, timeout);
  593. // 4.1. Expect Installation to be requested before deletion.
  594. [self expectInstallationStoreToBeRequestedAndReturnInstallation:storedInstallation];
  595. // 4. Expect for FIS to be deleted locally.
  596. NSArray<XCTestExpectation *> *deleteExpectations =
  597. [self expectInstallationToBeDeletedLocally:storedInstallation];
  598. // 6. Expect a new installation to be created and registered.
  599. // 6.1. Expect to request FIS from storage.
  600. [self expectInstallationsStoreGetInstallationNotFound];
  601. // 6.2. Expect stored IID not found.
  602. [self expectStoredIIDNotFound];
  603. // 6.3. Expect new Installation to be stored.
  604. __block FIRInstallationsItem *createdInstallation;
  605. OCMExpect([self.mockInstallationsStore
  606. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  607. [self assertValidCreatedInstallation:obj];
  608. createdInstallation = obj;
  609. return YES;
  610. }]])
  611. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  612. // 6.4 Expect backoff controller to be requested.
  613. [self expectIsNextRequestAllowedWithResult:YES];
  614. // 6.5. Expect registration API request to be sent.
  615. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  616. OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]]).andReturn(registerPromise);
  617. // 6.6. Expect backoff success.
  618. [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
  619. // 6.7. Reject API request promise with 404.
  620. NSError *error404 = [FIRInstallationsErrorUtil APIErrorWithHTTPCode:404];
  621. [rejectedAPIPromise reject:error404];
  622. // 6.8. Wait local FIS to be deleted.
  623. [self waitForExpectations:deleteExpectations timeout:timeout];
  624. // 6.9 Wait for the new Installation to be stored.
  625. OCMVerifyAllWithDelay(self.mockInstallationsStore, timeout);
  626. // 6.10. Wait for registration API request to be sent.
  627. OCMVerifyAllWithDelay(self.mockAPIService, timeout);
  628. // 6.11. Expect for the registered installation to be saved.
  629. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  630. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  631. appName:createdInstallation.firebaseAppName];
  632. OCMExpect([self.mockInstallationsStore
  633. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  634. XCTAssertEqual(registeredInstallation, obj);
  635. return YES;
  636. }]])
  637. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  638. // 6.12. Fulfill the registration API request promise.
  639. [registerPromise fulfill:registeredInstallation];
  640. // 7. Wait for promises.
  641. XCTAssert(FBLWaitForPromisesWithTimeout(timeout));
  642. // 8. Check.
  643. OCMVerifyAll(self.mockInstallationsStore);
  644. OCMVerifyAll(self.mockAPIService);
  645. OCMVerifyAll(self.mockBackoffController);
  646. XCTAssertNil(promise.error);
  647. XCTAssertNotNil(promise.value);
  648. XCTAssertNotEqualObjects(promise.value.firebaseInstallationID,
  649. storedInstallation.firebaseInstallationID);
  650. XCTAssertEqualObjects(promise.value, registeredInstallation);
  651. }
  652. - (void)testGetAuthToken_WhenNextRequestIsNotAllowed {
  653. // 1.1. Expect installation to be requested from the store.
  654. FIRInstallationsItem *storedInstallation =
  655. [FIRInstallationsItem createRegisteredInstallationItem];
  656. storedInstallation.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 - 1];
  657. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  658. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  659. // 1.2 Expect backoff controller to be requested.
  660. [self expectIsNextRequestAllowedWithResult:NO];
  661. // 1.3. Don't expect API request.
  662. OCMReject([self.mockAPIService refreshAuthTokenForInstallation:[OCMArg any]]);
  663. // 1.4. Don't expect new token to be stored.
  664. OCMReject([self.mockInstallationsStore saveInstallation:[OCMArg any]]);
  665. // 1.5. Don't expect backoff events.
  666. [self rejectBackoffEvent];
  667. // 2. Request auth token.
  668. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  669. // 3. Wait for the promise to resolve.
  670. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  671. // 4. Check.
  672. OCMVerifyAll(self.mockInstallationsStore);
  673. OCMVerifyAll(self.mockAPIService);
  674. OCMVerifyAll(self.mockBackoffController);
  675. XCTAssertNil(promise.value);
  676. XCTAssertNotNil(promise.error);
  677. XCTAssertEqualObjects(promise.error, [FIRInstallationsErrorUtil backoffIntervalWaitError]);
  678. }
  679. #pragma mark - FID Deletion
  680. - (void)testDeleteRegisteredInstallation {
  681. // 1. Expect installation to be requested from the store.
  682. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  683. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  684. appName:installation.firebaseAppName])
  685. .andReturn([FBLPromise resolvedWith:installation]);
  686. // 2. Expect API request to delete installation.
  687. OCMExpect([self.mockAPIService deleteInstallation:installation])
  688. .andReturn([FBLPromise resolvedWith:installation]);
  689. // 2.1. Don't expect backoff calls for the delete API method.
  690. OCMReject([self.mockBackoffController isNextRequestAllowed]);
  691. [self rejectBackoffEvent];
  692. // 3.1. Expect the installation to be removed from the storage.
  693. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  694. appName:installation.firebaseAppName])
  695. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  696. // 3.2. Expect IID to be deleted, because it is default app.
  697. OCMExpect([self.mockIIDStore deleteExistingIID])
  698. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  699. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  700. XCTestExpectation *notificationExpectation =
  701. [self installationIDDidChangeNotificationExpectation];
  702. // 5. Call delete installation.
  703. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  704. // 6. Wait for operations to complete and check.
  705. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  706. XCTAssertNil(promise.error);
  707. XCTAssertTrue(promise.isFulfilled);
  708. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  709. OCMVerifyAll(self.mockInstallationsStore);
  710. OCMVerifyAll(self.mockAPIService);
  711. OCMVerifyAll(self.mockIIDStore);
  712. OCMVerifyAll(self.mockBackoffController);
  713. }
  714. - (void)testDeleteUnregisteredInstallation {
  715. // 1. Expect installation to be requested from the store.
  716. FIRInstallationsItem *installation = [FIRInstallationsItem createUnregisteredInstallationItem];
  717. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  718. appName:installation.firebaseAppName])
  719. .andReturn([FBLPromise resolvedWith:installation]);
  720. // 2. Don't expect backoff calls for the delete API method.
  721. OCMReject([self.mockBackoffController isNextRequestAllowed]);
  722. [self rejectBackoffEvent];
  723. // 3. Don't expect API request to delete installation.
  724. OCMReject([self.mockAPIService deleteInstallation:[OCMArg any]]);
  725. // 4.1. Expect the installation to be removed from the storage.
  726. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  727. appName:installation.firebaseAppName])
  728. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  729. // 4.2. Expect IID to be deleted, because it is default app.
  730. OCMExpect([self.mockIIDStore deleteExistingIID])
  731. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  732. // 5. Expect FIRInstallationIDDidChangeNotification to be sent.
  733. XCTestExpectation *notificationExpectation =
  734. [self installationIDDidChangeNotificationExpectation];
  735. // 6. Call delete installation.
  736. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  737. // 7. Wait for operations to complete and check.
  738. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  739. XCTAssertNil(promise.error);
  740. XCTAssertTrue(promise.isFulfilled);
  741. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  742. OCMVerifyAll(self.mockInstallationsStore);
  743. OCMVerifyAll(self.mockAPIService);
  744. OCMVerifyAll(self.mockIIDStore);
  745. OCMVerifyAll(self.mockBackoffController);
  746. }
  747. - (void)testDeleteRegisteredInstallation_WhenAPIRequestFails_ThenFailsAndInstallationIsNotRemoved {
  748. // 1. Expect installation to be requested from the store.
  749. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  750. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  751. appName:installation.firebaseAppName])
  752. .andReturn([FBLPromise resolvedWith:installation]);
  753. // 2. Expect API request to delete installation.
  754. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  755. NSError *error500 =
  756. [FIRInstallationsErrorUtil APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError];
  757. [rejectedAPIPromise reject:error500];
  758. OCMExpect([self.mockAPIService deleteInstallation:installation]).andReturn(rejectedAPIPromise);
  759. // 2.1. Don't expect backoff calls for the delete API method.
  760. OCMReject([self.mockBackoffController isNextRequestAllowed]);
  761. [self rejectBackoffEvent];
  762. // 3.1. Don't expect the installation to be removed from the storage.
  763. OCMReject([self.mockInstallationsStore removeInstallationForAppID:[OCMArg any]
  764. appName:[OCMArg any]]);
  765. // 3.2. Don't expect IID to be deleted.
  766. OCMReject([self.mockIIDStore deleteExistingIID]);
  767. // 4. Don't expect FIRInstallationIDDidChangeNotification to be sent.
  768. XCTestExpectation *notificationExpectation =
  769. [self installationIDDidChangeNotificationExpectation];
  770. notificationExpectation.inverted = YES;
  771. // 5. Call delete installation.
  772. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  773. // 6. Wait for operations to complete and check.
  774. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  775. XCTAssertEqualObjects(promise.error, error500);
  776. XCTAssertTrue(promise.isRejected);
  777. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  778. OCMVerifyAll(self.mockInstallationsStore);
  779. OCMVerifyAll(self.mockAPIService);
  780. OCMVerifyAll(self.mockIIDStore);
  781. OCMVerifyAll(self.mockBackoffController);
  782. }
  783. - (void)testDeleteRegisteredInstallation_WhenAPIFailsWithNotFound_ThenInstallationIsRemoved {
  784. // 1. Expect installation to be requested from the store.
  785. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  786. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  787. appName:installation.firebaseAppName])
  788. .andReturn([FBLPromise resolvedWith:installation]);
  789. // 2. Expect API request to delete installation.
  790. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  791. [rejectedAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:404]];
  792. OCMExpect([self.mockAPIService deleteInstallation:installation]).andReturn(rejectedAPIPromise);
  793. // 2.1. Don't expect backoff calls for the delete API method.
  794. OCMReject([self.mockBackoffController isNextRequestAllowed]);
  795. [self rejectBackoffEvent];
  796. // 3. Expect the installation to be removed from the storage.
  797. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  798. appName:installation.firebaseAppName])
  799. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  800. // 3.2. Expect IID to be deleted, because it is default app.
  801. OCMExpect([self.mockIIDStore deleteExistingIID])
  802. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  803. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  804. XCTestExpectation *notificationExpectation =
  805. [self installationIDDidChangeNotificationExpectation];
  806. // 5. Call delete installation.
  807. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  808. // 6. Wait for operations to complete and check.
  809. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  810. XCTAssertNil(promise.error);
  811. XCTAssertTrue(promise.isFulfilled);
  812. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  813. OCMVerifyAll(self.mockInstallationsStore);
  814. OCMVerifyAll(self.mockAPIService);
  815. OCMVerifyAll(self.mockIIDStore);
  816. OCMVerifyAll(self.mockBackoffController);
  817. }
  818. - (void)testDeleteInstallation_WhenThereIsOngoingAuthTokenRequest_ThenUsesItsResult {
  819. // 1. Stub mocks for auth token request.
  820. // 1.1. Expect installation to be requested from the store.
  821. FIRInstallationsItem *storedInstallation =
  822. [FIRInstallationsItem createRegisteredInstallationItem];
  823. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  824. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  825. // 1.2 Expect backoff controller to be requested.
  826. [self expectIsNextRequestAllowedWithResult:YES];
  827. // 1.3. Expect API request.
  828. FIRInstallationsItem *responseInstallation =
  829. [FIRInstallationsItem createRegisteredInstallationItem];
  830. responseInstallation.authToken.token =
  831. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  832. FBLPromise *pendingAuthTokenAPIPromise = [FBLPromise pendingPromise];
  833. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  834. .andReturn(pendingAuthTokenAPIPromise);
  835. // 2. Send auth token request.
  836. [self.controller getAuthTokenForcingRefresh:YES];
  837. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  838. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  839. // 3. Delete installation.
  840. // 3.1. Don't expect installation to be requested from the store.
  841. OCMReject([self.mockInstallationsStore installationForAppID:[OCMArg any] appName:[OCMArg any]]);
  842. // 3.2. Expect API request to delete the UPDATED installation.
  843. OCMExpect([self.mockAPIService deleteInstallation:responseInstallation])
  844. .andReturn([FBLPromise resolvedWith:responseInstallation]);
  845. // 3.3. Expect the UPDATED installation to be removed from the storage.
  846. OCMExpect([self.mockInstallationsStore
  847. removeInstallationForAppID:responseInstallation.appID
  848. appName:responseInstallation.firebaseAppName])
  849. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  850. // 3.4. Expect IID to be deleted, because it is default app.
  851. OCMExpect([self.mockIIDStore deleteExistingIID])
  852. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  853. // 3.4. Call delete installation.
  854. FBLPromise<NSNull *> *deletePromise = [self.controller deleteInstallation];
  855. // 4. Fulfill auth token promise to proceed.
  856. // 4.1. Expect new token to be stored on API response.
  857. OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
  858. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  859. [pendingAuthTokenAPIPromise fulfill:responseInstallation];
  860. // 5. Wait for operations to complete and check the result.
  861. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  862. XCTAssertNil(deletePromise.error);
  863. XCTAssertTrue(deletePromise.isFulfilled);
  864. OCMVerifyAll(self.mockInstallationsStore);
  865. OCMVerifyAll(self.mockAPIService);
  866. OCMVerifyAll(self.mockIIDStore);
  867. OCMVerifyAll(self.mockBackoffController);
  868. }
  869. - (void)testDeleteInstallation_WhenNotDefaultApp_ThenIIDIsNotDeleted {
  870. // 0. Configure controller for not default app.
  871. NSString *appName = @"appName";
  872. [self setUpWithAppName:appName];
  873. // 1. Expect installation to be requested from the store.
  874. FIRInstallationsItem *installation =
  875. [FIRInstallationsItem createRegisteredInstallationItemWithAppID:self.appID appName:appName];
  876. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  877. appName:installation.firebaseAppName])
  878. .andReturn([FBLPromise resolvedWith:installation]);
  879. // 2. Expect API request to delete installation.
  880. OCMExpect([self.mockAPIService deleteInstallation:installation])
  881. .andReturn([FBLPromise resolvedWith:installation]);
  882. // 2.1. Don't expect backoff calls for the delete API method.
  883. OCMReject([self.mockBackoffController isNextRequestAllowed]);
  884. [self rejectBackoffEvent];
  885. // 3.1. Expect the installation to be removed from the storage.
  886. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  887. appName:installation.firebaseAppName])
  888. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  889. // 3.2. Don't expect IID to be deleted, because it is not a default app.
  890. OCMReject([self.mockIIDStore deleteExistingIID]);
  891. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  892. XCTestExpectation *notificationExpectation =
  893. [self installationIDDidChangeNotificationExpectation];
  894. // 5. Call delete installation.
  895. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  896. // 6. Wait for operations to complete and check.
  897. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  898. XCTAssertNil(promise.error);
  899. XCTAssertTrue(promise.isFulfilled);
  900. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  901. OCMVerifyAll(self.mockInstallationsStore);
  902. OCMVerifyAll(self.mockAPIService);
  903. OCMVerifyAll(self.mockIIDStore);
  904. OCMVerifyAll(self.mockBackoffController);
  905. }
  906. - (NSArray<XCTestExpectation *> *)expectInstallationToBeDeletedLocally:
  907. (FIRInstallationsItem *)installation {
  908. // 3.1. Expect the installation to be removed from the storage.
  909. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  910. appName:installation.firebaseAppName])
  911. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  912. // 3.2. Expect IID to be deleted, because it is default app.
  913. OCMExpect([self.mockIIDStore deleteExistingIID])
  914. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  915. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  916. XCTestExpectation *notificationExpectation =
  917. [self installationIDDidChangeNotificationExpectation];
  918. return @[ notificationExpectation ];
  919. }
  920. // TODO: Test a single delete installation request at a time.
  921. #pragma mark - Notifications
  922. - (void)testFIDDidChangeNotificationIsSentWhenFIDCreated {
  923. // 1. Stub - no installation.
  924. // 1.2. FID store.
  925. [self expectInstallationsStoreGetInstallationNotFound];
  926. OCMStub([self.mockInstallationsStore saveInstallation:[OCMArg any]])
  927. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  928. // 1.3. IID store.
  929. [self expectStoredIIDNotFound];
  930. // 1.4. Expect backoff controller to be requested.
  931. [self expectIsNextRequestAllowedWithResult:YES];
  932. // 1.5. API Service.
  933. OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]])
  934. .andReturn([FBLPromise resolvedWith:[FIRInstallationsItem createRegisteredInstallationItem]]);
  935. // 1.6. Expect backoff success.
  936. [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
  937. // 2. Expect FIRInstallationIDDidChangeNotification to be sent.
  938. XCTestExpectation *notificationExpectation =
  939. [self installationIDDidChangeNotificationExpectation];
  940. // 3. Request FID.
  941. FBLPromise *promise = [self.controller getInstallationItem];
  942. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  943. // 4. Check.
  944. XCTAssertNil(promise.error);
  945. XCTAssertNotNil(promise.value);
  946. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  947. OCMVerifyAll(self.mockInstallationsStore);
  948. OCMVerifyAll(self.mockIIDStore);
  949. OCMVerifyAll(self.mockAPIService);
  950. OCMVerifyAll(self.mockBackoffController);
  951. }
  952. - (void)testRegisterInstallation_WhenServerRespondsWithDifferentFID_ThenFIDDidChangeNotification {
  953. // 1.1. Expect installation to be requested from the store.
  954. FIRInstallationsItem *storedInstallation =
  955. [FIRInstallationsItem createUnregisteredInstallationItem];
  956. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  957. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  958. // 1.2. Expect backoff controller to be requested.
  959. [self expectIsNextRequestAllowedWithResult:YES];
  960. // 1.3. Expect register FID to be called.
  961. FIRInstallationsItem *receivedInstallation =
  962. [FIRInstallationsItem createRegisteredInstallationItem];
  963. receivedInstallation.firebaseInstallationID =
  964. [storedInstallation.firebaseInstallationID stringByAppendingString:@"_new"];
  965. OCMExpect([self.mockAPIService registerInstallation:storedInstallation])
  966. .andReturn([FBLPromise resolvedWith:receivedInstallation]);
  967. // 1.4. Expect backoff success.
  968. [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
  969. // 1.5. Expect the received installation to be stored.
  970. OCMExpect([self.mockInstallationsStore saveInstallation:receivedInstallation])
  971. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  972. // 2. Expect FIRInstallationIDDidChangeNotification to be sent.
  973. XCTestExpectation *notificationExpectation =
  974. [self installationIDDidChangeNotificationExpectation];
  975. // 3. Request Auth Token.
  976. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  977. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  978. // 4. Check.
  979. XCTAssertNil(promise.error);
  980. XCTAssertNotNil(promise.value);
  981. XCTAssertEqualObjects(promise.value.firebaseInstallationID,
  982. receivedInstallation.firebaseInstallationID);
  983. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  984. OCMVerifyAll(self.mockInstallationsStore);
  985. OCMVerifyAll(self.mockAPIService);
  986. OCMVerifyAll(self.mockBackoffController);
  987. }
  988. #pragma mark - Helpers
  989. - (void)expectInstallationsStoreGetInstallationNotFound {
  990. NSError *notFoundError =
  991. [FIRInstallationsErrorUtil installationItemNotFoundForAppID:self.appID appName:self.appName];
  992. FBLPromise *installationNotFoundPromise = [FBLPromise pendingPromise];
  993. [installationNotFoundPromise reject:notFoundError];
  994. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  995. .andReturn(installationNotFoundPromise);
  996. }
  997. - (void)expectInstallationsStoreGetInstallationCorruptedFID {
  998. FIRInstallationsItem *storedInstallations = [FIRInstallationsItem createCorruptedItem];
  999. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  1000. .andReturn([FBLPromise resolvedWith:storedInstallations]);
  1001. }
  1002. - (void)expectStoredIIDNotFound {
  1003. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  1004. [rejectedPromise reject:[FIRInstallationsErrorUtil keychainErrorWithFunction:@"" status:-1]];
  1005. OCMExpect([self.mockIIDStore existingIID]).andReturn(rejectedPromise);
  1006. OCMExpect([self.mockIIDTokenStore existingIIDDefaultToken]).andReturn(rejectedPromise);
  1007. }
  1008. - (void)assertValidCreatedInstallation:(FIRInstallationsItem *)installation {
  1009. XCTAssertEqualObjects([installation class], [FIRInstallationsItem class]);
  1010. XCTAssertEqualObjects(installation.appID, self.appID);
  1011. XCTAssertEqualObjects(installation.firebaseAppName, self.appName);
  1012. XCTAssertEqual(installation.registrationStatus, FIRInstallationStatusUnregistered);
  1013. XCTAssertNotNil(installation.firebaseInstallationID);
  1014. }
  1015. - (XCTestExpectation *)installationIDDidChangeNotificationExpectation {
  1016. XCTestExpectation *notificationExpectation = [self
  1017. expectationForNotification:FIRInstallationIDDidChangeNotification
  1018. object:nil
  1019. handler:^BOOL(NSNotification *_Nonnull notification) {
  1020. XCTAssertEqualObjects(
  1021. notification
  1022. .userInfo[kFIRInstallationIDDidChangeNotificationAppNameKey],
  1023. self.appName);
  1024. return YES;
  1025. }];
  1026. return notificationExpectation;
  1027. }
  1028. - (void)expectInstallationStoreToBeRequestedAndReturnInstallation:
  1029. (FIRInstallationsItem *)storedInstallation {
  1030. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  1031. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  1032. }
  1033. - (FIRInstallationsItem *)expectAuthTokenRefreshForInstallation:
  1034. (FIRInstallationsItem *)installation {
  1035. FIRInstallationsItem *responseInstallation =
  1036. [FIRInstallationsItem createRegisteredInstallationItem];
  1037. responseInstallation.authToken.token =
  1038. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  1039. // 1. Expect backoff controller to be requested.
  1040. [self expectIsNextRequestAllowedWithResult:YES];
  1041. // 2. Expect API request.
  1042. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:installation])
  1043. .andReturn([FBLPromise resolvedWith:responseInstallation]);
  1044. // 3. Expect new token to be stored.
  1045. OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
  1046. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  1047. // 4. Expect backoff success.
  1048. [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
  1049. return responseInstallation;
  1050. }
  1051. - (void)expectIsNextRequestAllowedWithResult:(BOOL)isNextRequestAllowed {
  1052. OCMExpect([self.mockBackoffController isNextRequestAllowed]).andReturn(isNextRequestAllowed);
  1053. }
  1054. - (void)expectBackoffEvent:(FIRInstallationsBackoffEvent)event {
  1055. OCMExpect([self.mockBackoffController registerEvent:event]);
  1056. }
  1057. - (void)rejectBackoffEvent {
  1058. OCMReject([self.mockBackoffController registerEvent:FIRInstallationsBackoffEventSuccess]);
  1059. OCMReject(
  1060. [self.mockBackoffController registerEvent:FIRInstallationsBackoffEventRecoverableFailure]);
  1061. OCMReject(
  1062. [self.mockBackoffController registerEvent:FIRInstallationsBackoffEventUnrecoverableFailure]);
  1063. }
  1064. static const NSInteger kNoBackoffEvents = -1;
  1065. - (void)expectBackoffEvent:(FIRInstallationsBackoffEvent)event
  1066. forRegisterFIDAPIError:(NSError *)error {
  1067. // 1. Stub store get installation.
  1068. [self expectInstallationsStoreGetInstallationNotFound];
  1069. // 2. Expect IIDStore to be checked for existing IID.
  1070. [self expectStoredIIDNotFound];
  1071. // 3. Stub store save installation.
  1072. __block FIRInstallationsItem *createdInstallation;
  1073. OCMExpect([self.mockInstallationsStore
  1074. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  1075. [self assertValidCreatedInstallation:obj];
  1076. createdInstallation = obj;
  1077. return YES;
  1078. }]])
  1079. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  1080. // 4. Stub API register installation.
  1081. // 4.1. Expect backoff controller to be requested.
  1082. [self expectIsNextRequestAllowedWithResult:YES];
  1083. // 4.2. Verify installation to be registered.
  1084. id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  1085. [self assertValidCreatedInstallation:obj];
  1086. XCTAssertEqual(obj.firebaseInstallationID.length, 22);
  1087. return YES;
  1088. }];
  1089. // 4.3. Expect for `registerInstallation` to be called.
  1090. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  1091. OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
  1092. .andReturn(registerPromise);
  1093. // 4.4. Expect backoff event.
  1094. if (event == kNoBackoffEvents) {
  1095. [self rejectBackoffEvent];
  1096. } else {
  1097. [self expectBackoffEvent:event];
  1098. }
  1099. // 5. Call get installation and check.
  1100. FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
  1101. [self.controller getInstallationItem];
  1102. // 5.1. Wait for the stored item to be read and saved.
  1103. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  1104. // 5.2. Wait for `registerInstallation` to be called.
  1105. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  1106. // 5.3. Resolve `registerPromise` to simulate finished registration.
  1107. [registerPromise reject:error];
  1108. // 5.4. Wait for the task to complete.
  1109. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  1110. // Get installation returns a value no matter what.
  1111. XCTAssertNil(getInstallationPromise.error);
  1112. // We expect the initially created installation to be returned - must not wait for registration to
  1113. // complete here.
  1114. XCTAssertEqual(getInstallationPromise.value, createdInstallation);
  1115. // 5.5. Verify registered installation was saved.
  1116. OCMVerifyAll(self.mockInstallationsStore);
  1117. OCMVerifyAll(self.mockIIDStore);
  1118. OCMVerifyAll(self.mockBackoffController);
  1119. }
  1120. - (void)assertGetInstallation_NewFIDCreatedAndRegistered {
  1121. // 2. Stub store save installation.
  1122. __block FIRInstallationsItem *createdInstallation;
  1123. OCMExpect([self.mockInstallationsStore
  1124. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  1125. [self assertValidCreatedInstallation:obj];
  1126. createdInstallation = obj;
  1127. return YES;
  1128. }]])
  1129. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  1130. // 3. Stub API register installation.
  1131. // 3.1. Expect backoff controller to be requested.
  1132. [self expectIsNextRequestAllowedWithResult:YES];
  1133. // 3.2. Verify installation to be registered.
  1134. id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  1135. [self assertValidCreatedInstallation:obj];
  1136. XCTAssertEqual(obj.firebaseInstallationID.length, 22);
  1137. return YES;
  1138. }];
  1139. // 3.3. Expect for `registerInstallation` to be called.
  1140. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  1141. OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
  1142. .andReturn(registerPromise);
  1143. // 3.4. Expect backoff success.
  1144. [self expectBackoffEvent:FIRInstallationsBackoffEventSuccess];
  1145. // 5. Call get installation and check.
  1146. FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
  1147. [self.controller getInstallationItem];
  1148. // 5.1. Wait for the stored item to be read and saved.
  1149. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  1150. // 5.2. Wait for `registerInstallation` to be called.
  1151. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  1152. // 5.3. Expect for the registered installation to be saved.
  1153. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  1154. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  1155. appName:createdInstallation.firebaseAppName];
  1156. OCMExpect([self.mockInstallationsStore
  1157. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  1158. XCTAssertEqual(registeredInstallation, obj);
  1159. return YES;
  1160. }]])
  1161. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  1162. // 5.5. Resolve `registerPromise` to simulate finished registration.
  1163. [registerPromise fulfill:registeredInstallation];
  1164. // 5.4. Wait for the task to complete.
  1165. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  1166. XCTAssertNil(getInstallationPromise.error);
  1167. // We expect the initially created installation to be returned - must not wait for registration to
  1168. // complete here.
  1169. XCTAssertEqual(getInstallationPromise.value, createdInstallation);
  1170. // 5.5. Verify registered installation was saved.
  1171. OCMVerifyAll(self.mockInstallationsStore);
  1172. OCMVerifyAll(self.mockIIDStore);
  1173. OCMVerifyAll(self.mockBackoffController);
  1174. }
  1175. @end