FIRResetPasswordResponseTests.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. /** @var _requestConfiguration
  85. @brief This is the request configuration used for testing.
  86. */
  87. FIRAuthRequestConfiguration *_requestConfiguration;
  88. }
  89. - (void)setUp {
  90. [super setUp];
  91. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  92. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  93. _RPCIssuer = RPCIssuer;
  94. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  95. }
  96. - (void)tearDown {
  97. _requestConfiguration = nil;
  98. _RPCIssuer = nil;
  99. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  100. [super tearDown];
  101. }
  102. /** @fn testUserDisabledError
  103. @brief Tests for @c FIRAuthErrorCodeUserDisabled.
  104. */
  105. - (void)testUserDisabledError {
  106. FIRResetPasswordRequest *request =
  107. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  108. newPassword:kTestNewPassword
  109. requestConfiguration:_requestConfiguration];
  110. __block BOOL callbackInvoked;
  111. __block FIRResetPasswordResponse *RPCResponse;
  112. __block NSError *RPCError;
  113. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  114. NSError *_Nullable error) {
  115. RPCResponse = response;
  116. RPCError = error;
  117. callbackInvoked = YES;
  118. }];
  119. [_RPCIssuer respondWithServerErrorMessage:kUserDisabledErrorMessage];
  120. XCTAssert(callbackInvoked);
  121. XCTAssertNil(RPCResponse);
  122. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeUserDisabled);
  123. }
  124. /** @fn testOperationNotAllowedError
  125. @brief Tests for @c FIRAuthErrorCodeOperationNotAllowed.
  126. */
  127. - (void)testOperationNotAllowedError {
  128. FIRResetPasswordRequest *request =
  129. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  130. newPassword:kTestNewPassword
  131. requestConfiguration:_requestConfiguration];
  132. __block BOOL callbackInvoked;
  133. __block FIRResetPasswordResponse *RPCResponse;
  134. __block NSError *RPCError;
  135. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  136. NSError *_Nullable error) {
  137. RPCResponse = response;
  138. RPCError = error;
  139. callbackInvoked = YES;
  140. }];
  141. [_RPCIssuer respondWithServerErrorMessage:kOperationNotAllowedErrorMessage];
  142. XCTAssert(callbackInvoked);
  143. XCTAssertNil(RPCResponse);
  144. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  145. }
  146. /** @fn testOOBExpiredError
  147. @brief Tests for @c FIRAuthErrorCodeExpiredActionCode.
  148. */
  149. - (void)testOOBExpiredError {
  150. FIRResetPasswordRequest *request =
  151. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  152. newPassword:kTestNewPassword
  153. requestConfiguration:_requestConfiguration];
  154. __block BOOL callbackInvoked;
  155. __block FIRResetPasswordResponse *RPCResponse;
  156. __block NSError *RPCError;
  157. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  158. NSError *_Nullable error) {
  159. RPCResponse = response;
  160. RPCError = error;
  161. callbackInvoked = YES;
  162. }];
  163. [_RPCIssuer respondWithServerErrorMessage:kExpiredActionCodeErrorMessage];
  164. XCTAssert(callbackInvoked);
  165. XCTAssertNil(RPCResponse);
  166. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeExpiredActionCode);
  167. }
  168. /** @fn testOOBInvalidError
  169. @brief Tests for @c FIRAuthErrorCodeInvalidActionCode.
  170. */
  171. - (void)testOOBInvalidError {
  172. FIRResetPasswordRequest *request =
  173. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  174. newPassword:kTestNewPassword
  175. requestConfiguration:_requestConfiguration];
  176. __block BOOL callbackInvoked;
  177. __block FIRResetPasswordResponse *RPCResponse;
  178. __block NSError *RPCError;
  179. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  180. NSError *_Nullable error) {
  181. RPCResponse = response;
  182. RPCError = error;
  183. callbackInvoked = YES;
  184. }];
  185. [_RPCIssuer respondWithServerErrorMessage:kInvalidActionCodeErrorMessage];
  186. XCTAssert(callbackInvoked);
  187. XCTAssertNil(RPCResponse);
  188. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidActionCode);
  189. }
  190. /** @fn testWeakPasswordError
  191. @brief Tests for @c FIRAuthErrorCodeWeakPassword.
  192. */
  193. - (void)testWeakPasswordError {
  194. FIRResetPasswordRequest *request =
  195. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  196. newPassword:kTestNewPassword
  197. requestConfiguration:_requestConfiguration];
  198. __block BOOL callbackInvoked;
  199. __block FIRResetPasswordResponse *RPCResponse;
  200. __block NSError *RPCError;
  201. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  202. NSError *_Nullable error) {
  203. RPCResponse = response;
  204. RPCError = error;
  205. callbackInvoked = YES;
  206. }];
  207. [_RPCIssuer respondWithServerErrorMessage:kWeakPasswordErrorMessagePrefix];
  208. XCTAssert(callbackInvoked);
  209. XCTAssertNil(RPCResponse);
  210. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeWeakPassword);
  211. }
  212. /** @fn testSuccessfulResetPassword
  213. @brief Tests a successful reset password flow.
  214. */
  215. - (void)testSuccessfulResetPassword {
  216. FIRResetPasswordRequest *request =
  217. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  218. newPassword:kTestNewPassword
  219. requestConfiguration:_requestConfiguration];
  220. __block BOOL callbackInvoked;
  221. __block FIRResetPasswordResponse *RPCResponse;
  222. __block NSError *RPCError;
  223. [FIRAuthBackend resetPassword:request callback:^(FIRResetPasswordResponse *_Nullable response,
  224. NSError *_Nullable error) {
  225. RPCResponse = response;
  226. RPCError = error;
  227. callbackInvoked = YES;
  228. }];
  229. [_RPCIssuer respondWithJSON:@{
  230. kEmailKey : kTestEmail,
  231. kRequestTypeKey : kExpectedResetPasswordRequestType
  232. }];
  233. XCTAssert(callbackInvoked);
  234. XCTAssertEqualObjects(RPCResponse.email, kTestEmail);
  235. XCTAssertEqualObjects(RPCResponse.requestType, kExpectedResetPasswordRequestType);
  236. XCTAssertNil(RPCError);
  237. }
  238. @end