FIRSignUpNewUserRequestTests.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 kIDTokenKey
  53. @brief the name of the "kIDTokenKey" property in the request.
  54. */
  55. static NSString *const kIDTokenKey = @"idToken";
  56. /** @var kTestIDToken
  57. @brief Testing id token.
  58. */
  59. static NSString *const kTestIDToken = @"testIDToken";
  60. /** @var kPasswordKey
  61. @brief the name of the "password" property in the request.
  62. */
  63. static NSString *const kPasswordKey = @"password";
  64. /** @var kTestPassword
  65. @brief Testing password.
  66. */
  67. static NSString *const kTestPassword = @"Password";
  68. /** @var kCaptchaResponseKey
  69. @brief The key for the "captchaResponse" value in the request.
  70. */
  71. static NSString *const kCaptchaResponseKey = @"captchaResponse";
  72. /** @var kTestCaptchaResponse
  73. @brief Fake captchaResponse for testing the request.
  74. */
  75. static NSString *const kTestCaptchaResponse = @"testCaptchaResponse";
  76. /** @var kClientTypeKey
  77. @brief The key for the "clientType" value in the request.
  78. */
  79. static NSString *const kClientTypeKey = @"clientType";
  80. /** @var kTestClientType
  81. @brief Fake clientType for testing the request.
  82. */
  83. static NSString *const kTestClientType = @"testClientType";
  84. /** @var kRecaptchaVersionKey
  85. @brief The key for the "recaptchaVersion" value in the request.
  86. */
  87. static NSString *const kRecaptchaVersionKey = @"recaptchaVersion";
  88. /** @var kTestRecaptchaVersion
  89. @brief Fake recaptchaVersion for testing the request.
  90. */
  91. static NSString *const kTestRecaptchaVersion = @"testRecaptchaVersion";
  92. /** @var kReturnSecureTokenKey
  93. @brief The key for the "returnSecureToken" value in the request.
  94. */
  95. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  96. @interface FIRSignUpNewUserRequestTests : XCTestCase
  97. @end
  98. @implementation FIRSignUpNewUserRequestTests {
  99. /** @var _RPCIssuer
  100. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  101. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  102. */
  103. FIRFakeBackendRPCIssuer *_RPCIssuer;
  104. /** @var _requestConfiguration
  105. @brief This is the request configuration used for testing.
  106. */
  107. FIRAuthRequestConfiguration *_requestConfiguration;
  108. }
  109. - (void)setUp {
  110. [super setUp];
  111. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  112. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  113. _RPCIssuer = RPCIssuer;
  114. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  115. appID:kTestFirebaseAppID];
  116. }
  117. - (void)tearDown {
  118. _requestConfiguration = nil;
  119. _RPCIssuer = nil;
  120. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  121. [super tearDown];
  122. }
  123. /** @fn testSignUpNewUserRequestAnonymous
  124. @brief Tests the encoding of a sign up new user request when user is signed in anonymously.
  125. */
  126. - (void)testSignUpNewUserRequestAnonymous {
  127. FIRSignUpNewUserRequest *request =
  128. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  129. request.returnSecureToken = NO;
  130. [FIRAuthBackend
  131. signUpNewUser:request
  132. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error){
  133. }];
  134. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  135. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  136. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  137. XCTAssertNil(_RPCIssuer.decodedRequest[kEmailKey]);
  138. XCTAssertNil(_RPCIssuer.decodedRequest[kDisplayNameKey]);
  139. XCTAssertNil(_RPCIssuer.decodedRequest[kPasswordKey]);
  140. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  141. }
  142. /** @fn testSignUpNewUserRequestNotAnonymous
  143. @brief Tests the encoding of a sign up new user request when user is not signed in anonymously.
  144. */
  145. - (void)testSignUpNewUserRequestNotAnonymous {
  146. FIRSignUpNewUserRequest *request =
  147. [[FIRSignUpNewUserRequest alloc] initWithEmail:kTestEmail
  148. password:kTestPassword
  149. displayName:kTestDisplayName
  150. idToken:kTestIDToken
  151. requestConfiguration:_requestConfiguration];
  152. [FIRAuthBackend
  153. signUpNewUser:request
  154. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error){
  155. }];
  156. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  157. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  158. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  159. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  160. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  161. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDisplayNameKey], kTestDisplayName);
  162. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  163. }
  164. /** @fn testSignUpNewUserRequestOptionalFields
  165. @brief Tests the encoding of a sign up new user request with optional fields.
  166. */
  167. - (void)testSignUpNewUserRequestOptionalFields {
  168. FIRSignUpNewUserRequest *request =
  169. [[FIRSignUpNewUserRequest alloc] initWithEmail:kTestEmail
  170. password:kTestPassword
  171. displayName:kTestDisplayName
  172. idToken:kTestIDToken
  173. requestConfiguration:_requestConfiguration];
  174. request.captchaResponse = kTestCaptchaResponse;
  175. request.clientType = kTestClientType;
  176. request.recaptchaVersion = kTestRecaptchaVersion;
  177. [FIRAuthBackend
  178. signUpNewUser:request
  179. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error){
  180. }];
  181. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  182. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  183. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  184. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  185. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  186. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDisplayNameKey], kTestDisplayName);
  187. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  188. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaResponseKey], kTestCaptchaResponse);
  189. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kClientTypeKey], kTestClientType);
  190. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kRecaptchaVersionKey], kTestRecaptchaVersion);
  191. }
  192. @end