FIRDeviceCheckProviderTests.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/DeviceCheckProvider/API/FIRDeviceCheckAPIService.h"
  21. #import "FirebaseAppCheck/Sources/DeviceCheckProvider/FIRDeviceCheckTokenGenerator.h"
  22. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRDeviceCheckProvider.h"
  23. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  24. API_AVAILABLE(ios(11.0), macos(10.15), tvos(11.0))
  25. API_UNAVAILABLE(watchos)
  26. @interface FIRDeviceCheckProviderTests : XCTestCase
  27. @property(nonatomic) FIRDeviceCheckProvider *provider;
  28. @property(nonatomic) id fakeAPIService;
  29. @property(nonatomic) id fakeTokenGenerator;
  30. @end
  31. @interface FIRDeviceCheckProvider (Tests)
  32. - (instancetype)initWithAPIService:(id<FIRDeviceCheckAPIServiceProtocol>)APIService
  33. deviceTokenGenerator:(id<FIRDeviceCheckTokenGenerator>)deviceTokenGenerator;
  34. @end
  35. @implementation FIRDeviceCheckProviderTests
  36. - (void)setUp {
  37. [super setUp];
  38. self.fakeAPIService = OCMProtocolMock(@protocol(FIRDeviceCheckAPIServiceProtocol));
  39. self.fakeTokenGenerator = OCMProtocolMock(@protocol(FIRDeviceCheckTokenGenerator));
  40. self.provider = [[FIRDeviceCheckProvider alloc] initWithAPIService:self.fakeAPIService
  41. deviceTokenGenerator:self.fakeTokenGenerator];
  42. }
  43. - (void)tearDown {
  44. self.provider = nil;
  45. self.fakeAPIService = nil;
  46. self.fakeTokenGenerator = nil;
  47. }
  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([[FIRDeviceCheckProvider 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([[FIRDeviceCheckProvider alloc] initWithApp:missingAPIKeyApp]);
  61. options.projectID = nil;
  62. options.APIKey = @"api_key";
  63. FIRApp *missingProjectIDApp = [[FIRApp alloc] initInstanceWithName:@"testInitWithValidApp"
  64. options:options];
  65. XCTAssertNil([[FIRDeviceCheckProvider alloc] initWithApp:missingProjectIDApp]);
  66. }
  67. - (void)testGetTokenSuccess {
  68. // 1. Expect device token to be generated.
  69. NSData *deviceToken = [NSData data];
  70. id generateTokenArg = [OCMArg invokeBlockWithArgs:deviceToken, [NSNull null], nil];
  71. OCMExpect([self.fakeTokenGenerator generateTokenWithCompletionHandler:generateTokenArg]);
  72. // 2. Expect FAA token to be requested.
  73. FIRAppCheckToken *validToken = [[FIRAppCheckToken alloc] initWithToken:@"valid_token"
  74. expirationDate:[NSDate distantFuture]
  75. receivedAtDate:[NSDate date]];
  76. OCMExpect([self.fakeAPIService appCheckTokenWithDeviceToken:deviceToken])
  77. .andReturn([FBLPromise resolvedWith:validToken]);
  78. // 3. Call getToken and validate the result.
  79. XCTestExpectation *completionExpectation =
  80. [self expectationWithDescription:@"completionExpectation"];
  81. [self.provider
  82. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  83. [completionExpectation fulfill];
  84. XCTAssertEqualObjects(token.token, validToken.token);
  85. XCTAssertEqualObjects(token.expirationDate, validToken.expirationDate);
  86. XCTAssertEqualObjects(token.receivedAtDate, validToken.receivedAtDate);
  87. XCTAssertNil(error);
  88. }];
  89. [self waitForExpectations:@[ completionExpectation ] timeout:0.5];
  90. // 4. Verify fakes.
  91. OCMVerifyAll(self.fakeAPIService);
  92. OCMVerifyAll(self.fakeTokenGenerator);
  93. }
  94. - (void)testGetTokenWhenDeviceTokenFails {
  95. // 1. Expect device token to be generated.
  96. NSError *deviceTokenError = [NSError errorWithDomain:@"FIRDeviceCheckProviderTests"
  97. code:-1
  98. userInfo:nil];
  99. id generateTokenArg = [OCMArg invokeBlockWithArgs:[NSNull null], deviceTokenError, nil];
  100. OCMExpect([self.fakeTokenGenerator generateTokenWithCompletionHandler:generateTokenArg]);
  101. // 2. Don't expect FAA token to be requested.
  102. OCMReject([self.fakeAPIService appCheckTokenWithDeviceToken:[OCMArg any]]);
  103. // 3. Call getToken and validate the result.
  104. XCTestExpectation *completionExpectation =
  105. [self expectationWithDescription:@"completionExpectation"];
  106. [self.provider
  107. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  108. [completionExpectation fulfill];
  109. XCTAssertNil(token);
  110. XCTAssertEqualObjects(error, deviceTokenError);
  111. }];
  112. [self waitForExpectations:@[ completionExpectation ] timeout:0.5];
  113. // 4. Verify fakes.
  114. OCMVerifyAll(self.fakeAPIService);
  115. OCMVerifyAll(self.fakeTokenGenerator);
  116. }
  117. - (void)testGetTokenWhenAPIServiceFails {
  118. // 1. Expect device token to be generated.
  119. NSData *deviceToken = [NSData data];
  120. id generateTokenArg = [OCMArg invokeBlockWithArgs:deviceToken, [NSNull null], nil];
  121. OCMExpect([self.fakeTokenGenerator generateTokenWithCompletionHandler:generateTokenArg]);
  122. // 2. Expect FAA token to be requested.
  123. NSError *APIServiceError = [NSError errorWithDomain:@"FIRDeviceCheckProviderTests"
  124. code:-1
  125. userInfo:nil];
  126. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  127. [rejectedPromise reject:APIServiceError];
  128. OCMExpect([self.fakeAPIService appCheckTokenWithDeviceToken:deviceToken])
  129. .andReturn(rejectedPromise);
  130. // 3. Call getToken and validate the result.
  131. XCTestExpectation *completionExpectation =
  132. [self expectationWithDescription:@"completionExpectation"];
  133. [self.provider
  134. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  135. [completionExpectation fulfill];
  136. XCTAssertNil(token);
  137. XCTAssertEqualObjects(error, APIServiceError);
  138. }];
  139. [self waitForExpectations:@[ completionExpectation ] timeout:0.5];
  140. // 4. Verify fakes.
  141. OCMVerifyAll(self.fakeAPIService);
  142. OCMVerifyAll(self.fakeTokenGenerator);
  143. }
  144. @end