FIRInstallationsAPIServiceTests.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "FIRInstallationsStoredAuthToken.h"
  23. #import "FIRInstallationsVersion.h"
  24. typedef FBLPromise * (^FIRInstallationsAPIServiceTask)(void);
  25. @interface FIRInstallationsAPIService (Tests)
  26. - (instancetype)initWithURLSession:(NSURLSession *)URLSession
  27. APIKey:(NSString *)APIKey
  28. projectID:(NSString *)projectID;
  29. @end
  30. @interface FIRInstallationsAPIServiceTests : XCTestCase
  31. @property(nonatomic) FIRInstallationsAPIService *service;
  32. @property(nonatomic) id mockURLSession;
  33. @property(nonatomic) NSString *APIKey;
  34. @property(nonatomic) NSString *projectID;
  35. @end
  36. @implementation FIRInstallationsAPIServiceTests
  37. - (void)setUp {
  38. self.APIKey = @"api-key";
  39. self.projectID = @"project-id";
  40. self.mockURLSession = OCMClassMock([NSURLSession class]);
  41. self.service = [[FIRInstallationsAPIService alloc] initWithURLSession:self.mockURLSession
  42. APIKey:self.APIKey
  43. projectID:self.projectID];
  44. }
  45. - (void)tearDown {
  46. self.service = nil;
  47. self.mockURLSession = nil;
  48. self.projectID = nil;
  49. self.APIKey = nil;
  50. }
  51. - (void)testRegisterInstallationSuccess {
  52. FIRInstallationsItem *installation = [[FIRInstallationsItem alloc] initWithAppID:@"app-id"
  53. firebaseAppName:@"name"];
  54. installation.firebaseInstallationID = [FIRInstallationsItem generateFID];
  55. // 1. Stub URL session:
  56. // 1.1. URL request validation.
  57. id URLRequestValidation = [OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  58. XCTAssertEqualObjects(request.HTTPMethod, @"POST");
  59. XCTAssertEqualObjects(
  60. request.URL.absoluteString,
  61. @"https://firebaseinstallations.googleapis.com/v1/projects/project-id/installations/");
  62. XCTAssertEqualObjects([request valueForHTTPHeaderField:@"Content-Type"], @"application/json");
  63. XCTAssertEqualObjects([request valueForHTTPHeaderField:@"X-Goog-Api-Key"], self.APIKey);
  64. NSError *error;
  65. NSDictionary *body = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
  66. options:0
  67. error:&error];
  68. XCTAssertNotNil(body, @"Error: %@", error);
  69. XCTAssertEqualObjects(body[@"fid"], installation.firebaseInstallationID);
  70. XCTAssertEqualObjects(body[@"authVersion"], @"FIS_v2");
  71. XCTAssertEqualObjects(body[@"appId"], installation.appID);
  72. XCTAssertEqualObjects(body[@"sdkVersion"], [self SDKVersion]);
  73. return YES;
  74. }];
  75. // 1.2. Capture completion to call it later.
  76. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  77. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  78. taskCompletion = obj;
  79. return YES;
  80. }];
  81. // 1.3. Create a data task mock.
  82. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  83. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  84. // 1.4. Expect `dataTaskWithRequest` to be called.
  85. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  86. completionHandler:completionArg])
  87. .andReturn(mockDataTask);
  88. // 2. Call
  89. FBLPromise<FIRInstallationsItem *> *promise = [self.service registerInstallation:installation];
  90. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  91. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  92. // 4. Wait for the data task `resume` to be called.
  93. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  94. // 5. Call the data task completion.
  95. NSData *successResponseData =
  96. [self loadFixtureNamed:@"APIRegisterInstallationResponseSuccess.json"];
  97. taskCompletion(successResponseData, [self responseWithStatusCode:201], nil);
  98. // 6. Check result.
  99. FBLWaitForPromisesWithTimeout(0.5);
  100. XCTAssertNil(promise.error);
  101. XCTAssertNotNil(promise.value);
  102. XCTAssertNotEqual(promise.value, installation);
  103. XCTAssertEqualObjects(promise.value.appID, installation.appID);
  104. XCTAssertEqualObjects(promise.value.firebaseAppName, installation.firebaseAppName);
  105. // Server may respond with a different FID if the sent FID cannot be accepted.
  106. XCTAssertEqualObjects(promise.value.firebaseInstallationID, @"aaaaaaaaaaaaaaaaaaaaaa");
  107. XCTAssertEqualObjects(promise.value.refreshToken, @"aaaaaaabbbbbbbbcccccccccdddddddd00000000");
  108. XCTAssertEqualObjects(promise.value.authToken.token,
  109. @"aaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbb.cccccccccccccccccccccccc");
  110. [self assertDate:promise.value.authToken.expirationDate
  111. isApproximatelyEqualCurrentPlusTimeInterval:604800];
  112. }
  113. // TODO: More tests for Register Installation API
  114. - (void)testRefreshAuthTokenSuccess {
  115. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  116. installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
  117. // 1. Stub URL session:
  118. // 1.1. URL request validation.
  119. id URLRequestValidation = [self refreshTokenRequestValidationArgWithInstallation:installation];
  120. // 1.2. Capture completion to call it later.
  121. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  122. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  123. taskCompletion = obj;
  124. return YES;
  125. }];
  126. // 1.3. Create a data task mock.
  127. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  128. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  129. // 1.4. Expect `dataTaskWithRequest` to be called.
  130. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  131. completionHandler:completionArg])
  132. .andReturn(mockDataTask);
  133. // 1.5. Prepare server response data.
  134. NSData *successResponseData = [self loadFixtureNamed:@"APIGenerateTokenResponseSuccess.json"];
  135. // 2. Call
  136. FBLPromise<FIRInstallationsItem *> *promise =
  137. [self.service refreshAuthTokenForInstallation:installation];
  138. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  139. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  140. // 4. Wait for the data task `resume` to be called.
  141. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  142. // 5. Call the data task completion.
  143. taskCompletion(successResponseData, [self responseWithStatusCode:200], nil);
  144. // 6. Check result.
  145. FBLWaitForPromisesWithTimeout(0.5);
  146. XCTAssertNil(promise.error);
  147. XCTAssertNotNil(promise.value);
  148. XCTAssertNotEqual(promise.value, installation);
  149. XCTAssertEqualObjects(promise.value.appID, installation.appID);
  150. XCTAssertEqualObjects(promise.value.firebaseAppName, installation.firebaseAppName);
  151. XCTAssertEqualObjects(promise.value.firebaseInstallationID, installation.firebaseInstallationID);
  152. XCTAssertEqualObjects(promise.value.refreshToken, installation.refreshToken);
  153. XCTAssertEqualObjects(promise.value.authToken.token,
  154. @"aaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbb.cccccccccccccccccccccccc");
  155. [self assertDate:promise.value.authToken.expirationDate
  156. isApproximatelyEqualCurrentPlusTimeInterval:3987465];
  157. }
  158. - (void)testRefreshAuthTokenAPIError {
  159. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  160. installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
  161. // 1. Stub URL session:
  162. // 1.1. URL request validation.
  163. id URLRequestValidation = [self refreshTokenRequestValidationArgWithInstallation:installation];
  164. // 1.2. Capture completion to call it later.
  165. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  166. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  167. taskCompletion = obj;
  168. return YES;
  169. }];
  170. // 1.3. Create a data task mock.
  171. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  172. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  173. // 1.4. Expect `dataTaskWithRequest` to be called.
  174. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  175. completionHandler:completionArg])
  176. .andReturn(mockDataTask);
  177. // 1.5. Prepare server response data.
  178. NSData *errorResponseData =
  179. [self loadFixtureNamed:@"APIGenerateTokenResponseInvalidRefreshToken.json"];
  180. // 2. Call
  181. FBLPromise<FIRInstallationsItem *> *promise =
  182. [self.service refreshAuthTokenForInstallation:installation];
  183. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  184. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  185. // 4. Wait for the data task `resume` to be called.
  186. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  187. // 5. Call the data task completion.
  188. taskCompletion(errorResponseData, [self responseWithStatusCode:401], nil);
  189. // 6. Check result.
  190. FBLWaitForPromisesWithTimeout(0.5);
  191. XCTAssertTrue([FIRInstallationsErrorUtil isAPIError:promise.error withHTTPCode:401]);
  192. XCTAssertNil(promise.value);
  193. }
  194. - (void)testRefreshAuthTokenDataNil {
  195. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  196. installation.firebaseInstallationID = @"qwertyuiopasdfghjklzxcvbnm";
  197. // 1. Stub URL session:
  198. // 1.1. URL request validation.
  199. id URLRequestValidation = [self refreshTokenRequestValidationArgWithInstallation:installation];
  200. // 1.2. Capture completion to call it later.
  201. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  202. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  203. taskCompletion = obj;
  204. return YES;
  205. }];
  206. // 1.3. Create a data task mock.
  207. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  208. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  209. // 1.4. Expect `dataTaskWithRequest` to be called.
  210. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  211. completionHandler:completionArg])
  212. .andReturn(mockDataTask);
  213. // 2. Call
  214. FBLPromise<FIRInstallationsItem *> *promise =
  215. [self.service refreshAuthTokenForInstallation:installation];
  216. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  217. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  218. // 4. Wait for the data task `resume` to be called.
  219. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  220. // 5. Call the data task completion.
  221. // HTTP 200 but no data (a potential server failure).
  222. taskCompletion(nil, [self responseWithStatusCode:200], nil);
  223. // 6. Check result.
  224. FBLWaitForPromisesWithTimeout(0.5);
  225. XCTAssertEqualObjects(promise.error.userInfo[NSLocalizedFailureReasonErrorKey],
  226. @"Failed to serialize JSON data.");
  227. XCTAssertNil(promise.value);
  228. }
  229. - (void)testDeleteInstallationSuccess {
  230. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  231. // 1. Stub URL session:
  232. // 1.1. URL request validation.
  233. id URLRequestValidation = [self deleteInstallationRequestValidationWithInstallation:installation];
  234. // 1.2. Capture completion to call it later.
  235. __block void (^taskCompletion)(NSData *, NSURLResponse *, NSError *);
  236. id completionArg = [OCMArg checkWithBlock:^BOOL(id obj) {
  237. taskCompletion = obj;
  238. return YES;
  239. }];
  240. // 1.3. Create a data task mock.
  241. id mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  242. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  243. // 1.4. Expect `dataTaskWithRequest` to be called.
  244. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  245. completionHandler:completionArg])
  246. .andReturn(mockDataTask);
  247. // 2. Call
  248. FBLPromise<FIRInstallationsItem *> *promise = [self.service deleteInstallation:installation];
  249. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  250. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  251. // 4. Wait for the data task `resume` to be called.
  252. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  253. // 5. Call the data task completion.
  254. // HTTP 200 but no data (a potential server failure).
  255. NSData *successResponseData = [@"{}" dataUsingEncoding:NSUTF8StringEncoding];
  256. taskCompletion(successResponseData, [self responseWithStatusCode:200], nil);
  257. // 6. Check result.
  258. FBLWaitForPromisesWithTimeout(0.5);
  259. XCTAssertNil(promise.error);
  260. XCTAssertEqual(promise.value, installation);
  261. }
  262. - (void)testDeleteInstallationErrorNotFound {
  263. FIRInstallationsItem *installation = [FIRInstallationsItem createRegisteredInstallationItem];
  264. // 1. Stub URL session:
  265. // 1.1. URL request validation.
  266. id URLRequestValidation = [self deleteInstallationRequestValidationWithInstallation: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 mockDataTask = OCMClassMock([NSURLSessionDataTask class]);
  275. OCMExpect([(NSURLSessionDataTask *)mockDataTask resume]);
  276. // 1.4. Expect `dataTaskWithRequest` to be called.
  277. OCMExpect([self.mockURLSession dataTaskWithRequest:URLRequestValidation
  278. completionHandler:completionArg])
  279. .andReturn(mockDataTask);
  280. // 2. Call
  281. FBLPromise<FIRInstallationsItem *> *promise = [self.service deleteInstallation:installation];
  282. // 3. Wait for `[NSURLSession dataTaskWithRequest...]` to be called
  283. OCMVerifyAllWithDelay(self.mockURLSession, 0.5);
  284. // 4. Wait for the data task `resume` to be called.
  285. OCMVerifyAllWithDelay(mockDataTask, 0.5);
  286. // 5. Call the data task completion.
  287. // HTTP 200 but no data (a potential server failure).
  288. taskCompletion(nil, [self responseWithStatusCode:404], nil);
  289. // 6. Check result.
  290. FBLWaitForPromisesWithTimeout(0.5);
  291. XCTAssertTrue([FIRInstallationsErrorUtil isAPIError:promise.error withHTTPCode:404]);
  292. XCTAssertNil(promise.value);
  293. }
  294. #pragma mark - Helpers
  295. - (NSData *)loadFixtureNamed:(NSString *)fileName {
  296. NSURL *fileURL = [[NSBundle bundleForClass:[self class]] URLForResource:fileName
  297. withExtension:nil];
  298. XCTAssertNotNil(fileURL);
  299. NSError *error;
  300. NSData *data = [NSData dataWithContentsOfURL:fileURL options:0 error:&error];
  301. XCTAssertNotNil(data, @"File name: %@ Error: %@", fileName, error);
  302. return data;
  303. }
  304. - (NSURLResponse *)responseWithStatusCode:(NSUInteger)statusCode {
  305. NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[NSURL fileURLWithPath:@"/"]
  306. statusCode:statusCode
  307. HTTPVersion:nil
  308. headerFields:nil];
  309. return response;
  310. }
  311. - (void)assertDate:(NSDate *)date
  312. isApproximatelyEqualCurrentPlusTimeInterval:(NSTimeInterval)timeInterval {
  313. NSDate *expectedDate = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
  314. NSTimeInterval precision = 10;
  315. XCTAssert(ABS([date timeIntervalSinceDate:expectedDate]) <= precision,
  316. @"date: %@ is not equal to expected %@ with precision %f - %@", date, expectedDate,
  317. precision, self.name);
  318. }
  319. - (id)refreshTokenRequestValidationArgWithInstallation:(FIRInstallationsItem *)installation {
  320. return [OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  321. XCTAssertEqualObjects(request.HTTPMethod, @"POST");
  322. XCTAssertEqualObjects(request.URL.absoluteString,
  323. @"https://firebaseinstallations.googleapis.com/v1/projects/project-id/"
  324. @"installations/qwertyuiopasdfghjklzxcvbnm/authTokens:generate");
  325. XCTAssertEqualObjects([request valueForHTTPHeaderField:@"Content-Type"], @"application/json",
  326. @"%@", self.name);
  327. XCTAssertEqualObjects([request valueForHTTPHeaderField:@"X-Goog-Api-Key"], self.APIKey, @"%@",
  328. self.name);
  329. NSString *expectedAuthHeader =
  330. [NSString stringWithFormat:@"FIS_v2 %@", installation.refreshToken];
  331. XCTAssertEqualObjects(request.allHTTPHeaderFields[@"Authorization"], expectedAuthHeader, @"%@",
  332. self.name);
  333. NSError *error;
  334. NSDictionary *body = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
  335. options:0
  336. error:&error];
  337. XCTAssertNotNil(body, @"Error: %@, test: %@", error, self.name);
  338. XCTAssertEqualObjects(body,
  339. @{@"installation" : @{@"sdkVersion" : [self SDKVersion]}}, @"%@",
  340. self.name);
  341. return YES;
  342. }];
  343. }
  344. - (id)deleteInstallationRequestValidationWithInstallation:(FIRInstallationsItem *)installation {
  345. return [OCMArg checkWithBlock:^BOOL(NSURLRequest *request) {
  346. XCTAssert([request isKindOfClass:[NSURLRequest class]], @"Unexpected class: %@",
  347. [request class]);
  348. XCTAssertEqualObjects(request.HTTPMethod, @"DELETE");
  349. NSString *expectedURL = [NSString
  350. stringWithFormat:
  351. @"https://firebaseinstallations.googleapis.com/v1/projects/%@/installations/%@/",
  352. self.projectID, installation.firebaseInstallationID];
  353. XCTAssertEqualObjects(request.URL.absoluteString, expectedURL);
  354. XCTAssertEqualObjects(request.allHTTPHeaderFields[@"Content-Type"], @"application/json");
  355. XCTAssertEqualObjects(request.allHTTPHeaderFields[@"X-Goog-Api-Key"], self.APIKey);
  356. NSString *expectedAuthHeader =
  357. [NSString stringWithFormat:@"FIS_v2 %@", installation.refreshToken];
  358. XCTAssertEqualObjects(request.allHTTPHeaderFields[@"Authorization"], expectedAuthHeader, @"%@",
  359. self.name);
  360. NSError *error;
  361. NSDictionary *JSONBody = [NSJSONSerialization JSONObjectWithData:request.HTTPBody
  362. options:0
  363. error:&error];
  364. XCTAssertNotNil(JSONBody, @"Error: %@", error);
  365. XCTAssertEqualObjects(JSONBody, @{});
  366. return YES;
  367. }];
  368. }
  369. #pragma mark - Helpers
  370. - (NSString *)SDKVersion {
  371. return [NSString stringWithFormat:@"i:%s", FIRInstallationsVersionStr];
  372. }
  373. @end