FIRInstallationsAPIServiceTests.m 27 KB

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