Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Integration Test for Phone Number Authentication in Firebase #13538

Open
wants to merge 21 commits into
base: rce-phone-tests
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 43 additions & 46 deletions FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,23 @@ import Foundation
uiDelegate: AuthUIDelegate? = nil,
multiFactorSession: MultiFactorSession? = nil,
completion: ((_: String?, _: Error?) -> Void)?) {
Task {
do {
let verificationID = try await verifyPhoneNumber(
phoneNumber,
uiDelegate: uiDelegate,
multiFactorSession: multiFactorSession
)
await MainActor.run {
completion?(verificationID, nil)
}
} catch {
await MainActor.run {
completion?(nil, error)
guard AuthWebUtils.isCallbackSchemeRegistered(forCustomURLScheme: callbackScheme,
urlTypes: auth.mainBundleUrlTypes) else {
fatalError(
"Please register custom URL scheme \(callbackScheme) in the app's Info.plist file."
)
}
kAuthGlobalWorkQueue.async {
Task {
do {
let verificationID = try await self.internalVerify(
phoneNumber: phoneNumber,
uiDelegate: uiDelegate,
multiFactorSession: multiFactorSession
)
Auth.wrapMainAsync(callback: completion, withParam: verificationID, error: nil)
} catch {
Auth.wrapMainAsync(callback: completion, withParam: nil, error: error)
}
}
}
Expand All @@ -103,19 +107,16 @@ import Foundation
uiDelegate: AuthUIDelegate? = nil,
multiFactorSession: MultiFactorSession? = nil) async throws
-> String {
guard AuthWebUtils.isCallbackSchemeRegistered(forCustomURLScheme: callbackScheme,
urlTypes: auth.mainBundleUrlTypes) else {
fatalError(
"Please register custom URL scheme \(callbackScheme) in the app's Info.plist file."
)
}

if let verificationID = try await internalVerify(phoneNumber: phoneNumber,
uiDelegate: uiDelegate,
multiFactorSession: multiFactorSession) {
return verificationID
} else {
throw AuthErrorUtils.invalidVerificationIDError(message: "Invalid verification ID")
return try await withCheckedThrowingContinuation { continuation in
self.verifyPhoneNumber(phoneNumber,
uiDelegate: uiDelegate,
multiFactorSession: multiFactorSession) { result, error in
if let error {
continuation.resume(throwing: error)
} else if let result {
continuation.resume(returning: result)
}
}
}
}

Expand All @@ -132,22 +133,11 @@ import Foundation
uiDelegate: AuthUIDelegate? = nil,
multiFactorSession: MultiFactorSession?,
completion: ((_: String?, _: Error?) -> Void)?) {
Task {
do {
let verificationID = try await verifyPhoneNumber(
with: multiFactorInfo,
uiDelegate: uiDelegate,
multiFactorSession: multiFactorSession
)
await MainActor.run {
completion?(verificationID, nil)
}
} catch {
await MainActor.run {
completion?(nil, error)
}
}
}
multiFactorSession?.multiFactorInfo = multiFactorInfo
verifyPhoneNumber(multiFactorInfo.phoneNumber,
uiDelegate: uiDelegate,
multiFactorSession: multiFactorSession,
completion: completion)
}

/// Verify ownership of the second factor phone number by the current user.
Expand All @@ -162,10 +152,17 @@ import Foundation
open func verifyPhoneNumber(with multiFactorInfo: PhoneMultiFactorInfo,
uiDelegate: AuthUIDelegate? = nil,
multiFactorSession: MultiFactorSession?) async throws -> String {
multiFactorSession?.multiFactorInfo = multiFactorInfo
return try await verifyPhoneNumber(multiFactorInfo.phoneNumber,
uiDelegate: uiDelegate,
multiFactorSession: multiFactorSession)
return try await withCheckedThrowingContinuation { continuation in
self.verifyPhoneNumber(with: multiFactorInfo,
uiDelegate: uiDelegate,
multiFactorSession: multiFactorSession) { result, error in
if let error {
continuation.resume(throwing: error)
} else if let result {
continuation.resume(returning: result)
}
}
}
}

/// Creates an `AuthCredential` for the phone number provider identified by the
Expand Down
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
B8F672CF2C805B5300DFDFD4 /* PhoneAuthTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8F672CE2C805B5200DFDFD4 /* PhoneAuthTest.swift */; };
pragatimodi marked this conversation as resolved.
Show resolved Hide resolved
8848765129D314A400780FA6 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 8848764F29D3149200780FA6 /* GoogleService-Info.plist */; };
DE8B636F2BEC2DC300607B82 /* FirebaseAuth in Frameworks */ = {isa = PBXBuildFile; productRef = DE8B636E2BEC2DC300607B82 /* FirebaseAuth */; };
DE8B63722BEC2FB900607B82 /* GoogleSignIn in Frameworks */ = {isa = PBXBuildFile; productRef = DE8B63712BEC2FB900607B82 /* GoogleSignIn */; };
Expand Down Expand Up @@ -87,6 +88,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
B8F672CE2C805B5200DFDFD4 /* PhoneAuthTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhoneAuthTest.swift; sourceTree = "<group>"; };
8848764F29D3149200780FA6 /* GoogleService-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = "GoogleService-Info.plist"; sourceTree = SOURCE_ROOT; };
DE4D8E1F2A8B0311001952B6 /* SwiftApplication.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = SwiftApplication.plist; sourceTree = "<group>"; };
DE8FD4682A7D660A00B6831A /* Credentials.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Credentials.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -326,6 +328,7 @@
EAE4CBDA24855E3E00245E92 /* SwiftApiTests */ = {
isa = PBXGroup;
children = (
B8F672CE2C805B5200DFDFD4 /* PhoneAuthTest.swift */,
DE8FD4912A7D9D9E00B6831A /* PhoneMultiFactorTests.swift */,
DE8FD4692A7D660A00B6831A /* AccountInfoTests.swift */,
DE8FD46E2A7D660B00B6831A /* AnonymousTests.swift */,
Expand Down Expand Up @@ -584,6 +587,7 @@
DE8FD4702A7D660B00B6831A /* AccountInfoTests.swift in Sources */,
DE8FD4942A7D9E2700B6831A /* PhoneMultiFactorTests.swift in Sources */,
DE8FD4742A7D660B00B6831A /* TestsBase.swift in Sources */,
B8F672CF2C805B5300DFDFD4 /* PhoneAuthTest.swift in Sources */,
DE8FD4712A7D660B00B6831A /* FacebookTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
86 changes: 86 additions & 0 deletions FirebaseAuth/Tests/SampleSwift/SwiftApiTests/PhoneAuthTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import FirebaseAuth
import Foundation
import XCTest

class PhoneAuthTests: TestsBase {
let phoneNumber = "+12345678910"
// This test verification code is specified for the given test phone number in the developer
// console.
let verificationCode = "123456"

func testSignInWithPhoneNumber() throws {
Auth.auth().settings?.isAppVerificationDisabledForTesting = true
let auth = Auth.auth()
let expectation = self.expectation(description: "Sign in with phone number")

// PhoneAuthProvider used to initiate the Verification process and obtain a verificationID.
PhoneAuthProvider.provider()
.verifyPhoneNumber(phoneNumber, uiDelegate: nil) { verificationID, error in
if let error {
XCTAssertNil(error, "Verification error should be nil")
XCTAssertNotNil(verificationID, "Verification ID should not be nil")
}

// Create a credential using the test verification code.
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationID ?? "",
verificationCode: self.verificationCode
)
// Signs in using the credential and verifies that the user is signed in correctly by
// checking auth.currentUser.
auth.signIn(with: credential) { authResult, error in
if let error {
XCTAssertNil(error, "Sign in error should be nil")
XCTAssertNotNil(authResult, "AuthResult should not be nil")
XCTAssertEqual(
auth.currentUser?.phoneNumber,
self.phoneNumber,
"Phone number does not match"
)
}
expectation.fulfill()
}
}

waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
}

func testSignInWithPhoneNumberAsync_Success() async throws {
Auth.auth().settings?.isAppVerificationDisabledForTesting = true
let auth = Auth.auth()

// Start phone number verification
let verificationID = try await PhoneAuthProvider.provider().verifyPhoneNumber(
phoneNumber,
uiDelegate: nil
)
XCTAssertNotNil(verificationID, "Expected a verification ID")

// Create the phone auth credential
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationID,
verificationCode: verificationCode
)

// Sign in with the credential
let authResult = try await auth.signIn(with: credential)
XCTAssertNotNil(authResult, "Expected a non-nil AuthResult")
XCTAssertEqual(auth.currentUser?.phoneNumber, phoneNumber, "Phone number does not match")
}
}
2 changes: 1 addition & 1 deletion FirebaseAuth/Tests/Unit/PhoneAuthProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@
}

/** @fn testVerifyClient
@brief Tests verifying client before sending verification code.
@brief Tests verifying client before sending verification code.
*/
private func internalTestVerify(errorString: String? = nil,
errorURLString: String? = nil,
Expand Down