ResetPasswordTests.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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() async throws {
  22. let kOOBCodeKey = "oobCode"
  23. let kNewPasswordKey = "newPassword"
  24. let kExpectedAPIURL =
  25. "https://www.googleapis.com/identitytoolkit/v3/relyingparty/resetPassword?key=APIKey"
  26. try await checkRequest(
  27. request: makeResetPasswordRequest(),
  28. expected: kExpectedAPIURL,
  29. key: kNewPasswordKey,
  30. value: kTestNewPassword
  31. )
  32. let requestDictionary = try XCTUnwrap(rpcIssuer.decodedRequest as? [String: AnyHashable])
  33. XCTAssertEqual(requestDictionary[kOOBCodeKey], kTestOOBCode)
  34. }
  35. func testResetPasswordRequestErrors() async 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 await checkBackendError(
  42. request: makeResetPasswordRequest(),
  43. message: kUserDisabledErrorMessage,
  44. errorCode: AuthErrorCode.userDisabled
  45. )
  46. try await checkBackendError(
  47. request: makeResetPasswordRequest(),
  48. message: kOperationNotAllowedErrorMessage,
  49. errorCode: AuthErrorCode.operationNotAllowed
  50. )
  51. try await checkBackendError(
  52. request: makeResetPasswordRequest(),
  53. message: kExpiredActionCodeErrorMessage,
  54. errorCode: AuthErrorCode.expiredActionCode
  55. )
  56. try await checkBackendError(
  57. request: makeResetPasswordRequest(),
  58. message: kInvalidActionCodeErrorMessage,
  59. errorCode: AuthErrorCode.invalidActionCode
  60. )
  61. try await 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() async throws {
  71. let kTestEmail = "test@email.com"
  72. let kExpectedResetPasswordRequestType = "PASSWORD_RESET"
  73. rpcIssuer.respondBlock = {
  74. try self.rpcIssuer?.respond(withJSON: ["email": kTestEmail,
  75. "requestType": kExpectedResetPasswordRequestType])
  76. }
  77. let rpcResponse = try await AuthBackend.call(with: makeResetPasswordRequest())
  78. XCTAssertEqual(rpcResponse.email, kTestEmail)
  79. XCTAssertEqual(rpcResponse.requestType, kExpectedResetPasswordRequestType)
  80. }
  81. private func makeResetPasswordRequest() -> ResetPasswordRequest {
  82. return ResetPasswordRequest(oobCode: kTestOOBCode, newPassword: kTestNewPassword,
  83. requestConfiguration: makeRequestConfiguration())
  84. }
  85. }