FIRResetPasswordResponseTests.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 "FIRAuthBackend.h"
  18. #import "FIRAuthErrors.h"
  19. #import "FIRFakeBackendRPCIssuer.h"
  20. #import "FIRResetPasswordRequest.h"
  21. #import "FIRResetPasswordResponse.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
  114. resetPassword:request
  115. callback:^(FIRResetPasswordResponse *_Nullable response, NSError *_Nullable error) {
  116. RPCResponse = response;
  117. RPCError = error;
  118. callbackInvoked = YES;
  119. }];
  120. [_RPCIssuer respondWithServerErrorMessage:kUserDisabledErrorMessage];
  121. XCTAssert(callbackInvoked);
  122. XCTAssertNil(RPCResponse);
  123. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeUserDisabled);
  124. }
  125. /** @fn testOperationNotAllowedError
  126. @brief Tests for @c FIRAuthErrorCodeOperationNotAllowed.
  127. */
  128. - (void)testOperationNotAllowedError {
  129. FIRResetPasswordRequest *request =
  130. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  131. newPassword:kTestNewPassword
  132. requestConfiguration:_requestConfiguration];
  133. __block BOOL callbackInvoked;
  134. __block FIRResetPasswordResponse *RPCResponse;
  135. __block NSError *RPCError;
  136. [FIRAuthBackend
  137. resetPassword:request
  138. callback:^(FIRResetPasswordResponse *_Nullable response, NSError *_Nullable error) {
  139. RPCResponse = response;
  140. RPCError = error;
  141. callbackInvoked = YES;
  142. }];
  143. [_RPCIssuer respondWithServerErrorMessage:kOperationNotAllowedErrorMessage];
  144. XCTAssert(callbackInvoked);
  145. XCTAssertNil(RPCResponse);
  146. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  147. }
  148. /** @fn testOOBExpiredError
  149. @brief Tests for @c FIRAuthErrorCodeExpiredActionCode.
  150. */
  151. - (void)testOOBExpiredError {
  152. FIRResetPasswordRequest *request =
  153. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  154. newPassword:kTestNewPassword
  155. requestConfiguration:_requestConfiguration];
  156. __block BOOL callbackInvoked;
  157. __block FIRResetPasswordResponse *RPCResponse;
  158. __block NSError *RPCError;
  159. [FIRAuthBackend
  160. resetPassword:request
  161. callback:^(FIRResetPasswordResponse *_Nullable response, NSError *_Nullable error) {
  162. RPCResponse = response;
  163. RPCError = error;
  164. callbackInvoked = YES;
  165. }];
  166. [_RPCIssuer respondWithServerErrorMessage:kExpiredActionCodeErrorMessage];
  167. XCTAssert(callbackInvoked);
  168. XCTAssertNil(RPCResponse);
  169. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeExpiredActionCode);
  170. }
  171. /** @fn testOOBInvalidError
  172. @brief Tests for @c FIRAuthErrorCodeInvalidActionCode.
  173. */
  174. - (void)testOOBInvalidError {
  175. FIRResetPasswordRequest *request =
  176. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  177. newPassword:kTestNewPassword
  178. requestConfiguration:_requestConfiguration];
  179. __block BOOL callbackInvoked;
  180. __block FIRResetPasswordResponse *RPCResponse;
  181. __block NSError *RPCError;
  182. [FIRAuthBackend
  183. resetPassword:request
  184. callback:^(FIRResetPasswordResponse *_Nullable response, NSError *_Nullable error) {
  185. RPCResponse = response;
  186. RPCError = error;
  187. callbackInvoked = YES;
  188. }];
  189. [_RPCIssuer respondWithServerErrorMessage:kInvalidActionCodeErrorMessage];
  190. XCTAssert(callbackInvoked);
  191. XCTAssertNil(RPCResponse);
  192. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidActionCode);
  193. }
  194. /** @fn testWeakPasswordError
  195. @brief Tests for @c FIRAuthErrorCodeWeakPassword.
  196. */
  197. - (void)testWeakPasswordError {
  198. FIRResetPasswordRequest *request =
  199. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  200. newPassword:kTestNewPassword
  201. requestConfiguration:_requestConfiguration];
  202. __block BOOL callbackInvoked;
  203. __block FIRResetPasswordResponse *RPCResponse;
  204. __block NSError *RPCError;
  205. [FIRAuthBackend
  206. resetPassword:request
  207. callback:^(FIRResetPasswordResponse *_Nullable response, NSError *_Nullable error) {
  208. RPCResponse = response;
  209. RPCError = error;
  210. callbackInvoked = YES;
  211. }];
  212. [_RPCIssuer respondWithServerErrorMessage:kWeakPasswordErrorMessagePrefix];
  213. XCTAssert(callbackInvoked);
  214. XCTAssertNil(RPCResponse);
  215. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeWeakPassword);
  216. }
  217. /** @fn testSuccessfulResetPassword
  218. @brief Tests a successful reset password flow.
  219. */
  220. - (void)testSuccessfulResetPassword {
  221. FIRResetPasswordRequest *request =
  222. [[FIRResetPasswordRequest alloc] initWithOobCode:kTestOOBCode
  223. newPassword:kTestNewPassword
  224. requestConfiguration:_requestConfiguration];
  225. __block BOOL callbackInvoked;
  226. __block FIRResetPasswordResponse *RPCResponse;
  227. __block NSError *RPCError;
  228. [FIRAuthBackend
  229. resetPassword:request
  230. callback:^(FIRResetPasswordResponse *_Nullable response, NSError *_Nullable error) {
  231. RPCResponse = response;
  232. RPCError = error;
  233. callbackInvoked = YES;
  234. }];
  235. [_RPCIssuer respondWithJSON:@{
  236. kEmailKey : kTestEmail,
  237. kRequestTypeKey : kExpectedResetPasswordRequestType
  238. }];
  239. XCTAssert(callbackInvoked);
  240. XCTAssertEqualObjects(RPCResponse.email, kTestEmail);
  241. XCTAssertEqualObjects(RPCResponse.requestType, kExpectedResetPasswordRequestType);
  242. XCTAssertNil(RPCError);
  243. }
  244. @end