ResetPasswordTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. import XCTest
  16. @testable import FirebaseAuth
  17. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  18. class ResetPasswordTests: RPCBaseTests {
  19. let kTestOOBCode = "OOBCode"
  20. let kTestNewPassword = "newPassword:-)"
  21. func testResetPasswordRequest() throws {
  22. let kOOBCodeKey = "oobCode"
  23. let kNewPasswordKey = "newPassword"
  24. let kExpectedAPIURL =
  25. "https://www.googleapis.com/identitytoolkit/v3/relyingparty/resetPassword?key=APIKey"
  26. let issuer = try checkRequest(
  27. request: makeResetPasswordRequest(),
  28. expected: kExpectedAPIURL,
  29. key: kNewPasswordKey,
  30. value: kTestNewPassword
  31. )
  32. let requestDictionary = try XCTUnwrap(issuer.decodedRequest as? [String: AnyHashable])
  33. XCTAssertEqual(requestDictionary[kOOBCodeKey], kTestOOBCode)
  34. }
  35. func testResetPasswordRequestErrors() throws {
  36. let kUserDisabledErrorMessage = "USER_DISABLED"
  37. let kOperationNotAllowedErrorMessage = "OPERATION_NOT_ALLOWED"
  38. let kExpiredActionCodeErrorMessage = "EXPIRED_OOB_CODE"
  39. let kInvalidActionCodeErrorMessage = "INVALID_OOB_CODE"
  40. let kWeakPasswordErrorMessagePrefix = "WEAK_PASSWORD : "
  41. try checkBackendError(
  42. request: makeResetPasswordRequest(),
  43. message: kUserDisabledErrorMessage,
  44. errorCode: AuthErrorCode.userDisabled
  45. )
  46. try checkBackendError(
  47. request: makeResetPasswordRequest(),
  48. message: kOperationNotAllowedErrorMessage,
  49. errorCode: AuthErrorCode.operationNotAllowed
  50. )
  51. try checkBackendError(
  52. request: makeResetPasswordRequest(),
  53. message: kExpiredActionCodeErrorMessage,
  54. errorCode: AuthErrorCode.expiredActionCode
  55. )
  56. try checkBackendError(
  57. request: makeResetPasswordRequest(),
  58. message: kInvalidActionCodeErrorMessage,
  59. errorCode: AuthErrorCode.invalidActionCode
  60. )
  61. try checkBackendError(
  62. request: makeResetPasswordRequest(),
  63. message: kWeakPasswordErrorMessagePrefix,
  64. errorCode: AuthErrorCode.weakPassword
  65. )
  66. }
  67. /** @fn testSuccessfulResetPassword
  68. @brief Tests a successful reset password flow.
  69. */
  70. func testSuccessfulResetPassword() throws {
  71. let kTestEmail = "test@email.com"
  72. let kExpectedResetPasswordRequestType = "PASSWORD_RESET"
  73. var callbackInvoked = false
  74. var rpcResponse: ResetPasswordResponse?
  75. var rpcError: NSError?
  76. AuthBackend.post(with: makeResetPasswordRequest()) { response, error in
  77. callbackInvoked = true
  78. rpcResponse = response
  79. rpcError = error as? NSError
  80. }
  81. _ = try rpcIssuer?.respond(withJSON: ["email": kTestEmail,
  82. "requestType": kExpectedResetPasswordRequestType])
  83. XCTAssert(callbackInvoked)
  84. XCTAssertNil(rpcError)
  85. XCTAssertEqual(rpcResponse?.email, kTestEmail)
  86. XCTAssertEqual(rpcResponse?.requestType, kExpectedResetPasswordRequestType)
  87. }
  88. private func makeResetPasswordRequest() -> ResetPasswordRequest {
  89. return ResetPasswordRequest(oobCode: kTestOOBCode, newPassword: kTestNewPassword,
  90. requestConfiguration: makeRequestConfiguration())
  91. }
  92. }