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