FIRSignUpNewUserResponseTests.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. idToken:kTestIDToken
  138. requestConfiguration:_requestConfiguration];
  139. __block BOOL callbackInvoked;
  140. __block FIRSignUpNewUserResponse *RPCResponse;
  141. __block NSError *RPCError;
  142. [FIRAuthBackend
  143. signUpNewUser:request
  144. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  145. callbackInvoked = YES;
  146. RPCResponse = response;
  147. RPCError = error;
  148. }];
  149. [_RPCIssuer respondWithJSON:@{
  150. kIDTokenKey : kTestIDToken,
  151. kExpiresInKey : kTestExpiresIn,
  152. kRefreshTokenKey : kTestRefreshToken
  153. }];
  154. XCTAssert(callbackInvoked);
  155. XCTAssertNil(RPCError);
  156. XCTAssertNotNil(RPCResponse);
  157. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  158. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  159. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  160. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  161. XCTAssertNil(RPCError, "There should be no error");
  162. }
  163. /** @fn testSignUpNewUserEmailAlreadyInUseError
  164. @brief This test simulates @c testSignUpNewUserEmailAlreadyInUseError with @c
  165. FIRAuthErrorCodeEmailAlreadyInUse error.
  166. */
  167. - (void)testSignUpNewUserEmailAlreadyInUseError {
  168. FIRSignUpNewUserRequest *request =
  169. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  170. __block BOOL callbackInvoked;
  171. __block FIRSignUpNewUserResponse *RPCResponse;
  172. __block NSError *RPCError;
  173. [FIRAuthBackend
  174. signUpNewUser:request
  175. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  176. callbackInvoked = YES;
  177. RPCResponse = response;
  178. RPCError = error;
  179. }];
  180. [_RPCIssuer respondWithServerErrorMessage:kEmailAlreadyInUseErrorMessage];
  181. XCTAssert(callbackInvoked);
  182. XCTAssertNil(RPCResponse);
  183. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeEmailAlreadyInUse);
  184. }
  185. /** @fn testSignUpNewUserOperationNotAllowedError
  186. @brief This test simulates @c testSignUpNewUserEmailExistsError with @c
  187. FIRAuthErrorCodeOperationNotAllowed error.
  188. */
  189. - (void)testSignUpNewUserOperationNotAllowedError {
  190. FIRSignUpNewUserRequest *request =
  191. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  192. __block BOOL callbackInvoked;
  193. __block FIRSignUpNewUserResponse *RPCResponse;
  194. __block NSError *RPCError;
  195. [FIRAuthBackend
  196. signUpNewUser:request
  197. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  198. callbackInvoked = YES;
  199. RPCResponse = response;
  200. RPCError = error;
  201. }];
  202. [_RPCIssuer respondWithServerErrorMessage:kEmailSignUpNotAllowedErrorMessage];
  203. XCTAssert(callbackInvoked);
  204. XCTAssertNil(RPCResponse);
  205. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  206. }
  207. /** @fn testSignUpNewUserPasswordLoginDisabledError
  208. @brief This test simulates @c signUpNewUserPasswordLoginDisabledError with @c
  209. FIRAuthErrorCodeOperationNotAllowed error.
  210. */
  211. - (void)testSignUpNewUserPasswordLoginDisabledError {
  212. FIRSignUpNewUserRequest *request =
  213. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  214. __block BOOL callbackInvoked;
  215. __block FIRSignUpNewUserResponse *RPCResponse;
  216. __block NSError *RPCError;
  217. [FIRAuthBackend
  218. signUpNewUser:request
  219. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  220. callbackInvoked = YES;
  221. RPCResponse = response;
  222. RPCError = error;
  223. }];
  224. [_RPCIssuer respondWithServerErrorMessage:kPasswordLoginDisabledErrorMessage];
  225. XCTAssert(callbackInvoked);
  226. XCTAssertNil(RPCResponse);
  227. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  228. }
  229. /** @fn testInvalidEmailError
  230. @brief This test simulates making a request containing an invalid email address and receiving @c
  231. FIRAuthErrorInvalidEmail error as a result.
  232. */
  233. - (void)testInvalidEmailError {
  234. FIRSignUpNewUserRequest *request =
  235. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  236. __block BOOL callbackInvoked;
  237. __block FIRSignUpNewUserResponse *RPCResponse;
  238. __block NSError *RPCError;
  239. [FIRAuthBackend
  240. signUpNewUser:request
  241. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  242. callbackInvoked = YES;
  243. RPCResponse = response;
  244. RPCError = error;
  245. }];
  246. [_RPCIssuer respondWithServerErrorMessage:kInvalidEmailErrorMessage];
  247. XCTAssert(callbackInvoked);
  248. XCTAssertNil(RPCResponse);
  249. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidEmail);
  250. }
  251. /** @fn testSignUpNewUserWeakPasswordError
  252. @brief This test simulates @c FIRAuthErrorCodeWeakPassword error.
  253. */
  254. - (void)testSignUpNewUserWeakPasswordError {
  255. FIRSignUpNewUserRequest *request =
  256. [[FIRSignUpNewUserRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  257. __block BOOL callbackInvoked;
  258. __block FIRSignUpNewUserResponse *RPCResponse;
  259. __block NSError *RPCError;
  260. [FIRAuthBackend
  261. signUpNewUser:request
  262. callback:^(FIRSignUpNewUserResponse *_Nullable response, NSError *_Nullable error) {
  263. callbackInvoked = YES;
  264. RPCResponse = response;
  265. RPCError = error;
  266. }];
  267. [_RPCIssuer respondWithServerErrorMessage:kWeakPasswordErrorMessage];
  268. XCTAssert(callbackInvoked);
  269. XCTAssertNil(RPCResponse);
  270. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeWeakPassword);
  271. XCTAssertEqualObjects(RPCError.userInfo[NSLocalizedFailureReasonErrorKey],
  272. kWeakPasswordClientErrorMessage);
  273. }
  274. @end