FIRInstallationsAPIServiceTests.m 28 KB

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