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

rCE Unit Tests #13547

Open
wants to merge 6 commits into
base: phone-auth-login
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
17 changes: 9 additions & 8 deletions FirebaseAuth/Sources/Swift/AuthProvider/PhoneAuthProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ import Foundation
}

let recaptchaVerifier = AuthRecaptchaVerifier.shared(auth: auth)
try await recaptchaVerifier.retrieveRecaptchaConfig(forceRefresh: true)
try await recaptchaVerifier.retrieveRecaptchaConfig(forceRefresh: false)

switch recaptchaVerifier.enablementStatus(forProvider: .phone) {
case .off:
Expand Down Expand Up @@ -228,10 +228,10 @@ import Foundation
}
}

private func verifyClAndSendVerificationCodeWithRecaptcha(toPhoneNumber phoneNumber: String,
retryOnInvalidAppCredential: Bool,
uiDelegate: AuthUIDelegate?,
recaptchaVerifier: AuthRecaptchaVerifier) async throws
func verifyClAndSendVerificationCodeWithRecaptcha(toPhoneNumber phoneNumber: String,
retryOnInvalidAppCredential: Bool,
uiDelegate: AuthUIDelegate?,
recaptchaVerifier: AuthRecaptchaVerifier) async throws
-> String? {
let request = SendVerificationCodeRequest(phoneNumber: phoneNumber,
codeIdentity: CodeIdentity.empty,
Expand Down Expand Up @@ -260,9 +260,9 @@ import Foundation
/// - Parameter phoneNumber: The phone number to be verified.
/// - Parameter callback: The callback to be invoked on the global work queue when the flow is
/// finished.
private func verifyClAndSendVerificationCode(toPhoneNumber phoneNumber: String,
retryOnInvalidAppCredential: Bool,
uiDelegate: AuthUIDelegate?) async throws
func verifyClAndSendVerificationCode(toPhoneNumber phoneNumber: String,
retryOnInvalidAppCredential: Bool,
uiDelegate: AuthUIDelegate?) async throws
-> String? {
let codeIdentity = try await verifyClient(withUIDelegate: uiDelegate)
let request = SendVerificationCodeRequest(phoneNumber: phoneNumber,
Expand Down Expand Up @@ -420,6 +420,7 @@ import Foundation
}
throw AuthErrorUtils.unexpectedResponse(deserializedResponse: nil, underlyingError: error)
}
print(error)
pragatimodi marked this conversation as resolved.
Show resolved Hide resolved
throw error
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private let kRecaptchaVersion = "recaptchaVersion"
private let kTenantIDKey = "tenantId"

/// A verification code can be an appCredential or a reCaptcha Token
enum CodeIdentity {
enum CodeIdentity: Equatable {
case credential(AuthAppCredential)
case recaptcha(String)
case empty
Expand Down
4 changes: 4 additions & 0 deletions FirebaseAuth/Sources/Swift/Utilities/AuthErrorUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ class AuthErrorUtils: NSObject {
error(code: .missingAndroidPackageName, message: message)
}

static func invalidRecaptchaTokenError() -> Error {
error(code: .invalidRecaptchaToken)
}

static func unauthorizedDomainError(message: String?) -> Error {
error(code: .unauthorizedDomain, message: message)
}
Expand Down
10 changes: 7 additions & 3 deletions FirebaseAuth/Sources/Swift/Utilities/AuthRecaptchaVerifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@
private(set) var agentConfig: AuthRecaptchaConfig?
private(set) var tenantConfigs: [String: AuthRecaptchaConfig] = [:]
private(set) var recaptchaClient: RCARecaptchaClientProtocol?

private static let _shared = AuthRecaptchaVerifier()
private static var _shared = AuthRecaptchaVerifier()
private let kRecaptchaVersion = "RECAPTCHA_ENTERPRISE"
private init() {}
init() {}

class func shared(auth: Auth?) -> AuthRecaptchaVerifier {
if _shared.auth != auth {
Expand All @@ -86,6 +85,11 @@
return _shared
}

class func setShared(_ instance: AuthRecaptchaVerifier, auth: Auth?) {
_shared = instance
_ = shared(auth: auth)
}

func siteKey() -> String? {
if let tenantID = auth?.tenantID {
if let config = tenantConfigs[tenantID] {
Expand Down
150 changes: 147 additions & 3 deletions FirebaseAuth/Tests/Unit/PhoneAuthProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,116 @@
try await internalTestVerify(function: #function)
}

/**
@fn testVerifyPhoneNumberWithRceEnforce
@brief Tests a successful invocation of @c verifyPhoneNumber with recaptcha enterprise enforced
*/
func testVerifyPhoneNumberWithRceEnforceSuccess() async throws {
initApp(#function)
let auth = try XCTUnwrap(PhoneAuthProviderTests.auth)
// TODO: Figure out how to mock objective C's FIRRecaptchaGetToken response
let provider = PhoneAuthProvider.provider(auth: auth)
let mockVerifier = FakeAuthRecaptchaVerifier(captchaResponse: kCaptchaResponse)
AuthRecaptchaVerifier.setShared(mockVerifier, auth: auth)
rpcIssuer.rceMode = "ENFORCE"
let requestExpectation = expectation(description: "verifyRequester")
rpcIssuer?.verifyRequester = { request in
XCTAssertEqual(request.phoneNumber, self.kTestPhoneNumber)
XCTAssertEqual(request.captchaResponse, self.kCaptchaResponse)
XCTAssertEqual(request.recaptchaVersion, "RECAPTCHA_ENTERPRISE")
XCTAssertEqual(request.codeIdentity, CodeIdentity.empty)
do {
try self.rpcIssuer?
.respond(withJSON: [self.kVerificationIDKey: self.kTestVerificationID])
} catch {
XCTFail("Failure sending response: \(error)")
}
}
requestExpectation.fulfill()
pragatimodi marked this conversation as resolved.
Show resolved Hide resolved
do {
let result = try await provider.verifyClAndSendVerificationCodeWithRecaptcha(
toPhoneNumber: kTestPhoneNumber,
retryOnInvalidAppCredential: false,
uiDelegate: nil,
recaptchaVerifier: mockVerifier
)
XCTAssertEqual(result, kTestVerificationID)
} catch {
XCTFail("Unexpected error")
}
await fulfillment(of: [requestExpectation], timeout: 5.0)
}

/**
@fn testVerifyPhoneNumberWithRceEnforceInvalidRecaptcha
@brief Tests a successful invocation of @c verifyPhoneNumber with recaptcha enterprise enforced
*/
func testVerifyPhoneNumberWithRceEnforceInvalidRecaptcha() async throws {
initApp(#function)
let auth = try XCTUnwrap(PhoneAuthProviderTests.auth)
// TODO: Figure out how to mock objective C's FIRRecaptchaGetToken response
let provider = PhoneAuthProvider.provider(auth: auth)
let mockVerifier = FakeAuthRecaptchaVerifier()
AuthRecaptchaVerifier.setShared(mockVerifier, auth: auth)
rpcIssuer.rceMode = "ENFORCE"
let requestExpectation = expectation(description: "verifyRequester")
rpcIssuer?.verifyRequester = { request in
XCTAssertEqual(request.phoneNumber, self.kTestPhoneNumber)
XCTAssertEqual(request.captchaResponse, "NO_RECAPTCHA")
XCTAssertEqual(request.recaptchaVersion, "RECAPTCHA_ENTERPRISE")
XCTAssertEqual(request.codeIdentity, CodeIdentity.empty)
do {
try self.rpcIssuer?
.respond(
serverErrorMessage: "INVALID_RECAPTCHA_TOKEN",
error: AuthErrorUtils.invalidRecaptchaTokenError() as NSError
)
} catch {
XCTFail("Failure sending response: \(error)")
}
}
requestExpectation.fulfill()
pragatimodi marked this conversation as resolved.
Show resolved Hide resolved
do {
_ = try await provider.verifyClAndSendVerificationCodeWithRecaptcha(
toPhoneNumber: kTestPhoneNumber,
retryOnInvalidAppCredential: false,
uiDelegate: nil,
recaptchaVerifier: mockVerifier
)
// XCTAssertEqual(result, kTestVerificationID)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very minor: we can remove this commented line.

} catch {
// Traverse the nested error to find the root cause
let underlyingError = (error as NSError).userInfo[NSUnderlyingErrorKey] as? NSError
let rootError = underlyingError?.userInfo[NSUnderlyingErrorKey] as? NSError

// Compare the root error code to the expected error code
XCTAssertEqual(rootError?.code, AuthErrorCode.invalidRecaptchaToken.code.rawValue)
}
await fulfillment(of: [requestExpectation], timeout: 5.0)
}

/**
@fn testVerifyPhoneNumberWithRceEnforceSDKNotLinked
@brief Tests a successful invocation of @c verifyPhoneNumber with recaptcha enterprise enforced
*/
func testVerifyPhoneNumberWithRceEnforceRecaptchaSDKNotLinked() async throws {
return try await testRecaptchaFlowError(
function: #function,
rceError: AuthErrorUtils.recaptchaSDKNotLinkedError()
)
}

/**
@fn testVerifyPhoneNumberWithRceEnforceSDKNotLinked
@brief Tests a successful invocation of @c verifyPhoneNumber with recaptcha enterprise enforced
*/
func testVerifyPhoneNumberWithRceEnforceRecaptchaActionCreationFailed() async throws {
return try await testRecaptchaFlowError(
function: #function,
rceError: AuthErrorUtils.recaptchaActionCreationFailed()
)
}

/** @fn testVerifyPhoneNumberInTestMode
@brief Tests a successful invocation of @c verifyPhoneNumber:completion: when app verification
is disabled.
Expand Down Expand Up @@ -340,6 +450,27 @@
XCTAssertEqual(unarchivedCredential.provider, PhoneAuthProvider.id)
}

private func testRecaptchaFlowError(function: String, rceError: Error) async throws {
initApp(function)
let auth = try XCTUnwrap(PhoneAuthProviderTests.auth)
// TODO: Figure out how to mock objective C's FIRRecaptchaGetToken response
// Mocking the output of verify() method
let provider = PhoneAuthProvider.provider(auth: auth)
let mockVerifier = FakeAuthRecaptchaVerifier(error: rceError)
AuthRecaptchaVerifier.setShared(mockVerifier, auth: auth)
rpcIssuer.rceMode = "ENFORCE"
do {
let _ = try await provider.verifyClAndSendVerificationCodeWithRecaptcha(
toPhoneNumber: kTestPhoneNumber,
retryOnInvalidAppCredential: false,
uiDelegate: nil,
recaptchaVerifier: mockVerifier
)
} catch {
XCTAssertEqual((error as NSError).code, (rceError as NSError).code)
}
}

private func internalFlowRetry(function: String, goodRetry: Bool = false) throws {
let function = function
initApp(function, useClientID: true, fakeToken: true)
Expand Down Expand Up @@ -551,15 +682,19 @@
forwardingNotification: forwardingNotification)
let auth = try XCTUnwrap(PhoneAuthProviderTests.auth)
let provider = PhoneAuthProvider.provider(auth: auth)
var expectations: [XCTestExpectation] = []

if !reCAPTCHAfallback {
// Fake out appCredentialManager flow.
auth.appCredentialManager?.credential = AuthAppCredential(receipt: kTestReceipt,
secret: kTestSecret)
} else {
// 1. Intercept, handle, and test the projectConfiguration RPC calls.
let projectConfigExpectation = expectation(description: "projectConfiguration")
expectations.append(projectConfigExpectation)
rpcIssuer?.projectConfigRequester = { request in
XCTAssertEqual(request.apiKey, PhoneAuthProviderTests.kFakeAPIKey)
projectConfigExpectation.fulfill()
do {
// Response for the underlying VerifyClientRequest RPC call.
try self.rpcIssuer?.respond(
Expand All @@ -586,6 +721,8 @@
)
}
if errorURLString == nil, presenterError == nil {
let requestExpectation = expectation(description: "verifyRequester")
expectations.append(requestExpectation)
rpcIssuer?.verifyRequester = { request in
XCTAssertEqual(request.phoneNumber, self.kTestPhoneNumber)
switch request.codeIdentity {
Expand All @@ -599,6 +736,7 @@
case .empty:
XCTAssertTrue(testMode)
}
requestExpectation.fulfill()
do {
// Response for the underlying SendVerificationCode RPC call.
if let errorString {
Expand Down Expand Up @@ -627,6 +765,7 @@
// expected value
XCTAssertEqual((error as NSError).code, errorCode)
}
await fulfillment(of: expectations, timeout: 5.0)
}

private func initApp(_ functionName: String,
Expand Down Expand Up @@ -680,11 +819,16 @@
}

class FakeAuthRecaptchaVerifier: AuthRecaptchaVerifier {
var captchaResponse: String = "captchaResponse"
var fakeError: Error?
var captchaResponse: String
var error: Error?
init(captchaResponse: String? = nil, error: Error? = nil) {
self.captchaResponse = captchaResponse ?? "NO_RECAPTCHA"
self.error = error
super.init()
}

override func verify(forceRefresh: Bool, action: AuthRecaptchaAction) async throws -> String {
if let error = fakeError {
if let error = error {
throw error
}
return captchaResponse
Expand Down
Loading