FIRInstallationsIDControllerTests.m 62 KB

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