FIRSignUpNewUserRequestTests.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 "FIRGetOOBConfirmationCodeResponse.h"
  20. #import "FIRSignUpNewUserRequest.h"
  21. #import "FIRSignUpNewUserResponse.h"
  22. #import "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. }
  69. - (void)setUp {
  70. [super setUp];
  71. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  72. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  73. _RPCIssuer = RPCIssuer;
  74. }
  75. - (void)tearDown {
  76. _RPCIssuer = nil;
  77. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  78. [super tearDown];
  79. }
  80. /** @fn testSignUpNewUserRequestAnonymous
  81. @brief Tests the encoding of a sign up new user request when user is signed in anonymously.
  82. */
  83. - (void)testSignUpNewUserRequestAnonymous {
  84. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  85. request.returnSecureToken = NO;
  86. [FIRAuthBackend signUpNewUser:request
  87. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  88. NSError *_Nullable error) {
  89. }];
  90. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  91. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  92. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  93. XCTAssertNil(_RPCIssuer.decodedRequest[kEmailKey]);
  94. XCTAssertNil(_RPCIssuer.decodedRequest[kDisplayNameKey]);
  95. XCTAssertNil(_RPCIssuer.decodedRequest[kPasswordKey]);
  96. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  97. }
  98. /** @fn testSignUpNewUserRequestNotAnonymous
  99. @brief Tests the encoding of a sign up new user request when user is not signed in anonymously.
  100. */
  101. - (void)testSignUpNewUserRequestNotAnonymous {
  102. FIRSignUpNewUserRequest *request =
  103. [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey
  104. email:kTestEmail
  105. password:kTestPassword
  106. displayName:kTestDisplayName];
  107. [FIRAuthBackend signUpNewUser:request
  108. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  109. NSError *_Nullable error) {
  110. }];
  111. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  112. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  113. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  114. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  115. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  116. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDisplayNameKey], kTestDisplayName);
  117. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  118. }
  119. @end