FIRInstallationsIDControllerTests.m 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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/FIRAppInternal.h>
  19. #import "FBLPromise+Testing.h"
  20. #import "FIRInstallationsErrorUtil+Tests.h"
  21. #import "FIRInstallationsItem+Tests.h"
  22. #import "FIRInstallations.h"
  23. #import "FIRInstallationsAPIService.h"
  24. #import "FIRInstallationsErrorUtil.h"
  25. #import "FIRInstallationsHTTPError.h"
  26. #import "FIRInstallationsIDController.h"
  27. #import "FIRInstallationsIIDCheckinStore.h"
  28. #import "FIRInstallationsIIDStore.h"
  29. #import "FIRInstallationsStore.h"
  30. #import "FIRInstallationsStoredIIDCheckin.h"
  31. #import "FIRInstallationsStoredAuthToken.h"
  32. @interface FIRInstallationsIDController (Tests)
  33. - (instancetype)initWithGoogleAppID:(NSString *)appID
  34. appName:(NSString *)appName
  35. installationsStore:(FIRInstallationsStore *)installationsStore
  36. APIService:(FIRInstallationsAPIService *)APIService
  37. IIDStore:(FIRInstallationsIIDStore *)IIDStore
  38. IIDCheckingStore:(FIRInstallationsIIDCheckinStore *)IIDCheckingStore;
  39. @end
  40. @interface FIRInstallationsIDControllerTests : XCTestCase
  41. @property(nonatomic) FIRInstallationsIDController *controller;
  42. @property(nonatomic) id mockInstallationsStore;
  43. @property(nonatomic) id mockAPIService;
  44. @property(nonatomic) id mockIIDStore;
  45. @property(nonatomic) id mockIIDCheckinStore;
  46. @property(nonatomic) NSString *appID;
  47. @property(nonatomic) NSString *appName;
  48. @end
  49. @implementation FIRInstallationsIDControllerTests
  50. - (void)setUp {
  51. [self setUpWithAppName:kFIRDefaultAppName];
  52. }
  53. - (void)setUpWithAppName:(NSString *)appName {
  54. self.appID = @"appID";
  55. self.appName = appName;
  56. self.mockInstallationsStore = OCMStrictClassMock([FIRInstallationsStore class]);
  57. self.mockAPIService = OCMStrictClassMock([FIRInstallationsAPIService class]);
  58. self.mockIIDStore = OCMStrictClassMock([FIRInstallationsIIDStore class]);
  59. self.mockIIDCheckinStore = OCMStrictClassMock([FIRInstallationsIIDCheckinStore class]);
  60. self.controller =
  61. [[FIRInstallationsIDController alloc] initWithGoogleAppID:self.appID
  62. appName:self.appName
  63. installationsStore:self.mockInstallationsStore
  64. APIService:self.mockAPIService
  65. IIDStore:self.mockIIDStore
  66. IIDCheckingStore:self.mockIIDCheckinStore];
  67. }
  68. - (void)tearDown {
  69. self.controller = nil;
  70. self.mockIIDStore = nil;
  71. self.mockAPIService = nil;
  72. self.mockInstallationsStore = nil;
  73. self.appID = nil;
  74. self.appName = nil;
  75. }
  76. #pragma mark - Get Installation
  77. - (void)testGetInstallationItem_WhenFIDExists_ThenItIsReturned {
  78. FIRInstallationsItem *storedInstallations =
  79. [FIRInstallationsItem createRegisteredInstallationItem];
  80. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  81. .andReturn([FBLPromise resolvedWith:storedInstallations]);
  82. // Don't expect FIRInstallationIDDidChangeNotification to be sent.
  83. XCTestExpectation *notificationExpectation =
  84. [self installationIDDidChangeNotificationExpectation];
  85. notificationExpectation.inverted = YES;
  86. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getInstallationItem];
  87. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  88. XCTAssertNil(promise.error);
  89. XCTAssertEqual(promise.value, storedInstallations);
  90. OCMVerifyAll(self.mockInstallationsStore);
  91. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  92. }
  93. - (void)testGetInstallationItem_WhenNoFIDAndNoIID_ThenFIDIsCreatedAndRegistered {
  94. // 1. Stub store get installation.
  95. [self expectInstallationsStoreGetInstallationNotFound];
  96. // 2. Stub store save installation.
  97. __block FIRInstallationsItem *createdInstallation;
  98. OCMExpect([self.mockInstallationsStore
  99. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  100. [self assertValidCreatedInstallation:obj];
  101. createdInstallation = obj;
  102. return YES;
  103. }]])
  104. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  105. // 3. Stub API register installation.
  106. // 3.1. Verify installation to be registered.
  107. id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  108. [self assertValidCreatedInstallation:obj];
  109. XCTAssertEqual(obj.firebaseInstallationID.length, 22);
  110. return YES;
  111. }];
  112. // 3.2. Expect for `registerInstallation` to be called.
  113. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  114. OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
  115. .andReturn(registerPromise);
  116. // 4. Expect IIDStore to be checked for existing IID.
  117. [self expectStoredIIDNotFound];
  118. // 5. Call get installation and check.
  119. FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
  120. [self.controller getInstallationItem];
  121. // 5.1. Wait for the stored item to be read and saved.
  122. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  123. // 5.2. Wait for `registerInstallation` to be called.
  124. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  125. // 5.3. Expect for the registered installation to be saved.
  126. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  127. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  128. appName:createdInstallation.firebaseAppName];
  129. OCMExpect([self.mockInstallationsStore
  130. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  131. XCTAssertEqual(registeredInstallation, obj);
  132. return YES;
  133. }]])
  134. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  135. // 5.5. Resolve `registerPromise` to simulate finished registration.
  136. [registerPromise fulfill:registeredInstallation];
  137. // 5.4. Wait for the task to complete.
  138. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  139. XCTAssertNil(getInstallationPromise.error);
  140. // We expect the initially created installation to be returned - must not wait for registration to
  141. // complete here.
  142. XCTAssertEqual(getInstallationPromise.value, createdInstallation);
  143. // 5.5. Verify registered installation was saved.
  144. OCMVerifyAll(self.mockInstallationsStore);
  145. OCMVerifyAll(self.mockIIDStore);
  146. }
  147. - (void)testGetInstallationItem_WhenThereIsIIDAndNoFIDNotDefaultApp_ThenIIDIsUsedAsFID {
  148. // 0. Configure controller with not default app.
  149. NSString *appName = @"appName";
  150. [self setUpWithAppName:appName];
  151. // 1. Stub store get installation.
  152. [self expectInstallationsStoreGetInstallationNotFound];
  153. // 2. Don't expect IIDStore to be checked for existing IID (not default app).
  154. OCMReject([self.mockIIDStore existingIID]);
  155. // 3. Stub store save installation.
  156. __block FIRInstallationsItem *createdInstallation;
  157. OCMExpect([self.mockInstallationsStore
  158. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  159. [self assertValidCreatedInstallation:obj];
  160. createdInstallation = obj;
  161. return YES;
  162. }]])
  163. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  164. // 4. Stub API register installation.
  165. // 4.1. Verify installation to be registered.
  166. id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  167. [self assertValidCreatedInstallation:obj];
  168. return YES;
  169. }];
  170. // 4.2. Expect for `registerInstallation` to be called.
  171. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  172. OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
  173. .andReturn(registerPromise);
  174. // 5. Call get installation and check.
  175. FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
  176. [self.controller getInstallationItem];
  177. // 5.1. Wait for the stored item to be read and saved.
  178. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  179. // 5.2. Wait for `registerInstallation` to be called.
  180. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  181. // 5.3. Expect for the registered installation to be saved.
  182. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  183. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  184. appName:createdInstallation.firebaseAppName];
  185. OCMExpect([self.mockInstallationsStore
  186. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  187. XCTAssertEqual(registeredInstallation, obj);
  188. return YES;
  189. }]])
  190. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  191. // 5.5. Resolve `registerPromise` to simulate finished registration.
  192. [registerPromise fulfill:registeredInstallation];
  193. // 5.4. Wait for the task to complete.
  194. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  195. XCTAssertNil(getInstallationPromise.error);
  196. // We expect the initially created installation to be returned - must not wait for registration to
  197. // complete here.
  198. XCTAssertEqual(getInstallationPromise.value, createdInstallation);
  199. // 5.5. Verify registered installation was saved.
  200. OCMVerifyAll(self.mockInstallationsStore);
  201. OCMVerifyAll(self.mockIIDStore);
  202. }
  203. - (void)testGetInstallationItem_WhenThereIsIIDAndNoFID_ThenFIDIsCreatedAndRegistered {
  204. // 1. Stub store get installation.
  205. [self expectInstallationsStoreGetInstallationNotFound];
  206. // 2. Expect IIDStore to be checked for existing IID.
  207. NSString *existingIID = @"existing-iid";
  208. OCMExpect([self.mockIIDStore existingIID]).andReturn([FBLPromise resolvedWith:existingIID]);
  209. // 3. Expect IID checkin store to be requested for checkin data.
  210. FIRInstallationsStoredIIDCheckin *existingCheckin =
  211. [[FIRInstallationsStoredIIDCheckin alloc] initWithDeviceID:@"IIDDeviceID"
  212. secretToken:@"IIDSecretToken"];
  213. OCMExpect([self.mockIIDCheckinStore existingCheckin])
  214. .andReturn([FBLPromise resolvedWith:existingCheckin]);
  215. // 3. Stub store save installation.
  216. __block FIRInstallationsItem *createdInstallation;
  217. OCMExpect([self.mockInstallationsStore
  218. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  219. [self assertValidCreatedInstallation:obj];
  220. XCTAssertEqualObjects(existingIID, obj.firebaseInstallationID);
  221. XCTAssertEqualObjects(obj.IIDCheckin.deviceID, existingCheckin.deviceID);
  222. XCTAssertEqualObjects(obj.IIDCheckin.secretToken, existingCheckin.secretToken);
  223. createdInstallation = obj;
  224. return YES;
  225. }]])
  226. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  227. // 4. Stub API register installation.
  228. // 4.1. Verify installation to be registered.
  229. id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  230. [self assertValidCreatedInstallation:obj];
  231. XCTAssertEqualObjects(existingIID, obj.firebaseInstallationID);
  232. return YES;
  233. }];
  234. // 4.2. Expect for `registerInstallation` to be called.
  235. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  236. OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
  237. .andReturn(registerPromise);
  238. // 5. Call get installation and check.
  239. FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
  240. [self.controller getInstallationItem];
  241. // 5.1. Wait for the stored item to be read and saved.
  242. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  243. // 5.2. Wait for `registerInstallation` to be called.
  244. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  245. // 5.3. Expect for the registered installation to be saved.
  246. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  247. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  248. appName:createdInstallation.firebaseAppName];
  249. OCMExpect([self.mockInstallationsStore
  250. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  251. XCTAssertEqual(registeredInstallation, obj);
  252. return YES;
  253. }]])
  254. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  255. // 5.5. Resolve `registerPromise` to simulate finished registration.
  256. [registerPromise fulfill:registeredInstallation];
  257. // 5.4. Wait for the task to complete.
  258. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  259. XCTAssertNil(getInstallationPromise.error);
  260. // We expect the initially created installation to be returned - must not wait for registration to
  261. // complete here.
  262. XCTAssertEqual(getInstallationPromise.value, createdInstallation);
  263. // 5.5. Verify registered installation was saved.
  264. OCMVerifyAll(self.mockInstallationsStore);
  265. OCMVerifyAll(self.mockIIDStore);
  266. OCMVerifyAll(self.mockIIDCheckinStore);
  267. }
  268. - (void)testGetInstallationItem_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
  269. // 1. Expect the installation to be requested from the store only once.
  270. FIRInstallationsItem *storedInstallation1 =
  271. [FIRInstallationsItem createRegisteredInstallationItem];
  272. FBLPromise<FIRInstallationsItem *> *pendingStorePromise = [FBLPromise pendingPromise];
  273. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  274. .andReturn(pendingStorePromise);
  275. // 3. Request installation n times
  276. NSInteger requestCount = 10;
  277. NSMutableArray *installationPromises = [NSMutableArray arrayWithCapacity:requestCount];
  278. for (NSInteger i = 0; i < requestCount; i++) {
  279. [installationPromises addObject:[self.controller getInstallationItem]];
  280. }
  281. // 4. Resolve store promise.
  282. [pendingStorePromise fulfill:storedInstallation1];
  283. // 5. Wait for operation to be completed and check.
  284. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  285. for (FBLPromise<FIRInstallationsItem *> *installationPromise in installationPromises) {
  286. XCTAssertNil(installationPromise.error);
  287. XCTAssertEqual(installationPromise.value, storedInstallation1);
  288. }
  289. OCMVerifyAll(self.mockInstallationsStore);
  290. OCMVerifyAll(self.mockAPIService);
  291. // 6. Check that a new request is performed once previous finished.
  292. FIRInstallationsItem *storedInstallation2 =
  293. [FIRInstallationsItem createRegisteredInstallationItem];
  294. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  295. .andReturn([FBLPromise resolvedWith:storedInstallation2]);
  296. FBLPromise<FIRInstallationsItem *> *installationPromise = [self.controller getInstallationItem];
  297. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  298. XCTAssertNil(installationPromise.error);
  299. XCTAssertEqual(installationPromise.value, storedInstallation2);
  300. OCMVerifyAll(self.mockInstallationsStore);
  301. OCMVerifyAll(self.mockAPIService);
  302. }
  303. #pragma mark - Get Auth Token
  304. - (void)testGetAuthToken_WhenValidInstallationExists_ThenItIsReturned {
  305. // 1. Expect installation to be requested from the store.
  306. FIRInstallationsItem *storedInstallation =
  307. [FIRInstallationsItem createRegisteredInstallationItem];
  308. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  309. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  310. // 2. Request auth token.
  311. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  312. // 3. Wait for the promise to resolve.
  313. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  314. // 4. Check.
  315. OCMVerifyAll(self.mockInstallationsStore);
  316. OCMVerifyAll(self.mockAPIService);
  317. XCTAssertNil(promise.error);
  318. XCTAssertNotNil(promise.value);
  319. XCTAssertEqualObjects(promise.value.authToken.token, storedInstallation.authToken.token);
  320. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  321. storedInstallation.authToken.expirationDate);
  322. }
  323. - (void)testGetAuthToken_WhenValidInstallationWithExpiredTokenExists_ThenTokenRequested {
  324. // 1.1. Expect installation to be requested from the store.
  325. FIRInstallationsItem *storedInstallation =
  326. [FIRInstallationsItem createRegisteredInstallationItem];
  327. storedInstallation.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 - 1];
  328. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  329. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  330. // 1.2. Auth Token refresh.
  331. FIRInstallationsItem *responseInstallation =
  332. [self expectAuthTokenRefreshForInstallation:storedInstallation];
  333. // 2. Request auth token.
  334. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  335. // 3. Wait for the promise to resolve.
  336. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  337. // 4. Check.
  338. OCMVerifyAll(self.mockInstallationsStore);
  339. OCMVerifyAll(self.mockAPIService);
  340. XCTAssertNil(promise.error);
  341. XCTAssertNotNil(promise.value);
  342. XCTAssertEqualObjects(promise.value.authToken.token, responseInstallation.authToken.token);
  343. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  344. responseInstallation.authToken.expirationDate);
  345. }
  346. - (void)testGetAuthTokenForcingRefresh_WhenValidInstallationExists_ThenTokenRequested {
  347. // 1.1. Expect installation to be requested from the store.
  348. FIRInstallationsItem *storedInstallation =
  349. [FIRInstallationsItem createRegisteredInstallationItem];
  350. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  351. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  352. // 1.2. Auth Token refresh.
  353. FIRInstallationsItem *responseInstallation =
  354. [self expectAuthTokenRefreshForInstallation:storedInstallation];
  355. // 2. Request auth token.
  356. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
  357. // 3. Wait for the promise to resolve.
  358. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  359. // 4. Check.
  360. OCMVerifyAll(self.mockInstallationsStore);
  361. OCMVerifyAll(self.mockAPIService);
  362. XCTAssertNil(promise.error);
  363. XCTAssertNotNil(promise.value);
  364. XCTAssertEqualObjects(promise.value.authToken.token, responseInstallation.authToken.token);
  365. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  366. responseInstallation.authToken.expirationDate);
  367. }
  368. - (void)testGetAuthToken_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
  369. // 1. Expect installation to be requested from the store.
  370. FIRInstallationsItem *storedInstallation =
  371. [FIRInstallationsItem createRegisteredInstallationItem];
  372. FBLPromise *storagePendingPromise = [FBLPromise pendingPromise];
  373. // Expect the installation to be requested only once.
  374. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  375. .andReturn(storagePendingPromise);
  376. // 2. Request auth token n times.
  377. NSInteger requestCount = 10;
  378. NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
  379. for (NSInteger i = 0; i < requestCount; i++) {
  380. [authTokenPromises addObject:[self.controller getAuthTokenForcingRefresh:NO]];
  381. }
  382. // 3. Finish the storage request.
  383. [storagePendingPromise fulfill:storedInstallation];
  384. // 4. Wait for the promise to resolve.
  385. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  386. // 5. Check.
  387. OCMVerifyAll(self.mockInstallationsStore);
  388. for (FBLPromise<FIRInstallationsItem *> *authPromise in authTokenPromises) {
  389. XCTAssertNil(authPromise.error);
  390. XCTAssertNotNil(authPromise.value);
  391. XCTAssertEqualObjects(authPromise.value.authToken.token, storedInstallation.authToken.token);
  392. XCTAssertEqualObjects(authPromise.value.authToken.expirationDate,
  393. storedInstallation.authToken.expirationDate);
  394. }
  395. }
  396. - (void)testGetAuthTokenForceRefresh_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
  397. // 1.1. Expect installation to be requested from the store.
  398. FIRInstallationsItem *storedInstallation =
  399. [FIRInstallationsItem createRegisteredInstallationItem];
  400. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  401. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  402. // 1.2. Expect API request.
  403. FIRInstallationsItem *responseInstallation =
  404. [FIRInstallationsItem createRegisteredInstallationItem];
  405. responseInstallation.authToken.token =
  406. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  407. FBLPromise *pendingAPIPromise = [FBLPromise pendingPromise];
  408. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  409. .andReturn(pendingAPIPromise);
  410. // 1.3. Expect new token to be stored.
  411. OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
  412. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  413. // 2. Request auth token n times.
  414. NSInteger requestCount = 10;
  415. NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
  416. for (NSInteger i = 0; i < requestCount; i++) {
  417. [authTokenPromises addObject:[self.controller getAuthTokenForcingRefresh:YES]];
  418. }
  419. // 3. Finish the API request.
  420. [pendingAPIPromise fulfill:responseInstallation];
  421. // 4. Wait for the promise to resolve.
  422. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  423. // 5. Check.
  424. OCMVerifyAll(self.mockInstallationsStore);
  425. for (FBLPromise<FIRInstallationsItem *> *authPromise in authTokenPromises) {
  426. XCTAssertNil(authPromise.error);
  427. XCTAssertNotNil(authPromise.value);
  428. XCTAssertEqualObjects(authPromise.value.authToken.token, responseInstallation.authToken.token);
  429. XCTAssertEqualObjects(authPromise.value.authToken.expirationDate,
  430. responseInstallation.authToken.expirationDate);
  431. }
  432. }
  433. - (void)testGetAuthToken_WhenAPIResponse401_ThenFISResetAndReregistered {
  434. NSTimeInterval timeout = 0.5;
  435. // 1.1. Expect installation to be requested from the store.
  436. FIRInstallationsItem *storedInstallation =
  437. [FIRInstallationsItem createRegisteredInstallationItem];
  438. [self expectInstallationStoreToBeRequestedAndReturnInstallation:storedInstallation];
  439. // 1.2. Expect API request.
  440. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  441. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  442. .andReturn(rejectedAPIPromise);
  443. // 2. Request auth token.
  444. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
  445. // 3. Wait for refresh token request.
  446. OCMVerifyAllWithDelay(self.mockAPIService, timeout);
  447. // 4.1. Expect Installation to be requested before deletion.
  448. [self expectInstallationStoreToBeRequestedAndReturnInstallation:storedInstallation];
  449. // 4. Expect for FIS to be deleted locally.
  450. NSArray<XCTestExpectation *> *deleteExpectations =
  451. [self expectInstallationToBeDeletedLocally:storedInstallation];
  452. // 6. Expect a new installation to be created and registered.
  453. // 6.1. Expect to request FIS from storage.
  454. [self expectInstallationsStoreGetInstallationNotFound];
  455. // 6.2. Expect stored IID not found.
  456. [self expectStoredIIDNotFound];
  457. // 6.3. Expect new Installation to be stored.
  458. __block FIRInstallationsItem *createdInstallation;
  459. OCMExpect([self.mockInstallationsStore
  460. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  461. [self assertValidCreatedInstallation:obj];
  462. createdInstallation = obj;
  463. return YES;
  464. }]])
  465. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  466. // 6.4. Expect registration API request to be sent.
  467. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  468. OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]]).andReturn(registerPromise);
  469. // 6.5. Reject API request promise with 401.
  470. NSError *error401 = [FIRInstallationsErrorUtil APIErrorWithHTTPCode:404];
  471. [rejectedAPIPromise reject:error401];
  472. // 6.6. Wait local FIS to be deleted.
  473. [self waitForExpectations:deleteExpectations timeout:timeout];
  474. // 6.7 Wait for the new Installation to be stored.
  475. OCMVerifyAllWithDelay(self.mockInstallationsStore, timeout);
  476. // 6.8. Wait for registration API request to be sent.
  477. OCMVerifyAllWithDelay(self.mockAPIService, timeout);
  478. // 6.9. Expect for the registered installation to be saved.
  479. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  480. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  481. appName:createdInstallation.firebaseAppName];
  482. OCMExpect([self.mockInstallationsStore
  483. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  484. XCTAssertEqual(registeredInstallation, obj);
  485. return YES;
  486. }]])
  487. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  488. // 6.9. Fulfill the registration API request promise.
  489. [registerPromise fulfill:registeredInstallation];
  490. // 7. Wait for promises.
  491. XCTAssert(FBLWaitForPromisesWithTimeout(timeout));
  492. // 8. Check.
  493. OCMVerifyAll(self.mockInstallationsStore);
  494. OCMVerifyAll(self.mockAPIService);
  495. XCTAssertNil(promise.error);
  496. XCTAssertNotNil(promise.value);
  497. XCTAssertNotEqualObjects(promise.value.firebaseInstallationID,
  498. storedInstallation.firebaseInstallationID);
  499. XCTAssertEqualObjects(promise.value, registeredInstallation);
  500. }
  501. #pragma mark - FID Deletion
  502. - (void)testDeleteRegisteredInstallation {
  503. // 1. Expect installation to be requested from the store.
  504. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  505. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  506. appName:installation.firebaseAppName])
  507. .andReturn([FBLPromise resolvedWith:installation]);
  508. // 2. Expect API request to delete installation.
  509. OCMExpect([self.mockAPIService deleteInstallation:installation])
  510. .andReturn([FBLPromise resolvedWith:installation]);
  511. // 3.1. Expect the installation to be removed from the storage.
  512. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  513. appName:installation.firebaseAppName])
  514. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  515. // 3.2. Expect IID to be deleted, because it is default app.
  516. OCMExpect([self.mockIIDStore deleteExistingIID])
  517. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  518. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  519. XCTestExpectation *notificationExpectation =
  520. [self installationIDDidChangeNotificationExpectation];
  521. // 5. Call delete installation.
  522. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  523. // 6. Wait for operations to complete and check.
  524. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  525. XCTAssertNil(promise.error);
  526. XCTAssertTrue(promise.isFulfilled);
  527. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  528. OCMVerifyAll(self.mockInstallationsStore);
  529. OCMVerifyAll(self.mockAPIService);
  530. OCMVerifyAll(self.mockIIDStore);
  531. }
  532. - (void)testDeleteUnregisteredInstallation {
  533. // 1. Expect installation to be requested from the store.
  534. FIRInstallationsItem *installation = [FIRInstallationsItem createUnregisteredInstallationItem];
  535. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  536. appName:installation.firebaseAppName])
  537. .andReturn([FBLPromise resolvedWith:installation]);
  538. // 2. Don't expect API request to delete installation.
  539. OCMReject([self.mockAPIService deleteInstallation:[OCMArg any]]);
  540. // 3.1. Expect the installation to be removed from the storage.
  541. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  542. appName:installation.firebaseAppName])
  543. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  544. // 3.2. Expect IID to be deleted, because it is default app.
  545. OCMExpect([self.mockIIDStore deleteExistingIID])
  546. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  547. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  548. XCTestExpectation *notificationExpectation =
  549. [self installationIDDidChangeNotificationExpectation];
  550. // 5. Call delete installation.
  551. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  552. // 6. Wait for operations to complete and check.
  553. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  554. XCTAssertNil(promise.error);
  555. XCTAssertTrue(promise.isFulfilled);
  556. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  557. OCMVerifyAll(self.mockInstallationsStore);
  558. OCMVerifyAll(self.mockAPIService);
  559. OCMVerifyAll(self.mockIIDStore);
  560. }
  561. - (void)testDeleteRegisteredInstallation_WhenAPIRequestFails_ThenFailsAndInstallationIsNotRemoved {
  562. // 1. Expect installation to be requested from the store.
  563. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  564. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  565. appName:installation.firebaseAppName])
  566. .andReturn([FBLPromise resolvedWith:installation]);
  567. // 2. Expect API request to delete installation.
  568. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  569. NSError *error500 =
  570. [FIRInstallationsErrorUtil APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError];
  571. [rejectedAPIPromise reject:error500];
  572. OCMExpect([self.mockAPIService deleteInstallation:installation]).andReturn(rejectedAPIPromise);
  573. // 3.1. Don't expect the installation to be removed from the storage.
  574. OCMReject([self.mockInstallationsStore removeInstallationForAppID:[OCMArg any]
  575. appName:[OCMArg any]]);
  576. // 3.2. Don't expect IID to be deleted.
  577. OCMReject([self.mockIIDStore deleteExistingIID]);
  578. // 4. Don't expect FIRInstallationIDDidChangeNotification to be sent.
  579. XCTestExpectation *notificationExpectation =
  580. [self installationIDDidChangeNotificationExpectation];
  581. notificationExpectation.inverted = YES;
  582. // 5. Call delete installation.
  583. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  584. // 6. Wait for operations to complete and check.
  585. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  586. XCTAssertEqualObjects(promise.error, error500);
  587. XCTAssertTrue(promise.isRejected);
  588. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  589. OCMVerifyAll(self.mockInstallationsStore);
  590. OCMVerifyAll(self.mockAPIService);
  591. OCMVerifyAll(self.mockIIDStore);
  592. }
  593. - (void)testDeleteRegisteredInstallation_WhenAPIFailsWithNotFound_ThenInstallationIsRemoved {
  594. // 1. Expect installation to be requested from the store.
  595. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  596. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  597. appName:installation.firebaseAppName])
  598. .andReturn([FBLPromise resolvedWith:installation]);
  599. // 2. Expect API request to delete installation.
  600. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  601. [rejectedAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:404]];
  602. OCMExpect([self.mockAPIService deleteInstallation:installation]).andReturn(rejectedAPIPromise);
  603. // 3. Expect the installation to be removed from the storage.
  604. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  605. appName:installation.firebaseAppName])
  606. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  607. // 3.2. Expect IID to be deleted, because it is default app.
  608. OCMExpect([self.mockIIDStore deleteExistingIID])
  609. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  610. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  611. XCTestExpectation *notificationExpectation =
  612. [self installationIDDidChangeNotificationExpectation];
  613. // 5. Call delete installation.
  614. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  615. // 6. Wait for operations to complete and check.
  616. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  617. XCTAssertNil(promise.error);
  618. XCTAssertTrue(promise.isFulfilled);
  619. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  620. OCMVerifyAll(self.mockInstallationsStore);
  621. OCMVerifyAll(self.mockAPIService);
  622. OCMVerifyAll(self.mockIIDStore);
  623. }
  624. - (void)testDeleteInstallation_WhenThereIsOngoingAuthTokenRequest_ThenUsesItsResult {
  625. // 1. Stub mocks for auth token request.
  626. // 1.1. Expect installation to be requested from the store.
  627. FIRInstallationsItem *storedInstallation =
  628. [FIRInstallationsItem createRegisteredInstallationItem];
  629. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  630. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  631. // 1.2. Expect API request.
  632. FIRInstallationsItem *responseInstallation =
  633. [FIRInstallationsItem createRegisteredInstallationItem];
  634. responseInstallation.authToken.token =
  635. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  636. FBLPromise *pendingAuthTokenAPIPromise = [FBLPromise pendingPromise];
  637. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  638. .andReturn(pendingAuthTokenAPIPromise);
  639. // 2. Send auth token request.
  640. [self.controller getAuthTokenForcingRefresh:YES];
  641. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  642. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  643. // 3. Delete installation.
  644. // 3.1. Don't expect installation to be requested from the store.
  645. OCMReject([self.mockInstallationsStore installationForAppID:[OCMArg any] appName:[OCMArg any]]);
  646. // 3.2. Expect API request to delete the UPDATED installation.
  647. OCMExpect([self.mockAPIService deleteInstallation:responseInstallation])
  648. .andReturn([FBLPromise resolvedWith:responseInstallation]);
  649. // 3.3. Expect the UPDATED installation to be removed from the storage.
  650. OCMExpect([self.mockInstallationsStore
  651. removeInstallationForAppID:responseInstallation.appID
  652. appName:responseInstallation.firebaseAppName])
  653. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  654. // 3.4. Expect IID to be deleted, because it is default app.
  655. OCMExpect([self.mockIIDStore deleteExistingIID])
  656. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  657. // 3.4. Call delete installation.
  658. FBLPromise<NSNull *> *deletePromise = [self.controller deleteInstallation];
  659. // 4. Fulfill auth token promise to proceed.
  660. // 4.1. Expect new token to be stored on API response.
  661. OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
  662. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  663. [pendingAuthTokenAPIPromise fulfill:responseInstallation];
  664. // 5. Wait for operations to complete and check the result.
  665. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  666. XCTAssertNil(deletePromise.error);
  667. XCTAssertTrue(deletePromise.isFulfilled);
  668. OCMVerifyAll(self.mockInstallationsStore);
  669. OCMVerifyAll(self.mockAPIService);
  670. OCMVerifyAll(self.mockIIDStore);
  671. }
  672. - (void)testDeleteInstallation_WhenNotDefaultApp_ThenIIDIsNotDeleted {
  673. // 0. Configure controller for not default app.
  674. NSString *appName = @"appName";
  675. [self setUpWithAppName:appName];
  676. // 1. Expect installation to be requested from the store.
  677. FIRInstallationsItem *installation =
  678. [FIRInstallationsItem createRegisteredInstallationItemWithAppID:self.appID appName:appName];
  679. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  680. appName:installation.firebaseAppName])
  681. .andReturn([FBLPromise resolvedWith:installation]);
  682. // 2. Expect API request to delete installation.
  683. OCMExpect([self.mockAPIService deleteInstallation:installation])
  684. .andReturn([FBLPromise resolvedWith:installation]);
  685. // 3.1. Expect the installation to be removed from the storage.
  686. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  687. appName:installation.firebaseAppName])
  688. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  689. // 3.2. Don't expect IID to be deleted, because it is not a default app.
  690. OCMReject([self.mockIIDStore deleteExistingIID]);
  691. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  692. XCTestExpectation *notificationExpectation =
  693. [self installationIDDidChangeNotificationExpectation];
  694. // 5. Call delete installation.
  695. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  696. // 6. Wait for operations to complete and check.
  697. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  698. XCTAssertNil(promise.error);
  699. XCTAssertTrue(promise.isFulfilled);
  700. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  701. OCMVerifyAll(self.mockInstallationsStore);
  702. OCMVerifyAll(self.mockAPIService);
  703. OCMVerifyAll(self.mockIIDStore);
  704. }
  705. - (NSArray<XCTestExpectation *> *)expectInstallationToBeDeletedLocally:
  706. (FIRInstallationsItem *)installation {
  707. // 3.1. Expect the installation to be removed from the storage.
  708. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  709. appName:installation.firebaseAppName])
  710. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  711. // 3.2. Expect IID to be deleted, because it is default app.
  712. OCMExpect([self.mockIIDStore deleteExistingIID])
  713. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  714. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  715. XCTestExpectation *notificationExpectation =
  716. [self installationIDDidChangeNotificationExpectation];
  717. return @[ notificationExpectation ];
  718. }
  719. // TODO: Test a single delete installation request at a time.
  720. #pragma mark - Notifications
  721. - (void)testFIDDidChangeNotificationIsSentWhenFIDCreated {
  722. // 1. Stub - no installation.
  723. // 1.2. FID store.
  724. [self expectInstallationsStoreGetInstallationNotFound];
  725. OCMStub([self.mockInstallationsStore saveInstallation:[OCMArg any]])
  726. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  727. // 1.3. IID store.
  728. [self expectStoredIIDNotFound];
  729. // 1.4. API Service.
  730. OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]])
  731. .andReturn([FBLPromise resolvedWith:[FIRInstallationsItem createRegisteredInstallationItem]]);
  732. // 2. Expect FIRInstallationIDDidChangeNotification to be sent.
  733. XCTestExpectation *notificationExpectation =
  734. [self installationIDDidChangeNotificationExpectation];
  735. // 3. Request FID.
  736. FBLPromise *promise = [self.controller getInstallationItem];
  737. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  738. // 4. Check.
  739. XCTAssertNil(promise.error);
  740. XCTAssertNotNil(promise.value);
  741. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  742. OCMVerifyAll(self.mockInstallationsStore);
  743. OCMVerifyAll(self.mockIIDStore);
  744. OCMVerifyAll(self.mockAPIService);
  745. }
  746. - (void)testRegisterInstallation_WhenServerRespondsWithDifferentFID_ThenFIDDidChangeNotification {
  747. // 1.1. Expect installation to be requested from the store.
  748. FIRInstallationsItem *storedInstallation =
  749. [FIRInstallationsItem createUnregisteredInstallationItem];
  750. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  751. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  752. // 1.2. Expect register FID to be called.
  753. FIRInstallationsItem *receivedInstallation =
  754. [FIRInstallationsItem createRegisteredInstallationItem];
  755. receivedInstallation.firebaseInstallationID =
  756. [storedInstallation.firebaseInstallationID stringByAppendingString:@"_new"];
  757. OCMExpect([self.mockAPIService registerInstallation:storedInstallation])
  758. .andReturn([FBLPromise resolvedWith:receivedInstallation]);
  759. // 1.3. Expect the received installation to be stored.
  760. OCMExpect([self.mockInstallationsStore saveInstallation:receivedInstallation])
  761. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  762. // 2. Expect FIRInstallationIDDidChangeNotification to be sent.
  763. XCTestExpectation *notificationExpectation =
  764. [self installationIDDidChangeNotificationExpectation];
  765. // 3. Request Auth Token.
  766. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  767. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  768. // 4. Check.
  769. XCTAssertNil(promise.error);
  770. XCTAssertNotNil(promise.value);
  771. XCTAssertEqualObjects(promise.value.firebaseInstallationID,
  772. receivedInstallation.firebaseInstallationID);
  773. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  774. OCMVerifyAll(self.mockInstallationsStore);
  775. OCMVerifyAll(self.mockAPIService);
  776. }
  777. #pragma mark - Helpers
  778. - (void)expectInstallationsStoreGetInstallationNotFound {
  779. NSError *notFoundError =
  780. [FIRInstallationsErrorUtil installationItemNotFoundForAppID:self.appID appName:self.appName];
  781. FBLPromise *installationNotFoundPromise = [FBLPromise pendingPromise];
  782. [installationNotFoundPromise reject:notFoundError];
  783. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  784. .andReturn(installationNotFoundPromise);
  785. }
  786. - (void)expectStoredIIDNotFound {
  787. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  788. [rejectedPromise reject:[FIRInstallationsErrorUtil keychainErrorWithFunction:@"" status:-1]];
  789. OCMExpect([self.mockIIDStore existingIID]).andReturn(rejectedPromise);
  790. OCMExpect([self.mockIIDCheckinStore existingCheckin]).andReturn(rejectedPromise);
  791. }
  792. - (void)assertValidCreatedInstallation:(FIRInstallationsItem *)installation {
  793. XCTAssertEqualObjects([installation class], [FIRInstallationsItem class]);
  794. XCTAssertEqualObjects(installation.appID, self.appID);
  795. XCTAssertEqualObjects(installation.firebaseAppName, self.appName);
  796. XCTAssertEqual(installation.registrationStatus, FIRInstallationStatusUnregistered);
  797. XCTAssertNotNil(installation.firebaseInstallationID);
  798. }
  799. - (XCTestExpectation *)installationIDDidChangeNotificationExpectation {
  800. XCTestExpectation *notificationExpectation = [self
  801. expectationForNotification:FIRInstallationIDDidChangeNotification
  802. object:nil
  803. handler:^BOOL(NSNotification *_Nonnull notification) {
  804. XCTAssertEqualObjects(
  805. notification
  806. .userInfo[kFIRInstallationIDDidChangeNotificationAppNameKey],
  807. self.appName);
  808. return YES;
  809. }];
  810. return notificationExpectation;
  811. }
  812. - (void)expectInstallationStoreToBeRequestedAndReturnInstallation:
  813. (FIRInstallationsItem *)storedInstallation {
  814. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  815. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  816. }
  817. - (FIRInstallationsItem *)expectAuthTokenRefreshForInstallation:
  818. (FIRInstallationsItem *)installation {
  819. FIRInstallationsItem *responseInstallation =
  820. [FIRInstallationsItem createRegisteredInstallationItem];
  821. responseInstallation.authToken.token =
  822. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  823. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:installation])
  824. .andReturn([FBLPromise resolvedWith:responseInstallation]);
  825. // 1.3. Expect new token to be stored.
  826. OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
  827. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  828. return responseInstallation;
  829. }
  830. @end