AccountInfoTests.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 XCTest
  17. import FirebaseAuth
  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 = 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. let expectation3 = expectation(description: "Update email address.")
  60. auth.currentUser?.updateEmail(to: kNewUserEmail) { error in
  61. XCTAssertNil(error)
  62. XCTAssertEqual(auth.currentUser?.email,
  63. self.kNewUserEmail,
  64. "Signed user does not match change.")
  65. expectation3.fulfill()
  66. }
  67. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  68. }
  69. #if compiler(>=5.5.2) && canImport(_Concurrency)
  70. @available(iOS 13, tvOS 13, macOS 10.15, watchOS 7, *)
  71. func testUpdatingUsersEmailAsync() async throws {
  72. let auth = Auth.auth()
  73. do {
  74. _ = try await auth.createUser(withEmail: kOldUserEmail, password: "password")
  75. XCTFail("Did not get error for recreating a user")
  76. } catch {
  77. XCTAssertEqual((error as NSError).code,
  78. AuthErrorCode.emailAlreadyInUse.rawValue,
  79. "Created a user despite it already exiting.")
  80. }
  81. let user = try await auth.signIn(withEmail: kOldUserEmail, password: "password")
  82. XCTAssertEqual(user.user.email, kOldUserEmail)
  83. XCTAssertEqual(auth.currentUser?.email,
  84. kOldUserEmail,
  85. "Signed user does not match request.")
  86. try await auth.currentUser?.updateEmail(to: kNewUserEmail)
  87. XCTAssertEqual(auth.currentUser?.email,
  88. kNewUserEmail,
  89. "Signed user does not match change.")
  90. }
  91. #endif
  92. }