FIRDeviceCheckProviderTests.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/DeviceCheckProvider/API/FIRDeviceCheckAPIService.h"
  20. #import "FirebaseAppCheck/Sources/DeviceCheckProvider/FIRDeviceCheckTokenGenerator.h"
  21. #import "FirebaseAppCheck/Sources/Public/FirebaseAppCheck/FIRAppCheckToken.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. OCMExpect([self.fakeAPIService appCheckTokenWithDeviceToken:deviceToken])
  76. .andReturn([FBLPromise resolvedWith:validToken]);
  77. // 3. Call getToken and validate the result.
  78. XCTestExpectation *completionExpectation =
  79. [self expectationWithDescription:@"completionExpectation"];
  80. [self.provider
  81. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  82. [completionExpectation fulfill];
  83. XCTAssertEqualObjects(token.token, validToken.token);
  84. XCTAssertEqualObjects(token.expirationDate, validToken.expirationDate);
  85. XCTAssertNil(error);
  86. }];
  87. [self waitForExpectations:@[ completionExpectation ] timeout:0.5];
  88. // 4. Verify fakes.
  89. OCMVerifyAll(self.fakeAPIService);
  90. OCMVerifyAll(self.fakeTokenGenerator);
  91. }
  92. - (void)testGetTokenWhenDeviceTokenFails {
  93. // 1. Expect device token to be generated.
  94. NSError *deviceTokenError = [NSError errorWithDomain:@"FIRDeviceCheckProviderTests"
  95. code:-1
  96. userInfo:nil];
  97. id generateTokenArg = [OCMArg invokeBlockWithArgs:[NSNull null], deviceTokenError, nil];
  98. OCMExpect([self.fakeTokenGenerator generateTokenWithCompletionHandler:generateTokenArg]);
  99. // 2. Don't expect FAA token to be requested.
  100. OCMReject([self.fakeAPIService appCheckTokenWithDeviceToken:[OCMArg any]]);
  101. // 3. Call getToken and validate the result.
  102. XCTestExpectation *completionExpectation =
  103. [self expectationWithDescription:@"completionExpectation"];
  104. [self.provider
  105. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  106. [completionExpectation fulfill];
  107. XCTAssertNil(token);
  108. XCTAssertEqualObjects(error, deviceTokenError);
  109. }];
  110. [self waitForExpectations:@[ completionExpectation ] timeout:0.5];
  111. // 4. Verify fakes.
  112. OCMVerifyAll(self.fakeAPIService);
  113. OCMVerifyAll(self.fakeTokenGenerator);
  114. }
  115. - (void)testGetTokenWhenAPIServiceFails {
  116. // 1. Expect device token to be generated.
  117. NSData *deviceToken = [NSData data];
  118. id generateTokenArg = [OCMArg invokeBlockWithArgs:deviceToken, [NSNull null], nil];
  119. OCMExpect([self.fakeTokenGenerator generateTokenWithCompletionHandler:generateTokenArg]);
  120. // 2. Expect FAA token to be requested.
  121. NSError *APIServiceError = [NSError errorWithDomain:@"FIRDeviceCheckProviderTests"
  122. code:-1
  123. userInfo:nil];
  124. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  125. [rejectedPromise reject:APIServiceError];
  126. OCMExpect([self.fakeAPIService appCheckTokenWithDeviceToken:deviceToken])
  127. .andReturn(rejectedPromise);
  128. // 3. Call getToken and validate the result.
  129. XCTestExpectation *completionExpectation =
  130. [self expectationWithDescription:@"completionExpectation"];
  131. [self.provider
  132. getTokenWithCompletion:^(FIRAppCheckToken *_Nullable token, NSError *_Nullable error) {
  133. [completionExpectation fulfill];
  134. XCTAssertNil(token);
  135. XCTAssertEqualObjects(error, APIServiceError);
  136. }];
  137. [self waitForExpectations:@[ completionExpectation ] timeout:0.5];
  138. // 4. Verify fakes.
  139. OCMVerifyAll(self.fakeAPIService);
  140. OCMVerifyAll(self.fakeTokenGenerator);
  141. }
  142. @end