FIRSignUpNewUserResponseTests.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "FIRSignUpNewUserRequest.h"
  20. #import "FIRSignUpNewUserResponse.h"
  21. #import "FIRFakeBackendRPCIssuer.h"
  22. /** @var kTestAPIKey
  23. @brief Fake API key used for testing.
  24. */
  25. static NSString *const kTestAPIKey = @"APIKey";
  26. /** @var kIDTokenKey
  27. @brief The name of the "IDToken" property in the response.
  28. */
  29. static NSString *const kIDTokenKey = @"idToken";
  30. /** @var kTestIDToken
  31. @brief Testing ID token for verifying assertion.
  32. */
  33. static NSString *const kTestIDToken = @"ID_TOKEN";
  34. /** @var kExpiresInKey
  35. @brief The name of the "expiresIn" property in the response.
  36. */
  37. static NSString *const kExpiresInKey = @"expiresIn";
  38. /** @var kTestExpiresIn
  39. @brief Fake token expiration time.
  40. */
  41. static NSString *const kTestExpiresIn = @"12345";
  42. /** @var kRefreshTokenKey
  43. @brief The name of the "refreshToken" property in the response.
  44. */
  45. static NSString *const kRefreshTokenKey = @"refreshToken";
  46. /** @var kTestRefreshToken
  47. @brief Fake refresh token.
  48. */
  49. static NSString *const kTestRefreshToken = @"REFRESH_TOKEN";
  50. /** @var kTestEmail
  51. @brief Testing user email adadress.
  52. */
  53. static NSString *const kTestEmail = @"test@gmail.com";
  54. /** @var kTestDisplayName
  55. @brief Testing display name.
  56. */
  57. static NSString *const kTestDisplayName = @"DisplayName";
  58. /** @var kTestPassword
  59. @brief Testing password.
  60. */
  61. static NSString *const kTestPassword = @"Password";
  62. /** @var kEmailAlreadyInUseErrorMessage
  63. @brief This is the error message the server will respond with if the user entered an invalid
  64. email address.
  65. */
  66. static NSString *const kEmailAlreadyInUseErrorMessage = @"EMAIL_EXISTS";
  67. /** @var kOperationNotAllowedErrorMessage
  68. @brief This is the error message the server will respond with if user/password account was
  69. disabled by the administrator.
  70. */
  71. static NSString *const kEmailSignUpNotAllowedErrorMessage = @"OPERATION_NOT_ALLOWED";
  72. /** @var kPasswordLoginDisabledErrorMessage
  73. @brief This is the error message the server responds with if password login is disabled.
  74. */
  75. static NSString *const kPasswordLoginDisabledErrorMessage = @"PASSWORD_LOGIN_DISABLED:";
  76. /** @var kInvalidEmailErrorMessage
  77. @brief The error returned by the server if the email is invalid.
  78. */
  79. static NSString *const kInvalidEmailErrorMessage = @"INVALID_EMAIL";
  80. /** @var kWeakPasswordErrorMessage
  81. @brief This is the error message the server will respond with if the new user's password
  82. is too weak that it is too short.
  83. */
  84. static NSString *const kWeakPasswordErrorMessage =
  85. @"WEAK_PASSWORD : Password should be at least 6 characters";
  86. /** @var kWeakPasswordClientErrorMessage
  87. @brief This is the error message the client will see if the new user's password is too weak
  88. that it is too short.
  89. @remarks This message should be derived from @c kWeakPasswordErrorMessage .
  90. */
  91. static NSString *const kWeakPasswordClientErrorMessage =
  92. @"Password should be at least 6 characters";
  93. /** @var kEpsilon
  94. @brief Allowed difference when comparing floating point numbers.
  95. */
  96. static const double kEpsilon = 1e-3;
  97. @interface FIRSignUpNewUserResponseTests : XCTestCase
  98. @end
  99. @implementation FIRSignUpNewUserResponseTests
  100. /** @var _RPCIssuer
  101. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  102. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  103. */
  104. FIRFakeBackendRPCIssuer *_RPCIssuer;
  105. - (void)setUp {
  106. [super setUp];
  107. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  108. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  109. _RPCIssuer = RPCIssuer;
  110. }
  111. - (void)tearDown {
  112. _RPCIssuer = nil;
  113. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  114. [super tearDown];
  115. }
  116. /** @fn testSuccessfulSignUp
  117. @brief This test simulates a complete sign up flow with no errors.
  118. */
  119. - (void)testSuccessfulSignUp {
  120. FIRSignUpNewUserRequest *request =
  121. [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey
  122. email:kTestEmail
  123. password:kTestPassword
  124. displayName:kTestDisplayName];
  125. __block BOOL callbackInvoked;
  126. __block FIRSignUpNewUserResponse *RPCResponse;
  127. __block NSError *RPCError;
  128. [FIRAuthBackend signUpNewUser:request
  129. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  130. NSError *_Nullable error) {
  131. callbackInvoked = YES;
  132. RPCResponse = response;
  133. RPCError = error;
  134. }];
  135. [_RPCIssuer respondWithJSON:@{
  136. kIDTokenKey : kTestIDToken,
  137. kExpiresInKey : kTestExpiresIn,
  138. kRefreshTokenKey : kTestRefreshToken
  139. }];
  140. XCTAssert(callbackInvoked);
  141. XCTAssertNil(RPCError);
  142. XCTAssertNotNil(RPCResponse);
  143. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  144. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  145. XCTAssertLessThanOrEqual(fabs(expiresIn - [kTestExpiresIn doubleValue]), kEpsilon);
  146. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  147. XCTAssertNil(RPCError, "There should be no error");
  148. }
  149. /** @fn testSignUpNewUserEmailAlreadyInUseError
  150. @brief This test simulates @c testSignUpNewUserEmailAlreadyInUseError with @c
  151. FIRAuthErrorCodeEmailAlreadyInUse error.
  152. */
  153. - (void)testSignUpNewUserEmailAlreadyInUseError {
  154. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  155. __block BOOL callbackInvoked;
  156. __block FIRSignUpNewUserResponse *RPCResponse;
  157. __block NSError *RPCError;
  158. [FIRAuthBackend signUpNewUser:request
  159. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  160. NSError *_Nullable error) {
  161. callbackInvoked = YES;
  162. RPCResponse = response;
  163. RPCError = error;
  164. }];
  165. [_RPCIssuer respondWithServerErrorMessage:kEmailAlreadyInUseErrorMessage];
  166. XCTAssert(callbackInvoked);
  167. XCTAssertNil(RPCResponse);
  168. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeEmailAlreadyInUse);
  169. }
  170. /** @fn testSignUpNewUserOperationNotAllowedError
  171. @brief This test simulates @c testSignUpNewUserEmailExistsError with @c
  172. FIRAuthErrorCodeOperationNotAllowed error.
  173. */
  174. - (void)testSignUpNewUserOperationNotAllowedError {
  175. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  176. __block BOOL callbackInvoked;
  177. __block FIRSignUpNewUserResponse *RPCResponse;
  178. __block NSError *RPCError;
  179. [FIRAuthBackend signUpNewUser:request
  180. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  181. NSError *_Nullable error) {
  182. callbackInvoked = YES;
  183. RPCResponse = response;
  184. RPCError = error;
  185. }];
  186. [_RPCIssuer respondWithServerErrorMessage:kEmailSignUpNotAllowedErrorMessage];
  187. XCTAssert(callbackInvoked);
  188. XCTAssertNil(RPCResponse);
  189. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  190. }
  191. /** @fn testSignUpNewUserPasswordLoginDisabledError
  192. @brief This test simulates @c signUpNewUserPasswordLoginDisabledError with @c
  193. FIRAuthErrorCodeOperationNotAllowed error.
  194. */
  195. - (void)testSignUpNewUserPasswordLoginDisabledError {
  196. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  197. __block BOOL callbackInvoked;
  198. __block FIRSignUpNewUserResponse *RPCResponse;
  199. __block NSError *RPCError;
  200. [FIRAuthBackend signUpNewUser:request
  201. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  202. NSError *_Nullable error) {
  203. callbackInvoked = YES;
  204. RPCResponse = response;
  205. RPCError = error;
  206. }];
  207. [_RPCIssuer respondWithServerErrorMessage:kPasswordLoginDisabledErrorMessage];
  208. XCTAssert(callbackInvoked);
  209. XCTAssertNil(RPCResponse);
  210. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  211. }
  212. /** @fn testinvalidEmailError
  213. @brief This test simulates making a request containing an invalid email address and receiving @c
  214. FIRAuthErrorInvalidEmail error as a result.
  215. */
  216. - (void)testinvalidEmailError {
  217. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  218. __block BOOL callbackInvoked;
  219. __block FIRSignUpNewUserResponse *RPCResponse;
  220. __block NSError *RPCError;
  221. [FIRAuthBackend signUpNewUser:request
  222. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  223. NSError *_Nullable error) {
  224. callbackInvoked = YES;
  225. RPCResponse = response;
  226. RPCError = error;
  227. }];
  228. [_RPCIssuer respondWithServerErrorMessage:kInvalidEmailErrorMessage];
  229. XCTAssert(callbackInvoked);
  230. XCTAssertNil(RPCResponse);
  231. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidEmail);
  232. }
  233. /** @fn testSignUpNewUserWeakPasswordError
  234. @brief This test simulates @c FIRAuthErrorCodeWeakPassword error.
  235. */
  236. - (void)testSignUpNewUserWeakPasswordError {
  237. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  238. __block BOOL callbackInvoked;
  239. __block FIRSignUpNewUserResponse *RPCResponse;
  240. __block NSError *RPCError;
  241. [FIRAuthBackend signUpNewUser:request
  242. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  243. NSError *_Nullable error) {
  244. callbackInvoked = YES;
  245. RPCResponse = response;
  246. RPCError = error;
  247. }];
  248. [_RPCIssuer respondWithServerErrorMessage:kWeakPasswordErrorMessage];
  249. XCTAssert(callbackInvoked);
  250. XCTAssertNil(RPCResponse);
  251. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeWeakPassword);
  252. XCTAssertEqualObjects(RPCError.userInfo[NSLocalizedFailureReasonErrorKey],
  253. kWeakPasswordClientErrorMessage);
  254. }
  255. @end