FIRVerifyClientResponseTests.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2017 Google
  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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import <XCTest/XCTest.h>
  19. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
  20. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyClientRequest.h"
  22. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyClientResponse.h"
  23. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  24. /** @var kFakeAppToken
  25. @brief The fake app token to use in the test request.
  26. */
  27. static NSString *const kFakeAppToken = @"appToken";
  28. /** @var kFakeAPIKey
  29. @brief The fake API key to use in the test request.
  30. */
  31. static NSString *const kFakeAPIKey = @"APIKey";
  32. /** @var kFakeFirebaseAppID
  33. @brief The fake Firebase app ID to use in the test request.
  34. */
  35. static NSString *const kFakeFirebaseAppID = @"appID";
  36. /** @var kAppTokenKey
  37. @brief The key for the appToken request paramenter.
  38. */
  39. static NSString *const kAPPTokenKey = @"appToken";
  40. /** @var kIsSandboxKey
  41. @brief The key for the isSandbox request parameter
  42. */
  43. static NSString *const kIsSandboxKey = @"isSandbox";
  44. /** @var kReceiptKey
  45. @brief The key for the receipt response paramenter.
  46. */
  47. static NSString *const kReceiptKey = @"receipt";
  48. /** @var kFakeReceipt
  49. @brief The fake receipt returned in the response.
  50. */
  51. static NSString *const kFakeReceipt = @"receipt";
  52. /** @var kSuggestedTimeOutKey
  53. @brief The key for the suggested timeout response parameter
  54. */
  55. static NSString *const kSuggestedTimeOutKey = @"suggestedTimeout";
  56. /** @var kFakeSuggestedTimeout
  57. @brief The fake suggested timeout returned in the response.
  58. */
  59. static NSString *const kFakeSuggestedTimeout = @"1234";
  60. /** @var kAllowedTimeDifference
  61. @brief Allowed difference when comparing times because of execution time and floating point
  62. error.
  63. */
  64. static const double kAllowedTimeDifference = 0.1;
  65. /** @var kMissingAppCredentialErrorMessage
  66. @brief This is the error message the server will respond with if the APNS token is missing in a
  67. verifyClient request is missing.
  68. */
  69. static NSString *const kMissingAppCredentialErrorMessage = @"MISSING_APP_CREDENTIAL";
  70. /** @var kMissingAppCredentialErrorMessage
  71. @brief This is the error message the server will respond with if the APNS token is missing in a
  72. verifyClient request is invalid.
  73. */
  74. static NSString *const kInvalidAppCredentialErrorMessage = @"INVALID_APP_CREDENTIAL";
  75. @interface FIRVerifyClientResponseTests : XCTestCase
  76. @end
  77. @implementation FIRVerifyClientResponseTests {
  78. /** @var _RPCIssuer
  79. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  80. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  81. */
  82. FIRFakeBackendRPCIssuer *_RPCIssuer;
  83. /** @var _requestConfiguration
  84. @brief This is the request configuration used for testing.
  85. */
  86. FIRAuthRequestConfiguration *_requestConfiguration;
  87. }
  88. - (void)setUp {
  89. [super setUp];
  90. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  91. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  92. _RPCIssuer = RPCIssuer;
  93. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kFakeAPIKey
  94. appID:kFakeFirebaseAppID];
  95. }
  96. /** @fn testMissingAppCredentialError
  97. @brief Tests that @c FIRAuthErrorCodeMissingAppCredential error.
  98. */
  99. - (void)testMissingAppCredentialError {
  100. FIRVerifyClientRequest *request =
  101. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  102. isSandbox:YES
  103. requestConfiguration:_requestConfiguration];
  104. __block BOOL callbackInvoked;
  105. __block FIRVerifyClientResponse *RPCResponse;
  106. __block NSError *RPCError;
  107. [FIRAuthBackend
  108. verifyClient:request
  109. callback:^(FIRVerifyClientResponse *_Nullable response, NSError *_Nullable error) {
  110. RPCResponse = response;
  111. RPCError = error;
  112. callbackInvoked = YES;
  113. }];
  114. [_RPCIssuer respondWithServerErrorMessage:kMissingAppCredentialErrorMessage];
  115. XCTAssert(callbackInvoked);
  116. XCTAssertNil(RPCResponse);
  117. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeMissingAppCredential);
  118. }
  119. /** @fn testInvalidAppCredentialError
  120. @brief Tests that @c FIRAuthErrorCodeInvalidAppCredential error.
  121. */
  122. - (void)testInvalidAppCredentialError {
  123. FIRVerifyClientRequest *request =
  124. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  125. isSandbox:YES
  126. requestConfiguration:_requestConfiguration];
  127. __block BOOL callbackInvoked;
  128. __block FIRVerifyClientResponse *RPCResponse;
  129. __block NSError *RPCError;
  130. [FIRAuthBackend
  131. verifyClient:request
  132. callback:^(FIRVerifyClientResponse *_Nullable response, NSError *_Nullable error) {
  133. RPCResponse = response;
  134. RPCError = error;
  135. callbackInvoked = YES;
  136. }];
  137. [_RPCIssuer respondWithServerErrorMessage:kInvalidAppCredentialErrorMessage];
  138. XCTAssert(callbackInvoked);
  139. XCTAssertNil(RPCResponse);
  140. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidAppCredential);
  141. }
  142. /** @fn testSuccessfulVerifyClientResponse
  143. @brief Tests a succesful attempt of the verify password flow.
  144. */
  145. - (void)testSuccessfulVerifyPasswordResponse {
  146. FIRVerifyClientRequest *request =
  147. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  148. isSandbox:YES
  149. requestConfiguration:_requestConfiguration];
  150. __block BOOL callbackInvoked;
  151. __block FIRVerifyClientResponse *RPCResponse;
  152. __block NSError *RPCError;
  153. [FIRAuthBackend
  154. verifyClient:request
  155. callback:^(FIRVerifyClientResponse *_Nullable response, NSError *_Nullable error) {
  156. RPCResponse = response;
  157. RPCError = error;
  158. callbackInvoked = YES;
  159. }];
  160. [_RPCIssuer
  161. respondWithJSON:@{kReceiptKey : kFakeReceipt, kSuggestedTimeOutKey : kFakeSuggestedTimeout}];
  162. XCTAssert(callbackInvoked);
  163. XCTAssertNotNil(RPCResponse);
  164. XCTAssertEqualObjects(RPCResponse.receipt, kFakeReceipt);
  165. NSTimeInterval suggestedTimeout = [RPCResponse.suggestedTimeOutDate timeIntervalSinceNow];
  166. XCTAssertEqualWithAccuracy(suggestedTimeout, [kFakeSuggestedTimeout doubleValue],
  167. kAllowedTimeDifference);
  168. }
  169. @end
  170. #endif