FIRVerifyPasswordRequestTest.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <XCTest/XCTest.h>
  17. #import "FIRAuthErrors.h"
  18. #import "FIRAuthBackend.h"
  19. #import "FIRFakeBackendRPCIssuer.h"
  20. #import "FIRVerifyPasswordRequest.h"
  21. #import "FIRVerifyPasswordResponse.h"
  22. /** @var kTestAPIKey
  23. @brief Fake API key used for testing.
  24. */
  25. static NSString *const kTestAPIKey = @"APIKey";
  26. /** @var kEmailKey
  27. @brief The key for the "email" value in the request.
  28. */
  29. static NSString *const kEmailKey = @"email";
  30. /** @var kPasswordKey
  31. @brief The key for the "password" value in the request.
  32. */
  33. static NSString *const kPasswordKey = @"password";
  34. /** @var kTestEmail
  35. @brief Fake email address for testing the request.
  36. */
  37. static NSString *const kTestEmail = @"testEmail.";
  38. /** @var kTestPassword
  39. @brief Fake password for testing the request.
  40. */
  41. static NSString *const kTestPassword = @"testPassword";
  42. /** @var kPendingIDTokenKey
  43. @brief The key for the "pendingIdToken" value in the request.
  44. */
  45. static NSString *const kPendingIDTokenKey = @"pendingIdToken";
  46. /** @var kTestPendingToken
  47. @brief Fake pendingToken for testing the request.
  48. */
  49. static NSString *const kTestPendingToken = @"testPendingToken";
  50. /** @var kCaptchaChallengeKey
  51. @brief The key for the "captchaChallenge" value in the request.
  52. */
  53. static NSString *const kCaptchaChallengeKey = @"captchaChallenge";
  54. /** @var kTestCaptchaChallenge
  55. @brief Fake captchaChallenge for testing the request.
  56. */
  57. static NSString *const kTestCaptchaChallenge = @"testCaptchaChallenge";
  58. /** @var kCaptchaResponseKey
  59. @brief The key for the "captchaResponse" value in the request.
  60. */
  61. static NSString *const kCaptchaResponseKey = @"captchaResponse";
  62. /** @var kTestCaptchaResponse
  63. @brief Fake captchaResponse for testing the request.
  64. */
  65. static NSString *const kTestCaptchaResponse = @"captchaResponse";
  66. /** @var kReturnSecureTokenKey
  67. @brief The key for the "returnSecureToken" value in the request.
  68. */
  69. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  70. /** @var kExpectedAPIURL
  71. @brief The expected URL for test calls.
  72. */
  73. static NSString *const kExpectedAPIURL =
  74. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=APIKey";
  75. /** @class FIRVerifyPasswordRequestTest
  76. @brief Tests for @c FIRVerifyPasswordRequestTest.
  77. */
  78. @interface FIRVerifyPasswordRequestTest : XCTestCase
  79. @end
  80. @implementation FIRVerifyPasswordRequestTest {
  81. /** @var _RPCIssuer
  82. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  83. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  84. */
  85. FIRFakeBackendRPCIssuer *_RPCIssuer;
  86. /** @var _requestConfiguration
  87. @brief This is the request configuration used for testing.
  88. */
  89. FIRAuthRequestConfiguration *_requestConfiguration;
  90. }
  91. - (void)setUp {
  92. [super setUp];
  93. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  94. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  95. _RPCIssuer = RPCIssuer;
  96. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  97. }
  98. - (void)tearDown {
  99. _RPCIssuer = nil;
  100. _requestConfiguration = nil;
  101. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  102. [super tearDown];
  103. }
  104. /** @fn testVerifyPasswordRequest
  105. @brief Tests the verify password request.
  106. */
  107. - (void)testVerifyPasswordRequest {
  108. FIRVerifyPasswordRequest * request =
  109. [[FIRVerifyPasswordRequest alloc] initWithEmail:kTestEmail
  110. password:kTestPassword
  111. requestConfiguration:_requestConfiguration];
  112. request.returnSecureToken = NO;
  113. [FIRAuthBackend verifyPassword:request
  114. callback:^(FIRVerifyPasswordResponse *_Nullable response,
  115. NSError *_Nullable error) {
  116. }];
  117. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  118. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  119. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  120. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  121. XCTAssertNil(_RPCIssuer.decodedRequest[kCaptchaChallengeKey]);
  122. XCTAssertNil(_RPCIssuer.decodedRequest[kCaptchaResponseKey]);
  123. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  124. }
  125. /** @fn testVerifyPasswordRequestOptionalFields
  126. @brief Tests the verify password request with optional fields.
  127. */
  128. - (void)testVerifyPasswordRequestOptionalFields {
  129. FIRVerifyPasswordRequest * request =
  130. [[FIRVerifyPasswordRequest alloc] initWithEmail:kTestEmail
  131. password:kTestPassword
  132. requestConfiguration:_requestConfiguration];
  133. request.pendingIDToken = kTestPendingToken;
  134. request.captchaChallenge = kTestCaptchaChallenge;
  135. request.captchaResponse = kTestCaptchaResponse;
  136. [FIRAuthBackend verifyPassword:request
  137. callback:^(FIRVerifyPasswordResponse *_Nullable response,
  138. NSError *_Nullable error) {
  139. }];
  140. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  141. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  142. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  143. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  144. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaChallengeKey], kTestCaptchaChallenge);
  145. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaResponseKey], kTestCaptchaResponse);
  146. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  147. }
  148. @end