FIRInstallationsIDControllerTests.m 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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 "FBLPromise+Then.h"
  21. #import "FIRInstallationsErrorUtil+Tests.h"
  22. #import "FIRInstallationsItem+Tests.h"
  23. #import "FIRInstallations.h"
  24. #import "FIRInstallationsAPIService.h"
  25. #import "FIRInstallationsErrorUtil.h"
  26. #import "FIRInstallationsHTTPError.h"
  27. #import "FIRInstallationsIDController.h"
  28. #import "FIRInstallationsIIDStore.h"
  29. #import "FIRInstallationsIIDTokenStore.h"
  30. #import "FIRInstallationsStore.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. IIDTokenStore:(FIRInstallationsIIDTokenStore *)IIDTokenStore;
  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 mockIIDTokenStore;
  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.mockIIDTokenStore = OCMStrictClassMock([FIRInstallationsIIDTokenStore 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. IIDTokenStore:self.mockIIDTokenStore];
  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. NSString *existingIIDDefaultToken = @"existing-iid-token";
  211. OCMExpect([self.mockIIDTokenStore existingIIDDefaultToken])
  212. .andReturn([FBLPromise resolvedWith:existingIIDDefaultToken]);
  213. // 3. Stub store save installation.
  214. __block FIRInstallationsItem *createdInstallation;
  215. OCMExpect([self.mockInstallationsStore
  216. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  217. [self assertValidCreatedInstallation:obj];
  218. XCTAssertEqualObjects(existingIID, obj.firebaseInstallationID);
  219. XCTAssertEqualObjects(obj.IIDDefaultToken, existingIIDDefaultToken);
  220. createdInstallation = obj;
  221. return YES;
  222. }]])
  223. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  224. // 4. Stub API register installation.
  225. // 4.1. Verify installation to be registered.
  226. id registerInstallationValidation = [OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  227. [self assertValidCreatedInstallation:obj];
  228. XCTAssertEqualObjects(existingIID, obj.firebaseInstallationID);
  229. return YES;
  230. }];
  231. // 4.2. Expect for `registerInstallation` to be called.
  232. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  233. OCMExpect([self.mockAPIService registerInstallation:registerInstallationValidation])
  234. .andReturn(registerPromise);
  235. // 5. Call get installation and check.
  236. FBLPromise<FIRInstallationsItem *> *getInstallationPromise =
  237. [self.controller getInstallationItem];
  238. // 5.1. Wait for the stored item to be read and saved.
  239. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  240. // 5.2. Wait for `registerInstallation` to be called.
  241. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  242. // 5.3. Expect for the registered installation to be saved.
  243. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  244. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  245. appName:createdInstallation.firebaseAppName];
  246. OCMExpect([self.mockInstallationsStore
  247. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  248. XCTAssertEqual(registeredInstallation, obj);
  249. return YES;
  250. }]])
  251. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  252. // 5.5. Resolve `registerPromise` to simulate finished registration.
  253. [registerPromise fulfill:registeredInstallation];
  254. // 5.4. Wait for the task to complete.
  255. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  256. XCTAssertNil(getInstallationPromise.error);
  257. // We expect the initially created installation to be returned - must not wait for registration to
  258. // complete here.
  259. XCTAssertEqual(getInstallationPromise.value, createdInstallation);
  260. // 5.5. Verify registered installation was saved.
  261. OCMVerifyAll(self.mockInstallationsStore);
  262. OCMVerifyAll(self.mockIIDStore);
  263. OCMVerifyAll(self.mockIIDTokenStore);
  264. }
  265. - (void)testGetInstallationItem_WhenCalledWhileRegistering_DoesNotWaitForAPIResponse {
  266. // 1. Expect the installation to be requested from the store only once.
  267. FIRInstallationsItem *storedInstallation1 =
  268. [FIRInstallationsItem createUnregisteredInstallationItem];
  269. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  270. .andReturn([FBLPromise resolvedWith:storedInstallation1]);
  271. // 2. Expect registration API request to be sent.
  272. FBLPromise<FIRInstallationsItem *> *pendingAPIPromise = [FBLPromise pendingPromise];
  273. OCMExpect([self.mockAPIService registerInstallation:storedInstallation1])
  274. .andReturn(pendingAPIPromise);
  275. // 3. Request and wait for 1st FID.
  276. FBLPromise<FIRInstallationsItem *> *getInstallationPromise1 =
  277. [self.controller getInstallationItem];
  278. XCTestExpectation *getInstallationsExpectation1 =
  279. [self expectationWithDescription:@"getInstallationsExpectation1"];
  280. getInstallationPromise1.then(^id(FIRInstallationsItem *installation) {
  281. [getInstallationsExpectation1 fulfill];
  282. return nil;
  283. });
  284. [self waitForExpectations:@[ getInstallationsExpectation1 ] timeout:0.5];
  285. // 4. Request FID 2nd time.
  286. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  287. .andReturn([FBLPromise resolvedWith:storedInstallation1]);
  288. FBLPromise<FIRInstallationsItem *> *getInstallationPromise2 =
  289. [self.controller getInstallationItem];
  290. XCTestExpectation *getInstallationsExpectation2 =
  291. [self expectationWithDescription:@"getInstallationsExpectation2"];
  292. getInstallationPromise2.then(^id(FIRInstallationsItem *installation) {
  293. [getInstallationsExpectation2 fulfill];
  294. return nil;
  295. });
  296. [self waitForExpectations:@[ getInstallationsExpectation2 ] timeout:0.5];
  297. // 5. Resolve API promise.
  298. [pendingAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:400]];
  299. // 6. Check
  300. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  301. OCMVerifyAll(self.mockInstallationsStore);
  302. OCMVerifyAll(self.mockAPIService);
  303. }
  304. - (void)testGetInstallationItem_WhenCalledSeveralTimesWaitingForStore_OnlyOneOperationIsPerformed {
  305. // 1. Expect the installation to be requested from the store only once.
  306. FIRInstallationsItem *storedInstallation1 =
  307. [FIRInstallationsItem createRegisteredInstallationItem];
  308. FBLPromise<FIRInstallationsItem *> *pendingStorePromise = [FBLPromise pendingPromise];
  309. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  310. .andReturn(pendingStorePromise);
  311. // 2. Request installation n times
  312. NSInteger requestCount = 10;
  313. NSMutableArray *installationPromises = [NSMutableArray arrayWithCapacity:requestCount];
  314. for (NSInteger i = 0; i < requestCount; i++) {
  315. [installationPromises addObject:[self.controller getInstallationItem]];
  316. }
  317. // 3. Resolve store promise.
  318. [pendingStorePromise fulfill:storedInstallation1];
  319. // 4. Wait for operation to be completed and check.
  320. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  321. for (FBLPromise<FIRInstallationsItem *> *installationPromise in installationPromises) {
  322. XCTAssertNil(installationPromise.error);
  323. XCTAssertEqual(installationPromise.value, storedInstallation1);
  324. }
  325. OCMVerifyAll(self.mockInstallationsStore);
  326. OCMVerifyAll(self.mockAPIService);
  327. // 5. Check that a new request is performed once previous finished.
  328. FIRInstallationsItem *storedInstallation2 =
  329. [FIRInstallationsItem createRegisteredInstallationItem];
  330. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  331. .andReturn([FBLPromise resolvedWith:storedInstallation2]);
  332. FBLPromise<FIRInstallationsItem *> *installationPromise = [self.controller getInstallationItem];
  333. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  334. XCTAssertNil(installationPromise.error);
  335. XCTAssertEqual(installationPromise.value, storedInstallation2);
  336. OCMVerifyAll(self.mockInstallationsStore);
  337. OCMVerifyAll(self.mockAPIService);
  338. }
  339. - (void)testGetInstallationItem_WhenCalledSeveralTimesWaitingForAPI_OnlyOneAPIRequestIsSent {
  340. // 1. Expect a single API request.
  341. FBLPromise<FIRInstallationsItem *> *registerAPIPromise = [FBLPromise pendingPromise];
  342. OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]]).andReturn(registerAPIPromise);
  343. // 2. Request FID multiple times.
  344. NSInteger requestCount = 10;
  345. for (NSInteger i = 0; i < requestCount; i++) {
  346. XCTestExpectation *getFIDExpectation = [self
  347. expectationWithDescription:[NSString stringWithFormat:@"getFIDExpectation%ld", (long)i]];
  348. // 2.1. Expect stored FID to be requested.
  349. FIRInstallationsItem *storedInstallation =
  350. [FIRInstallationsItem createUnregisteredInstallationItem];
  351. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  352. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  353. // 2.2. Expect the FID to be returned.
  354. FBLPromise<FIRInstallationsItem *> *getFIDPromise = [self.controller getInstallationItem];
  355. [getFIDPromise then:^id _Nullable(FIRInstallationsItem *_Nullable value) {
  356. XCTAssertNotNil(value);
  357. XCTAssertEqualObjects(value.firebaseInstallationID,
  358. storedInstallation.firebaseInstallationID);
  359. [getFIDExpectation fulfill];
  360. return nil;
  361. }];
  362. [self waitForExpectations:@[ getFIDExpectation ] timeout:0.5];
  363. }
  364. // 3. Finish API request.
  365. [registerAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:400]];
  366. // 4. Verify mocks
  367. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  368. OCMVerifyAll(self.mockInstallationsStore);
  369. OCMVerifyAll(self.mockAPIService);
  370. }
  371. #pragma mark - Get Auth Token
  372. - (void)testGetAuthToken_WhenValidInstallationExists_ThenItIsReturned {
  373. // 1. Expect installation to be requested from the store.
  374. FIRInstallationsItem *storedInstallation =
  375. [FIRInstallationsItem createRegisteredInstallationItem];
  376. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  377. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  378. // 2. Request auth token.
  379. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  380. // 3. Wait for the promise to resolve.
  381. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  382. // 4. Check.
  383. OCMVerifyAll(self.mockInstallationsStore);
  384. OCMVerifyAll(self.mockAPIService);
  385. XCTAssertNil(promise.error);
  386. XCTAssertNotNil(promise.value);
  387. XCTAssertEqualObjects(promise.value.authToken.token, storedInstallation.authToken.token);
  388. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  389. storedInstallation.authToken.expirationDate);
  390. }
  391. - (void)testGetAuthToken_WhenValidInstallationWithExpiredTokenExists_ThenTokenRequested {
  392. // 1.1. Expect installation to be requested from the store.
  393. FIRInstallationsItem *storedInstallation =
  394. [FIRInstallationsItem createRegisteredInstallationItem];
  395. storedInstallation.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 - 1];
  396. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  397. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  398. // 1.2. Auth Token refresh.
  399. FIRInstallationsItem *responseInstallation =
  400. [self expectAuthTokenRefreshForInstallation:storedInstallation];
  401. // 2. Request auth token.
  402. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  403. // 3. Wait for the promise to resolve.
  404. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  405. // 4. Check.
  406. OCMVerifyAll(self.mockInstallationsStore);
  407. OCMVerifyAll(self.mockAPIService);
  408. XCTAssertNil(promise.error);
  409. XCTAssertNotNil(promise.value);
  410. XCTAssertEqualObjects(promise.value.authToken.token, responseInstallation.authToken.token);
  411. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  412. responseInstallation.authToken.expirationDate);
  413. }
  414. - (void)testGetAuthTokenForcingRefresh_WhenValidInstallationExists_ThenTokenRequested {
  415. // 1.1. Expect installation to be requested from the store.
  416. FIRInstallationsItem *storedInstallation =
  417. [FIRInstallationsItem createRegisteredInstallationItem];
  418. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  419. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  420. // 1.2. Auth Token refresh.
  421. FIRInstallationsItem *responseInstallation =
  422. [self expectAuthTokenRefreshForInstallation:storedInstallation];
  423. // 2. Request auth token.
  424. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
  425. // 3. Wait for the promise to resolve.
  426. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  427. // 4. Check.
  428. OCMVerifyAll(self.mockInstallationsStore);
  429. OCMVerifyAll(self.mockAPIService);
  430. XCTAssertNil(promise.error);
  431. XCTAssertNotNil(promise.value);
  432. XCTAssertEqualObjects(promise.value.authToken.token, responseInstallation.authToken.token);
  433. XCTAssertEqualObjects(promise.value.authToken.expirationDate,
  434. responseInstallation.authToken.expirationDate);
  435. }
  436. - (void)testGetAuthToken_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
  437. // 1. Expect installation to be requested from the store.
  438. FIRInstallationsItem *storedInstallation =
  439. [FIRInstallationsItem createRegisteredInstallationItem];
  440. FBLPromise *storagePendingPromise = [FBLPromise pendingPromise];
  441. // Expect the installation to be requested only once.
  442. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  443. .andReturn(storagePendingPromise);
  444. // 2. Request auth token n times.
  445. NSInteger requestCount = 10;
  446. NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
  447. for (NSInteger i = 0; i < requestCount; i++) {
  448. [authTokenPromises addObject:[self.controller getAuthTokenForcingRefresh:NO]];
  449. }
  450. // 3. Finish the storage request.
  451. [storagePendingPromise fulfill:storedInstallation];
  452. // 4. Wait for the promise to resolve.
  453. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  454. // 5. Check.
  455. OCMVerifyAll(self.mockInstallationsStore);
  456. for (FBLPromise<FIRInstallationsItem *> *authPromise in authTokenPromises) {
  457. XCTAssertNil(authPromise.error);
  458. XCTAssertNotNil(authPromise.value);
  459. XCTAssertEqualObjects(authPromise.value.authToken.token, storedInstallation.authToken.token);
  460. XCTAssertEqualObjects(authPromise.value.authToken.expirationDate,
  461. storedInstallation.authToken.expirationDate);
  462. }
  463. }
  464. - (void)testGetAuthTokenForceRefresh_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
  465. // 1.1. Expect installation to be requested from the store.
  466. FIRInstallationsItem *storedInstallation =
  467. [FIRInstallationsItem createRegisteredInstallationItem];
  468. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  469. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  470. // 1.2. Expect API request.
  471. FIRInstallationsItem *responseInstallation =
  472. [FIRInstallationsItem createRegisteredInstallationItem];
  473. responseInstallation.authToken.token =
  474. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  475. FBLPromise *pendingAPIPromise = [FBLPromise pendingPromise];
  476. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  477. .andReturn(pendingAPIPromise);
  478. // 1.3. Expect new token to be stored.
  479. OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
  480. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  481. // 2. Request auth token n times.
  482. NSInteger requestCount = 10;
  483. NSMutableArray *authTokenPromises = [NSMutableArray arrayWithCapacity:requestCount];
  484. for (NSInteger i = 0; i < requestCount; i++) {
  485. [authTokenPromises addObject:[self.controller getAuthTokenForcingRefresh:YES]];
  486. }
  487. // 3. Finish the API request.
  488. [pendingAPIPromise fulfill:responseInstallation];
  489. // 4. Wait for the promise to resolve.
  490. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  491. // 5. Check.
  492. OCMVerifyAll(self.mockInstallationsStore);
  493. for (FBLPromise<FIRInstallationsItem *> *authPromise in authTokenPromises) {
  494. XCTAssertNil(authPromise.error);
  495. XCTAssertNotNil(authPromise.value);
  496. XCTAssertEqualObjects(authPromise.value.authToken.token, responseInstallation.authToken.token);
  497. XCTAssertEqualObjects(authPromise.value.authToken.expirationDate,
  498. responseInstallation.authToken.expirationDate);
  499. }
  500. }
  501. - (void)testGetAuthToken_WhenAPIResponse401_ThenFISResetAndReregistered {
  502. NSTimeInterval timeout = 0.5;
  503. // 1.1. Expect installation to be requested from the store.
  504. FIRInstallationsItem *storedInstallation =
  505. [FIRInstallationsItem createRegisteredInstallationItem];
  506. [self expectInstallationStoreToBeRequestedAndReturnInstallation:storedInstallation];
  507. // 1.2. Expect API request.
  508. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  509. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  510. .andReturn(rejectedAPIPromise);
  511. // 2. Request auth token.
  512. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:YES];
  513. // 3. Wait for refresh token request.
  514. OCMVerifyAllWithDelay(self.mockAPIService, timeout);
  515. // 4.1. Expect Installation to be requested before deletion.
  516. [self expectInstallationStoreToBeRequestedAndReturnInstallation:storedInstallation];
  517. // 4. Expect for FIS to be deleted locally.
  518. NSArray<XCTestExpectation *> *deleteExpectations =
  519. [self expectInstallationToBeDeletedLocally:storedInstallation];
  520. // 6. Expect a new installation to be created and registered.
  521. // 6.1. Expect to request FIS from storage.
  522. [self expectInstallationsStoreGetInstallationNotFound];
  523. // 6.2. Expect stored IID not found.
  524. [self expectStoredIIDNotFound];
  525. // 6.3. Expect new Installation to be stored.
  526. __block FIRInstallationsItem *createdInstallation;
  527. OCMExpect([self.mockInstallationsStore
  528. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  529. [self assertValidCreatedInstallation:obj];
  530. createdInstallation = obj;
  531. return YES;
  532. }]])
  533. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  534. // 6.4. Expect registration API request to be sent.
  535. FBLPromise<FIRInstallationsItem *> *registerPromise = [FBLPromise pendingPromise];
  536. OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]]).andReturn(registerPromise);
  537. // 6.5. Reject API request promise with 401.
  538. NSError *error401 = [FIRInstallationsErrorUtil APIErrorWithHTTPCode:404];
  539. [rejectedAPIPromise reject:error401];
  540. // 6.6. Wait local FIS to be deleted.
  541. [self waitForExpectations:deleteExpectations timeout:timeout];
  542. // 6.7 Wait for the new Installation to be stored.
  543. OCMVerifyAllWithDelay(self.mockInstallationsStore, timeout);
  544. // 6.8. Wait for registration API request to be sent.
  545. OCMVerifyAllWithDelay(self.mockAPIService, timeout);
  546. // 6.9. Expect for the registered installation to be saved.
  547. FIRInstallationsItem *registeredInstallation = [FIRInstallationsItem
  548. createRegisteredInstallationItemWithAppID:createdInstallation.appID
  549. appName:createdInstallation.firebaseAppName];
  550. OCMExpect([self.mockInstallationsStore
  551. saveInstallation:[OCMArg checkWithBlock:^BOOL(FIRInstallationsItem *obj) {
  552. XCTAssertEqual(registeredInstallation, obj);
  553. return YES;
  554. }]])
  555. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  556. // 6.9. Fulfill the registration API request promise.
  557. [registerPromise fulfill:registeredInstallation];
  558. // 7. Wait for promises.
  559. XCTAssert(FBLWaitForPromisesWithTimeout(timeout));
  560. // 8. Check.
  561. OCMVerifyAll(self.mockInstallationsStore);
  562. OCMVerifyAll(self.mockAPIService);
  563. XCTAssertNil(promise.error);
  564. XCTAssertNotNil(promise.value);
  565. XCTAssertNotEqualObjects(promise.value.firebaseInstallationID,
  566. storedInstallation.firebaseInstallationID);
  567. XCTAssertEqualObjects(promise.value, registeredInstallation);
  568. }
  569. #pragma mark - FID Deletion
  570. - (void)testDeleteRegisteredInstallation {
  571. // 1. Expect installation to be requested from the store.
  572. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  573. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  574. appName:installation.firebaseAppName])
  575. .andReturn([FBLPromise resolvedWith:installation]);
  576. // 2. Expect API request to delete installation.
  577. OCMExpect([self.mockAPIService deleteInstallation:installation])
  578. .andReturn([FBLPromise resolvedWith:installation]);
  579. // 3.1. Expect the installation to be removed from the storage.
  580. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  581. appName:installation.firebaseAppName])
  582. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  583. // 3.2. Expect IID to be deleted, because it is default app.
  584. OCMExpect([self.mockIIDStore deleteExistingIID])
  585. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  586. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  587. XCTestExpectation *notificationExpectation =
  588. [self installationIDDidChangeNotificationExpectation];
  589. // 5. Call delete installation.
  590. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  591. // 6. Wait for operations to complete and check.
  592. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  593. XCTAssertNil(promise.error);
  594. XCTAssertTrue(promise.isFulfilled);
  595. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  596. OCMVerifyAll(self.mockInstallationsStore);
  597. OCMVerifyAll(self.mockAPIService);
  598. OCMVerifyAll(self.mockIIDStore);
  599. }
  600. - (void)testDeleteUnregisteredInstallation {
  601. // 1. Expect installation to be requested from the store.
  602. FIRInstallationsItem *installation = [FIRInstallationsItem createUnregisteredInstallationItem];
  603. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  604. appName:installation.firebaseAppName])
  605. .andReturn([FBLPromise resolvedWith:installation]);
  606. // 2. Don't expect API request to delete installation.
  607. OCMReject([self.mockAPIService deleteInstallation:[OCMArg any]]);
  608. // 3.1. 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. XCTAssert(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)testDeleteRegisteredInstallation_WhenAPIRequestFails_ThenFailsAndInstallationIsNotRemoved {
  630. // 1. Expect installation to be requested from the store.
  631. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  632. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  633. appName:installation.firebaseAppName])
  634. .andReturn([FBLPromise resolvedWith:installation]);
  635. // 2. Expect API request to delete installation.
  636. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  637. NSError *error500 =
  638. [FIRInstallationsErrorUtil APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError];
  639. [rejectedAPIPromise reject:error500];
  640. OCMExpect([self.mockAPIService deleteInstallation:installation]).andReturn(rejectedAPIPromise);
  641. // 3.1. Don't expect the installation to be removed from the storage.
  642. OCMReject([self.mockInstallationsStore removeInstallationForAppID:[OCMArg any]
  643. appName:[OCMArg any]]);
  644. // 3.2. Don't expect IID to be deleted.
  645. OCMReject([self.mockIIDStore deleteExistingIID]);
  646. // 4. Don't expect FIRInstallationIDDidChangeNotification to be sent.
  647. XCTestExpectation *notificationExpectation =
  648. [self installationIDDidChangeNotificationExpectation];
  649. notificationExpectation.inverted = YES;
  650. // 5. Call delete installation.
  651. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  652. // 6. Wait for operations to complete and check.
  653. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  654. XCTAssertEqualObjects(promise.error, error500);
  655. XCTAssertTrue(promise.isRejected);
  656. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  657. OCMVerifyAll(self.mockInstallationsStore);
  658. OCMVerifyAll(self.mockAPIService);
  659. OCMVerifyAll(self.mockIIDStore);
  660. }
  661. - (void)testDeleteRegisteredInstallation_WhenAPIFailsWithNotFound_ThenInstallationIsRemoved {
  662. // 1. Expect installation to be requested from the store.
  663. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  664. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  665. appName:installation.firebaseAppName])
  666. .andReturn([FBLPromise resolvedWith:installation]);
  667. // 2. Expect API request to delete installation.
  668. FBLPromise *rejectedAPIPromise = [FBLPromise pendingPromise];
  669. [rejectedAPIPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:404]];
  670. OCMExpect([self.mockAPIService deleteInstallation:installation]).andReturn(rejectedAPIPromise);
  671. // 3. Expect the installation to be removed from the storage.
  672. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  673. appName:installation.firebaseAppName])
  674. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  675. // 3.2. Expect IID to be deleted, because it is default app.
  676. OCMExpect([self.mockIIDStore deleteExistingIID])
  677. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  678. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  679. XCTestExpectation *notificationExpectation =
  680. [self installationIDDidChangeNotificationExpectation];
  681. // 5. Call delete installation.
  682. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  683. // 6. Wait for operations to complete and check.
  684. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  685. XCTAssertNil(promise.error);
  686. XCTAssertTrue(promise.isFulfilled);
  687. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  688. OCMVerifyAll(self.mockInstallationsStore);
  689. OCMVerifyAll(self.mockAPIService);
  690. OCMVerifyAll(self.mockIIDStore);
  691. }
  692. - (void)testDeleteInstallation_WhenThereIsOngoingAuthTokenRequest_ThenUsesItsResult {
  693. // 1. Stub mocks for auth token request.
  694. // 1.1. Expect installation to be requested from the store.
  695. FIRInstallationsItem *storedInstallation =
  696. [FIRInstallationsItem createRegisteredInstallationItem];
  697. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  698. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  699. // 1.2. Expect API request.
  700. FIRInstallationsItem *responseInstallation =
  701. [FIRInstallationsItem createRegisteredInstallationItem];
  702. responseInstallation.authToken.token =
  703. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  704. FBLPromise *pendingAuthTokenAPIPromise = [FBLPromise pendingPromise];
  705. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:storedInstallation])
  706. .andReturn(pendingAuthTokenAPIPromise);
  707. // 2. Send auth token request.
  708. [self.controller getAuthTokenForcingRefresh:YES];
  709. OCMVerifyAllWithDelay(self.mockInstallationsStore, 0.5);
  710. OCMVerifyAllWithDelay(self.mockAPIService, 0.5);
  711. // 3. Delete installation.
  712. // 3.1. Don't expect installation to be requested from the store.
  713. OCMReject([self.mockInstallationsStore installationForAppID:[OCMArg any] appName:[OCMArg any]]);
  714. // 3.2. Expect API request to delete the UPDATED installation.
  715. OCMExpect([self.mockAPIService deleteInstallation:responseInstallation])
  716. .andReturn([FBLPromise resolvedWith:responseInstallation]);
  717. // 3.3. Expect the UPDATED installation to be removed from the storage.
  718. OCMExpect([self.mockInstallationsStore
  719. removeInstallationForAppID:responseInstallation.appID
  720. appName:responseInstallation.firebaseAppName])
  721. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  722. // 3.4. Expect IID to be deleted, because it is default app.
  723. OCMExpect([self.mockIIDStore deleteExistingIID])
  724. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  725. // 3.4. Call delete installation.
  726. FBLPromise<NSNull *> *deletePromise = [self.controller deleteInstallation];
  727. // 4. Fulfill auth token promise to proceed.
  728. // 4.1. Expect new token to be stored on API response.
  729. OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
  730. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  731. [pendingAuthTokenAPIPromise fulfill:responseInstallation];
  732. // 5. Wait for operations to complete and check the result.
  733. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  734. XCTAssertNil(deletePromise.error);
  735. XCTAssertTrue(deletePromise.isFulfilled);
  736. OCMVerifyAll(self.mockInstallationsStore);
  737. OCMVerifyAll(self.mockAPIService);
  738. OCMVerifyAll(self.mockIIDStore);
  739. }
  740. - (void)testDeleteInstallation_WhenNotDefaultApp_ThenIIDIsNotDeleted {
  741. // 0. Configure controller for not default app.
  742. NSString *appName = @"appName";
  743. [self setUpWithAppName:appName];
  744. // 1. Expect installation to be requested from the store.
  745. FIRInstallationsItem *installation =
  746. [FIRInstallationsItem createRegisteredInstallationItemWithAppID:self.appID appName:appName];
  747. OCMExpect([self.mockInstallationsStore installationForAppID:installation.appID
  748. appName:installation.firebaseAppName])
  749. .andReturn([FBLPromise resolvedWith:installation]);
  750. // 2. Expect API request to delete installation.
  751. OCMExpect([self.mockAPIService deleteInstallation:installation])
  752. .andReturn([FBLPromise resolvedWith:installation]);
  753. // 3.1. Expect the installation to be removed from the storage.
  754. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  755. appName:installation.firebaseAppName])
  756. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  757. // 3.2. Don't expect IID to be deleted, because it is not a default app.
  758. OCMReject([self.mockIIDStore deleteExistingIID]);
  759. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  760. XCTestExpectation *notificationExpectation =
  761. [self installationIDDidChangeNotificationExpectation];
  762. // 5. Call delete installation.
  763. FBLPromise<NSNull *> *promise = [self.controller deleteInstallation];
  764. // 6. Wait for operations to complete and check.
  765. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  766. XCTAssertNil(promise.error);
  767. XCTAssertTrue(promise.isFulfilled);
  768. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  769. OCMVerifyAll(self.mockInstallationsStore);
  770. OCMVerifyAll(self.mockAPIService);
  771. OCMVerifyAll(self.mockIIDStore);
  772. }
  773. - (NSArray<XCTestExpectation *> *)expectInstallationToBeDeletedLocally:
  774. (FIRInstallationsItem *)installation {
  775. // 3.1. Expect the installation to be removed from the storage.
  776. OCMExpect([self.mockInstallationsStore removeInstallationForAppID:installation.appID
  777. appName:installation.firebaseAppName])
  778. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  779. // 3.2. Expect IID to be deleted, because it is default app.
  780. OCMExpect([self.mockIIDStore deleteExistingIID])
  781. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  782. // 4. Expect FIRInstallationIDDidChangeNotification to be sent.
  783. XCTestExpectation *notificationExpectation =
  784. [self installationIDDidChangeNotificationExpectation];
  785. return @[ notificationExpectation ];
  786. }
  787. // TODO: Test a single delete installation request at a time.
  788. #pragma mark - Notifications
  789. - (void)testFIDDidChangeNotificationIsSentWhenFIDCreated {
  790. // 1. Stub - no installation.
  791. // 1.2. FID store.
  792. [self expectInstallationsStoreGetInstallationNotFound];
  793. OCMStub([self.mockInstallationsStore saveInstallation:[OCMArg any]])
  794. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  795. // 1.3. IID store.
  796. [self expectStoredIIDNotFound];
  797. // 1.4. API Service.
  798. OCMExpect([self.mockAPIService registerInstallation:[OCMArg any]])
  799. .andReturn([FBLPromise resolvedWith:[FIRInstallationsItem createRegisteredInstallationItem]]);
  800. // 2. Expect FIRInstallationIDDidChangeNotification to be sent.
  801. XCTestExpectation *notificationExpectation =
  802. [self installationIDDidChangeNotificationExpectation];
  803. // 3. Request FID.
  804. FBLPromise *promise = [self.controller getInstallationItem];
  805. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  806. // 4. Check.
  807. XCTAssertNil(promise.error);
  808. XCTAssertNotNil(promise.value);
  809. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  810. OCMVerifyAll(self.mockInstallationsStore);
  811. OCMVerifyAll(self.mockIIDStore);
  812. OCMVerifyAll(self.mockAPIService);
  813. }
  814. - (void)testRegisterInstallation_WhenServerRespondsWithDifferentFID_ThenFIDDidChangeNotification {
  815. // 1.1. Expect installation to be requested from the store.
  816. FIRInstallationsItem *storedInstallation =
  817. [FIRInstallationsItem createUnregisteredInstallationItem];
  818. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  819. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  820. // 1.2. Expect register FID to be called.
  821. FIRInstallationsItem *receivedInstallation =
  822. [FIRInstallationsItem createRegisteredInstallationItem];
  823. receivedInstallation.firebaseInstallationID =
  824. [storedInstallation.firebaseInstallationID stringByAppendingString:@"_new"];
  825. OCMExpect([self.mockAPIService registerInstallation:storedInstallation])
  826. .andReturn([FBLPromise resolvedWith:receivedInstallation]);
  827. // 1.3. Expect the received installation to be stored.
  828. OCMExpect([self.mockInstallationsStore saveInstallation:receivedInstallation])
  829. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  830. // 2. Expect FIRInstallationIDDidChangeNotification to be sent.
  831. XCTestExpectation *notificationExpectation =
  832. [self installationIDDidChangeNotificationExpectation];
  833. // 3. Request Auth Token.
  834. FBLPromise<FIRInstallationsItem *> *promise = [self.controller getAuthTokenForcingRefresh:NO];
  835. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  836. // 4. Check.
  837. XCTAssertNil(promise.error);
  838. XCTAssertNotNil(promise.value);
  839. XCTAssertEqualObjects(promise.value.firebaseInstallationID,
  840. receivedInstallation.firebaseInstallationID);
  841. [self waitForExpectations:@[ notificationExpectation ] timeout:0.5];
  842. OCMVerifyAll(self.mockInstallationsStore);
  843. OCMVerifyAll(self.mockAPIService);
  844. }
  845. #pragma mark - Helpers
  846. - (void)expectInstallationsStoreGetInstallationNotFound {
  847. NSError *notFoundError =
  848. [FIRInstallationsErrorUtil installationItemNotFoundForAppID:self.appID appName:self.appName];
  849. FBLPromise *installationNotFoundPromise = [FBLPromise pendingPromise];
  850. [installationNotFoundPromise reject:notFoundError];
  851. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  852. .andReturn(installationNotFoundPromise);
  853. }
  854. - (void)expectStoredIIDNotFound {
  855. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  856. [rejectedPromise reject:[FIRInstallationsErrorUtil keychainErrorWithFunction:@"" status:-1]];
  857. OCMExpect([self.mockIIDStore existingIID]).andReturn(rejectedPromise);
  858. OCMExpect([self.mockIIDTokenStore existingIIDDefaultToken]).andReturn(rejectedPromise);
  859. }
  860. - (void)assertValidCreatedInstallation:(FIRInstallationsItem *)installation {
  861. XCTAssertEqualObjects([installation class], [FIRInstallationsItem class]);
  862. XCTAssertEqualObjects(installation.appID, self.appID);
  863. XCTAssertEqualObjects(installation.firebaseAppName, self.appName);
  864. XCTAssertEqual(installation.registrationStatus, FIRInstallationStatusUnregistered);
  865. XCTAssertNotNil(installation.firebaseInstallationID);
  866. }
  867. - (XCTestExpectation *)installationIDDidChangeNotificationExpectation {
  868. XCTestExpectation *notificationExpectation = [self
  869. expectationForNotification:FIRInstallationIDDidChangeNotification
  870. object:nil
  871. handler:^BOOL(NSNotification *_Nonnull notification) {
  872. XCTAssertEqualObjects(
  873. notification
  874. .userInfo[kFIRInstallationIDDidChangeNotificationAppNameKey],
  875. self.appName);
  876. return YES;
  877. }];
  878. return notificationExpectation;
  879. }
  880. - (void)expectInstallationStoreToBeRequestedAndReturnInstallation:
  881. (FIRInstallationsItem *)storedInstallation {
  882. OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
  883. .andReturn([FBLPromise resolvedWith:storedInstallation]);
  884. }
  885. - (FIRInstallationsItem *)expectAuthTokenRefreshForInstallation:
  886. (FIRInstallationsItem *)installation {
  887. FIRInstallationsItem *responseInstallation =
  888. [FIRInstallationsItem createRegisteredInstallationItem];
  889. responseInstallation.authToken.token =
  890. [responseInstallation.authToken.token stringByAppendingString:@"_new"];
  891. OCMExpect([self.mockAPIService refreshAuthTokenForInstallation:installation])
  892. .andReturn([FBLPromise resolvedWith:responseInstallation]);
  893. // 1.3. Expect new token to be stored.
  894. OCMExpect([self.mockInstallationsStore saveInstallation:responseInstallation])
  895. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  896. return responseInstallation;
  897. }
  898. @end