FIRDeviceCheckProviderTests.m 5.3 KB

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