FIRVerifyPasswordRequestTest.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. }
  87. - (void)setUp {
  88. [super setUp];
  89. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  90. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  91. _RPCIssuer = RPCIssuer;
  92. }
  93. - (void)tearDown {
  94. _RPCIssuer = nil;
  95. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  96. [super tearDown];
  97. }
  98. /** @fn testVerifyPasswordRequest
  99. @brief Tests the verify password request.
  100. */
  101. - (void)testVerifyPasswordRequest {
  102. FIRVerifyPasswordRequest * request = [[FIRVerifyPasswordRequest alloc] initWithEmail:kTestEmail
  103. password:kTestPassword
  104. APIKey:kTestAPIKey];
  105. request.returnSecureToken = NO;
  106. [FIRAuthBackend verifyPassword:request
  107. callback:^(FIRVerifyPasswordResponse *_Nullable response,
  108. NSError *_Nullable error) {
  109. }];
  110. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  111. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  112. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  113. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  114. XCTAssertNil(_RPCIssuer.decodedRequest[kCaptchaChallengeKey]);
  115. XCTAssertNil(_RPCIssuer.decodedRequest[kCaptchaResponseKey]);
  116. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  117. }
  118. /** @fn testVerifyPasswordRequestOptionalFields
  119. @brief Tests the verify password request with optional fields.
  120. */
  121. - (void)testVerifyPasswordRequestOptionalFields {
  122. FIRVerifyPasswordRequest * request = [[FIRVerifyPasswordRequest alloc] initWithEmail:kTestEmail
  123. password:kTestPassword
  124. APIKey:kTestAPIKey];
  125. request.pendingIDToken = kTestPendingToken;
  126. request.captchaChallenge = kTestCaptchaChallenge;
  127. request.captchaResponse = kTestCaptchaResponse;
  128. [FIRAuthBackend verifyPassword:request
  129. callback:^(FIRVerifyPasswordResponse *_Nullable response,
  130. NSError *_Nullable error) {
  131. }];
  132. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  133. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  134. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  135. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  136. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaChallengeKey], kTestCaptchaChallenge);
  137. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaResponseKey], kTestCaptchaResponse);
  138. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  139. }
  140. @end