FIRVerifyPasswordRequestTest.m 6.3 KB

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