FIRInstallationsAPIServiceTests.m 26 KB

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