Skip to content

Commit

Permalink
Revert "Temporarily disable recaptcha since we went over quota"
Browse files Browse the repository at this point in the history
This reverts commit 41e4e61.
  • Loading branch information
wrandall22 committed Dec 2, 2024
1 parent 7875cc6 commit 18e3bdc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
20 changes: 10 additions & 10 deletions src/common/components/Recaptcha/Recaptcha.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('Recaptcha component', () => {
expect(recaptchaEnabledButton.innerHTML).toEqual('Label')
})

xit('should disable the button until ready', async () => {
it('should disable the button until ready', async () => {
global.window.grecaptcha = undefined

const { getByRole } = render(buildRecaptcha())
Expand All @@ -56,7 +56,7 @@ describe('Recaptcha component', () => {
await waitFor(() => expect((recaptchaEnabledButton as HTMLButtonElement).disabled).toEqual(false))
})

xit('should successfully pass the recaptcha', async () => {
it('should successfully pass the recaptcha', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
Expand All @@ -77,7 +77,7 @@ describe('Recaptcha component', () => {
})
})

xit('should successfully pass the recaptcha on branded checkout', async () => {
it('should successfully pass the recaptcha on branded checkout', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
Expand All @@ -98,7 +98,7 @@ describe('Recaptcha component', () => {
})
})

xit('should log a warning due to low score', async () => {
it('should log a warning due to low score', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
Expand All @@ -119,7 +119,7 @@ describe('Recaptcha component', () => {
})
})

xit('should fail the recaptcha call', async () => {
it('should fail the recaptcha call', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
Expand All @@ -140,7 +140,7 @@ describe('Recaptcha component', () => {
})
})

xit('should call the fail function when not a valid action', async () => {
it('should call the fail function when not a valid action', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
Expand All @@ -161,7 +161,7 @@ describe('Recaptcha component', () => {
})
})

xit('should skip the recaptcha call', async () => {
it('should skip the recaptcha call', async () => {
//@ts-ignore
global.window.grecaptcha = { ready: mockRecaptchaReady }

Expand All @@ -178,7 +178,7 @@ describe('Recaptcha component', () => {
})
})

xit('should not block the gift if something went wrong with recaptcha', async () => {
it('should not block the gift if something went wrong with recaptcha', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.reject('Failed')
Expand All @@ -198,7 +198,7 @@ describe('Recaptcha component', () => {
})
})

xit('should not block the gift if something went wrong with recaptcha JSON', async () => {
it('should not block the gift if something went wrong with recaptcha JSON', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
Expand All @@ -220,7 +220,7 @@ describe('Recaptcha component', () => {
})
})

xit('should not block gifts if something weird happens', async () => {
it('should not block gifts if something weird happens', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
Expand Down
83 changes: 41 additions & 42 deletions src/common/components/Recaptcha/Recaptcha.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,50 +71,49 @@ export const Recaptcha = ({
}, [grecaptcha, buttonId])

const handleReCaptchaVerify = useCallback(async () => {
// if (!ready) {
// $log.info('Execute recaptcha not yet available')
// return
// }
if (!ready) {
$log.info('Execute recaptcha not yet available')
return
}

// grecaptcha.ready(async () => {
// try {
// const token = await grecaptcha.execute(recaptchaKey, { action: action })
// const serverResponse = await fetch(`${apiUrl}/recaptcha/verify`, {
// method: 'POST',
// body: JSON.stringify({ token: token }),
// headers: { 'Content-Type': 'application/json' }
// })
// const data = await serverResponse.json()
grecaptcha.ready(async () => {
try {
const token = await grecaptcha.execute(recaptchaKey, { action: action })
const serverResponse = await fetch(`${apiUrl}/recaptcha/verify`, {
method: 'POST',
body: JSON.stringify({ token: token }),
headers: { 'Content-Type': 'application/json' }
})
const data = await serverResponse.json()

// if (data?.success === true && isValidAction(data?.action)) {
// if (data.score < 0.5) {
// $log.warn(`Captcha score was below the threshold: ${data.score}`)
// onFailure(componentInstance)
// return
// }
// onSuccess(componentInstance)
// return
// }
// if (data?.success === false && isValidAction(data?.action)) {
// $log.warn('Recaptcha call was unsuccessful, continuing anyway')
// onSuccess(componentInstance)
// return
// }
// if (!data) {
// $log.warn('Data was missing!')
// onSuccess(componentInstance)
// return
// }
// if (!isValidAction(data?.action)) {
// $log.warn(`Invalid action: ${data?.action}`)
// onFailure(componentInstance)
// }
// } catch (error) {
// $log.error(`Failed to verify recaptcha, continuing on: ${error}`)
// onSuccess(componentInstance)
// }
// })
onSuccess(componentInstance)
if (data?.success === true && isValidAction(data?.action)) {
if (data.score < 0.5) {
$log.warn(`Captcha score was below the threshold: ${data.score}`)
onFailure(componentInstance)
return
}
onSuccess(componentInstance)
return
}
if (data?.success === false && isValidAction(data?.action)) {
$log.warn('Recaptcha call was unsuccessful, continuing anyway')
onSuccess(componentInstance)
return
}
if (!data) {
$log.warn('Data was missing!')
onSuccess(componentInstance)
return
}
if (!isValidAction(data?.action)) {
$log.warn(`Invalid action: ${data?.action}`)
onFailure(componentInstance)
}
} catch (error) {
$log.error(`Failed to verify recaptcha, continuing on: ${error}`)
onSuccess(componentInstance)
}
})
}, [grecaptcha, buttonId, ready])

return (
Expand Down

0 comments on commit 18e3bdc

Please sign in to comment.