FIRDeviceCheckProviderTests.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 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. // The following disables automatic token refresh, which could interfere with tests.
  57. app.dataCollectionDefaultEnabled = NO;
  58. XCTAssertNotNil([[FIRDeviceCheckProvider alloc] initWithApp:app]);
  59. }
  60. - (void)testInitWithIncompleteApp {
  61. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kAppID GCMSenderID:kProjectNumber];
  62. options.projectID = kProjectID;
  63. FIRApp *missingAPIKeyApp = [[FIRApp alloc] initInstanceWithName:kAppName options:options];
  64. // The following disables automatic token refresh, which could interfere with tests.
  65. missingAPIKeyApp.dataCollectionDefaultEnabled = NO;
  66. XCTAssertNil([[FIRDeviceCheckProvider alloc] initWithApp:missingAPIKeyApp]);
  67. options.projectID = nil;
  68. options.APIKey = kAPIKey;
  69. FIRApp *missingProjectIDApp = [[FIRApp alloc] initInstanceWithName:kAppName options:options];
  70. // The following disables automatic token refresh, which could interfere with tests.
  71. missingProjectIDApp.dataCollectionDefaultEnabled = NO;
  72. XCTAssertNil([[FIRDeviceCheckProvider alloc] initWithApp:missingProjectIDApp]);
  73. }
  74. - (void)testGetTokenSuccess {
  75. // 1. Stub internal DeviceCheck provider.
  76. GACAppCheckToken *validInternalToken = [[GACAppCheckToken alloc] initWithToken:@"valid_token"
  77. expirationDate:[NSDate date]
  78. receivedAtDate:[NSDate date]];
  79. OCMExpect([self.deviceCheckProviderMock
  80. getTokenWithCompletion:([OCMArg
  81. invokeBlockWithArgs:validInternalToken, [NSNull null], nil])]);
  82. // 2. Validate get token.
  83. [self.provider
  84. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  85. XCTAssertEqualObjects(token.token, validInternalToken.token);
  86. XCTAssertEqualObjects(token.expirationDate, validInternalToken.expirationDate);
  87. XCTAssertEqualObjects(token.receivedAtDate, validInternalToken.receivedAtDate);
  88. XCTAssertNil(error);
  89. }];
  90. // 3. Verify mock DeviceCheck provider.
  91. OCMVerifyAll(self.deviceCheckProviderMock);
  92. }
  93. - (void)testGetTokenAPIError {
  94. // 1. Stub internal DeviceCheck provider.
  95. NSError *expectedError = [NSError errorWithDomain:@"testGetTokenAPIError" code:-1 userInfo:nil];
  96. OCMExpect([self.deviceCheckProviderMock
  97. getTokenWithCompletion:([OCMArg invokeBlockWithArgs:[NSNull null], expectedError, nil])]);
  98. // 2. Validate get token.
  99. [self.provider
  100. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  101. XCTAssertNil(token);
  102. XCTAssertEqualObjects(error, expectedError);
  103. }];
  104. // 3. Verify mock DeviceCheck provider.
  105. OCMVerifyAll(self.deviceCheckProviderMock);
  106. }
  107. @end