FIRInstallationsAPIServiceTests.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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 "FBLPromise+Testing.h"
  19. #import "FIRInstallationsItem+Tests.h"
  20. #import "FIRInstallationsAPIService.h"
  21. #import "FIRInstallationsErrorUtil.h"
  22. #import "FIRInstallationsHTTPError.h"
  23. #import "FIRInstallationsStoredAuthToken.h"
  24. #import "FIRInstallationsStoredIIDCheckin.h"
  25. #import "FIRInstallationsVersion.h"
  26. typedef FBLPromise * (^FIRInstallationsAPIServiceTask)(void);
  27. @interface FIRInstallationsAPIService (Tests)
  28. - (instancetype)initWithURLSession:(NSURLSession *)URLSession
  29. APIKey:(NSString *)APIKey
  30. projectID:(NSString *)projectID;
  31. @end
  32. @interface FIRInstallationsAPIServiceTests : XCTestCase
  33. @property(nonatomic) FIRInstallationsAPIService *service;
  34. @property(nonatomic) id mockURLSession;
  35. @property(nonatomic) NSString *APIKey;
  36. @property(nonatomic) NSString *projectID;
  37. @end
  38. @implementation FIRInstallationsAPIServiceTests
  39. - (void)setUp {
  40. self.APIKey = @"api-key";
  41. self.projectID = @"project-id";
  42. self.mockURLSession = OCMClassMock([NSURLSession class]);
  43. self.service = [[FIRInstallationsAPIService alloc] initWithURLSession:self.mockURLSession
  44. APIKey:self.APIKey
  45. projectID:self.projectID];
  46. }
  47. - (void)tearDown {
  48. self.service = nil;
  49. self.mockURLSession = nil;
  50. self.projectID = nil;
  51. self.APIKey = nil;
  52. // // Wait for any pending promises to complete.
  53. // XCTAssert(FBLWaitForPromisesWithTimeout(2));
  54. }
  55. - (void)testRegisterInstallationSuccess {
  56. FIRInstallationsItem *installation = [[FIRInstallationsItem alloc] initWithAppID:@"app-id"
  57. firebaseAppName:@"name"];
  58. installation.firebaseInstallationID = [FIRInstallationsItem generateFID];
  59. installation.IIDCheckin =
  60. [[FIRInstallationsStoredIIDCheckin alloc] initWithDeviceID:@"IIDDeviceID"
  61. secretToken:@"IIDSecretToken"];
  62. // 1. Stub URL session:
  63. // 1.1. URL request validation.
  64. id URLRequestValidation = [OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  65. XCTAssertEqualObjects(request.HTTPMethod, @"POST");
  66. XCTAssertEqualObjects(
  67. request.URL.absoluteString,
  68. @"https://firebaseinstallations.googleapis.com/v1/projects/project-id/installations/");
  69. XCTAssertEqualObjects([request valueForHTTPHeaderField:@"Content-Type"], @"application/json");
  70. XCTAssertEqualObjects([request valueForHTTPHeaderField:@"X-Goog-Api-Key"], self.APIKey);
  71. NSString *expectedIIDMigrationHeader = @"IIDDeviceID:IIDSecretToken";
  72. XCTAssertEqualObjects([request valueForHTTPHeaderField:@"x-goog-fis-ios-iid-migration-auth"],
  73. expectedIIDMigrationHeader);
  74. NSError *error;
  75. NSDictionary *body = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
  76. options:0
  77. error:&error];
  78. XCTAssertNotNil(body, @"Error: %@", error);
  79. XCTAssertEqualObjects(body[@"fid"], installation.firebaseInstallationID);
  80. XCTAssertEqualObjects(body[@"authVersion"], @"FIS_v2");
  81. XCTAssertEqualObjects(body[@"appId"], installation.appID);
  82. XCTAssertEqualObjects(body[@"sdkVersion"], [self SDKVersion]);
  83. return YES;
  84. }];
  85. // 1.2. Capture completion to call it later.
  86. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  87. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  88. taskCompletion = obj;
  89. return YES;
  90. }];
  91. // 1.3. Create a data task mock.
  92. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  93. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  94. // 1.4. Expect `dataTaskWithRequest` to be called.
  95. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  96. completionHandler:completionArg])
  97. .andReturn(mockDataTask);
  98. // 2. Call
  99. FBLPromise<FIRInstallationsItem *> *promise = [self.service registerInstallation:installation];
  100. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  101. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  102. // 4. Wait for the data task `resume` to be called.
  103. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  104. // 5. Call the data task completion.
  105. NSData *successResponseData =
  106. [self loadFixtureNamed:@"APIRegisterInstallationResponseSuccess.json"];
  107. taskCompletion(successResponseData, [self responseWithStatusCode:201], nil);
  108. // 6. Check result.
  109. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  110. XCTAssertNil(promise.error);
  111. XCTAssertNotNil(promise.value);
  112. XCTAssertNotEqual(promise.value, installation);
  113. XCTAssertEqualObjects(promise.value.appID, installation.appID);
  114. XCTAssertEqualObjects(promise.value.firebaseAppName, installation.firebaseAppName);
  115. // Server may respond with a different FID if the sent FID cannot be accepted.
  116. XCTAssertEqualObjects(promise.value.firebaseInstallationID, @"aaaaaaaaaaaaaaaaaaaaaa");
  117. XCTAssertEqualObjects(promise.value.refreshToken, @"aaaaaaabbbbbbbbcccccccccdddddddd00000000");
  118. XCTAssertEqualObjects(promise.value.authToken.token,
  119. @"aaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbb.cccccccccccccccccccccccc");
  120. [self assertDate:promise.value.authToken.expirationDate
  121. isApproximatelyEqualCurrentPlusTimeInterval:604800];
  122. }
  123. - (void)testRegisterInstallation_WhenError500_ThenRetriesOnce {
  124. FIRInstallationsItem *installation = [[FIRInstallationsItem alloc] initWithAppID:@"app-id"
  125. firebaseAppName:@"name"];
  126. installation.firebaseInstallationID = [FIRInstallationsItem generateFID];
  127. // 1. Stub URL session:
  128. // 1.2. Capture completion to call it later.
  129. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  130. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  131. taskCompletion = obj;
  132. return YES;
  133. }];
  134. // 1.3. Create a data task mock.
  135. id mockDataTask1 = OCMClassMock([NSURLSessionDataTask class]);
  136. OCMExpect([(NSURLSessionDataTask *)mockDataTask1 resume]);
  137. // 1.4. Expect `dataTaskWithRequest` to be called.
  138. OCMExpect([self.mockURLSession dataTaskWithRequest:[OCMArg any] completionHandler:completionArg])
  139. .andReturn(mockDataTask1);
  140. // 2. Call
  141. FBLPromise<FIRInstallationsItem *> *promise = [self.service registerInstallation:installation];
  142. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  143. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  144. // 4. Wait for the data task `resume` to be called.
  145. OCMVerifyAllWithDelay(mockDataTask1, 0.5);
  146. // 5. Call the data task completion.
  147. NSData *successResponseData =
  148. [self loadFixtureNamed:@"APIRegisterInstallationResponseSuccess.json"];
  149. taskCompletion(successResponseData,
  150. [self responseWithStatusCode:FIRInstallationsHTTPCodesServerInternalError], nil);
  151. // 6.1. Expect network request to send again.
  152. id mockDataTask2 = OCMClassMock([NSURLSessionDataTask class]);
  153. OCMExpect([(NSURLSessionDataTask *)mockDataTask2 resume]);
  154. OCMExpect([self.mockURLSession dataTaskWithRequest:[OCMArg any] completionHandler:completionArg])
  155. .andReturn(mockDataTask2);
  156. // 6.2. Wait for the second network request to complete.
  157. OCMVerifyAllWithDelay(self.mockURLSession, 1.5);
  158. OCMVerifyAllWithDelay(mockDataTask2, 1.5);
  159. // 6.3. Send network response again.
  160. taskCompletion(successResponseData,
  161. [self responseWithStatusCode:FIRInstallationsHTTPCodesServerInternalError], nil);
  162. // 7. Check result.
  163. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  164. XCTAssertNil(promise.value);
  165. XCTAssertNotNil(promise.error);
  166. XCTAssertTrue([promise.error isKindOfClass:[FIRInstallationsHTTPError class]]);
  167. FIRInstallationsHTTPError *HTTPError = (FIRInstallationsHTTPError *)promise.error;
  168. XCTAssertEqual(HTTPError.HTTPResponse.statusCode, FIRInstallationsHTTPCodesServerInternalError);
  169. }
  170. - (void)testRefreshAuthTokenSuccess {
  171. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  172. installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
  173. // 1. Stub URL session:
  174. // 1.1. URL request validation.
  175. id URLRequestValidation = [self refreshTokenRequestValidationArgWithInstallation:installation];
  176. // 1.2. Capture completion to call it later.
  177. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  178. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  179. taskCompletion = obj;
  180. return YES;
  181. }];
  182. // 1.3. Create a data task mock.
  183. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  184. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  185. // 1.4. Expect `dataTaskWithRequest` to be called.
  186. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  187. completionHandler:completionArg])
  188. .andReturn(mockDataTask);
  189. // 1.5. Prepare server response data.
  190. NSData *successResponseData = [self loadFixtureNamed:@"APIGenerateTokenResponseSuccess.json"];
  191. // 2. Call
  192. FBLPromise<FIRInstallationsItem *> *promise =
  193. [self.service refreshAuthTokenForInstallation:installation];
  194. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  195. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  196. // 4. Wait for the data task `resume` to be called.
  197. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  198. // 5. Call the data task completion.
  199. taskCompletion(successResponseData, [self responseWithStatusCode:200], nil);
  200. // 6. Check result.
  201. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  202. XCTAssertNil(promise.error);
  203. XCTAssertNotNil(promise.value);
  204. XCTAssertNotEqual(promise.value, installation);
  205. XCTAssertEqualObjects(promise.value.appID, installation.appID);
  206. XCTAssertEqualObjects(promise.value.firebaseAppName, installation.firebaseAppName);
  207. XCTAssertEqualObjects(promise.value.firebaseInstallationID, installation.firebaseInstallationID);
  208. XCTAssertEqualObjects(promise.value.refreshToken, installation.refreshToken);
  209. XCTAssertEqualObjects(promise.value.authToken.token,
  210. @"aaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbb.cccccccccccccccccccccccc");
  211. [self assertDate:promise.value.authToken.expirationDate
  212. isApproximatelyEqualCurrentPlusTimeInterval:3987465];
  213. }
  214. - (void)testRefreshAuthTokenAPIError {
  215. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  216. installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
  217. // 1. Stub URL session:
  218. // 1.1. URL request validation.
  219. id URLRequestValidation = [self refreshTokenRequestValidationArgWithInstallation:installation];
  220. // 1.2. Capture completion to call it later.
  221. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  222. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  223. taskCompletion = obj;
  224. return YES;
  225. }];
  226. // 1.3. Create a data task mock.
  227. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  228. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  229. // 1.4. Expect `dataTaskWithRequest` to be called.
  230. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  231. completionHandler:completionArg])
  232. .andReturn(mockDataTask);
  233. // 1.5. Prepare server response data.
  234. NSData *errorResponseData =
  235. [self loadFixtureNamed:@"APIGenerateTokenResponseInvalidRefreshToken.json"];
  236. // 2. Call
  237. FBLPromise<FIRInstallationsItem *> *promise =
  238. [self.service refreshAuthTokenForInstallation:installation];
  239. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  240. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  241. // 4. Wait for the data task `resume` to be called.
  242. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  243. // 5. Call the data task completion.
  244. taskCompletion(errorResponseData, [self responseWithStatusCode:401], nil);
  245. // 6. Check result.
  246. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  247. XCTAssertTrue([FIRInstallationsErrorUtil isAPIError:promise.error withHTTPCode:401]);
  248. XCTAssertNil(promise.value);
  249. }
  250. - (void)testRefreshAuthToken_WhenAPIError500_ThenRetriesOnce {
  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. // 1.2. Capture completion to call it later.
  257. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  258. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  259. taskCompletion = obj;
  260. return YES;
  261. }];
  262. // 1.3. Create a data task mock.
  263. id mockDataTask1 = OCMClassMock([NSURLSessionDataTask class]);
  264. OCMExpect([(NSURLSessionDataTask *)mockDataTask1 resume]);
  265. // 1.4. Expect `dataTaskWithRequest` to be called.
  266. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  267. completionHandler:completionArg])
  268. .andReturn(mockDataTask1);
  269. // 1.5. Prepare server response data.
  270. NSData *errorResponseData =
  271. [self loadFixtureNamed:@"APIGenerateTokenResponseInvalidRefreshToken.json"];
  272. // 2. Call
  273. FBLPromise<FIRInstallationsItem *> *promise =
  274. [self.service refreshAuthTokenForInstallation:installation];
  275. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  276. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  277. // 4. Wait for the data task `resume` to be called.
  278. OCMVerifyAllWithDelay(mockDataTask1, 0.5);
  279. // 5. Call the data task completion.
  280. taskCompletion(errorResponseData,
  281. [self responseWithStatusCode:FIRInstallationsHTTPCodesServerInternalError], nil);
  282. // 6. Retry:
  283. // 6.1. Expect another API request to be sent.
  284. id mockDataTask2 = OCMClassMock([NSURLSessionDataTask class]);
  285. OCMExpect([(NSURLSessionDataTask *)mockDataTask2 resume]);
  286. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  287. completionHandler:completionArg])
  288. .andReturn(mockDataTask2);
  289. OCMVerifyAllWithDelay(self.mockURLSession, 1.5);
  290. OCMVerifyAllWithDelay(mockDataTask2, 1.5);
  291. // 6.2. Send the API response again.
  292. taskCompletion(errorResponseData,
  293. [self responseWithStatusCode:FIRInstallationsHTTPCodesServerInternalError], nil);
  294. // 6. Check result.
  295. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  296. XCTAssertTrue([FIRInstallationsErrorUtil
  297. isAPIError:promise.error
  298. withHTTPCode:FIRInstallationsHTTPCodesServerInternalError]);
  299. XCTAssertNil(promise.value);
  300. }
  301. - (void)testRefreshAuthTokenDataNil {
  302. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  303. installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
  304. // 1. Stub URL session:
  305. // 1.1. URL request validation.
  306. id URLRequestValidation = [self refreshTokenRequestValidationArgWithInstallation:installation];
  307. // 1.2. Capture completion to call it later.
  308. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  309. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  310. taskCompletion = obj;
  311. return YES;
  312. }];
  313. // 1.3. Create a data task mock.
  314. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  315. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  316. // 1.4. Expect `dataTaskWithRequest` to be called.
  317. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  318. completionHandler:completionArg])
  319. .andReturn(mockDataTask);
  320. // 2. Call
  321. FBLPromise<FIRInstallationsItem *> *promise =
  322. [self.service refreshAuthTokenForInstallation:installation];
  323. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  324. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  325. // 4. Wait for the data task `resume` to be called.
  326. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  327. // 5. Call the data task completion.
  328. // HTTP 200 but no data (a potential server failure).
  329. taskCompletion(nil, [self responseWithStatusCode:200], nil);
  330. // 6. Check result.
  331. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  332. XCTAssertEqualObjects(promise.error.userInfo[NSLocalizedFailureReasonErrorKey],
  333. @"Failed to serialize JSON data.");
  334. XCTAssertNil(promise.value);
  335. }
  336. - (void)testDeleteInstallationSuccess {
  337. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  338. // 1. Stub URL session:
  339. // 1.1. URL request validation.
  340. id URLRequestValidation = [self deleteInstallationRequestValidationWithInstallation:installation];
  341. // 1.2. Capture completion to call it later.
  342. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  343. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  344. taskCompletion = obj;
  345. return YES;
  346. }];
  347. // 1.3. Create a data task mock.
  348. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  349. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  350. // 1.4. Expect `dataTaskWithRequest` to be called.
  351. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  352. completionHandler:completionArg])
  353. .andReturn(mockDataTask);
  354. // 2. Call
  355. FBLPromise<FIRInstallationsItem *> *promise = [self.service deleteInstallation:installation];
  356. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  357. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  358. // 4. Wait for the data task `resume` to be called.
  359. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  360. // 5. Call the data task completion.
  361. // HTTP 200 but no data (a potential server failure).
  362. NSData *successResponseData = [@"{}" dataUsingEncoding:NSUTF8StringEncoding];
  363. taskCompletion(successResponseData, [self responseWithStatusCode:200], nil);
  364. // 6. Check result.
  365. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  366. XCTAssertNil(promise.error);
  367. XCTAssertEqual(promise.value, installation);
  368. }
  369. - (void)testDeleteInstallationErrorNotFound {
  370. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  371. // 1. Stub URL session:
  372. // 1.1. URL request validation.
  373. id URLRequestValidation = [self deleteInstallationRequestValidationWithInstallation:installation];
  374. // 1.2. Capture completion to call it later.
  375. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  376. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  377. taskCompletion = obj;
  378. return YES;
  379. }];
  380. // 1.3. Create a data task mock.
  381. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  382. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  383. // 1.4. Expect `dataTaskWithRequest` to be called.
  384. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  385. completionHandler:completionArg])
  386. .andReturn(mockDataTask);
  387. // 2. Call
  388. FBLPromise<FIRInstallationsItem *> *promise = [self.service deleteInstallation:installation];
  389. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  390. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  391. // 4. Wait for the data task `resume` to be called.
  392. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  393. // 5. Call the data task completion.
  394. // HTTP 200 but no data (a potential server failure).
  395. taskCompletion(nil, [self responseWithStatusCode:404], nil);
  396. // 6. Check result.
  397. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  398. XCTAssertTrue([FIRInstallationsErrorUtil isAPIError:promise.error withHTTPCode:404]);
  399. XCTAssertNil(promise.value);
  400. }
  401. - (void)testDeleteInstallation_WhenAPIError500_ThenRetriesOnce {
  402. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  403. // 1. Stub URL session:
  404. // 1.1. URL request validation.
  405. id URLRequestValidation = [self deleteInstallationRequestValidationWithInstallation:installation];
  406. // 1.2. Capture completion to call it later.
  407. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  408. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  409. taskCompletion = obj;
  410. return YES;
  411. }];
  412. // 1.3. Create a data task mock.
  413. id mockDataTask1 = OCMClassMock([NSURLSessionDataTask class]);
  414. OCMExpect([(NSURLSessionDataTask *)mockDataTask1 resume]);
  415. // 1.4. Expect `dataTaskWithRequest` to be called.
  416. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  417. completionHandler:completionArg])
  418. .andReturn(mockDataTask1);
  419. // 2. Call
  420. FBLPromise<FIRInstallationsItem *> *promise = [self.service deleteInstallation: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(mockDataTask1, 0.5);
  425. // 5. Call the data task completion.
  426. // HTTP 200 but no data (a potential server failure).
  427. taskCompletion(nil, [self responseWithStatusCode:FIRInstallationsHTTPCodesServerInternalError],
  428. nil);
  429. // 6. Retry:
  430. // 6.1. Wait for the API request to be sent again.
  431. id mockDataTask2 = OCMClassMock([NSURLSessionDataTask class]);
  432. OCMExpect([(NSURLSessionDataTask *)mockDataTask2 resume]);
  433. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  434. completionHandler:completionArg])
  435. .andReturn(mockDataTask2);
  436. OCMVerifyAllWithDelay(self.mockURLSession, 1.5);
  437. OCMVerifyAllWithDelay(mockDataTask1, 1.5);
  438. // 6.1. Send another response.
  439. taskCompletion(nil, [self responseWithStatusCode:FIRInstallationsHTTPCodesServerInternalError],
  440. nil);
  441. // 7. Check result.
  442. XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
  443. XCTAssertTrue([FIRInstallationsErrorUtil
  444. isAPIError:promise.error
  445. withHTTPCode:FIRInstallationsHTTPCodesServerInternalError]);
  446. XCTAssertNil(promise.value);
  447. }
  448. #pragma mark - Helpers
  449. - (NSData *)loadFixtureNamed:(NSString *)fileName {
  450. NSURL *fileURL = [[NSBundle bundleForClass:[self class]] URLForResource:fileName
  451. withExtension:nil];
  452. XCTAssertNotNil(fileURL);
  453. NSError *error;
  454. NSData *data = [NSData dataWithContentsOfURL:fileURL options:0 error:&error];
  455. XCTAssertNotNil(data, @"File name: %@ Error: %@", fileName, error);
  456. return data;
  457. }
  458. - (NSURLResponse *)responseWithStatusCode:(NSUInteger)statusCode {
  459. NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL fileURLWithPath:@"/"]
  460. statusCode:statusCode
  461. HTTPVersion:nil
  462. headerFields:nil];
  463. return response;
  464. }
  465. - (void)assertDate:(NSDate *)date
  466. isApproximatelyEqualCurrentPlusTimeInterval:(NSTimeInterval)timeInterval {
  467. NSDate *expectedDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  468. NSTimeInterval precision = 10;
  469. XCTAssert(ABS([date timeIntervalSinceDate:expectedDate]) <= precision,
  470. @"date: %@ is not equal to expected %@ with precision %f - %@", date, expectedDate,
  471. precision, self.name);
  472. }
  473. - (id)refreshTokenRequestValidationArgWithInstallation:(FIRInstallationsItem *)installation {
  474. return [OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  475. XCTAssertEqualObjects(request.HTTPMethod, @"POST");
  476. XCTAssertEqualObjects(request.URL.absoluteString,
  477. @"https://firebaseinstallations.googleapis.com/v1/projects/project-id/"
  478. @"installations/qwertyuiopasdfghjklzxcvbnm/authTokens:generate");
  479. XCTAssertEqualObjects([request valueForHTTPHeaderField:@"Content-Type"], @"application/json",
  480. @"%@", self.name);
  481. XCTAssertEqualObjects([request valueForHTTPHeaderField:@"X-Goog-Api-Key"], self.APIKey, @"%@",
  482. self.name);
  483. NSString *expectedAuthHeader =
  484. [NSString stringWithFormat:@"FIS_v2 %@", installation.refreshToken];
  485. XCTAssertEqualObjects(request.allHTTPHeaderFields[@"Authorization"], expectedAuthHeader, @"%@",
  486. self.name);
  487. NSError *error;
  488. NSDictionary *body = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
  489. options:0
  490. error:&error];
  491. XCTAssertNotNil(body, @"Error: %@, test: %@", error, self.name);
  492. XCTAssertEqualObjects(
  493. body,
  494. @{@"installation" : @{@"sdkVersion" : [self SDKVersion]}}, @"%@", self.name);
  495. return YES;
  496. }];
  497. }
  498. - (id)deleteInstallationRequestValidationWithInstallation:(FIRInstallationsItem *)installation {
  499. return [OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  500. XCTAssert([request isKindOfClass:[NSURLRequest class]], @"Unexpected class: %@",
  501. [request class]);
  502. XCTAssertEqualObjects(request.HTTPMethod, @"DELETE");
  503. NSString *expectedURL = [NSString
  504. stringWithFormat:
  505. @"https://firebaseinstallations.googleapis.com/v1/projects/%@/installations/%@/",
  506. self.projectID, installation.firebaseInstallationID];
  507. XCTAssertEqualObjects(request.URL.absoluteString, expectedURL);
  508. XCTAssertEqualObjects(request.allHTTPHeaderFields[@"Content-Type"], @"application/json");
  509. XCTAssertEqualObjects(request.allHTTPHeaderFields[@"X-Goog-Api-Key"], self.APIKey);
  510. NSString *expectedAuthHeader =
  511. [NSString stringWithFormat:@"FIS_v2 %@", installation.refreshToken];
  512. XCTAssertEqualObjects(request.allHTTPHeaderFields[@"Authorization"], expectedAuthHeader, @"%@",
  513. self.name);
  514. NSError *error;
  515. NSDictionary *JSONBody = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
  516. options:0
  517. error:&error];
  518. XCTAssertNotNil(JSONBody, @"Error: %@", error);
  519. XCTAssertEqualObjects(JSONBody, @{});
  520. return YES;
  521. }];
  522. }
  523. - (NSString *)SDKVersion {
  524. return [NSString stringWithFormat:@"i:%s", FIRInstallationsVersionStr];
  525. }
  526. @end