FIRSignUpNewUserResponseTests.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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/FIRSignUpNewUserRequest.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRSignUpNewUserResponse.h"
  21. #import "FirebaseAuth/Tests/Unit/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. /** @var _requestConfiguration
  107. @brief This is the request configuration used for testing.
  108. */
  109. FIRAuthRequestConfiguration *_requestConfiguration;
  110. }
  111. - (void)setUp {
  112. [super setUp];
  113. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  114. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  115. _RPCIssuer = RPCIssuer;
  116. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  117. }
  118. - (void)tearDown {
  119. _RPCIssuer = nil;
  120. _requestConfiguration = nil;
  121. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  122. [super tearDown];
  123. }
  124. /** @fn testSuccessfulSignUp
  125. @brief This test simulates a complete sign up flow with no errors.
  126. */
  127. - (void)testSuccessfulSignUp {
  128. FIRSignUpNewUserRequest *request =
  129. [[FIRSignUpNewUserRequest alloc] initWithEmail:kTestEmail
  130. password:kTestPassword
  131. displayName:kTestDisplayName
  132. requestConfiguration:_requestConfiguration];
  133. __block BOOL callbackInvoked;
  134. __block FIRSignUpNewUserResponse *RPCResponse;
  135. __block NSError *RPCError;
  136. [FIRAuthBackend
  137. signUpNewUser:request
  138. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  139. callbackInvoked = YES;
  140. RPCResponse = response;
  141. RPCError = error;
  142. }];
  143. [_RPCIssuer respondWithJSON:@{
  144. kIDTokenKey : kTestIDToken,
  145. kExpiresInKey : kTestExpiresIn,
  146. kRefreshTokenKey : kTestRefreshToken
  147. }];
  148. XCTAssert(callbackInvoked);
  149. XCTAssertNil(RPCError);
  150. XCTAssertNotNil(RPCResponse);
  151. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  152. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  153. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  154. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  155. XCTAssertNil(RPCError, "There should be no error");
  156. }
  157. /** @fn testSignUpNewUserEmailAlreadyInUseError
  158. @brief This test simulates @c testSignUpNewUserEmailAlreadyInUseError with @c
  159. FIRAuthErrorCodeEmailAlreadyInUse error.
  160. */
  161. - (void)testSignUpNewUserEmailAlreadyInUseError {
  162. FIRSignUpNewUserRequest *request =
  163. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  164. __block BOOL callbackInvoked;
  165. __block FIRSignUpNewUserResponse *RPCResponse;
  166. __block NSError *RPCError;
  167. [FIRAuthBackend
  168. signUpNewUser:request
  169. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  170. callbackInvoked = YES;
  171. RPCResponse = response;
  172. RPCError = error;
  173. }];
  174. [_RPCIssuer respondWithServerErrorMessage:kEmailAlreadyInUseErrorMessage];
  175. XCTAssert(callbackInvoked);
  176. XCTAssertNil(RPCResponse);
  177. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeEmailAlreadyInUse);
  178. }
  179. /** @fn testSignUpNewUserOperationNotAllowedError
  180. @brief This test simulates @c testSignUpNewUserEmailExistsError with @c
  181. FIRAuthErrorCodeOperationNotAllowed error.
  182. */
  183. - (void)testSignUpNewUserOperationNotAllowedError {
  184. FIRSignUpNewUserRequest *request =
  185. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  186. __block BOOL callbackInvoked;
  187. __block FIRSignUpNewUserResponse *RPCResponse;
  188. __block NSError *RPCError;
  189. [FIRAuthBackend
  190. signUpNewUser:request
  191. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  192. callbackInvoked = YES;
  193. RPCResponse = response;
  194. RPCError = error;
  195. }];
  196. [_RPCIssuer respondWithServerErrorMessage:kEmailSignUpNotAllowedErrorMessage];
  197. XCTAssert(callbackInvoked);
  198. XCTAssertNil(RPCResponse);
  199. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  200. }
  201. /** @fn testSignUpNewUserPasswordLoginDisabledError
  202. @brief This test simulates @c signUpNewUserPasswordLoginDisabledError with @c
  203. FIRAuthErrorCodeOperationNotAllowed error.
  204. */
  205. - (void)testSignUpNewUserPasswordLoginDisabledError {
  206. FIRSignUpNewUserRequest *request =
  207. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  208. __block BOOL callbackInvoked;
  209. __block FIRSignUpNewUserResponse *RPCResponse;
  210. __block NSError *RPCError;
  211. [FIRAuthBackend
  212. signUpNewUser:request
  213. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  214. callbackInvoked = YES;
  215. RPCResponse = response;
  216. RPCError = error;
  217. }];
  218. [_RPCIssuer respondWithServerErrorMessage:kPasswordLoginDisabledErrorMessage];
  219. XCTAssert(callbackInvoked);
  220. XCTAssertNil(RPCResponse);
  221. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  222. }
  223. /** @fn testInvalidEmailError
  224. @brief This test simulates making a request containing an invalid email address and receiving @c
  225. FIRAuthErrorInvalidEmail error as a result.
  226. */
  227. - (void)testInvalidEmailError {
  228. FIRSignUpNewUserRequest *request =
  229. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  230. __block BOOL callbackInvoked;
  231. __block FIRSignUpNewUserResponse *RPCResponse;
  232. __block NSError *RPCError;
  233. [FIRAuthBackend
  234. signUpNewUser:request
  235. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  236. callbackInvoked = YES;
  237. RPCResponse = response;
  238. RPCError = error;
  239. }];
  240. [_RPCIssuer respondWithServerErrorMessage:kInvalidEmailErrorMessage];
  241. XCTAssert(callbackInvoked);
  242. XCTAssertNil(RPCResponse);
  243. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidEmail);
  244. }
  245. /** @fn testSignUpNewUserWeakPasswordError
  246. @brief This test simulates @c FIRAuthErrorCodeWeakPassword error.
  247. */
  248. - (void)testSignUpNewUserWeakPasswordError {
  249. FIRSignUpNewUserRequest *request =
  250. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  251. __block BOOL callbackInvoked;
  252. __block FIRSignUpNewUserResponse *RPCResponse;
  253. __block NSError *RPCError;
  254. [FIRAuthBackend
  255. signUpNewUser:request
  256. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  257. callbackInvoked = YES;
  258. RPCResponse = response;
  259. RPCError = error;
  260. }];
  261. [_RPCIssuer respondWithServerErrorMessage:kWeakPasswordErrorMessage];
  262. XCTAssert(callbackInvoked);
  263. XCTAssertNil(RPCResponse);
  264. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeWeakPassword);
  265. XCTAssertEqualObjects(RPCError.userInfo[NSLocalizedFailureReasonErrorKey],
  266. kWeakPasswordClientErrorMessage);
  267. }
  268. @end