FIRInstallationsAPIServiceTests.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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 FirebaseCoreInternal;
  19. #import "FBLPromise+Testing.h"
  20. #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallationsItem+Tests.h"
  21. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsErrorUtil.h"
  22. #import "FirebaseInstallations/Source/Library/Errors/FIRInstallationsHTTPError.h"
  23. #import "FirebaseInstallations/Source/Library/InstallationsAPI/FIRInstallationsAPIService.h"
  24. #import "FirebaseInstallations/Source/Library/InstallationsStore/FIRInstallationsStoredAuthToken.h"
  25. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  26. typedef FBLPromise * (^FIRInstallationsAPIServiceTask)(void);
  27. #pragma mark - Fakes
  28. /// A fake heartbeat logger used for dependency injection during testing.
  29. @interface FIRHeartbeatLoggerFake : NSObject <FIRHeartbeatLoggerProtocol>
  30. @property(nonatomic, copy, nullable) FIRHeartbeatsPayload * (^onFlushHeartbeatsIntoPayloadHandler)
  31. (void);
  32. @property(nonatomic, copy, nullable) FIRDailyHeartbeatCode (^onHeartbeatCodeForTodayHandler)(void);
  33. @end
  34. @implementation FIRHeartbeatLoggerFake
  35. - (nonnull FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
  36. if (self.onFlushHeartbeatsIntoPayloadHandler) {
  37. return self.onFlushHeartbeatsIntoPayloadHandler();
  38. } else {
  39. return nil;
  40. }
  41. }
  42. - (FIRDailyHeartbeatCode)heartbeatCodeForToday {
  43. // This API should not be used by the below tests because the Installations
  44. // SDK uses only the V2 heartbeat API (`flushHeartbeatsIntoPayload`) for
  45. // getting heartbeats.
  46. [self doesNotRecognizeSelector:_cmd];
  47. return FIRDailyHeartbeatCodeNone;
  48. }
  49. - (void)log {
  50. // This API should not be used by the below tests because the Installations
  51. // SDK does not log heartbeats in its networking context.
  52. [self doesNotRecognizeSelector:_cmd];
  53. }
  54. - (NSString *_Nullable)headerValue {
  55. return FIRHeaderValueFromHeartbeatsPayload([self flushHeartbeatsIntoPayload]);
  56. }
  57. - (void)asyncHeaderValueWithCompletionHandler:
  58. (nonnull void (^)(NSString *_Nullable))completionHandler {
  59. [self doesNotRecognizeSelector:_cmd];
  60. }
  61. @end
  62. #pragma mark - FIRInstallationsAPIService + Internal
  63. @interface FIRInstallationsAPIService (Internal)
  64. - (instancetype)initWithURLSession:(NSURLSession *)URLSession
  65. APIKey:(NSString *)APIKey
  66. projectID:(NSString *)projectID
  67. heartbeatLogger:(id<FIRHeartbeatLoggerProtocol>)heartbeatLogger;
  68. @end
  69. #pragma mark - FIRInstallationsAPIServiceTests
  70. @interface FIRInstallationsAPIServiceTests : XCTestCase
  71. @property(nonatomic) FIRInstallationsAPIService *service;
  72. @property(nonatomic) FIRHeartbeatLoggerFake *heartbeatLoggerFake;
  73. @property(nonatomic) NSString *APIKey;
  74. @property(nonatomic) NSString *projectID;
  75. @property(nonatomic) id mockURLSession;
  76. @end
  77. @implementation FIRInstallationsAPIServiceTests
  78. - (void)setUp {
  79. self.APIKey = @"api-key";
  80. self.projectID = @"project-id";
  81. self.mockURLSession = OCMClassMock([NSURLSession class]);
  82. self.heartbeatLoggerFake = [[FIRHeartbeatLoggerFake alloc] init];
  83. self.service = [[FIRInstallationsAPIService alloc] initWithURLSession:self.mockURLSession
  84. APIKey:self.APIKey
  85. projectID:self.projectID
  86. heartbeatLogger:self.heartbeatLoggerFake];
  87. }
  88. - (void)tearDown {
  89. self.service = nil;
  90. self.heartbeatLoggerFake = nil;
  91. self.mockURLSession = nil;
  92. self.projectID = nil;
  93. self.APIKey = nil;
  94. // Wait for any pending promises to complete.
  95. XCTAssert(FBLWaitForPromisesWithTimeout(2));
  96. }
  97. - (void)testRegisterInstallationSuccessWhenHeartbeatsNeedSending {
  98. // Given
  99. NSString *fixtureName = @"APIRegisterInstallationResponseSuccess.json";
  100. FIRHeartbeatsPayload *nonEmptyHeartbeatsPayload =
  101. [FIRHeartbeatLoggingTestUtils nonEmptyHeartbeatsPayload];
  102. // When
  103. self.heartbeatLoggerFake.onFlushHeartbeatsIntoPayloadHandler = ^FIRHeartbeatsPayload * {
  104. return nonEmptyHeartbeatsPayload;
  105. };
  106. // Then
  107. [self assertRegisterInstallationSuccessWithResponseFixtureName:fixtureName
  108. responseCode:201
  109. expectedFIDOverride:@"aaaaaaaaaaaaaaaaaaaaaa"
  110. heartbeatsPayload:nonEmptyHeartbeatsPayload];
  111. }
  112. - (void)testRegisterInstallationSuccessWhenNoHeartbeatsNeedSending {
  113. // Given
  114. NSString *fixtureName = @"APIRegisterInstallationResponseSuccess.json";
  115. FIRHeartbeatsPayload *emptyHeartbeatsPayload =
  116. [FIRHeartbeatLoggingTestUtils emptyHeartbeatsPayload];
  117. // When
  118. self.heartbeatLoggerFake.onFlushHeartbeatsIntoPayloadHandler = ^FIRHeartbeatsPayload * {
  119. return emptyHeartbeatsPayload;
  120. };
  121. // Then
  122. [self assertRegisterInstallationSuccessWithResponseFixtureName:fixtureName
  123. responseCode:201
  124. expectedFIDOverride:@"aaaaaaaaaaaaaaaaaaaaaa"
  125. heartbeatsPayload:emptyHeartbeatsPayload];
  126. }
  127. - (void)testRegisterInstallationSuccess_NoFIDInResponse {
  128. NSString *fixtureName = @"APIRegisterInstallationResponseSuccessNoFID.json";
  129. [self assertRegisterInstallationSuccessWithResponseFixtureName:fixtureName
  130. responseCode:201
  131. expectedFIDOverride:nil
  132. heartbeatsPayload:nil];
  133. }
  134. - (void)testRegisterInstallationSuccess_InvalidInstallation {
  135. FIRInstallationsItem *installation = [FIRInstallationsItem createUnregisteredInstallationItem];
  136. installation.firebaseInstallationID = nil;
  137. __auto_type promise = [self.service registerInstallation:installation];
  138. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  139. XCTAssertTrue(promise.isRejected);
  140. XCTAssertNil(promise.value);
  141. XCTAssertNotNil(promise.error);
  142. }
  143. - (void)testRefreshAuthTokenSuccessWhenHeartbeatsNeedSending {
  144. // Given
  145. FIRHeartbeatsPayload *nonEmptyHeartbeatsPayload =
  146. [FIRHeartbeatLoggingTestUtils nonEmptyHeartbeatsPayload];
  147. // When
  148. self.heartbeatLoggerFake.onFlushHeartbeatsIntoPayloadHandler = ^FIRHeartbeatsPayload * {
  149. return nonEmptyHeartbeatsPayload;
  150. };
  151. // Then
  152. [self assertRefreshAuthTokenSuccessWhenSendingHeartbeatsPayload:nonEmptyHeartbeatsPayload];
  153. }
  154. - (void)testRefreshAuthTokenSuccessWhenNoHeartbeatsNeedSending {
  155. // Given
  156. FIRHeartbeatsPayload *emptyHeartbeatsPayload =
  157. [FIRHeartbeatLoggingTestUtils emptyHeartbeatsPayload];
  158. // When
  159. self.heartbeatLoggerFake.onFlushHeartbeatsIntoPayloadHandler = ^FIRHeartbeatsPayload * {
  160. return emptyHeartbeatsPayload;
  161. };
  162. // Then
  163. [self assertRefreshAuthTokenSuccessWhenSendingHeartbeatsPayload:emptyHeartbeatsPayload];
  164. }
  165. - (void)testRefreshAuthTokenAPIError {
  166. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  167. installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
  168. // 1. Stub URL session:
  169. // 1.1. URL request validation.
  170. id URLRequestValidation = [self refreshTokenRequestValidationArgWithInstallation:installation
  171. heartbeatsPayload:nil];
  172. // 1.2. Capture completion to call it later.
  173. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  174. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  175. taskCompletion = obj;
  176. return YES;
  177. }];
  178. // 1.3. Create a data task mock.
  179. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  180. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  181. // 1.4. Expect `dataTaskWithRequest` to be called.
  182. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  183. completionHandler:completionArg])
  184. .andReturn(mockDataTask);
  185. // 1.5. Prepare server response data.
  186. NSData *errorResponseData =
  187. [self loadFixtureNamed:@"APIGenerateTokenResponseInvalidRefreshToken.json"];
  188. // 2. Call
  189. FBLPromise<FIRInstallationsItem *> *promise =
  190. [self.service refreshAuthTokenForInstallation:installation];
  191. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  192. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  193. // 4. Wait for the data task `resume` to be called.
  194. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  195. // 5. Call the data task completion.
  196. taskCompletion(errorResponseData, [self responseWithStatusCode:401], nil);
  197. // 6. Check result.
  198. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  199. XCTAssertTrue([FIRInstallationsErrorUtil isAPIError:promise.error withHTTPCode:401]);
  200. XCTAssertNil(promise.value);
  201. }
  202. - (void)testRefreshAuthToken_WhenAPIError500_ThenRetriesOnce {
  203. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  204. installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
  205. // 1. Stub URL session:
  206. // 1.1. URL request validation.
  207. id URLRequestValidation = [self refreshTokenRequestValidationArgWithInstallation:installation
  208. heartbeatsPayload:nil];
  209. // 1.2. Capture completion to call it later.
  210. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  211. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  212. taskCompletion = obj;
  213. return YES;
  214. }];
  215. // 1.3. Create a data task mock.
  216. id mockDataTask1 = OCMClassMock([NSURLSessionDataTask class]);
  217. OCMExpect([(NSURLSessionDataTask *)mockDataTask1 resume]);
  218. // 1.4. Expect `dataTaskWithRequest` to be called.
  219. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  220. completionHandler:completionArg])
  221. .andReturn(mockDataTask1);
  222. // 1.5. Prepare server response data.
  223. NSData *errorResponseData =
  224. [self loadFixtureNamed:@"APIGenerateTokenResponseInvalidRefreshToken.json"];
  225. // 2. Call
  226. FBLPromise<FIRInstallationsItem *> *promise =
  227. [self.service refreshAuthTokenForInstallation:installation];
  228. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  229. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  230. // 4. Wait for the data task `resume` to be called.
  231. OCMVerifyAllWithDelay(mockDataTask1, 0.5);
  232. // 5. Call the data task completion.
  233. taskCompletion(errorResponseData,
  234. [self responseWithStatusCode:FIRInstallationsHTTPCodesServerInternalError], nil);
  235. // 6. Retry:
  236. // 6.1. Expect another API request to be sent.
  237. id mockDataTask2 = OCMClassMock([NSURLSessionDataTask class]);
  238. OCMExpect([(NSURLSessionDataTask *)mockDataTask2 resume]);
  239. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  240. completionHandler:completionArg])
  241. .andReturn(mockDataTask2);
  242. OCMVerifyAllWithDelay(self.mockURLSession, 1.5);
  243. OCMVerifyAllWithDelay(mockDataTask2, 1.5);
  244. // 6.2. Send the API response again.
  245. taskCompletion(errorResponseData,
  246. [self responseWithStatusCode:FIRInstallationsHTTPCodesServerInternalError], nil);
  247. // 6. Check result.
  248. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  249. XCTAssertTrue([FIRInstallationsErrorUtil
  250. isAPIError:promise.error
  251. withHTTPCode:FIRInstallationsHTTPCodesServerInternalError]);
  252. XCTAssertNil(promise.value);
  253. }
  254. - (void)testRefreshAuthTokenDataNil {
  255. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  256. installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
  257. // 1. Stub URL session:
  258. // 1.1. URL request validation.
  259. id URLRequestValidation = [self refreshTokenRequestValidationArgWithInstallation:installation
  260. heartbeatsPayload:nil];
  261. // 1.2. Capture completion to call it later.
  262. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  263. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  264. taskCompletion = obj;
  265. return YES;
  266. }];
  267. // 1.3. Create a data task mock.
  268. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  269. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  270. // 1.4. Expect `dataTaskWithRequest` to be called.
  271. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  272. completionHandler:completionArg])
  273. .andReturn(mockDataTask);
  274. // 2. Call
  275. FBLPromise<FIRInstallationsItem *> *promise =
  276. [self.service refreshAuthTokenForInstallation:installation];
  277. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  278. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  279. // 4. Wait for the data task `resume` to be called.
  280. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  281. // 5. Call the data task completion.
  282. // HTTP 200 but no data (a potential server failure).
  283. taskCompletion(nil, [self responseWithStatusCode:200], nil);
  284. // 6. Check result.
  285. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  286. XCTAssertEqualObjects(promise.error.userInfo[NSLocalizedFailureReasonErrorKey],
  287. @"Failed to serialize JSON data.");
  288. XCTAssertNil(promise.value);
  289. }
  290. - (void)testDeleteInstallationSuccessWhenHeartbeatsNeedSending {
  291. // Given
  292. FIRHeartbeatsPayload *nonEmptyHeartbeatsPayload =
  293. [FIRHeartbeatLoggingTestUtils nonEmptyHeartbeatsPayload];
  294. // When
  295. self.heartbeatLoggerFake.onFlushHeartbeatsIntoPayloadHandler = ^FIRHeartbeatsPayload * {
  296. return nonEmptyHeartbeatsPayload;
  297. };
  298. // Then
  299. [self assertDeleteInstallationSuccessWhenSendingHeartbeatsPayload:nonEmptyHeartbeatsPayload];
  300. }
  301. - (void)testDeleteInstallationSuccessWhenNoHeartbeatsNeedSending {
  302. // Given
  303. FIRHeartbeatsPayload *emptyHeartbeatsPayload =
  304. [FIRHeartbeatLoggingTestUtils emptyHeartbeatsPayload];
  305. // When
  306. self.heartbeatLoggerFake.onFlushHeartbeatsIntoPayloadHandler = ^FIRHeartbeatsPayload * {
  307. return emptyHeartbeatsPayload;
  308. };
  309. // Then
  310. [self assertDeleteInstallationSuccessWhenSendingHeartbeatsPayload:emptyHeartbeatsPayload];
  311. }
  312. - (void)testDeleteInstallationErrorNotFound {
  313. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  314. // 1. Stub URL session:
  315. // 1.1. URL request validation.
  316. id URLRequestValidation = [self deleteInstallationRequestValidationWithInstallation:installation
  317. heartbeatsPayload:nil];
  318. // 1.2. Capture completion to call it later.
  319. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  320. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  321. taskCompletion = obj;
  322. return YES;
  323. }];
  324. // 1.3. Create a data task mock.
  325. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  326. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  327. // 1.4. Expect `dataTaskWithRequest` to be called.
  328. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  329. completionHandler:completionArg])
  330. .andReturn(mockDataTask);
  331. // 2. Call
  332. FBLPromise<FIRInstallationsItem *> *promise = [self.service deleteInstallation:installation];
  333. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  334. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  335. // 4. Wait for the data task `resume` to be called.
  336. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  337. // 5. Call the data task completion.
  338. // HTTP 200 but no data (a potential server failure).
  339. taskCompletion(nil, [self responseWithStatusCode:404], nil);
  340. // 6. Check result.
  341. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  342. XCTAssertTrue([FIRInstallationsErrorUtil isAPIError:promise.error withHTTPCode:404]);
  343. XCTAssertNil(promise.value);
  344. }
  345. - (void)testDeleteInstallation_WhenAPIError500_ThenRetriesOnce {
  346. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  347. // 1. Stub URL session:
  348. // 1.1. URL request validation.
  349. id URLRequestValidation = [self deleteInstallationRequestValidationWithInstallation:installation
  350. heartbeatsPayload:nil];
  351. // 1.2. Capture completion to call it later.
  352. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  353. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  354. taskCompletion = obj;
  355. return YES;
  356. }];
  357. // 1.3. Create a data task mock.
  358. id mockDataTask1 = OCMClassMock([NSURLSessionDataTask class]);
  359. OCMExpect([(NSURLSessionDataTask *)mockDataTask1 resume]);
  360. // 1.4. Expect `dataTaskWithRequest` to be called.
  361. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  362. completionHandler:completionArg])
  363. .andReturn(mockDataTask1);
  364. // 2. Call
  365. FBLPromise<FIRInstallationsItem *> *promise = [self.service deleteInstallation:installation];
  366. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  367. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  368. // 4. Wait for the data task `resume` to be called.
  369. OCMVerifyAllWithDelay(mockDataTask1, 0.5);
  370. // 5. Call the data task completion.
  371. // HTTP 200 but no data (a potential server failure).
  372. taskCompletion(nil, [self responseWithStatusCode:FIRInstallationsHTTPCodesServerInternalError],
  373. nil);
  374. // 6. Retry:
  375. // 6.1. Wait for the API request to be sent again.
  376. id mockDataTask2 = OCMClassMock([NSURLSessionDataTask class]);
  377. OCMExpect([(NSURLSessionDataTask *)mockDataTask2 resume]);
  378. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  379. completionHandler:completionArg])
  380. .andReturn(mockDataTask2);
  381. OCMVerifyAllWithDelay(self.mockURLSession, 1.5);
  382. OCMVerifyAllWithDelay(mockDataTask1, 1.5);
  383. // 6.1. Send another response.
  384. taskCompletion(nil, [self responseWithStatusCode:FIRInstallationsHTTPCodesServerInternalError],
  385. nil);
  386. // 7. Check result.
  387. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  388. XCTAssertTrue([FIRInstallationsErrorUtil
  389. isAPIError:promise.error
  390. withHTTPCode:FIRInstallationsHTTPCodesServerInternalError]);
  391. XCTAssertNil(promise.value);
  392. }
  393. #pragma mark - Helpers
  394. - (void)assertRefreshAuthTokenSuccessWhenSendingHeartbeatsPayload:
  395. (FIRHeartbeatsPayload *)heartbeatsPayload {
  396. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  397. installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
  398. // 1. Stub URL session:
  399. // 1.1. URL request validation.
  400. id URLRequestValidation =
  401. [self refreshTokenRequestValidationArgWithInstallation:installation
  402. heartbeatsPayload:heartbeatsPayload];
  403. // 1.2. Capture completion to call it later.
  404. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  405. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  406. taskCompletion = obj;
  407. return YES;
  408. }];
  409. // 1.3. Create a data task mock.
  410. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  411. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  412. // 1.4. Expect `dataTaskWithRequest` to be called.
  413. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  414. completionHandler:completionArg])
  415. .andReturn(mockDataTask);
  416. // 1.5. Prepare server response data.
  417. NSData *successResponseData = [self loadFixtureNamed:@"APIGenerateTokenResponseSuccess.json"];
  418. // 2. Call
  419. FBLPromise<FIRInstallationsItem *> *promise =
  420. [self.service refreshAuthTokenForInstallation:installation];
  421. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  422. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  423. // 4. Wait for the data task `resume` to be called.
  424. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  425. // 5. Call the data task completion.
  426. taskCompletion(successResponseData, [self responseWithStatusCode:200], nil);
  427. // 6. Check result.
  428. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  429. XCTAssertNil(promise.error);
  430. XCTAssertNotNil(promise.value);
  431. XCTAssertNotEqual(promise.value, installation);
  432. XCTAssertEqualObjects(promise.value.appID, installation.appID);
  433. XCTAssertEqualObjects(promise.value.firebaseAppName, installation.firebaseAppName);
  434. XCTAssertEqualObjects(promise.value.firebaseInstallationID, installation.firebaseInstallationID);
  435. XCTAssertEqualObjects(promise.value.refreshToken, installation.refreshToken);
  436. XCTAssertEqualObjects(promise.value.authToken.token,
  437. @"aaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbb.cccccccccccccccccccccccc");
  438. [self assertDate:promise.value.authToken.expirationDate
  439. isApproximatelyEqualCurrentPlusTimeInterval:3987465];
  440. }
  441. - (void)assertDeleteInstallationSuccessWhenSendingHeartbeatsPayload:
  442. (FIRHeartbeatsPayload *)heartbeatsPayload {
  443. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  444. // 1. Stub URL session:
  445. // 1.1. URL request validation.
  446. id URLRequestValidation =
  447. [self deleteInstallationRequestValidationWithInstallation:installation
  448. heartbeatsPayload:heartbeatsPayload];
  449. // 1.2. Capture completion to call it later.
  450. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  451. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  452. taskCompletion = obj;
  453. return YES;
  454. }];
  455. // 1.3. Create a data task mock.
  456. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  457. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  458. // 1.4. Expect `dataTaskWithRequest` to be called.
  459. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  460. completionHandler:completionArg])
  461. .andReturn(mockDataTask);
  462. // 2. Call
  463. FBLPromise<FIRInstallationsItem *> *promise = [self.service deleteInstallation:installation];
  464. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  465. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  466. // 4. Wait for the data task `resume` to be called.
  467. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  468. // 5. Call the data task completion.
  469. // HTTP 200 but no data (a potential server failure).
  470. NSData *successResponseData = [@"{}" dataUsingEncoding:NSUTF8StringEncoding];
  471. taskCompletion(successResponseData, [self responseWithStatusCode:200], nil);
  472. // 6. Check result.
  473. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  474. XCTAssertNil(promise.error);
  475. XCTAssertEqual(promise.value, installation);
  476. }
  477. - (NSData *)loadFixtureNamed:(NSString *)fileName {
  478. NSURL *fileURL = [[NSBundle bundleForClass:[self class]] URLForResource:fileName
  479. withExtension:nil];
  480. XCTAssertNotNil(fileURL);
  481. NSError *error;
  482. NSData *data = [NSData dataWithContentsOfURL:fileURL options:0 error:&error];
  483. XCTAssertNotNil(data, @"File name: %@ Error: %@", fileName, error);
  484. return data;
  485. }
  486. - (NSURLResponse *)responseWithStatusCode:(NSUInteger)statusCode {
  487. NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL fileURLWithPath:@"/"]
  488. statusCode:statusCode
  489. HTTPVersion:nil
  490. headerFields:nil];
  491. return response;
  492. }
  493. - (void)assertDate:(NSDate *)date
  494. isApproximatelyEqualCurrentPlusTimeInterval:(NSTimeInterval)timeInterval {
  495. NSDate *expectedDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  496. NSTimeInterval precision = 10;
  497. XCTAssert(ABS([date timeIntervalSinceDate:expectedDate]) <= precision,
  498. @"date: %@ is not equal to expected %@ with precision %f - %@", date, expectedDate,
  499. precision, self.name);
  500. }
  501. - (id)refreshTokenRequestValidationArgWithInstallation:(FIRInstallationsItem *)installation
  502. heartbeatsPayload:(FIRHeartbeatsPayload *)heartbeatsPayload {
  503. return [OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  504. XCTAssertEqualObjects(request.HTTPMethod, @"POST");
  505. XCTAssertEqualObjects(request.URL.absoluteString,
  506. @"https://firebaseinstallations.googleapis.com/v1/projects/project-id/"
  507. @"installations/qwertyuiopasdfghjklzxcvbnm/authTokens:generate");
  508. NSMutableDictionary<NSString *, NSString *> *expectedHTTPHeaderFields = @{
  509. @"Content-Type" : @"application/json",
  510. @"X-Goog-Api-Key" : self.APIKey,
  511. @"X-Ios-Bundle-Identifier" : [[NSBundle mainBundle] bundleIdentifier],
  512. @"Authorization" : [NSString stringWithFormat:@"FIS_v2 %@", installation.refreshToken]
  513. }
  514. .mutableCopy;
  515. NSString *_Nullable heartbeatHeaderValue =
  516. FIRHeaderValueFromHeartbeatsPayload(heartbeatsPayload);
  517. if (heartbeatHeaderValue) {
  518. expectedHTTPHeaderFields[@"X-firebase-client"] = heartbeatHeaderValue;
  519. }
  520. XCTAssertEqualObjects([request allHTTPHeaderFields], expectedHTTPHeaderFields);
  521. NSError *error;
  522. NSDictionary *body = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
  523. options:0
  524. error:&error];
  525. XCTAssertNotNil(body, @"Error: %@, test: %@", error, self.name);
  526. XCTAssertEqualObjects(
  527. body,
  528. @{@"installation" : @{@"sdkVersion" : [self SDKVersion]}}, @"%@", self.name);
  529. return YES;
  530. }];
  531. }
  532. - (id)deleteInstallationRequestValidationWithInstallation:(FIRInstallationsItem *)installation
  533. heartbeatsPayload:
  534. (FIRHeartbeatsPayload *)heartbeatsPayload {
  535. return [OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  536. XCTAssert([request isKindOfClass:[NSURLRequest class]], @"Unexpected class: %@",
  537. [request class]);
  538. XCTAssertEqualObjects(request.HTTPMethod, @"DELETE");
  539. NSString *expectedURL = [NSString
  540. stringWithFormat:
  541. @"https://firebaseinstallations.googleapis.com/v1/projects/%@/installations/%@/",
  542. self.projectID, installation.firebaseInstallationID];
  543. XCTAssertEqualObjects(request.URL.absoluteString, expectedURL);
  544. NSMutableDictionary<NSString *, NSString *> *expectedHTTPHeaderFields = @{
  545. @"Content-Type" : @"application/json",
  546. @"X-Goog-Api-Key" : self.APIKey,
  547. @"X-Ios-Bundle-Identifier" : [[NSBundle mainBundle] bundleIdentifier],
  548. @"Authorization" : [NSString stringWithFormat:@"FIS_v2 %@", installation.refreshToken],
  549. }
  550. .mutableCopy;
  551. NSString *_Nullable heartbeatHeaderValue =
  552. FIRHeaderValueFromHeartbeatsPayload(heartbeatsPayload);
  553. if (heartbeatHeaderValue) {
  554. expectedHTTPHeaderFields[@"X-firebase-client"] = heartbeatHeaderValue;
  555. }
  556. XCTAssertEqualObjects([request allHTTPHeaderFields], expectedHTTPHeaderFields);
  557. NSString *expectedAuthHeader =
  558. [NSString stringWithFormat:@"FIS_v2 %@", installation.refreshToken];
  559. XCTAssertEqualObjects(request.allHTTPHeaderFields[@"Authorization"], expectedAuthHeader, @"%@",
  560. self.name);
  561. NSError *error;
  562. NSDictionary *JSONBody = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
  563. options:0
  564. error:&error];
  565. XCTAssertNotNil(JSONBody, @"Error: %@", error);
  566. XCTAssertEqualObjects(JSONBody, @{});
  567. return YES;
  568. }];
  569. }
  570. - (void)assertRegisterInstallationSuccessWithResponseFixtureName:(NSString *)fixtureName
  571. responseCode:(NSInteger)responseCode
  572. expectedFIDOverride:(nullable NSString *)overrideFID
  573. heartbeatsPayload:
  574. (FIRHeartbeatsPayload *)heartbeatsPayload {
  575. FIRInstallationsItem *installation = [FIRInstallationsItem createUnregisteredInstallationItem];
  576. installation.IIDDefaultToken = @"iid-auth-token";
  577. NSString *expectedFID = overrideFID ?: installation.firebaseInstallationID;
  578. // 1. Stub URL session:
  579. // 1.1. URL request validation.
  580. id URLRequestValidation = [OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  581. XCTAssertEqualObjects(request.HTTPMethod, @"POST");
  582. XCTAssertEqualObjects(
  583. request.URL.absoluteString,
  584. @"https://firebaseinstallations.googleapis.com/v1/projects/project-id/installations/");
  585. NSMutableDictionary<NSString *, NSString *> *expectedHTTPHeaderFields = @{
  586. @"Content-Type" : @"application/json",
  587. @"X-Goog-Api-Key" : self.APIKey,
  588. @"X-Ios-Bundle-Identifier" : [[NSBundle mainBundle] bundleIdentifier],
  589. }
  590. .mutableCopy;
  591. NSString *_Nullable heartbeatHeaderValue =
  592. FIRHeaderValueFromHeartbeatsPayload(heartbeatsPayload);
  593. if (heartbeatHeaderValue) {
  594. expectedHTTPHeaderFields[@"X-firebase-client"] = heartbeatHeaderValue;
  595. }
  596. [expectedHTTPHeaderFields addEntriesFromDictionary:@{
  597. @"x-goog-fis-ios-iid-migration-auth" : installation.IIDDefaultToken
  598. }];
  599. XCTAssertEqualObjects([request allHTTPHeaderFields], expectedHTTPHeaderFields);
  600. NSError *error;
  601. NSDictionary *body = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
  602. options:0
  603. error:&error];
  604. XCTAssertNotNil(body, @"Error: %@", error);
  605. XCTAssertEqualObjects(body[@"fid"], installation.firebaseInstallationID);
  606. XCTAssertEqualObjects(body[@"authVersion"], @"FIS_v2");
  607. XCTAssertEqualObjects(body[@"appId"], installation.appID);
  608. XCTAssertEqualObjects(body[@"sdkVersion"], [self SDKVersion]);
  609. return YES;
  610. }];
  611. // 1.2. Capture completion to call it later.
  612. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  613. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  614. taskCompletion = obj;
  615. return YES;
  616. }];
  617. // 1.3. Create a data task mock.
  618. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  619. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  620. // 1.4. Expect `dataTaskWithRequest` to be called.
  621. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  622. completionHandler:completionArg])
  623. .andReturn(mockDataTask);
  624. // 2. Call
  625. FBLPromise<FIRInstallationsItem *> *promise = [self.service registerInstallation:installation];
  626. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  627. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  628. // 4. Wait for the data task `resume` to be called.
  629. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  630. // 5. Call the data task completion.
  631. NSData *responseData = [self loadFixtureNamed:fixtureName];
  632. taskCompletion(responseData, [self responseWithStatusCode:responseCode], nil);
  633. // 6. Check result.
  634. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  635. XCTAssertNil(promise.error);
  636. XCTAssertNotNil(promise.value);
  637. XCTAssertNotEqual(promise.value, installation);
  638. XCTAssertEqualObjects(promise.value.appID, installation.appID);
  639. XCTAssertEqualObjects(promise.value.firebaseAppName, installation.firebaseAppName);
  640. // Server may respond with a different FID if the sent FID cannot be accepted.
  641. XCTAssertEqualObjects(promise.value.firebaseInstallationID, expectedFID);
  642. XCTAssertEqualObjects(promise.value.refreshToken, @"aaaaaaabbbbbbbbcccccccccdddddddd00000000");
  643. XCTAssertEqualObjects(promise.value.authToken.token,
  644. @"aaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbb.cccccccccccccccccccccccc");
  645. [self assertDate:promise.value.authToken.expirationDate
  646. isApproximatelyEqualCurrentPlusTimeInterval:604800];
  647. }
  648. - (NSString *)SDKVersion {
  649. return [NSString stringWithFormat:@"i:%@", FIRFirebaseVersion()];
  650. }
  651. @end