FIRAppCheckDebugProviderAPIServiceTests.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2020 Google LLC
  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 <FirebaseAppCheck/FirebaseAppCheck.h>
  20. #import <GoogleUtilities/GULURLSessionDataResponse.h>
  21. #import "FirebaseAppCheck/Sources/Core/APIService/FIRAppCheckAPIService.h"
  22. #import "FirebaseAppCheck/Sources/Core/Errors/FIRAppCheckErrorUtil.h"
  23. #import "FirebaseAppCheck/Sources/DebugProvider/API/FIRAppCheckDebugProviderAPIService.h"
  24. #import "SharedTestUtilities/URLSession/FIRURLSessionOCMockStub.h"
  25. @interface FIRAppCheckDebugProviderAPIServiceTests : XCTestCase
  26. @property(nonatomic) FIRAppCheckDebugProviderAPIService *debugAPIService;
  27. @property(nonatomic) id mockAPIService;
  28. @property(nonatomic) NSString *projectID;
  29. @property(nonatomic) NSString *appID;
  30. @end
  31. @implementation FIRAppCheckDebugProviderAPIServiceTests
  32. - (void)setUp {
  33. [super setUp];
  34. self.projectID = @"project_id";
  35. self.appID = @"app_id";
  36. self.mockAPIService = OCMProtocolMock(@protocol(FIRAppCheckAPIServiceProtocol));
  37. OCMStub([self.mockAPIService baseURL]).andReturn(@"https://test.appcheck.url.com/alpha");
  38. self.debugAPIService =
  39. [[FIRAppCheckDebugProviderAPIService alloc] initWithAPIService:self.mockAPIService
  40. projectID:self.projectID
  41. appID:self.appID];
  42. }
  43. - (void)tearDown {
  44. self.debugAPIService = nil;
  45. [self.mockAPIService stopMocking];
  46. self.mockAPIService = nil;
  47. [super tearDown];
  48. }
  49. - (void)testAppCheckTokenSuccess {
  50. NSString *debugToken = [NSUUID UUID].UUIDString;
  51. FIRAppCheckToken *expectedResult = [[FIRAppCheckToken alloc] initWithToken:@"app_check_token"
  52. expirationDate:[NSDate date]];
  53. // 1. Stub API service.
  54. // 1.1. Stub API response.
  55. NSString *expectedRequestURL =
  56. [NSString stringWithFormat:@"%@%@", [self.mockAPIService baseURL],
  57. @"/projects/project_id/apps/app_id:exchangeDebugToken"];
  58. id URLValidationArg = [OCMArg checkWithBlock:^BOOL(NSURL *URL) {
  59. XCTAssertEqualObjects(URL.absoluteString, expectedRequestURL);
  60. return YES;
  61. }];
  62. id HTTPBodyValidationArg = [self HTTPBodyValidationArgWithDebugToken:debugToken];
  63. NSData *fakeResponseData = [@"fake response" dataUsingEncoding:NSUTF8StringEncoding];
  64. NSHTTPURLResponse *HTTPResponse = [FIRURLSessionOCMockStub HTTPResponseWithCode:200];
  65. GULURLSessionDataResponse *APIResponse =
  66. [[GULURLSessionDataResponse alloc] initWithResponse:HTTPResponse HTTPBody:fakeResponseData];
  67. OCMExpect([self.mockAPIService sendRequestWithURL:URLValidationArg
  68. HTTPMethod:@"POST"
  69. body:HTTPBodyValidationArg
  70. additionalHeaders:@{@"Content-Type" : @"application/json"}])
  71. .andReturn([FBLPromise resolvedWith:APIResponse]);
  72. // 1.2. Stub response parsing.
  73. OCMExpect([self.mockAPIService appCheckTokenWithAPIResponse:APIResponse])
  74. .andReturn([FBLPromise resolvedWith:expectedResult]);
  75. // 2. Send request.
  76. __auto_type tokenPromise = [self.debugAPIService appCheckTokenWithDebugToken:debugToken];
  77. // 3. Verify.
  78. XCTAssert(FBLWaitForPromisesWithTimeout(1));
  79. XCTAssertTrue(tokenPromise.isFulfilled);
  80. XCTAssertNil(tokenPromise.error);
  81. XCTAssertEqualObjects(tokenPromise.value.token, expectedResult.token);
  82. XCTAssertEqualObjects(tokenPromise.value.expirationDate, expectedResult.expirationDate);
  83. OCMVerifyAll(self.mockAPIService);
  84. }
  85. - (void)testAppCheckTokenResponseParsingError {
  86. NSString *debugToken = [NSUUID UUID].UUIDString;
  87. NSError *parsingError = [NSError errorWithDomain:@"testAppCheckTokenResponseParsingError"
  88. code:-1
  89. userInfo:nil];
  90. // 1. Stub API service.
  91. // 1.1. Stub API response.
  92. NSString *expectedRequestURL =
  93. [NSString stringWithFormat:@"%@%@", [self.mockAPIService baseURL],
  94. @"/projects/project_id/apps/app_id:exchangeDebugToken"];
  95. id URLValidationArg = [OCMArg checkWithBlock:^BOOL(NSURL *URL) {
  96. XCTAssertEqualObjects(URL.absoluteString, expectedRequestURL);
  97. return YES;
  98. }];
  99. id HTTPBodyValidationArg = [self HTTPBodyValidationArgWithDebugToken:debugToken];
  100. NSData *fakeResponseData = [@"fake response" dataUsingEncoding:NSUTF8StringEncoding];
  101. NSHTTPURLResponse *HTTPResponse = [FIRURLSessionOCMockStub HTTPResponseWithCode:200];
  102. GULURLSessionDataResponse *APIResponse =
  103. [[GULURLSessionDataResponse alloc] initWithResponse:HTTPResponse HTTPBody:fakeResponseData];
  104. OCMExpect([self.mockAPIService sendRequestWithURL:URLValidationArg
  105. HTTPMethod:@"POST"
  106. body:HTTPBodyValidationArg
  107. additionalHeaders:@{@"Content-Type" : @"application/json"}])
  108. .andReturn([FBLPromise resolvedWith:APIResponse]);
  109. // 1.2. Stub response parsing.
  110. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  111. [rejectedPromise reject:parsingError];
  112. OCMExpect([self.mockAPIService appCheckTokenWithAPIResponse:APIResponse])
  113. .andReturn(rejectedPromise);
  114. // 2. Send request.
  115. __auto_type tokenPromise = [self.debugAPIService appCheckTokenWithDebugToken:debugToken];
  116. // 3. Verify.
  117. XCTAssert(FBLWaitForPromisesWithTimeout(1));
  118. XCTAssertTrue(tokenPromise.isRejected);
  119. XCTAssertEqualObjects(tokenPromise.error, parsingError);
  120. XCTAssertNil(tokenPromise.value);
  121. OCMVerifyAll(self.mockAPIService);
  122. }
  123. - (void)testAppCheckTokenNetworkError {
  124. NSString *debugToken = [NSUUID UUID].UUIDString;
  125. NSError *APIError = [NSError errorWithDomain:@"testAppCheckTokenNetworkError"
  126. code:-1
  127. userInfo:nil];
  128. // 1. Stub API service.
  129. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  130. [rejectedPromise reject:APIError];
  131. id HTTPBodyValidationArg = [self HTTPBodyValidationArgWithDebugToken:debugToken];
  132. OCMExpect([self.mockAPIService sendRequestWithURL:[OCMArg any]
  133. HTTPMethod:@"POST"
  134. body:HTTPBodyValidationArg
  135. additionalHeaders:@{@"Content-Type" : @"application/json"}])
  136. .andReturn(rejectedPromise);
  137. // 2. Send request.
  138. __auto_type tokenPromise = [self.debugAPIService appCheckTokenWithDebugToken:debugToken];
  139. // 3. Verify.
  140. XCTAssert(FBLWaitForPromisesWithTimeout(1));
  141. XCTAssertTrue(tokenPromise.isRejected);
  142. XCTAssertNil(tokenPromise.value);
  143. XCTAssertEqualObjects(tokenPromise.error, APIError);
  144. OCMVerifyAll(self.mockAPIService);
  145. }
  146. #pragma mark - Helpores
  147. - (id)HTTPBodyValidationArgWithDebugToken:(NSString *)debugToken {
  148. return [OCMArg checkWithBlock:^BOOL(NSData *body) {
  149. NSDictionary<NSString *, id> *decodedData = [NSJSONSerialization JSONObjectWithData:body
  150. options:0
  151. error:nil];
  152. XCTAssert([decodedData isKindOfClass:[NSDictionary class]]);
  153. NSString *decodeDebugToken = decodedData[@"debug_token"];
  154. XCTAssertNotNil(decodeDebugToken);
  155. XCTAssertEqualObjects(decodeDebugToken, debugToken);
  156. return YES;
  157. }];
  158. }
  159. @end