AccountInfoTests.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = self.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. self.deleteCurrentUser()
  35. }
  36. func testUpdatingUsersEmail() {
  37. let auth = Auth.auth()
  38. let expectation1 = self.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. XCTAssertTrue(false, "Did not get error for recreating a user")
  46. }
  47. expectation1.fulfill()
  48. }
  49. waitForExpectations(timeout:TestsBase.kExpectationsTimeout)
  50. let expectation2 = self.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 = self.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. }