Skip to content

Commit 6058a7c

Browse files
committed
adding unit test for audit mode
1 parent f46582d commit 6058a7c

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

FirebaseAuth/Tests/Unit/PhoneAuthProviderTests.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,86 @@
208208
)
209209
}
210210

211+
/// @fn testVerifyPhoneNumberWithRceAudit
212+
/// @brief Tests a successful invocation of @c verifyPhoneNumber with recaptcha enterprise in
213+
/// audit mode
214+
func testVerifyPhoneNumberWithRceAuditSuccess() async throws {
215+
initApp(#function)
216+
let auth = try XCTUnwrap(PhoneAuthProviderTests.auth)
217+
let provider = PhoneAuthProvider.provider(auth: auth)
218+
let mockVerifier = FakeAuthRecaptchaVerifier(captchaResponse: kCaptchaResponse)
219+
AuthRecaptchaVerifier.setShared(mockVerifier, auth: auth)
220+
rpcIssuer.rceMode = "AUDIT"
221+
let requestExpectation = expectation(description: "verifyRequester")
222+
rpcIssuer?.verifyRequester = { request in
223+
XCTAssertEqual(request.phoneNumber, self.kTestPhoneNumber)
224+
XCTAssertEqual(request.captchaResponse, self.kCaptchaResponse)
225+
XCTAssertEqual(request.recaptchaVersion, "RECAPTCHA_ENTERPRISE")
226+
XCTAssertEqual(request.codeIdentity, CodeIdentity.empty)
227+
requestExpectation.fulfill()
228+
do {
229+
try self.rpcIssuer?
230+
.respond(withJSON: [self.kVerificationIDKey: self.kTestVerificationID])
231+
} catch {
232+
XCTFail("Failure sending response: \(error)")
233+
}
234+
}
235+
do {
236+
let result = try await provider.verifyClAndSendVerificationCodeWithRecaptcha(
237+
toPhoneNumber: kTestPhoneNumber,
238+
retryOnInvalidAppCredential: false,
239+
uiDelegate: nil,
240+
recaptchaVerifier: mockVerifier
241+
)
242+
XCTAssertEqual(result, kTestVerificationID)
243+
} catch {
244+
XCTFail("Unexpected error")
245+
}
246+
await fulfillment(of: [requestExpectation], timeout: 5.0)
247+
}
248+
249+
/// @fn testVerifyPhoneNumberWithRceAuditInvalidRecaptcha
250+
/// @brief Tests a successful invocation of @c verifyPhoneNumber with recaptcha enterprise in
251+
/// audit mode
252+
func testVerifyPhoneNumberWithRceAuditInvalidRecaptcha() async throws {
253+
initApp(#function)
254+
let auth = try XCTUnwrap(PhoneAuthProviderTests.auth)
255+
let provider = PhoneAuthProvider.provider(auth: auth)
256+
let mockVerifier = FakeAuthRecaptchaVerifier()
257+
AuthRecaptchaVerifier.setShared(mockVerifier, auth: auth)
258+
rpcIssuer.rceMode = "AUDIT"
259+
let requestExpectation = expectation(description: "verifyRequester")
260+
rpcIssuer?.verifyRequester = { request in
261+
XCTAssertEqual(request.phoneNumber, self.kTestPhoneNumber)
262+
XCTAssertEqual(request.captchaResponse, "NO_RECAPTCHA")
263+
XCTAssertEqual(request.recaptchaVersion, "RECAPTCHA_ENTERPRISE")
264+
XCTAssertEqual(request.codeIdentity, CodeIdentity.empty)
265+
requestExpectation.fulfill()
266+
do {
267+
try self.rpcIssuer?
268+
.respond(
269+
serverErrorMessage: "INVALID_RECAPTCHA_TOKEN",
270+
error: AuthErrorUtils.invalidRecaptchaTokenError() as NSError
271+
)
272+
} catch {
273+
XCTFail("Failure sending response: \(error)")
274+
}
275+
}
276+
do {
277+
_ = try await provider.verifyClAndSendVerificationCodeWithRecaptcha(
278+
toPhoneNumber: kTestPhoneNumber,
279+
retryOnInvalidAppCredential: false,
280+
uiDelegate: nil,
281+
recaptchaVerifier: mockVerifier
282+
)
283+
} catch {
284+
let underlyingError = (error as NSError).userInfo[NSUnderlyingErrorKey] as? NSError
285+
let rootError = underlyingError?.userInfo[NSUnderlyingErrorKey] as? NSError
286+
XCTAssertEqual(rootError?.code, AuthErrorCode.invalidRecaptchaToken.code.rawValue)
287+
}
288+
await fulfillment(of: [requestExpectation], timeout: 5.0)
289+
}
290+
211291
/** @fn testVerifyPhoneNumberInTestMode
212292
@brief Tests a successful invocation of @c verifyPhoneNumber:completion: when app verification
213293
is disabled.

0 commit comments

Comments
 (0)