EmailPasswordTests.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 Foundation
  18. import XCTest
  19. class EmailPasswordTests: TestsBase {
  20. /// ** The testing email address for testCreateAccountWithEmailAndPassword. */
  21. let kNewEmailToCreateUser = "user+email_new_user@example.com"
  22. /// ** The testing email address for testSignInExistingUserWithEmailAndPassword. */
  23. let kExistingEmailToSignIn = "user+email_existing_user@example.com"
  24. func testCreateAccountWithEmailAndPassword() async throws {
  25. let auth = Auth.auth()
  26. // Ensure the account that will be created does not already exist.
  27. let result = try? await auth.signIn(withEmail: kNewEmailToCreateUser, password: "password")
  28. try? await result?.user.delete()
  29. let expectation = self.expectation(description: "Created account with email and password.")
  30. auth.createUser(withEmail: kNewEmailToCreateUser, password: "password") { result, error in
  31. if let error {
  32. print("createUserWithEmail has error: \(error)")
  33. }
  34. expectation.fulfill()
  35. }
  36. await fulfillment(of: [expectation], timeout: TestsBase.kExpectationsTimeout)
  37. XCTAssertEqual(auth.currentUser?.email, kNewEmailToCreateUser, "Expected email doesn't match")
  38. try? await deleteCurrentUserAsync()
  39. }
  40. func testCreateAccountWithEmailAndPasswordAsync() async throws {
  41. let auth = Auth.auth()
  42. // Ensure the account that will be created does not already exist.
  43. let result = try? await auth.signIn(withEmail: kNewEmailToCreateUser, password: "password")
  44. try? await result?.user.delete()
  45. try await auth.createUser(withEmail: kNewEmailToCreateUser, password: "password")
  46. XCTAssertEqual(auth.currentUser?.email, kNewEmailToCreateUser, "Expected email doesn't match")
  47. try await deleteCurrentUserAsync()
  48. }
  49. func testSignInExistingUserWithEmailAndPassword() {
  50. let auth = Auth.auth()
  51. let expectation = self
  52. .expectation(description: "Signed in existing account with email and password.")
  53. auth.signIn(withEmail: kExistingEmailToSignIn, password: "password") { user, error in
  54. XCTAssertNil(error)
  55. XCTAssertEqual(auth.currentUser?.email,
  56. self.kExistingEmailToSignIn,
  57. "Signed user does not match request.")
  58. expectation.fulfill()
  59. }
  60. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  61. }
  62. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  63. func testSignInExistingUserWithEmailAndPasswordAsync() async throws {
  64. let auth = Auth.auth()
  65. try await auth.signIn(withEmail: kExistingEmailToSignIn, password: "password")
  66. XCTAssertEqual(auth.currentUser?.email,
  67. kExistingEmailToSignIn,
  68. "Signed user does not match request.")
  69. }
  70. }