FIRSignUpNewUserRequestTests.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <FirebaseAuth/FIRAuthErrors.h>
  17. #import <XCTest/XCTest.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 kEmailKey
  33. @brief The name of the "email" property in the request.
  34. */
  35. static NSString *const kEmailKey = @"email";
  36. /** @var kTestEmail
  37. @brief Testing user email adadress.
  38. */
  39. static NSString *const kTestEmail = @"test@gmail.com";
  40. /** @var kDisplayNameKey
  41. @brief the name of the "displayName" property in the request.
  42. */
  43. static NSString *const kDisplayNameKey = @"displayName";
  44. /** @var kTestDisplayName
  45. @brief Testing display name.
  46. */
  47. static NSString *const kTestDisplayName = @"DisplayName";
  48. /** @var kPasswordKey
  49. @brief the name of the "password" property in the request.
  50. */
  51. static NSString *const kPasswordKey = @"password";
  52. /** @var kTestPassword
  53. @brief Testing password.
  54. */
  55. static NSString *const kTestPassword = @"Password";
  56. /** @var kReturnSecureTokenKey
  57. @brief The key for the "returnSecureToken" value in the request.
  58. */
  59. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  60. @interface FIRSignUpNewUserRequestTests : XCTestCase
  61. @end
  62. @implementation FIRSignUpNewUserRequestTests {
  63. /** @var _RPCIssuer
  64. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  65. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  66. */
  67. FIRFakeBackendRPCIssuer *_RPCIssuer;
  68. /** @var _requestConfiguration
  69. @brief This is the request configuration used for testing.
  70. */
  71. FIRAuthRequestConfiguration *_requestConfiguration;
  72. }
  73. - (void)setUp {
  74. [super setUp];
  75. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  76. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  77. _RPCIssuer = RPCIssuer;
  78. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  79. }
  80. - (void)tearDown {
  81. _requestConfiguration = nil;
  82. _RPCIssuer = nil;
  83. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  84. [super tearDown];
  85. }
  86. /** @fn testSignUpNewUserRequestAnonymous
  87. @brief Tests the encoding of a sign up new user request when user is signed in anonymously.
  88. */
  89. - (void)testSignUpNewUserRequestAnonymous {
  90. FIRSignUpNewUserRequest *request =
  91. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  92. request.returnSecureToken = NO;
  93. [FIRAuthBackend
  94. signUpNewUser:request
  95. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error){
  96. }];
  97. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  98. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  99. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  100. XCTAssertNil(_RPCIssuer.decodedRequest[kEmailKey]);
  101. XCTAssertNil(_RPCIssuer.decodedRequest[kDisplayNameKey]);
  102. XCTAssertNil(_RPCIssuer.decodedRequest[kPasswordKey]);
  103. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  104. }
  105. /** @fn testSignUpNewUserRequestNotAnonymous
  106. @brief Tests the encoding of a sign up new user request when user is not signed in anonymously.
  107. */
  108. - (void)testSignUpNewUserRequestNotAnonymous {
  109. FIRSignUpNewUserRequest *request =
  110. [[FIRSignUpNewUserRequest alloc] initWithEmail:kTestEmail
  111. password:kTestPassword
  112. displayName:kTestDisplayName
  113. requestConfiguration:_requestConfiguration];
  114. [FIRAuthBackend
  115. signUpNewUser:request
  116. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error){
  117. }];
  118. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  119. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  120. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  121. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  122. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  123. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDisplayNameKey], kTestDisplayName);
  124. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  125. }
  126. @end