FIRSignUpNewUserResponseTests.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 kAllowedTimeDifference
  94. @brief Allowed difference when comparing times because of execution time and floating point
  95. error.
  96. */
  97. static const double kAllowedTimeDifference = 0.1;
  98. @interface FIRSignUpNewUserResponseTests : XCTestCase
  99. @end
  100. @implementation FIRSignUpNewUserResponseTests
  101. /** @var _RPCIssuer
  102. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  103. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  104. */
  105. FIRFakeBackendRPCIssuer *_RPCIssuer;
  106. - (void)setUp {
  107. [super setUp];
  108. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  109. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  110. _RPCIssuer = RPCIssuer;
  111. }
  112. - (void)tearDown {
  113. _RPCIssuer = nil;
  114. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  115. [super tearDown];
  116. }
  117. /** @fn testSuccessfulSignUp
  118. @brief This test simulates a complete sign up flow with no errors.
  119. */
  120. - (void)testSuccessfulSignUp {
  121. FIRSignUpNewUserRequest *request =
  122. [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey
  123. email:kTestEmail
  124. password:kTestPassword
  125. displayName:kTestDisplayName];
  126. __block BOOL callbackInvoked;
  127. __block FIRSignUpNewUserResponse *RPCResponse;
  128. __block NSError *RPCError;
  129. [FIRAuthBackend signUpNewUser:request
  130. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  131. NSError *_Nullable error) {
  132. callbackInvoked = YES;
  133. RPCResponse = response;
  134. RPCError = error;
  135. }];
  136. [_RPCIssuer respondWithJSON:@{
  137. kIDTokenKey : kTestIDToken,
  138. kExpiresInKey : kTestExpiresIn,
  139. kRefreshTokenKey : kTestRefreshToken
  140. }];
  141. XCTAssert(callbackInvoked);
  142. XCTAssertNil(RPCError);
  143. XCTAssertNotNil(RPCResponse);
  144. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  145. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  146. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  147. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  148. XCTAssertNil(RPCError, "There should be no error");
  149. }
  150. /** @fn testSignUpNewUserEmailAlreadyInUseError
  151. @brief This test simulates @c testSignUpNewUserEmailAlreadyInUseError with @c
  152. FIRAuthErrorCodeEmailAlreadyInUse error.
  153. */
  154. - (void)testSignUpNewUserEmailAlreadyInUseError {
  155. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  156. __block BOOL callbackInvoked;
  157. __block FIRSignUpNewUserResponse *RPCResponse;
  158. __block NSError *RPCError;
  159. [FIRAuthBackend signUpNewUser:request
  160. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  161. NSError *_Nullable error) {
  162. callbackInvoked = YES;
  163. RPCResponse = response;
  164. RPCError = error;
  165. }];
  166. [_RPCIssuer respondWithServerErrorMessage:kEmailAlreadyInUseErrorMessage];
  167. XCTAssert(callbackInvoked);
  168. XCTAssertNil(RPCResponse);
  169. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeEmailAlreadyInUse);
  170. }
  171. /** @fn testSignUpNewUserOperationNotAllowedError
  172. @brief This test simulates @c testSignUpNewUserEmailExistsError with @c
  173. FIRAuthErrorCodeOperationNotAllowed error.
  174. */
  175. - (void)testSignUpNewUserOperationNotAllowedError {
  176. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  177. __block BOOL callbackInvoked;
  178. __block FIRSignUpNewUserResponse *RPCResponse;
  179. __block NSError *RPCError;
  180. [FIRAuthBackend signUpNewUser:request
  181. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  182. NSError *_Nullable error) {
  183. callbackInvoked = YES;
  184. RPCResponse = response;
  185. RPCError = error;
  186. }];
  187. [_RPCIssuer respondWithServerErrorMessage:kEmailSignUpNotAllowedErrorMessage];
  188. XCTAssert(callbackInvoked);
  189. XCTAssertNil(RPCResponse);
  190. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  191. }
  192. /** @fn testSignUpNewUserPasswordLoginDisabledError
  193. @brief This test simulates @c signUpNewUserPasswordLoginDisabledError with @c
  194. FIRAuthErrorCodeOperationNotAllowed error.
  195. */
  196. - (void)testSignUpNewUserPasswordLoginDisabledError {
  197. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  198. __block BOOL callbackInvoked;
  199. __block FIRSignUpNewUserResponse *RPCResponse;
  200. __block NSError *RPCError;
  201. [FIRAuthBackend signUpNewUser:request
  202. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  203. NSError *_Nullable error) {
  204. callbackInvoked = YES;
  205. RPCResponse = response;
  206. RPCError = error;
  207. }];
  208. [_RPCIssuer respondWithServerErrorMessage:kPasswordLoginDisabledErrorMessage];
  209. XCTAssert(callbackInvoked);
  210. XCTAssertNil(RPCResponse);
  211. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  212. }
  213. /** @fn testInvalidEmailError
  214. @brief This test simulates making a request containing an invalid email address and receiving @c
  215. FIRAuthErrorInvalidEmail error as a result.
  216. */
  217. - (void)testInvalidEmailError {
  218. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  219. __block BOOL callbackInvoked;
  220. __block FIRSignUpNewUserResponse *RPCResponse;
  221. __block NSError *RPCError;
  222. [FIRAuthBackend signUpNewUser:request
  223. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  224. NSError *_Nullable error) {
  225. callbackInvoked = YES;
  226. RPCResponse = response;
  227. RPCError = error;
  228. }];
  229. [_RPCIssuer respondWithServerErrorMessage:kInvalidEmailErrorMessage];
  230. XCTAssert(callbackInvoked);
  231. XCTAssertNil(RPCResponse);
  232. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidEmail);
  233. }
  234. /** @fn testSignUpNewUserWeakPasswordError
  235. @brief This test simulates @c FIRAuthErrorCodeWeakPassword error.
  236. */
  237. - (void)testSignUpNewUserWeakPasswordError {
  238. FIRSignUpNewUserRequest *request = [[FIRSignUpNewUserRequest alloc] initWithAPIKey:kTestAPIKey];
  239. __block BOOL callbackInvoked;
  240. __block FIRSignUpNewUserResponse *RPCResponse;
  241. __block NSError *RPCError;
  242. [FIRAuthBackend signUpNewUser:request
  243. callback:^(FIRSignUpNewUserResponse *_Nullable response,
  244. NSError *_Nullable error) {
  245. callbackInvoked = YES;
  246. RPCResponse = response;
  247. RPCError = error;
  248. }];
  249. [_RPCIssuer respondWithServerErrorMessage:kWeakPasswordErrorMessage];
  250. XCTAssert(callbackInvoked);
  251. XCTAssertNil(RPCResponse);
  252. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeWeakPassword);
  253. XCTAssertEqualObjects(RPCError.userInfo[NSLocalizedFailureReasonErrorKey],
  254. kWeakPasswordClientErrorMessage);
  255. }
  256. @end