FIRInstallationsAPIServiceTests.m 32 KB

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