FIRSignUpNewUserRequestTests.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 kReturnSecureTokenKey
  61. @brief The key for the "returnSecureToken" value in the request.
  62. */
  63. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  64. @interface FIRSignUpNewUserRequestTests : XCTestCase
  65. @end
  66. @implementation FIRSignUpNewUserRequestTests {
  67. /** @var _RPCIssuer
  68. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  69. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  70. */
  71. FIRFakeBackendRPCIssuer *_RPCIssuer;
  72. /** @var _requestConfiguration
  73. @brief This is the request configuration used for testing.
  74. */
  75. FIRAuthRequestConfiguration *_requestConfiguration;
  76. }
  77. - (void)setUp {
  78. [super setUp];
  79. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  80. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  81. _RPCIssuer = RPCIssuer;
  82. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  83. appID:kTestFirebaseAppID];
  84. }
  85. - (void)tearDown {
  86. _requestConfiguration = nil;
  87. _RPCIssuer = nil;
  88. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  89. [super tearDown];
  90. }
  91. /** @fn testSignUpNewUserRequestAnonymous
  92. @brief Tests the encoding of a sign up new user request when user is signed in anonymously.
  93. */
  94. - (void)testSignUpNewUserRequestAnonymous {
  95. FIRSignUpNewUserRequest *request =
  96. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  97. request.returnSecureToken = NO;
  98. [FIRAuthBackend
  99. signUpNewUser:request
  100. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error){
  101. }];
  102. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  103. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  104. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  105. XCTAssertNil(_RPCIssuer.decodedRequest[kEmailKey]);
  106. XCTAssertNil(_RPCIssuer.decodedRequest[kDisplayNameKey]);
  107. XCTAssertNil(_RPCIssuer.decodedRequest[kPasswordKey]);
  108. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  109. }
  110. /** @fn testSignUpNewUserRequestNotAnonymous
  111. @brief Tests the encoding of a sign up new user request when user is not signed in anonymously.
  112. */
  113. - (void)testSignUpNewUserRequestNotAnonymous {
  114. FIRSignUpNewUserRequest *request =
  115. [[FIRSignUpNewUserRequest alloc] initWithEmail:kTestEmail
  116. password:kTestPassword
  117. displayName:kTestDisplayName
  118. requestConfiguration:_requestConfiguration];
  119. [FIRAuthBackend
  120. signUpNewUser:request
  121. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error){
  122. }];
  123. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  124. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  125. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  126. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  127. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  128. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDisplayNameKey], kTestDisplayName);
  129. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  130. }
  131. @end