FIRDeviceCheckProviderTests.m 5.2 KB

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