FIRAppCheckDebugProviderTests.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "FirebaseAppCheck/Sources/Core/FIRApp+AppCheck.h"
  19. #import "FirebaseAppCheck/Sources/Core/FIRAppCheckToken+Internal.h"
  20. #import "FirebaseAppCheck/Sources/Core/FIRHeartbeatLogger+AppCheck.h"
  21. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckDebugProvider.h"
  22. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  23. static NSString *const kAppName = @"test_app_name";
  24. static NSString *const kAppID = @"test_app_id";
  25. static NSString *const kAPIKey = @"test_api_key";
  26. static NSString *const kProjectID = @"test_project_id";
  27. static NSString *const kProjectNumber = @"123456789";
  28. @interface FIRAppCheckDebugProvider (Tests)
  29. - (instancetype)initWithDebugProvider:(GACAppCheckDebugProvider *)debugProvider;
  30. @end
  31. @interface FIRAppCheckDebugProviderTests : XCTestCase
  32. @property(nonatomic, copy) NSString *resourceName;
  33. @property(nonatomic) id debugProviderMock;
  34. @property(nonatomic) FIRAppCheckDebugProvider *provider;
  35. @end
  36. @implementation FIRAppCheckDebugProviderTests
  37. - (void)setUp {
  38. self.resourceName = [NSString stringWithFormat:@"projects/%@/apps/%@", kProjectID, kAppID];
  39. self.debugProviderMock = OCMStrictClassMock([GACAppCheckDebugProvider class]);
  40. self.provider = [[FIRAppCheckDebugProvider alloc] initWithDebugProvider:self.debugProviderMock];
  41. }
  42. - (void)tearDown {
  43. self.provider = nil;
  44. [self.debugProviderMock stopMocking];
  45. self.debugProviderMock = nil;
  46. }
  47. #pragma mark - Initialization
  48. - (void)testInitWithValidApp {
  49. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kAppID GCMSenderID:kProjectNumber];
  50. options.APIKey = kAPIKey;
  51. options.projectID = kProjectID;
  52. FIRApp *app = [[FIRApp alloc] initInstanceWithName:kAppName options:options];
  53. // The following disables automatic token refresh, which could interfere with tests.
  54. app.dataCollectionDefaultEnabled = NO;
  55. XCTAssertNotNil([[FIRAppCheckDebugProvider alloc] initWithApp:app]);
  56. }
  57. - (void)testInitWithIncompleteApp {
  58. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kAppID GCMSenderID:kProjectNumber];
  59. options.projectID = kProjectID;
  60. FIRApp *missingAPIKeyApp = [[FIRApp alloc] initInstanceWithName:kAppName options:options];
  61. // The following disables automatic token refresh, which could interfere with tests.
  62. missingAPIKeyApp.dataCollectionDefaultEnabled = NO;
  63. XCTAssertNil([[FIRAppCheckDebugProvider alloc] initWithApp:missingAPIKeyApp]);
  64. options.projectID = nil;
  65. options.APIKey = kAPIKey;
  66. FIRApp *missingProjectIDApp = [[FIRApp alloc] initInstanceWithName:kAppName options:options];
  67. // The following disables automatic token refresh, which could interfere with tests.
  68. missingProjectIDApp.dataCollectionDefaultEnabled = NO;
  69. XCTAssertNil([[FIRAppCheckDebugProvider alloc] initWithApp:missingProjectIDApp]);
  70. }
  71. #pragma mark - Current Debug token
  72. - (void)testCurrentTokenShim {
  73. NSString *currentToken = @"debug_token";
  74. OCMExpect([self.debugProviderMock currentDebugToken]).andReturn(currentToken);
  75. XCTAssertEqualObjects([self.provider currentDebugToken], currentToken);
  76. OCMVerifyAll(self.debugProviderMock);
  77. }
  78. #pragma mark - Debug token to FAC token exchange
  79. - (void)testGetTokenSuccess {
  80. // 1. Stub internal debug provider.
  81. GACAppCheckToken *validInternalToken = [[GACAppCheckToken alloc] initWithToken:@"valid_token"
  82. expirationDate:[NSDate date]
  83. receivedAtDate:[NSDate date]];
  84. OCMExpect([self.debugProviderMock
  85. getTokenWithCompletion:([OCMArg
  86. invokeBlockWithArgs:validInternalToken, [NSNull null], nil])]);
  87. // 2. Validate get token.
  88. [self.provider
  89. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  90. XCTAssertEqualObjects(token.token, validInternalToken.token);
  91. XCTAssertEqualObjects(token.expirationDate, validInternalToken.expirationDate);
  92. XCTAssertEqualObjects(token.receivedAtDate, validInternalToken.receivedAtDate);
  93. XCTAssertNil(error);
  94. }];
  95. // 3. Verify mock debug provider.
  96. OCMVerifyAll(self.debugProviderMock);
  97. }
  98. - (void)testGetTokenAPIError {
  99. // 1. Stub internal debug provider.
  100. NSError *expectedError = [NSError errorWithDomain:@"testGetTokenAPIError" code:-1 userInfo:nil];
  101. OCMExpect([self.debugProviderMock
  102. getTokenWithCompletion:([OCMArg invokeBlockWithArgs:[NSNull null], expectedError, nil])]);
  103. // 2. Validate get token.
  104. [self.provider
  105. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  106. XCTAssertNil(token);
  107. XCTAssertEqualObjects(error, expectedError);
  108. }];
  109. // 3. Verify mock debug provider.
  110. OCMVerifyAll(self.debugProviderMock);
  111. }
  112. @end