DeleteAccountTests.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 DeleteAccountTests: RPCBaseTests {
  19. /** @var kLocalIDKey
  20. @brief The name of the "localID" property in the request.
  21. */
  22. let kLocalIDKey = "localId"
  23. /** @var kExpectedAPIURL
  24. @brief The expected URL for the test calls.
  25. */
  26. let kExpectedAPIURL =
  27. "https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key=APIKey"
  28. func testDeleteAccount() throws {
  29. let kUserDisabledErrorMessage = "USER_DISABLED"
  30. let kInvalidUserTokenErrorMessage = "INVALID_ID_TOKEN:"
  31. let kCredentialTooOldErrorMessage = "CREDENTIAL_TOO_OLD_LOGIN_AGAIN:"
  32. try checkRequest(
  33. request: makeDeleteAccountRequest(),
  34. expected: kExpectedAPIURL,
  35. key: kLocalIDKey,
  36. value: kLocalID
  37. )
  38. try checkBackendError(
  39. request: makeDeleteAccountRequest(),
  40. message: kUserDisabledErrorMessage,
  41. errorCode: AuthErrorCode.userDisabled
  42. )
  43. try checkBackendError(
  44. request: makeDeleteAccountRequest(),
  45. message: kInvalidUserTokenErrorMessage,
  46. errorCode: AuthErrorCode.invalidUserToken
  47. )
  48. try checkBackendError(
  49. request: makeDeleteAccountRequest(),
  50. message: kCredentialTooOldErrorMessage,
  51. errorCode: AuthErrorCode.requiresRecentLogin
  52. )
  53. }
  54. /** @fn testSuccessfulDeleteAccount
  55. @brief This test checks for a successful response
  56. */
  57. func testSuccessfulDeleteAccountResponse() throws {
  58. var callbackInvoked = false
  59. var rpcError: NSError?
  60. AuthBackend.post(with: makeDeleteAccountRequest()) { response, error in
  61. callbackInvoked = true
  62. rpcError = error as? NSError
  63. }
  64. _ = try rpcIssuer?.respond(withJSON: [:])
  65. XCTAssert(callbackInvoked)
  66. XCTAssertNil(rpcError)
  67. }
  68. private func makeDeleteAccountRequest() -> DeleteAccountRequest {
  69. return DeleteAccountRequest(localID: kLocalID,
  70. accessToken: "Access Token",
  71. requestConfiguration: makeRequestConfiguration())
  72. }
  73. }