FIRInstallationsIDControllerTests.m 62 KB

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