FIRAppCheckDebugProviderTests.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/Sources/Core/FIRAppCheckToken+Internal.h"
  20. #import "FirebaseAppCheck/Sources/DebugProvider/API/FIRAppCheckDebugProviderAPIService.h"
  21. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckDebugProvider.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. receivedAtDate:[NSDate date]];
  101. OCMExpect([self.fakeAPIService appCheckTokenWithDebugToken:expectedDebugToken])
  102. .andReturn([FBLPromise resolvedWith:validToken]);
  103. // 2. Validate get token.
  104. [self validateGetToken:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  105. XCTAssertNil(error);
  106. XCTAssertEqualObjects(token.token, validToken.token);
  107. XCTAssertEqualObjects(token.expirationDate, validToken.expirationDate);
  108. XCTAssertEqualObjects(token.receivedAtDate, validToken.receivedAtDate);
  109. }];
  110. // 3. Verify fakes.
  111. OCMVerifyAll(self.fakeAPIService);
  112. }
  113. - (void)testGetTokenAPIError {
  114. // 1. Stub API service.
  115. NSString *expectedDebugToken = [self.provider currentDebugToken];
  116. NSError *APIError = [NSError errorWithDomain:@"testGetTokenAPIError" code:-1 userInfo:nil];
  117. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  118. [rejectedPromise reject:APIError];
  119. OCMExpect([self.fakeAPIService appCheckTokenWithDebugToken:expectedDebugToken])
  120. .andReturn(rejectedPromise);
  121. // 2. Validate get token.
  122. [self validateGetToken:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  123. XCTAssertEqualObjects(error, APIError);
  124. XCTAssertNil(token);
  125. }];
  126. // 3. Verify fakes.
  127. OCMVerifyAll(self.fakeAPIService);
  128. }
  129. #pragma mark - Helpers
  130. - (void)validateGetToken:(FIRAppCheckTokenValidationBlock)validationBlock {
  131. XCTestExpectation *expectation = [self expectationWithDescription:@"getToken"];
  132. [self.provider
  133. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  134. validationBlock(token, error);
  135. [expectation fulfill];
  136. }];
  137. [self waitForExpectations:@[ expectation ] timeout:0.5];
  138. }
  139. @end