|
| 1 | +// |
| 2 | +// PhoneAuthSignIn.swift |
| 3 | +// SwiftApiTests |
| 4 | +// |
| 5 | +// Created by Srushti Vaidya on 12/08/24. |
| 6 | +// Copyright © 2024 Firebase. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import FirebaseAuth |
| 11 | +import XCTest |
| 12 | + |
| 13 | +class PhoneAuthTests: TestsBase { |
| 14 | + |
| 15 | + let kPhoneNumber = "+19999999999" |
| 16 | + // This test verification code is specified for the given test phone number in the developer console. |
| 17 | + let kVerificationCode = "777777" |
| 18 | + |
| 19 | + func testSignInWithPhoneNumber() throws { |
| 20 | + Auth.auth().settings?.isAppVerificationDisabledForTesting = true //toAdd |
| 21 | + let auth = Auth.auth() |
| 22 | + let expectation = self.expectation(description: "Sign in with phone number") |
| 23 | + |
| 24 | + //PhoneAuthProvider used to initiate the Verification process and obtain a verificationID. |
| 25 | + PhoneAuthProvider.provider().verifyPhoneNumber(kPhoneNumber, uiDelegate: nil) { verificationID, error in |
| 26 | + if let error{ |
| 27 | + XCTAssertNil(error, "Verification error should be nil") |
| 28 | + XCTAssertNotNil(verificationID, "Verification ID should not be nil") |
| 29 | + } |
| 30 | + |
| 31 | + // Create a credential using the test verification code. |
| 32 | + let credential = PhoneAuthProvider.provider().credential(withVerificationID: verificationID ?? "", verificationCode: self.kVerificationCode) |
| 33 | + //signs in using the credential and verifies that the user is signed in correctly by checking auth.currentUser. |
| 34 | + auth.signIn(with: credential) { authResult, error in |
| 35 | + if let error{ |
| 36 | + XCTAssertNil(error, "Sign in error should be nil") |
| 37 | + XCTAssertNotNil(authResult, "AuthResult should not be nil") |
| 38 | + XCTAssertEqual(auth.currentUser?.phoneNumber, self.kPhoneNumber, "Phone number does not match") |
| 39 | + } |
| 40 | + expectation.fulfill() |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + waitForExpectations(timeout: TestsBase.kExpectationsTimeout) |
| 45 | + //deleteCurrentUser() |
| 46 | + } |
| 47 | +} |
0 commit comments