GACAppCheckDebugProviderAPIServiceTests.m 7.9 KB

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