FIRInstallationsAPIServiceTests.m 27 KB

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