PhoneAuthTests.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2024 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 PhoneAuthTests: TestsBase {
  20. let phoneNumber = "+12345678910"
  21. // This test verification code is specified for the given test phone number in the developer
  22. // console.
  23. let verificationCode = "123456"
  24. func testSignInWithPhoneNumber() throws {
  25. Auth.auth().settings?.isAppVerificationDisabledForTesting = true
  26. let auth = Auth.auth()
  27. let expectation = self.expectation(description: "Sign in with phone number")
  28. // PhoneAuthProvider used to initiate the Verification process and obtain a verificationID.
  29. PhoneAuthProvider.provider()
  30. .verifyPhoneNumber(phoneNumber, uiDelegate: nil) { verificationID, error in
  31. if let error {
  32. XCTAssertNil(error, "Verification error should be nil")
  33. XCTAssertNotNil(verificationID, "Verification ID should not be nil")
  34. }
  35. // Create a credential using the test verification code.
  36. let credential = PhoneAuthProvider.provider().credential(
  37. withVerificationID: verificationID ?? "",
  38. verificationCode: self.verificationCode
  39. )
  40. // Signs in using the credential and verifies that the user is signed in correctly by
  41. // checking auth.currentUser.
  42. auth.signIn(with: credential) { authResult, error in
  43. if let error {
  44. XCTAssertNil(error, "Sign in error should be nil")
  45. XCTAssertNotNil(authResult, "AuthResult should not be nil")
  46. XCTAssertEqual(
  47. auth.currentUser?.phoneNumber,
  48. self.phoneNumber,
  49. "Phone number does not match"
  50. )
  51. }
  52. expectation.fulfill()
  53. }
  54. }
  55. waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  56. }
  57. func testSignInWithPhoneNumberAsync_Success() async throws {
  58. Auth.auth().settings?.isAppVerificationDisabledForTesting = true
  59. let auth = Auth.auth()
  60. // Start phone number verification
  61. let verificationID = try await PhoneAuthProvider.provider().verifyPhoneNumber(
  62. phoneNumber,
  63. uiDelegate: nil
  64. )
  65. XCTAssertNotNil(verificationID, "Expected a verification ID")
  66. // Create the phone auth credential
  67. let credential = PhoneAuthProvider.provider().credential(
  68. withVerificationID: verificationID,
  69. verificationCode: verificationCode
  70. )
  71. // Sign in with the credential
  72. let authResult = try await auth.signIn(with: credential)
  73. XCTAssertNotNil(authResult, "Expected a non-nil AuthResult")
  74. XCTAssertEqual(auth.currentUser?.phoneNumber, phoneNumber, "Phone number does not match")
  75. }
  76. }