FIRSignUpNewUserRequestTests.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/FIRGetOOBConfirmationCodeResponse.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignUpNewUserRequest.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignUpNewUserResponse.h"
  22. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  23. /** @var kExpectedAPIURL
  24. @brief The expected URL for the test calls.
  25. */
  26. static NSString *const kExpectedAPIURL =
  27. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=APIKey";
  28. /** @var kTestAPIKey
  29. @brief Fake API key used for testing.
  30. */
  31. static NSString *const kTestAPIKey = @"APIKey";
  32. /** @var kTestFirebaseAppID
  33. @brief Fake Firebase app ID used for testing.
  34. */
  35. static NSString *const kTestFirebaseAppID = @"appID";
  36. /** @var kEmailKey
  37. @brief The name of the "email" property in the request.
  38. */
  39. static NSString *const kEmailKey = @"email";
  40. /** @var kTestEmail
  41. @brief Testing user email adadress.
  42. */
  43. static NSString *const kTestEmail = @"test@gmail.com";
  44. /** @var kDisplayNameKey
  45. @brief the name of the "displayName" property in the request.
  46. */
  47. static NSString *const kDisplayNameKey = @"displayName";
  48. /** @var kTestDisplayName
  49. @brief Testing display name.
  50. */
  51. static NSString *const kTestDisplayName = @"DisplayName";
  52. /** @var kPasswordKey
  53. @brief the name of the "password" property in the request.
  54. */
  55. static NSString *const kPasswordKey = @"password";
  56. /** @var kTestPassword
  57. @brief Testing password.
  58. */
  59. static NSString *const kTestPassword = @"Password";
  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 = @"testCaptchaResponse";
  68. /** @var kClientTypeKey
  69. @brief The key for the "clientType" value in the request.
  70. */
  71. static NSString *const kClientTypeKey = @"clientType";
  72. /** @var kTestClientType
  73. @brief Fake clientType for testing the request.
  74. */
  75. static NSString *const kTestClientType = @"testClientType";
  76. /** @var kRecaptchaVersionKey
  77. @brief The key for the "recaptchaVersion" value in the request.
  78. */
  79. static NSString *const kRecaptchaVersionKey = @"recaptchaVersion";
  80. /** @var kTestRecaptchaVersion
  81. @brief Fake recaptchaVersion for testing the request.
  82. */
  83. static NSString *const kTestRecaptchaVersion = @"testRecaptchaVersion";
  84. /** @var kReturnSecureTokenKey
  85. @brief The key for the "returnSecureToken" value in the request.
  86. */
  87. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  88. @interface FIRSignUpNewUserRequestTests : XCTestCase
  89. @end
  90. @implementation FIRSignUpNewUserRequestTests {
  91. /** @var _RPCIssuer
  92. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  93. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  94. */
  95. FIRFakeBackendRPCIssuer *_RPCIssuer;
  96. /** @var _requestConfiguration
  97. @brief This is the request configuration used for testing.
  98. */
  99. FIRAuthRequestConfiguration *_requestConfiguration;
  100. }
  101. - (void)setUp {
  102. [super setUp];
  103. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  104. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  105. _RPCIssuer = RPCIssuer;
  106. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  107. appID:kTestFirebaseAppID];
  108. }
  109. - (void)tearDown {
  110. _requestConfiguration = nil;
  111. _RPCIssuer = nil;
  112. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  113. [super tearDown];
  114. }
  115. /** @fn testSignUpNewUserRequestAnonymous
  116. @brief Tests the encoding of a sign up new user request when user is signed in anonymously.
  117. */
  118. - (void)testSignUpNewUserRequestAnonymous {
  119. FIRSignUpNewUserRequest *request =
  120. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  121. request.returnSecureToken = NO;
  122. [FIRAuthBackend
  123. signUpNewUser:request
  124. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error){
  125. }];
  126. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  127. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  128. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  129. XCTAssertNil(_RPCIssuer.decodedRequest[kEmailKey]);
  130. XCTAssertNil(_RPCIssuer.decodedRequest[kDisplayNameKey]);
  131. XCTAssertNil(_RPCIssuer.decodedRequest[kPasswordKey]);
  132. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  133. }
  134. /** @fn testSignUpNewUserRequestNotAnonymous
  135. @brief Tests the encoding of a sign up new user request when user is not signed in anonymously.
  136. */
  137. - (void)testSignUpNewUserRequestNotAnonymous {
  138. FIRSignUpNewUserRequest *request =
  139. [[FIRSignUpNewUserRequest alloc] initWithEmail:kTestEmail
  140. password:kTestPassword
  141. displayName:kTestDisplayName
  142. requestConfiguration:_requestConfiguration];
  143. [FIRAuthBackend
  144. signUpNewUser:request
  145. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error){
  146. }];
  147. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  148. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  149. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  150. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  151. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  152. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDisplayNameKey], kTestDisplayName);
  153. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  154. }
  155. /** @fn testSignUpNewUserRequestOptionalFields
  156. @brief Tests the encoding of a sign up new user request with optional fields.
  157. */
  158. - (void)testSignUpNewUserRequestOptionalFields {
  159. FIRSignUpNewUserRequest *request =
  160. [[FIRSignUpNewUserRequest alloc] initWithEmail:kTestEmail
  161. password:kTestPassword
  162. displayName:kTestDisplayName
  163. requestConfiguration:_requestConfiguration];
  164. request.captchaResponse = kTestCaptchaResponse;
  165. request.clientType = kTestClientType;
  166. request.recaptchaVersion = kTestRecaptchaVersion;
  167. [FIRAuthBackend
  168. signUpNewUser:request
  169. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error){
  170. }];
  171. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  172. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  173. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  174. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  175. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  176. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDisplayNameKey], kTestDisplayName);
  177. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  178. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaResponseKey], kTestCaptchaResponse);
  179. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kClientTypeKey], kTestClientType);
  180. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kRecaptchaVersionKey], kTestRecaptchaVersion);
  181. }
  182. @end