FIRInstallationsIDControllerTests.m 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. 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. Expect API request.
  331. FIRInstallationsItem *responseInstallation =
  332. [FIRInstallationsItem createRegisteredInstallationItem];
  333. responseInstallation.authToken.token =
  334. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  335. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  336. .andReturn([FBLPromise resolvedWith:responseInstallation]);
  337. // 2. Request auth token.
  338. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  339. // 3. Wait for the promise to resolve.
  340. FBLWaitForPromisesWithTimeout(0.5);
  341. // 4. Check.
  342. OCMVerifyAll(self.mockInstallationsStore);
  343. OCMVerifyAll(self.mockAPIService);
  344. XCTAssertNil(promise.error);
  345. XCTAssertNotNil(promise.value);
  346. XCTAssertEqualObjects(promise.value.authToken.token, responseInstallation.authToken.token);
  347. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  348. responseInstallation.authToken.expirationDate);
  349. }
  350. - (void)testGetAuthTokenForcingRefresh_WhenValidInstallationExists_ThenTokenRequested {
  351. // 1.1. Expect installation to be requested from the store.
  352. FIRInstallationsItem *storedInstallation =
  353. [FIRInstallationsItem createRegisteredInstallationItem];
  354. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  355. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  356. // 1.2. Expect API request.
  357. FIRInstallationsItem *responseInstallation =
  358. [FIRInstallationsItem createRegisteredInstallationItem];
  359. responseInstallation.authToken.token =
  360. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  361. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  362. .andReturn([FBLPromise resolvedWith:responseInstallation]);
  363. // 2. Request auth token.
  364. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
  365. // 3. Wait for the promise to resolve.
  366. FBLWaitForPromisesWithTimeout(0.5);
  367. // 4. Check.
  368. OCMVerifyAll(self.mockInstallationsStore);
  369. OCMVerifyAll(self.mockAPIService);
  370. XCTAssertNil(promise.error);
  371. XCTAssertNotNil(promise.value);
  372. XCTAssertEqualObjects(promise.value.authToken.token, responseInstallation.authToken.token);
  373. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  374. responseInstallation.authToken.expirationDate);
  375. }
  376. - (void)testGetAuthToken_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
  377. // 1. Expect installation to be requested from the store.
  378. FIRInstallationsItem *storedInstallation =
  379. [FIRInstallationsItem createRegisteredInstallationItem];
  380. FBLPromise *storagePendingPromise = [FBLPromise pendingPromise];
  381. // Expect the installation to be requested only once.
  382. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  383. .andReturn(storagePendingPromise);
  384. // 2. Request auth token n times.
  385. NSInteger requestCount = 10;
  386. NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
  387. for (NSInteger i = 0; i < requestCount; i++) {
  388. [authTokenPromises addObject:[self.controller getAuthTokenForcingRefresh:NO]];
  389. }
  390. // 3. Finish the storage request.
  391. [storagePendingPromise fulfill:storedInstallation];
  392. // 4. Wait for the promise to resolve.
  393. FBLWaitForPromisesWithTimeout(0.5);
  394. // 5. Check.
  395. OCMVerifyAll(self.mockInstallationsStore);
  396. for (FBLPromise<FIRInstallationsItem *> *authPromise in authTokenPromises) {
  397. XCTAssertNil(authPromise.error);
  398. XCTAssertNotNil(authPromise.value);
  399. XCTAssertEqualObjects(authPromise.value.authToken.token, storedInstallation.authToken.token);
  400. XCTAssertEqualObjects(authPromise.value.authToken.expirationDate,
  401. storedInstallation.authToken.expirationDate);
  402. }
  403. }
  404. - (void)testGetAuthTokenForceRefresh_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
  405. // 1.1. Expect installation to be requested from the store.
  406. FIRInstallationsItem *storedInstallation =
  407. [FIRInstallationsItem createRegisteredInstallationItem];
  408. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  409. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  410. // 1.2. Expect API request.
  411. FIRInstallationsItem *responseInstallation =
  412. [FIRInstallationsItem createRegisteredInstallationItem];
  413. responseInstallation.authToken.token =
  414. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  415. FBLPromise *pendingAPIPromise = [FBLPromise pendingPromise];
  416. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  417. .andReturn(pendingAPIPromise);
  418. // 2. Request auth token n times.
  419. NSInteger requestCount = 10;
  420. NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
  421. for (NSInteger i = 0; i < requestCount; i++) {
  422. [authTokenPromises addObject:[self.controller getAuthTokenForcingRefresh:YES]];
  423. }
  424. // 3. Finish the API request.
  425. [pendingAPIPromise fulfill:responseInstallation];
  426. // 4. Wait for the promise to resolve.
  427. FBLWaitForPromisesWithTimeout(0.5);
  428. // 5. Check.
  429. OCMVerifyAll(self.mockInstallationsStore);
  430. for (FBLPromise<FIRInstallationsItem *> *authPromise in authTokenPromises) {
  431. XCTAssertNil(authPromise.error);
  432. XCTAssertNotNil(authPromise.value);
  433. XCTAssertEqualObjects(authPromise.value.authToken.token, responseInstallation.authToken.token);
  434. XCTAssertEqualObjects(authPromise.value.authToken.expirationDate,
  435. responseInstallation.authToken.expirationDate);
  436. }
  437. }
  438. - (void)testGetAuthToken_WhenAPIResponse401_ThenFISResetAndReregistered {
  439. NSTimeInterval timeout = 0.5;
  440. // 1.1. Expect installation to be requested from the store.
  441. FIRInstallationsItem *storedInstallation =
  442. [FIRInstallationsItem createRegisteredInstallationItem];
  443. [self expectInstallationStoreToBeRequestedAndReturnInstallation:storedInstallation];
  444. // 1.2. Expect API request.
  445. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  446. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  447. .andReturn(rejectedAPIPromise);
  448. // 2. Request auth token.
  449. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
  450. // 3. Wait for refresh token request.
  451. OCMVerifyAllWithDelay(self.mockAPIService, timeout);
  452. // 4.1. Expect Installation to be requested before deletion.
  453. [self expectInstallationStoreToBeRequestedAndReturnInstallation:storedInstallation];
  454. // 4. Expect for FIS to be deleted locally.
  455. NSArray<XCTestExpectation *> *deleteExpectations =
  456. [self expectInstallationToBeDeletedLocally:storedInstallation];
  457. // 6. Expect a new installation to be created and registered.
  458. // 6.1. Expect to request FIS from storage.
  459. [self expectInstallationsStoreGetInstallationNotFound];
  460. // 6.2. Expect stored IID not found.
  461. [self expectStoredIIDNotFound];
  462. // 6.3. Expect new Installation to be stored.
  463. __block FIRInstallationsItem *createdInstallation;
  464. OCMExpect([self.mockInstallationsStore
  465. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  466. [self assertValidCreatedInstallation:obj];
  467. createdInstallation = obj;
  468. return YES;
  469. }]])
  470. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  471. // 6.4. Expect registration API request to be sent.
  472. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  473. OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]]).andReturn(registerPromise);
  474. // 6.5. Reject API request promise with 401.
  475. NSError *error401 = [FIRInstallationsErrorUtil APIErrorWithHTTPCode:404];
  476. [rejectedAPIPromise reject:error401];
  477. // 6.6. Wait local FIS to be deleted.
  478. [self waitForExpectations:deleteExpectations timeout:timeout];
  479. // 6.7 Wait for the new Installation to be stored.
  480. OCMVerifyAllWithDelay(self.mockInstallationsStore, timeout);
  481. // 6.8. Wait for registration API request to be sent.
  482. OCMVerifyAllWithDelay(self.mockAPIService, timeout);
  483. // 6.9. Expect for the registered installation to be saved.
  484. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  485. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  486. appName:createdInstallation.firebaseAppName];
  487. OCMExpect([self.mockInstallationsStore
  488. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  489. XCTAssertEqual(registeredInstallation, obj);
  490. return YES;
  491. }]])
  492. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  493. // 6.9. Fulfill the registration API request promise.
  494. [registerPromise fulfill:registeredInstallation];
  495. // 7. Wait for promises.
  496. XCTAssert(FBLWaitForPromisesWithTimeout(timeout));
  497. // 8. Check.
  498. OCMVerifyAll(self.mockInstallationsStore);
  499. OCMVerifyAll(self.mockAPIService);
  500. XCTAssertNil(promise.error);
  501. XCTAssertNotNil(promise.value);
  502. XCTAssertNotEqualObjects(promise.value.firebaseInstallationID,
  503. storedInstallation.firebaseInstallationID);
  504. XCTAssertEqualObjects(promise.value, registeredInstallation);
  505. }
  506. #pragma mark - FID Deletion
  507. - (void)testDeleteRegisteredInstallation {
  508. // 1. Expect installation to be requested from the store.
  509. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  510. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  511. appName:installation.firebaseAppName])
  512. .andReturn([FBLPromise resolvedWith:installation]);
  513. // 2. Expect API request to delete installation.
  514. OCMExpect([self.mockAPIService deleteInstallation:installation])
  515. .andReturn([FBLPromise resolvedWith:installation]);
  516. // 3.1. Expect the installation to be removed from the storage.
  517. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  518. appName:installation.firebaseAppName])
  519. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  520. // 3.2. Expect IID to be deleted, because it is default app.
  521. OCMExpect([self.mockIIDStore deleteExistingIID])
  522. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  523. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  524. XCTestExpectation *notificationExpectation =
  525. [self installationIDDidChangeNotificationExpectation];
  526. // 5. Call delete installation.
  527. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  528. // 6. Wait for operations to complete and check.
  529. FBLWaitForPromisesWithTimeout(0.5);
  530. XCTAssertNil(promise.error);
  531. XCTAssertTrue(promise.isFulfilled);
  532. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  533. OCMVerifyAll(self.mockInstallationsStore);
  534. OCMVerifyAll(self.mockAPIService);
  535. OCMVerifyAll(self.mockIIDStore);
  536. }
  537. - (void)testDeleteUnregisteredInstallation {
  538. // 1. Expect installation to be requested from the store.
  539. FIRInstallationsItem *installation = [FIRInstallationsItem createUnregisteredInstallationItem];
  540. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  541. appName:installation.firebaseAppName])
  542. .andReturn([FBLPromise resolvedWith:installation]);
  543. // 2. Don't expect API request to delete installation.
  544. OCMReject([self.mockAPIService deleteInstallation:[OCMArg any]]);
  545. // 3.1. Expect the installation to be removed from the storage.
  546. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  547. appName:installation.firebaseAppName])
  548. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  549. // 3.2. Expect IID to be deleted, because it is default app.
  550. OCMExpect([self.mockIIDStore deleteExistingIID])
  551. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  552. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  553. XCTestExpectation *notificationExpectation =
  554. [self installationIDDidChangeNotificationExpectation];
  555. // 5. Call delete installation.
  556. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  557. // 6. Wait for operations to complete and check.
  558. FBLWaitForPromisesWithTimeout(0.5);
  559. XCTAssertNil(promise.error);
  560. XCTAssertTrue(promise.isFulfilled);
  561. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  562. OCMVerifyAll(self.mockInstallationsStore);
  563. OCMVerifyAll(self.mockAPIService);
  564. OCMVerifyAll(self.mockIIDStore);
  565. }
  566. - (void)testDeleteRegisteredInstallation_WhenAPIRequestFails_ThenFailsAndInstallationIsNotRemoved {
  567. // 1. Expect installation to be requested from the store.
  568. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  569. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  570. appName:installation.firebaseAppName])
  571. .andReturn([FBLPromise resolvedWith:installation]);
  572. // 2. Expect API request to delete installation.
  573. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  574. NSError *error500 =
  575. [FIRInstallationsErrorUtil APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError];
  576. [rejectedAPIPromise reject:error500];
  577. OCMExpect([self.mockAPIService deleteInstallation:installation]).andReturn(rejectedAPIPromise);
  578. // 3.1. Don't expect the installation to be removed from the storage.
  579. OCMReject([self.mockInstallationsStore removeInstallationForAppID:[OCMArg any]
  580. appName:[OCMArg any]]);
  581. // 3.2. Don't expect IID to be deleted.
  582. OCMReject([self.mockIIDStore deleteExistingIID]);
  583. // 4. Don't expect FIRInstallationIDDidChangeNotification to be sent.
  584. XCTestExpectation *notificationExpectation =
  585. [self installationIDDidChangeNotificationExpectation];
  586. notificationExpectation.inverted = YES;
  587. // 5. Call delete installation.
  588. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  589. // 6. Wait for operations to complete and check.
  590. FBLWaitForPromisesWithTimeout(0.5);
  591. XCTAssertEqualObjects(promise.error, error500);
  592. XCTAssertTrue(promise.isRejected);
  593. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  594. OCMVerifyAll(self.mockInstallationsStore);
  595. OCMVerifyAll(self.mockAPIService);
  596. OCMVerifyAll(self.mockIIDStore);
  597. }
  598. - (void)testDeleteRegisteredInstallation_WhenAPIFailsWithNotFound_ThenInstallationIsRemoved {
  599. // 1. Expect installation to be requested from the store.
  600. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  601. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  602. appName:installation.firebaseAppName])
  603. .andReturn([FBLPromise resolvedWith:installation]);
  604. // 2. Expect API request to delete installation.
  605. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  606. [rejectedAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:404]];
  607. OCMExpect([self.mockAPIService deleteInstallation:installation]).andReturn(rejectedAPIPromise);
  608. // 3. Expect the installation to be removed from the storage.
  609. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  610. appName:installation.firebaseAppName])
  611. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  612. // 3.2. Expect IID to be deleted, because it is default app.
  613. OCMExpect([self.mockIIDStore deleteExistingIID])
  614. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  615. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  616. XCTestExpectation *notificationExpectation =
  617. [self installationIDDidChangeNotificationExpectation];
  618. // 5. Call delete installation.
  619. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  620. // 6. Wait for operations to complete and check.
  621. FBLWaitForPromisesWithTimeout(0.5);
  622. XCTAssertNil(promise.error);
  623. XCTAssertTrue(promise.isFulfilled);
  624. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  625. OCMVerifyAll(self.mockInstallationsStore);
  626. OCMVerifyAll(self.mockAPIService);
  627. OCMVerifyAll(self.mockIIDStore);
  628. }
  629. - (void)testDeleteInstallation_WhenThereIsOngoingAuthTokenRequest_ThenUsesItsResult {
  630. // 1. Stub mocks for auth token request.
  631. // 1.1. Expect installation to be requested from the store.
  632. FIRInstallationsItem *storedInstallation =
  633. [FIRInstallationsItem createRegisteredInstallationItem];
  634. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  635. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  636. // 1.2. Expect API request.
  637. FIRInstallationsItem *responseInstallation =
  638. [FIRInstallationsItem createRegisteredInstallationItem];
  639. responseInstallation.authToken.token =
  640. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  641. FBLPromise *pendingAuthTokenAPIPromise = [FBLPromise pendingPromise];
  642. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  643. .andReturn(pendingAuthTokenAPIPromise);
  644. // 2. Send auth token request.
  645. [self.controller getAuthTokenForcingRefresh:YES];
  646. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  647. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  648. // 3. Delete installation.
  649. // 3.1. Don't expect installation to be requested from the store.
  650. OCMReject([self.mockInstallationsStore installationForAppID:[OCMArg any] appName:[OCMArg any]]);
  651. // 3.2. Expect API request to delete the UPDATED installation.
  652. OCMExpect([self.mockAPIService deleteInstallation:responseInstallation])
  653. .andReturn([FBLPromise resolvedWith:responseInstallation]);
  654. // 3.3. Expect the UPDATED installation to be removed from the storage.
  655. OCMExpect([self.mockInstallationsStore
  656. removeInstallationForAppID:responseInstallation.appID
  657. appName:responseInstallation.firebaseAppName])
  658. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  659. // 3.4. Expect IID to be deleted, because it is default app.
  660. OCMExpect([self.mockIIDStore deleteExistingIID])
  661. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  662. // 3.4. Call delete installation.
  663. FBLPromise<NSNull *> *deletePromise = [self.controller deleteInstallation];
  664. // 4. Fulfill auth token promise to proceed.
  665. [pendingAuthTokenAPIPromise fulfill:responseInstallation];
  666. // 5. Wait for operations to complete and check the result.
  667. FBLWaitForPromisesWithTimeout(0.5);
  668. XCTAssertNil(deletePromise.error);
  669. XCTAssertTrue(deletePromise.isFulfilled);
  670. OCMVerifyAll(self.mockInstallationsStore);
  671. OCMVerifyAll(self.mockAPIService);
  672. OCMVerifyAll(self.mockIIDStore);
  673. }
  674. - (void)testDeleteInstallation_WhenNotDefaultApp_ThenIIDIsNotDeleted {
  675. // 0. Configure controller for not default app.
  676. NSString *appName = @"appName";
  677. [self setUpWithAppName:appName];
  678. // 1. Expect installation to be requested from the store.
  679. FIRInstallationsItem *installation =
  680. [FIRInstallationsItem createRegisteredInstallationItemWithAppID:self.appID appName:appName];
  681. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  682. appName:installation.firebaseAppName])
  683. .andReturn([FBLPromise resolvedWith:installation]);
  684. // 2. Expect API request to delete installation.
  685. OCMExpect([self.mockAPIService deleteInstallation:installation])
  686. .andReturn([FBLPromise resolvedWith:installation]);
  687. // 3.1. Expect the installation to be removed from the storage.
  688. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  689. appName:installation.firebaseAppName])
  690. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  691. // 3.2. Don't expect IID to be deleted, because it is not a default app.
  692. OCMReject([self.mockIIDStore deleteExistingIID]);
  693. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  694. XCTestExpectation *notificationExpectation =
  695. [self installationIDDidChangeNotificationExpectation];
  696. // 5. Call delete installation.
  697. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  698. // 6. Wait for operations to complete and check.
  699. FBLWaitForPromisesWithTimeout(0.5);
  700. XCTAssertNil(promise.error);
  701. XCTAssertTrue(promise.isFulfilled);
  702. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  703. OCMVerifyAll(self.mockInstallationsStore);
  704. OCMVerifyAll(self.mockAPIService);
  705. OCMVerifyAll(self.mockIIDStore);
  706. }
  707. - (NSArray<XCTestExpectation *> *)expectInstallationToBeDeletedLocally:
  708. (FIRInstallationsItem *)installation {
  709. // 3.1. Expect the installation to be removed from the storage.
  710. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  711. appName:installation.firebaseAppName])
  712. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  713. // 3.2. Expect IID to be deleted, because it is default app.
  714. OCMExpect([self.mockIIDStore deleteExistingIID])
  715. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  716. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  717. XCTestExpectation *notificationExpectation =
  718. [self installationIDDidChangeNotificationExpectation];
  719. return @[ notificationExpectation ];
  720. }
  721. // TODO: Test a single delete installation request at a time.
  722. #pragma mark - Notifications
  723. - (void)testFIDDidChangeNotificationIsSentWhenFIDCreated {
  724. // 1. Stub - no installation.
  725. // 1.2. FID store.
  726. [self expectInstallationsStoreGetInstallationNotFound];
  727. OCMStub([self.mockInstallationsStore saveInstallation:[OCMArg any]])
  728. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  729. // 1.3. IID store.
  730. [self expectStoredIIDNotFound];
  731. // 1.4. API Service.
  732. OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]])
  733. .andReturn([FBLPromise resolvedWith:[FIRInstallationsItem createRegisteredInstallationItem]]);
  734. // 2. Expect FIRInstallationIDDidChangeNotification to be sent.
  735. XCTestExpectation *notificationExpectation =
  736. [self installationIDDidChangeNotificationExpectation];
  737. // 3. Request FID.
  738. FBLPromise *promise = [self.controller getInstallationItem];
  739. FBLWaitForPromisesWithTimeout(0.5);
  740. // 4. Check.
  741. XCTAssertNil(promise.error);
  742. XCTAssertNotNil(promise.value);
  743. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  744. OCMVerifyAll(self.mockInstallationsStore);
  745. OCMVerifyAll(self.mockIIDStore);
  746. OCMVerifyAll(self.mockAPIService);
  747. }
  748. - (void)testRegisterInstallation_WhenServerRespondsWithDifferentFID_ThenFIDDidChangeNotification {
  749. // 1.1. Expect installation to be requested from the store.
  750. FIRInstallationsItem *storedInstallation =
  751. [FIRInstallationsItem createUnregisteredInstallationItem];
  752. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  753. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  754. // 1.2. Expect register FID to be called.
  755. FIRInstallationsItem *receivedInstallation =
  756. [FIRInstallationsItem createRegisteredInstallationItem];
  757. receivedInstallation.firebaseInstallationID =
  758. [storedInstallation.firebaseInstallationID stringByAppendingString:@"_new"];
  759. OCMExpect([self.mockAPIService registerInstallation:storedInstallation])
  760. .andReturn([FBLPromise resolvedWith:receivedInstallation]);
  761. // 1.3. Expect the received installation to be stored.
  762. OCMExpect([self.mockInstallationsStore saveInstallation:receivedInstallation])
  763. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  764. // 2. Expect FIRInstallationIDDidChangeNotification to be sent.
  765. XCTestExpectation *notificationExpectation =
  766. [self installationIDDidChangeNotificationExpectation];
  767. // 3. Request Auth Token.
  768. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  769. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  770. // 4. Check.
  771. XCTAssertNil(promise.error);
  772. XCTAssertNotNil(promise.value);
  773. XCTAssertEqualObjects(promise.value.firebaseInstallationID,
  774. receivedInstallation.firebaseInstallationID);
  775. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  776. OCMVerifyAll(self.mockInstallationsStore);
  777. OCMVerifyAll(self.mockAPIService);
  778. }
  779. #pragma mark - Helpers
  780. - (void)expectInstallationsStoreGetInstallationNotFound {
  781. NSError *notFoundError =
  782. [FIRInstallationsErrorUtil installationItemNotFoundForAppID:self.appID appName:self.appName];
  783. FBLPromise *installationNotFoundPromise = [FBLPromise pendingPromise];
  784. [installationNotFoundPromise reject:notFoundError];
  785. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  786. .andReturn(installationNotFoundPromise);
  787. }
  788. - (void)expectStoredIIDNotFound {
  789. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  790. [rejectedPromise reject:[FIRInstallationsErrorUtil keychainErrorWithFunction:@"" status:-1]];
  791. OCMExpect([self.mockIIDStore existingIID]).andReturn(rejectedPromise);
  792. OCMExpect([self.mockIIDCheckinStore existingCheckin]).andReturn(rejectedPromise);
  793. }
  794. - (void)assertValidCreatedInstallation:(FIRInstallationsItem *)installation {
  795. XCTAssertEqualObjects([installation class], [FIRInstallationsItem class]);
  796. XCTAssertEqualObjects(installation.appID, self.appID);
  797. XCTAssertEqualObjects(installation.firebaseAppName, self.appName);
  798. XCTAssertEqual(installation.registrationStatus, FIRInstallationStatusUnregistered);
  799. XCTAssertNotNil(installation.firebaseInstallationID);
  800. }
  801. - (XCTestExpectation *)installationIDDidChangeNotificationExpectation {
  802. XCTestExpectation *notificationExpectation = [self
  803. expectationForNotification:FIRInstallationIDDidChangeNotification
  804. object:nil
  805. handler:^BOOL(NSNotification *_Nonnull notification) {
  806. XCTAssertEqualObjects(
  807. notification
  808. .userInfo[kFIRInstallationIDDidChangeNotificationAppNameKey],
  809. self.appName);
  810. return YES;
  811. }];
  812. return notificationExpectation;
  813. }
  814. - (void)expectInstallationStoreToBeRequestedAndReturnInstallation:
  815. (FIRInstallationsItem *)storedInstallation {
  816. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  817. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  818. }
  819. @end