FIRVerifyPasswordRequestTest.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "FIRVerifyPasswordRequest.h"
  20. #import "FIRVerifyPasswordResponse.h"
  21. #import "FIRFakeBackendRPCIssuer.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 = [[FIRVerifyPasswordRequest alloc] initWithEmail:kTestEmail
  109. password:kTestPassword
  110. requestConfiguration:_requestConfiguration];
  111. request.returnSecureToken = NO;
  112. [FIRAuthBackend verifyPassword:request
  113. callback:^(FIRVerifyPasswordResponse *_Nullable response,
  114. NSError *_Nullable error) {
  115. }];
  116. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  117. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  118. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  119. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  120. XCTAssertNil(_RPCIssuer.decodedRequest[kCaptchaChallengeKey]);
  121. XCTAssertNil(_RPCIssuer.decodedRequest[kCaptchaResponseKey]);
  122. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  123. }
  124. /** @fn testVerifyPasswordRequestOptionalFields
  125. @brief Tests the verify password request with optional fields.
  126. */
  127. - (void)testVerifyPasswordRequestOptionalFields {
  128. FIRVerifyPasswordRequest * request = [[FIRVerifyPasswordRequest alloc] initWithEmail:kTestEmail
  129. password:kTestPassword
  130. requestConfiguration:_requestConfiguration];
  131. request.pendingIDToken = kTestPendingToken;
  132. request.captchaChallenge = kTestCaptchaChallenge;
  133. request.captchaResponse = kTestCaptchaResponse;
  134. [FIRAuthBackend verifyPassword:request
  135. callback:^(FIRVerifyPasswordResponse *_Nullable response,
  136. NSError *_Nullable error) {
  137. }];
  138. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  139. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  140. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  141. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  142. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaChallengeKey], kTestCaptchaChallenge);
  143. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaResponseKey], kTestCaptchaResponse);
  144. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  145. }
  146. @end