AccountInfoTests.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2020 Google LLC
  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 FirebaseAuth
  17. import XCTest
  18. class AccountInfoTests: TestsBase {
  19. /** The testing email address for testCreateAccountWithEmailAndPassword. */
  20. let kOldUserEmail = "user+user_old_email@example.com"
  21. /** The testing email address for testUpdatingUsersEmail. */
  22. let kNewUserEmail = "user+user_new_email@example.com"
  23. override func setUp() {
  24. let auth = Auth.auth()
  25. let expectation1 = expectation(description: "Created account with email and password.")
  26. auth.createUser(withEmail: kOldUserEmail, password: "password") { user, error in
  27. // Succeed whether or not the user already exists.
  28. expectation1.fulfill()
  29. }
  30. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  31. }
  32. override func tearDown() {
  33. // Clean up the created Firebase user for future runs.
  34. deleteCurrentUser()
  35. }
  36. func testUpdatingUsersEmail() {
  37. let auth = Auth.auth()
  38. let expectation1 = expectation(description: "Created account with email and password.")
  39. auth.createUser(withEmail: kOldUserEmail, password: "password") { user, error in
  40. if let error {
  41. XCTAssertEqual((error as NSError).code,
  42. AuthErrorCode.emailAlreadyInUse.rawValue,
  43. "Created a user despite it already exiting.")
  44. } else {
  45. XCTFail("Did not get error for recreating a user")
  46. }
  47. expectation1.fulfill()
  48. }
  49. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  50. let expectation2 = expectation(description: "Sign in with email and password.")
  51. auth.signIn(withEmail: kOldUserEmail, password: "password") { user, error in
  52. XCTAssertNil(error)
  53. XCTAssertEqual(auth.currentUser?.email,
  54. self.kOldUserEmail,
  55. "Signed user does not match request.")
  56. expectation2.fulfill()
  57. }
  58. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  59. }
  60. func testUpdatingUsersEmailAsync() async throws {
  61. let auth = Auth.auth()
  62. do {
  63. _ = try await auth.createUser(withEmail: kOldUserEmail, password: "password")
  64. XCTFail("Did not get error for recreating a user")
  65. } catch {
  66. XCTAssertEqual((error as NSError).code,
  67. AuthErrorCode.emailAlreadyInUse.rawValue,
  68. "Created a user despite it already exiting.")
  69. }
  70. let user = try await auth.signIn(withEmail: kOldUserEmail, password: "password")
  71. XCTAssertEqual(user.user.email, kOldUserEmail)
  72. XCTAssertEqual(auth.currentUser?.email,
  73. kOldUserEmail,
  74. "Signed user does not match request.")
  75. }
  76. }