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

[auth-swift] Add six missing recaptcha unit tests #12089

Merged
merged 1 commit into from
Nov 10, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@
// Response's site key is of the format projects/<project-id>/keys/<site-key>'
guard let keys = response.recaptchaKey?.components(separatedBy: "/"),
keys.count == 4 else {
throw AuthErrorUtils.error(code: .recaptchaNotEnabled,
message: "Invalid siteKey")
throw AuthErrorUtils.error(code: .recaptchaNotEnabled, message: "Invalid siteKey")
}
let siteKey = keys[3]
var enablementStatus: [String: Bool] = [:]
Expand Down
265 changes: 265 additions & 0 deletions FirebaseAuth/Tests/Unit/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,117 @@ class AuthTests: RPCBaseTests {
XCTAssertNil(auth?.currentUser)
}

#if os(iOS)
/** @fn testCreateUserWithEmailPasswordWithRecaptchaVerificationSuccess
@brief Tests the flow of a successful @c createUserWithEmail:password:completion: call.
*/
func testCreateUserWithEmailPasswordWithRecaptchaVerificationSuccess() throws {
let expectation = self.expectation(description: #function)
let kTestRecaptchaKey = "projects/123/keys/456"
rpcIssuer.recaptchaSiteKey = kTestRecaptchaKey
setFakeSecureTokenService()
setFakeGetAccountProvider()

// 1. Setup respond block to test and fake send request.
rpcIssuer.respondBlock = {
// 2. Validate the created Request instance.
let request = try XCTUnwrap(self.rpcIssuer.request as? SignUpNewUserRequest)
XCTAssertEqual(request.apiKey, AuthTests.kFakeAPIKey)
XCTAssertEqual(request.email, self.kEmail)
XCTAssertEqual(request.password, self.kFakePassword)
XCTAssertTrue(request.returnSecureToken)

request.injectRecaptchaFields(recaptchaResponse: AuthTests.kFakeRecaptchaResponse,
recaptchaVersion: AuthTests.kFakeRecaptchaVersion)

// 3. Send the response from the fake backend.
try self.rpcIssuer.respond(withJSON: ["idToken": AuthTests.kAccessToken,
"email": self.kEmail,
"isNewUser": true,
"refreshToken": self.kRefreshToken])
}
try auth?.signOut()
auth?.createUser(withEmail: kEmail, password: kFakePassword) { authResult, error in
// 4. After the response triggers the callback, verify the returned result.
XCTAssertTrue(Thread.isMainThread)
self.assertUser(authResult?.user)
guard let userInfo = authResult?.additionalUserInfo else {
XCTFail("authResult.additionalUserInfo is missing")
return
}
XCTAssertTrue(userInfo.isNewUser)
XCTAssertNil(userInfo.username)
XCTAssertNil(userInfo.profile)
XCTAssertEqual(userInfo.providerID, EmailAuthProvider.id)
XCTAssertNil(error)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
assertUser(auth?.currentUser)
}

/** @fn testCreateUserWithEmailPasswordWithRecaptchaVerificationFallbackSuccess
@brief Tests the flow of a successful @c createUserWithEmail:password:completion: call.
*/
func testCreateUserWithEmailPasswordWithRecaptchaVerificationFallbackSuccess() throws {
let expectation = self.expectation(description: #function)
let kTestRecaptchaKey = "projects/123/keys/456"
rpcIssuer.recaptchaSiteKey = kTestRecaptchaKey
setFakeSecureTokenService()
setFakeGetAccountProvider()

// 1. Setup respond block to test and fake send request.
rpcIssuer.respondBlock = {
// 2. Validate the created Request instance.
let request = try XCTUnwrap(self.rpcIssuer.request as? SignUpNewUserRequest)
XCTAssertEqual(request.apiKey, AuthTests.kFakeAPIKey)
XCTAssertEqual(request.email, self.kEmail)
XCTAssertEqual(request.password, self.kFakePassword)
XCTAssertTrue(request.returnSecureToken)

request.injectRecaptchaFields(recaptchaResponse: AuthTests.kFakeRecaptchaResponse,
recaptchaVersion: AuthTests.kFakeRecaptchaVersion)

// 3. Send the response from the fake backend.
try self.rpcIssuer.respond(serverErrorMessage: "MISSING_RECAPTCHA_TOKEN")
}
rpcIssuer.nextRespondBlock = {
// 4. Validate again the created Request instance after the recaptcha retry.
let request = try XCTUnwrap(self.rpcIssuer.request as? SignUpNewUserRequest)
XCTAssertEqual(request.email, self.kEmail)
XCTAssertEqual(request.password, self.kFakePassword)
XCTAssertEqual(request.apiKey, AuthTests.kFakeAPIKey)
XCTAssertTrue(request.returnSecureToken)
request.injectRecaptchaFields(recaptchaResponse: AuthTests.kFakeRecaptchaResponse,
recaptchaVersion: AuthTests.kFakeRecaptchaVersion)
// 5. Send the response from the fake backend.
try self.rpcIssuer.respond(withJSON: ["idToken": AuthTests.kAccessToken,
"email": self.kEmail,
"isNewUser": true,
"refreshToken": self.kRefreshToken])
}

try auth?.signOut()
auth?.createUser(withEmail: kEmail, password: kFakePassword) { authResult, error in
// 4. After the response triggers the callback, verify the returned result.
XCTAssertTrue(Thread.isMainThread)
self.assertUser(authResult?.user)
guard let userInfo = authResult?.additionalUserInfo else {
XCTFail("authResult.additionalUserInfo is missing")
return
}
XCTAssertTrue(userInfo.isNewUser)
XCTAssertNil(userInfo.username)
XCTAssertNil(userInfo.profile)
XCTAssertEqual(userInfo.providerID, EmailAuthProvider.id)
XCTAssertNil(error)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
assertUser(auth?.currentUser)
}
#endif

/** @fn testCreateUserWithEmailPasswordSuccess
@brief Tests the flow of a successful @c createUserWithEmail:password:completion: call.
*/
Expand Down Expand Up @@ -1406,6 +1517,80 @@ class AuthTests: RPCBaseTests {
waitForExpectations(timeout: 5)
}

#if os(iOS)
/** @fn testSendPasswordResetEmailWithRecaptchaSuccess
@brief Tests the flow of a successful @c sendPasswordResetWithEmail:completion: call.
*/
func testSendPasswordResetEmailWithRecaptchaSuccess() throws {
let expectation = self.expectation(description: #function)
let kTestRecaptchaKey = "projects/123/keys/456"
rpcIssuer.recaptchaSiteKey = kTestRecaptchaKey

// 1. Setup respond block to test and fake send request.
rpcIssuer.respondBlock = {
// 2. Validate the created Request instance.
let request = try XCTUnwrap(self.rpcIssuer.request as? GetOOBConfirmationCodeRequest)
XCTAssertEqual(request.email, self.kEmail)
XCTAssertEqual(request.apiKey, AuthTests.kFakeAPIKey)
request.injectRecaptchaFields(recaptchaResponse: AuthTests.kFakeRecaptchaResponse,
recaptchaVersion: AuthTests.kFakeRecaptchaVersion)

// 3. Send the response from the fake backend.
_ = try self.rpcIssuer.respond(withJSON: [:])
}
auth?.sendPasswordReset(withEmail: kEmail) { error in
// 4. After the response triggers the callback, verify success.
XCTAssertTrue(Thread.isMainThread)
XCTAssertNil(error)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}

/** @fn testSendPasswordResetEmailWithRecaptchaFallbackSuccess
@brief Tests the flow of a successful @c sendPasswordResetWithEmail:completion: call.
*/
func testSendPasswordResetEmailWithRecaptchaFallbackSuccess() throws {
let expectation = self.expectation(description: #function)
let kTestRecaptchaKey = "projects/123/keys/456"
rpcIssuer.recaptchaSiteKey = kTestRecaptchaKey

// 1. Setup respond block to test and fake send request.
rpcIssuer.respondBlock = {
// 2. Validate the created Request instance.
let request = try XCTUnwrap(self.rpcIssuer.request as? GetOOBConfirmationCodeRequest)
XCTAssertEqual(request.email, self.kEmail)
XCTAssertEqual(request.apiKey, AuthTests.kFakeAPIKey)
request.injectRecaptchaFields(recaptchaResponse: AuthTests.kFakeRecaptchaResponse,
recaptchaVersion: AuthTests.kFakeRecaptchaVersion)

// 3. Send the response from the fake backend.
try self.rpcIssuer.respond(serverErrorMessage: "MISSING_RECAPTCHA_TOKEN")
}
rpcIssuer.nextRespondBlock = {
// 4. Validate again the created Request instance after the recaptcha retry.
let request = try XCTUnwrap(self.rpcIssuer.request as? GetOOBConfirmationCodeRequest)
XCTAssertEqual(request.email, self.kEmail)
XCTAssertEqual(request.apiKey, AuthTests.kFakeAPIKey)
request.injectRecaptchaFields(recaptchaResponse: AuthTests.kFakeRecaptchaResponse,
recaptchaVersion: AuthTests.kFakeRecaptchaVersion)
// 5. Send the response from the fake backend.
try self.rpcIssuer.respond(withJSON: ["idToken": AuthTests.kAccessToken,
"email": self.kEmail,
"isNewUser": true,
"refreshToken": self.kRefreshToken])
}

auth?.sendPasswordReset(withEmail: kEmail) { error in
// 4. After the response triggers the callback, verify success.
XCTAssertTrue(Thread.isMainThread)
XCTAssertNil(error)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}
#endif

/** @fn testSendPasswordResetEmailSuccess
@brief Tests the flow of a successful @c sendPasswordReset call.
*/
Expand Down Expand Up @@ -1449,6 +1634,86 @@ class AuthTests: RPCBaseTests {
waitForExpectations(timeout: 5)
}

#if os(iOS)
/** @fn testSendSignInLinkToEmailWithRecaptchaSuccess
@brief Tests the flow of a successful @c sendSignInLinkToEmail:actionCodeSettings: call.
*/
func testSendSignInLinkToEmailWithRecaptchaSuccess() throws {
let expectation = self.expectation(description: #function)
let kTestRecaptchaKey = "projects/123/keys/456"
rpcIssuer.recaptchaSiteKey = kTestRecaptchaKey

// 1. Setup respond block to test and fake send request.
rpcIssuer.respondBlock = {
// 2. Validate the created Request instance.
let request = try XCTUnwrap(self.rpcIssuer.request as? GetOOBConfirmationCodeRequest)
XCTAssertEqual(request.email, self.kEmail)
XCTAssertEqual(request.apiKey, AuthTests.kFakeAPIKey)
XCTAssertEqual(request.continueURL, self.kContinueURL)
XCTAssertTrue(request.handleCodeInApp)
request.injectRecaptchaFields(recaptchaResponse: AuthTests.kFakeRecaptchaResponse,
recaptchaVersion: AuthTests.kFakeRecaptchaVersion)

// 3. Send the response from the fake backend.
_ = try self.rpcIssuer.respond(withJSON: [:])
}
auth?.sendSignInLink(toEmail: kEmail,
actionCodeSettings: fakeActionCodeSettings()) { error in
// 4. After the response triggers the callback, verify success.
XCTAssertTrue(Thread.isMainThread)
XCTAssertNil(error)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}

/** @fn testSendSignInLinkToEmailWithRecaptchaFallbackSuccess
@brief Tests the flow of a successful @c sendSignInLinkToEmail:actionCodeSettings: call.
*/
func testSendSignInLinkToEmailWithRecaptchaFallbackSuccess() throws {
let expectation = self.expectation(description: #function)
let kTestRecaptchaKey = "projects/123/keys/456"
rpcIssuer.recaptchaSiteKey = kTestRecaptchaKey

// 1. Setup respond block to test and fake send request.
rpcIssuer.respondBlock = {
// 2. Validate the created Request instance.
let request = try XCTUnwrap(self.rpcIssuer.request as? GetOOBConfirmationCodeRequest)
XCTAssertEqual(request.email, self.kEmail)
XCTAssertEqual(request.apiKey, AuthTests.kFakeAPIKey)
XCTAssertEqual(request.continueURL, self.kContinueURL)
XCTAssertTrue(request.handleCodeInApp)
request.injectRecaptchaFields(recaptchaResponse: AuthTests.kFakeRecaptchaResponse,
recaptchaVersion: AuthTests.kFakeRecaptchaVersion)

// 3. Send the response from the fake backend.
_ = try self.rpcIssuer.respond(withJSON: [:])
}
rpcIssuer.nextRespondBlock = {
// 4. Validate again the created Request instance after the recaptcha retry.
let request = try XCTUnwrap(self.rpcIssuer.request as? GetOOBConfirmationCodeRequest)
XCTAssertEqual(request.email, self.kEmail)
XCTAssertEqual(request.apiKey, AuthTests.kFakeAPIKey)
request.injectRecaptchaFields(recaptchaResponse: AuthTests.kFakeRecaptchaResponse,
recaptchaVersion: AuthTests.kFakeRecaptchaVersion)
// 5. Send the response from the fake backend.
try self.rpcIssuer.respond(withJSON: ["idToken": AuthTests.kAccessToken,
"email": self.kEmail,
"isNewUser": true,
"refreshToken": self.kRefreshToken])
}

auth?.sendSignInLink(toEmail: kEmail,
actionCodeSettings: fakeActionCodeSettings()) { error in
// 4. After the response triggers the callback, verify success.
XCTAssertTrue(Thread.isMainThread)
XCTAssertNil(error)
expectation.fulfill()
}
waitForExpectations(timeout: 5)
}
#endif

/** @fn testSendSignInLinkToEmailSuccess
@brief Tests the flow of a successful @c sendSignInLinkToEmail call.
*/
Expand Down