FIRResetPasswordResponseTests.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "FIRResetPasswordRequest.h"
  20. #import "FIRResetPasswordResponse.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 kUserDisabledErrorMessage
  27. @brief This is the error message the server will respond with if the user's account has been
  28. disabled.
  29. */
  30. static NSString *const kUserDisabledErrorMessage = @"USER_DISABLED";
  31. /** @var kOperationNotAllowedErrorMessage
  32. @brief This is the error message the server will respond with if Admin disables IDP specified by
  33. provider.
  34. */
  35. static NSString *const kOperationNotAllowedErrorMessage = @"OPERATION_NOT_ALLOWED";
  36. /** @var kExpiredActionCodeErrorMessage
  37. @brief This is the error message the server will respond with if the action code is expired.
  38. */
  39. static NSString *const kExpiredActionCodeErrorMessage = @"EXPIRED_OOB_CODE";
  40. /** @var kInvalidActionCodeErrorMessage
  41. @brief This is the error message the server will respond with if the action code is invalid.
  42. */
  43. static NSString *const kInvalidActionCodeErrorMessage = @"INVALID_OOB_CODE";
  44. /** @var kWeakPasswordErrorMessagePrefix
  45. @brief This is the prefix for the error message the server responds with if user's new password
  46. to be set is too weak.
  47. */
  48. static NSString *const kWeakPasswordErrorMessagePrefix = @"WEAK_PASSWORD : ";
  49. /** @var kTestOOBCode
  50. @brief Fake OOBCode used for testing.
  51. */
  52. static NSString *const kTestOOBCode = @"OOBCode";
  53. /** @var kTestNewPassword
  54. @brief Fake new password used for testing.
  55. */
  56. static NSString *const kTestNewPassword = @"newPassword";
  57. /** @var kEmailKey
  58. @brief The key for the email returned in the response.
  59. */
  60. static NSString *const kEmailKey = @"email";
  61. /** @var kRequestTypeKey
  62. @brief The key for the request type returned in the response.
  63. */
  64. static NSString *const kRequestTypeKey = @"requestType";
  65. /** @var kTestEmail
  66. @brief The email returned in the response.
  67. */
  68. static NSString *const kTestEmail = @"test@email.com";
  69. /** @var kResetPasswordExpectedRequestType.
  70. @brief The expected request type returned for reset password request.
  71. */
  72. static NSString *const kExpectedResetPasswordRequestType = @"PASSWORD_RESET";
  73. /** @class FIRResetPasswordRequestTests
  74. @brief Tests for @c FIRResetPasswordRequest.
  75. */
  76. @interface FIRResetPasswordResponseTests : XCTestCase
  77. @end
  78. @implementation FIRResetPasswordResponseTests {
  79. /** @var _RPCIssuer
  80. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  81. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  82. */
  83. FIRFakeBackendRPCIssuer *_RPCIssuer;
  84. }
  85. - (void)setUp {
  86. [super setUp];
  87. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  88. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  89. _RPCIssuer = RPCIssuer;
  90. }
  91. - (void)tearDown {
  92. _RPCIssuer = nil;
  93. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  94. [super tearDown];
  95. }
  96. /** @fn testUserDisabledError
  97. @brief Tests for @c FIRAuthErrorCodeUserDisabled.
  98. */
  99. - (void)testUserDisabledError {
  100. FIRResetPasswordRequest *request =
  101. [[FIRResetPasswordRequest alloc] initWithAPIKey:kTestAPIKey
  102. oobCode:kTestOOBCode
  103. newPassword:kTestNewPassword];
  104. __block BOOL callbackInvoked;
  105. __block FIRResetPasswordResponse *RPCResponse;
  106. __block NSError *RPCError;
  107. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  108. NSError *_Nullable error) {
  109. RPCResponse = response;
  110. RPCError = error;
  111. callbackInvoked = YES;
  112. }];
  113. [_RPCIssuer respondWithServerErrorMessage:kUserDisabledErrorMessage];
  114. XCTAssert(callbackInvoked);
  115. XCTAssertNil(RPCResponse);
  116. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeUserDisabled);
  117. }
  118. /** @fn testOperationNotAllowedError
  119. @brief Tests for @c FIRAuthErrorCodeOperationNotAllowed.
  120. */
  121. - (void)testOperationNotAllowedError {
  122. FIRResetPasswordRequest *request =
  123. [[FIRResetPasswordRequest alloc] initWithAPIKey:kTestAPIKey
  124. oobCode:kTestOOBCode
  125. newPassword:kTestNewPassword];
  126. __block BOOL callbackInvoked;
  127. __block FIRResetPasswordResponse *RPCResponse;
  128. __block NSError *RPCError;
  129. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  130. NSError *_Nullable error) {
  131. RPCResponse = response;
  132. RPCError = error;
  133. callbackInvoked = YES;
  134. }];
  135. [_RPCIssuer respondWithServerErrorMessage:kOperationNotAllowedErrorMessage];
  136. XCTAssert(callbackInvoked);
  137. XCTAssertNil(RPCResponse);
  138. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  139. }
  140. /** @fn testOOBExpiredError
  141. @brief Tests for @c FIRAuthErrorCodeExpiredActionCode.
  142. */
  143. - (void)testOOBExpiredError {
  144. FIRResetPasswordRequest *request =
  145. [[FIRResetPasswordRequest alloc] initWithAPIKey:kTestAPIKey
  146. oobCode:kTestOOBCode
  147. newPassword:kTestNewPassword];
  148. __block BOOL callbackInvoked;
  149. __block FIRResetPasswordResponse *RPCResponse;
  150. __block NSError *RPCError;
  151. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  152. NSError *_Nullable error) {
  153. RPCResponse = response;
  154. RPCError = error;
  155. callbackInvoked = YES;
  156. }];
  157. [_RPCIssuer respondWithServerErrorMessage:kExpiredActionCodeErrorMessage];
  158. XCTAssert(callbackInvoked);
  159. XCTAssertNil(RPCResponse);
  160. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeExpiredActionCode);
  161. }
  162. /** @fn testOOBInvalidError
  163. @brief Tests for @c FIRAuthErrorCodeInvalidActionCode.
  164. */
  165. - (void)testOOBInvalidError {
  166. FIRResetPasswordRequest *request =
  167. [[FIRResetPasswordRequest alloc] initWithAPIKey:kTestAPIKey
  168. oobCode:kTestOOBCode
  169. newPassword:kTestNewPassword];
  170. __block BOOL callbackInvoked;
  171. __block FIRResetPasswordResponse *RPCResponse;
  172. __block NSError *RPCError;
  173. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  174. NSError *_Nullable error) {
  175. RPCResponse = response;
  176. RPCError = error;
  177. callbackInvoked = YES;
  178. }];
  179. [_RPCIssuer respondWithServerErrorMessage:kInvalidActionCodeErrorMessage];
  180. XCTAssert(callbackInvoked);
  181. XCTAssertNil(RPCResponse);
  182. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidActionCode);
  183. }
  184. /** @fn testWeakPasswordError
  185. @brief Tests for @c FIRAuthErrorCodeWeakPassword.
  186. */
  187. - (void)testWeakPasswordError {
  188. FIRResetPasswordRequest *request =
  189. [[FIRResetPasswordRequest alloc] initWithAPIKey:kTestAPIKey
  190. oobCode:kTestOOBCode
  191. newPassword:kTestNewPassword];
  192. __block BOOL callbackInvoked;
  193. __block FIRResetPasswordResponse *RPCResponse;
  194. __block NSError *RPCError;
  195. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  196. NSError *_Nullable error) {
  197. RPCResponse = response;
  198. RPCError = error;
  199. callbackInvoked = YES;
  200. }];
  201. [_RPCIssuer respondWithServerErrorMessage:kWeakPasswordErrorMessagePrefix];
  202. XCTAssert(callbackInvoked);
  203. XCTAssertNil(RPCResponse);
  204. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeWeakPassword);
  205. }
  206. /** @fn testSuccessfulResetPassword
  207. @brief Tests a successful reset password flow.
  208. */
  209. - (void)testSuccessfulResetPassword {
  210. FIRResetPasswordRequest *request =
  211. [[FIRResetPasswordRequest alloc] initWithAPIKey:kTestAPIKey
  212. oobCode:kTestOOBCode
  213. newPassword:kTestNewPassword];
  214. __block BOOL callbackInvoked;
  215. __block FIRResetPasswordResponse *RPCResponse;
  216. __block NSError *RPCError;
  217. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  218. NSError *_Nullable error) {
  219. RPCResponse = response;
  220. RPCError = error;
  221. callbackInvoked = YES;
  222. }];
  223. [_RPCIssuer respondWithJSON:@{
  224. kEmailKey : kTestEmail,
  225. kRequestTypeKey : kExpectedResetPasswordRequestType
  226. }];
  227. XCTAssert(callbackInvoked);
  228. XCTAssertEqualObjects(RPCResponse.email, kTestEmail);
  229. XCTAssertEqualObjects(RPCResponse.requestType, kExpectedResetPasswordRequestType);
  230. XCTAssertNil(RPCError);
  231. }
  232. @end