FIRSignUpNewUserResponseTests.m 11 KB

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