FIRVerifyPasswordRequestTest.m 6.5 KB

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