GACAppCheckDebugProviderAPIServiceTests.m 7.8 KB

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