FIRAppCheckDebugProviderTests.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "FBLPromise+Testing.h"
  18. #import "OCMock.h"
  19. #import "FirebaseAppCheck/Sources/DebugProvider/API/FIRAppCheckDebugProviderAPIService.h"
  20. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckDebugProvider.h"
  21. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckToken.h"
  22. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  23. static NSString *const kDebugTokenEnvKey = @"FIRAAppCheckDebugToken";
  24. static NSString *const kDebugTokenUserDefaultsKey = @"FIRAAppCheckDebugToken";
  25. @interface FIRAppCheckDebugProvider (Tests)
  26. - (instancetype)initWithAPIService:(id<FIRAppCheckDebugProviderAPIServiceProtocol>)APIService;
  27. @end
  28. @interface FIRAppCheckDebugProviderTests : XCTestCase
  29. @property(nonatomic) FIRAppCheckDebugProvider *provider;
  30. @property(nonatomic) id processInfoMock;
  31. @property(nonatomic) id fakeAPIService;
  32. @end
  33. typedef void (^FIRAppCheckTokenValidationBlock)(FIRAppCheckToken *_Nullable token,
  34. NSError *_Nullable error);
  35. @implementation FIRAppCheckDebugProviderTests
  36. - (void)setUp {
  37. self.processInfoMock = OCMPartialMock([NSProcessInfo processInfo]);
  38. self.fakeAPIService = OCMProtocolMock(@protocol(FIRAppCheckDebugProviderAPIServiceProtocol));
  39. self.provider = [[FIRAppCheckDebugProvider alloc] initWithAPIService:self.fakeAPIService];
  40. }
  41. - (void)tearDown {
  42. self.provider = nil;
  43. [self.processInfoMock stopMocking];
  44. self.processInfoMock = nil;
  45. [[NSUserDefaults standardUserDefaults] removeObjectForKey:kDebugTokenUserDefaultsKey];
  46. }
  47. #pragma mark - Initialization
  48. - (void)testInitWithValidApp {
  49. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"app_id" GCMSenderID:@"sender_id"];
  50. options.APIKey = @"api_key";
  51. options.projectID = @"project_id";
  52. FIRApp *app = [[FIRApp alloc] initInstanceWithName:@"testInitWithValidApp" options:options];
  53. XCTAssertNotNil([[FIRAppCheckDebugProvider alloc] initWithApp:app]);
  54. }
  55. - (void)testInitWithIncompleteApp {
  56. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"app_id" GCMSenderID:@"sender_id"];
  57. options.projectID = @"project_id";
  58. FIRApp *missingAPIKeyApp = [[FIRApp alloc] initInstanceWithName:@"testInitWithValidApp"
  59. options:options];
  60. XCTAssertNil([[FIRAppCheckDebugProvider alloc] initWithApp:missingAPIKeyApp]);
  61. options.projectID = nil;
  62. options.APIKey = @"api_key";
  63. FIRApp *missingProjectIDApp = [[FIRApp alloc] initInstanceWithName:@"testInitWithValidApp"
  64. options:options];
  65. XCTAssertNil([[FIRAppCheckDebugProvider alloc] initWithApp:missingProjectIDApp]);
  66. }
  67. #pragma mark - Debug token generating/storing
  68. - (void)testCurrentTokenWhenEnvironmentVariableSetAndTokenStored {
  69. [[NSUserDefaults standardUserDefaults] setObject:@"stored token"
  70. forKey:kDebugTokenUserDefaultsKey];
  71. NSString *envToken = @"env token";
  72. OCMStub([self.processInfoMock processInfo]).andReturn(self.processInfoMock);
  73. OCMExpect([self.processInfoMock environment]).andReturn(@{kDebugTokenEnvKey : envToken});
  74. XCTAssertEqualObjects([self.provider currentDebugToken], envToken);
  75. }
  76. - (void)testCurrentTokenWhenNoEnvironmentVariableAndTokenStored {
  77. NSString *storedToken = @"stored token";
  78. [[NSUserDefaults standardUserDefaults] setObject:storedToken forKey:kDebugTokenUserDefaultsKey];
  79. XCTAssertNil(NSProcessInfo.processInfo.environment[kDebugTokenEnvKey]);
  80. XCTAssertEqualObjects([self.provider currentDebugToken], storedToken);
  81. }
  82. - (void)testCurrentTokenWhenNoEnvironmentVariableAndNoTokenStored {
  83. XCTAssertNil(NSProcessInfo.processInfo.environment[kDebugTokenEnvKey]);
  84. XCTAssertNil([[NSUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey]);
  85. NSString *generatedToken = [self.provider currentDebugToken];
  86. XCTAssertNotNil(generatedToken);
  87. // Check if the generated token is stored to the user defaults.
  88. XCTAssertEqualObjects(
  89. [[NSUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey],
  90. generatedToken);
  91. // Check if the same token is used once generated.
  92. XCTAssertEqualObjects([self.provider currentDebugToken], generatedToken);
  93. }
  94. #pragma mark - Debug token to FAC token exchange
  95. - (void)testGetTokenSuccess {
  96. // 1. Stub API service.
  97. NSString *expectedDebugToken = [self.provider currentDebugToken];
  98. FIRAppCheckToken *validToken = [[FIRAppCheckToken alloc] initWithToken:@"valid_token"
  99. expirationDate:[NSDate date]];
  100. OCMExpect([self.fakeAPIService appCheckTokenWithDebugToken:expectedDebugToken])
  101. .andReturn([FBLPromise resolvedWith:validToken]);
  102. // 2. Validate get token.
  103. [self validateGetToken:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  104. XCTAssertNil(error);
  105. XCTAssertEqualObjects(token.token, validToken.token);
  106. XCTAssertEqualObjects(token.expirationDate, validToken.expirationDate);
  107. }];
  108. // 3. Verify fakes.
  109. OCMVerifyAll(self.fakeAPIService);
  110. }
  111. - (void)testGetTokenAPIError {
  112. // 1. Stub API service.
  113. NSString *expectedDebugToken = [self.provider currentDebugToken];
  114. NSError *APIError = [NSError errorWithDomain:@"testGetTokenAPIError" code:-1 userInfo:nil];
  115. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  116. [rejectedPromise reject:APIError];
  117. OCMExpect([self.fakeAPIService appCheckTokenWithDebugToken:expectedDebugToken])
  118. .andReturn(rejectedPromise);
  119. // 2. Validate get token.
  120. [self validateGetToken:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  121. XCTAssertEqualObjects(error, APIError);
  122. XCTAssertNil(token);
  123. }];
  124. // 3. Verify fakes.
  125. OCMVerifyAll(self.fakeAPIService);
  126. }
  127. #pragma mark - Helpers
  128. - (void)validateGetToken:(FIRAppCheckTokenValidationBlock)validationBlock {
  129. XCTestExpectation *expectation = [self expectationWithDescription:@"getToken"];
  130. [self.provider
  131. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  132. validationBlock(token, error);
  133. [expectation fulfill];
  134. }];
  135. [self waitForExpectations:@[ expectation ] timeout:0.5];
  136. }
  137. @end