FIRResetPasswordResponseTests.m 10 KB

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