Skip to content

Commit

Permalink
Revert files.
Browse files Browse the repository at this point in the history
  • Loading branch information
wjames111 committed Dec 12, 2024
1 parent c8f9d81 commit b9a9e63
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 52 deletions.
68 changes: 63 additions & 5 deletions src/common/components/Recaptcha/Recaptcha.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { render, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { ButtonType, Recaptcha } from './Recaptcha'
import React from 'react'
import { datadogRum } from '@datadog/browser-rum'

jest.mock('@datadog/browser-rum', () => {
return {
datadogRum: {
addError: jest.fn()
}
}
})

let mockExecuteRecaptcha = jest.fn()
const mockRecaptchaReady = jest.fn()
Expand Down Expand Up @@ -114,7 +123,9 @@ describe('Recaptcha component', () => {

await userEvent.click(getByRole('button'))
await waitFor(() => {
expect($log.warn).toHaveBeenCalledWith('Captcha score was below the threshold: 0.2')
const errorMessage = 'Captcha score was below the threshold: 0.2'
expect($log.warn).toHaveBeenCalledWith(errorMessage)
expect(datadogRum.addError).toHaveBeenCalledWith(new Error(`Error submitting purchase: ${errorMessage}`), { context: 'Recaptcha', errorCode: 'lowScore' })
expect(onFailure).toHaveBeenCalledTimes(1)
})
})
Expand Down Expand Up @@ -144,7 +155,7 @@ describe('Recaptcha component', () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.resolve({ success: true, action: 'read' })
json: () => Promise.resolve({ success: true, action: 'read', score: 0.9 })
})
})

Expand All @@ -156,8 +167,11 @@ describe('Recaptcha component', () => {

await userEvent.click(getByRole('button'))
await waitFor(() => {
const errorMessage = 'Invalid action: read'
expect(onSuccess).not.toHaveBeenCalled()
expect(onFailure).toHaveBeenCalled()
expect($log.warn).toHaveBeenCalledWith(errorMessage)
expect(datadogRum.addError).toHaveBeenCalledWith(new Error(`Error submitting purchase: ${errorMessage}`), { context: 'Recaptcha', errorCode: 'invalidAction' })
})
})

Expand Down Expand Up @@ -220,11 +234,55 @@ describe('Recaptcha component', () => {
})
})

it('should not block gifts if something weird happens', async () => {
it('should not block gifts if data is empty', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.resolve({})
})
})

onSuccess.mockImplementation(() => console.log('success after weird'))

const { getByRole } = render(
buildRecaptcha()
)

await userEvent.click(getByRole('button'))
await waitFor(() => {
expect(onSuccess).toHaveBeenCalledTimes(1)
expect(onFailure).not.toHaveBeenCalled()
expect($log.warn).toHaveBeenCalledWith('Recaptcha returned an unusual response:', {})
})
})

it('should not block gifts if action is undefined', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.resolve({ success: true, score: 0.9 })
})
})

onSuccess.mockImplementation(() => console.log('success after weird'))

const { getByRole } = render(
buildRecaptcha()
)

await userEvent.click(getByRole('button'))
await waitFor(() => {
expect(onSuccess).toHaveBeenCalledTimes(1)
expect(onFailure).not.toHaveBeenCalled()
expect($log.warn).toHaveBeenCalledWith('Recaptcha returned an unusual response:', { success: true, score: 0.9 })
})
})

it('should not block gifts if score is undefined', async () => {
//@ts-ignore
global.fetch = jest.fn(() => {
return Promise.resolve({
json: () => Promise.resolve()
json: () => Promise.resolve({ success: true, action: 'submit_gift' })
})
})

Expand All @@ -238,7 +296,7 @@ describe('Recaptcha component', () => {
await waitFor(() => {
expect(onSuccess).toHaveBeenCalledTimes(1)
expect(onFailure).not.toHaveBeenCalled()
expect($log.warn).toHaveBeenCalledWith('Data was missing!')
expect($log.warn).toHaveBeenCalledWith('Recaptcha returned an unusual response:', { success: true, action: 'submit_gift' })
})
})

Expand Down
20 changes: 13 additions & 7 deletions src/common/components/Recaptcha/Recaptcha.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import angular from 'angular'
import { react2angular } from 'react2angular'
import React, { useCallback, useEffect, useState } from 'react'
import { datadogRum } from '@datadog/browser-rum'

const componentName = 'recaptcha'

Expand Down Expand Up @@ -86,9 +87,17 @@ export const Recaptcha = ({
})
const data = await serverResponse.json()

if (!data || !data.score || !data.action) {
$log.warn('Recaptcha returned an unusual response:', data)
onSuccess(componentInstance)
return
}

if (data?.success === true && isValidAction(data?.action)) {
if (data.score < 0.5) {
$log.warn(`Captcha score was below the threshold: ${data.score}`)
const errorMessage = `Captcha score was below the threshold: ${data.score}`
$log.warn(errorMessage)
datadogRum.addError(new Error(`Error submitting purchase: ${errorMessage}`), { context: 'Recaptcha', errorCode: 'lowScore' })
onFailure(componentInstance)
return
}
Expand All @@ -100,13 +109,10 @@ export const Recaptcha = ({
onSuccess(componentInstance)
return
}
if (!data) {
$log.warn('Data was missing!')
onSuccess(componentInstance)
return
}
if (!isValidAction(data?.action)) {
$log.warn(`Invalid action: ${data?.action}`)
const errorMessage = `Invalid action: ${data?.action}`
$log.warn(errorMessage)
datadogRum.addError(new Error(`Error submitting purchase: ${errorMessage}`), { context: 'Recaptcha', errorCode: 'invalidAction' })
onFailure(componentInstance)
}
} catch (error) {
Expand Down
10 changes: 9 additions & 1 deletion src/common/services/api/designations.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import toFinite from 'lodash/toFinite'
import startsWith from 'lodash/startsWith'
import { Observable } from 'rxjs/Observable'
import 'rxjs/add/observable/from'
import 'rxjs/add/observable/of'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/catch'
import moment from 'moment'
Expand Down Expand Up @@ -233,7 +234,7 @@ class DesignationsService {
}
return suggestedAmounts
})
.catch(() => [])
.catch(() => Observable.of([]))
}

facebookPixel (code) {
Expand All @@ -253,6 +254,13 @@ class DesignationsService {
// Map giving links
if (data.data['jcr:content'].givingLinks) {
angular.forEach(data.data['jcr:content'].givingLinks, (v, k) => {
if (!v || !v.name || !v.url) {
// Some accounts contain multiple, empty giving links. Until we figure how how they
// are being created, we are ignoring them on the frontend.
// https://jira.cru.org/browse/EP-2554
return
}

if (toFinite(k) > 0 || startsWith(k, 'item')) {
givingLinks.push({
name: v.name,
Expand Down
Loading

0 comments on commit b9a9e63

Please sign in to comment.