FIRInstallationsAPIServiceTests.m 26 KB

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