-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'arc-themes-release-version-2.3.0' into THEMES-1466
- Loading branch information
Showing
12 changed files
with
330 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
blocks/identity-block/components/bot-challenge-protection/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { useFusionContext } from "fusion:context"; | ||
import getProperties from "fusion:properties"; | ||
import getTranslatedPhrases from "fusion:intl"; | ||
import { Paragraph, useIdentity, useSales } from "@wpmedia/arc-themes-components"; | ||
import ReCAPTCHA from "react-google-recaptcha"; | ||
|
||
const BotChallengeProtection = ({ challengeIn, setCaptchaToken, className, captchaError, setCaptchaError }) => { | ||
const { Identity, isInitialized } = useIdentity(); | ||
const { Sales } = useSales(); | ||
const [siteKey, setSiteKey] = useState(); | ||
|
||
const onChange = (value) => { | ||
setCaptchaToken(value); | ||
setCaptchaError(null); | ||
localStorage.setItem('ArcXP_captchaToken', value); | ||
}; | ||
|
||
useEffect(() => { | ||
const checkCaptcha = async () => { | ||
const config = await Identity.getConfig(); | ||
const {recaptchaSiteKey, recaptchaScore } = config; | ||
const isIdentityCaptchaEnabled = config?.[`${challengeIn}Recaptcha`]; | ||
|
||
if(['signup', 'signin', 'magicLink'].includes(challengeIn)) { | ||
if (isIdentityCaptchaEnabled && recaptchaScore === '-1' && recaptchaSiteKey) { | ||
setSiteKey(recaptchaSiteKey); | ||
} | ||
} | ||
|
||
if (challengeIn === 'checkout') { | ||
const salesConfig = await Sales.getConfig(); | ||
const isSalesCaptchaEnabled = salesConfig?.checkoutRecaptchaEnabled; | ||
if (isSalesCaptchaEnabled && recaptchaScore === '-1' && recaptchaSiteKey) { | ||
setSiteKey(recaptchaSiteKey); | ||
} | ||
} | ||
|
||
}; | ||
checkCaptcha(); | ||
|
||
}, [Identity, Sales, challengeIn]); | ||
|
||
const { arcSite } = useFusionContext(); | ||
const { locale } = getProperties(arcSite); | ||
const phrases = getTranslatedPhrases(locale); | ||
|
||
if (!isInitialized) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<section className={`${className}__bot-protection-section`} data-testid="bot-challege-protection-container"> | ||
{!!siteKey && <ReCAPTCHA | ||
sitekey={siteKey} | ||
onChange={onChange} | ||
onExpired={() => {}} | ||
/>} | ||
{captchaError && <Paragraph data-testid="bot-challege-captcha-error">{phrases.t("identity-block.bot-protection-error")}</Paragraph>} | ||
</section> | ||
); | ||
}; | ||
|
||
export default BotChallengeProtection; |
50 changes: 50 additions & 0 deletions
50
blocks/identity-block/components/bot-challenge-protection/index.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { useIdentity } from "@wpmedia/arc-themes-components"; | ||
import BotChallengeProtection from "."; | ||
|
||
const mockLogin = jest.fn(() => Promise.resolve()); | ||
|
||
const mockIdentity = { | ||
isLoggedIn: jest.fn(() => false), | ||
getConfig: jest.fn(() => ({})), | ||
login: mockLogin, | ||
}; | ||
|
||
const mockSales = { | ||
getConfig: jest.fn(() => {}) | ||
} | ||
|
||
jest.mock("@wpmedia/arc-themes-components", () => ({ | ||
...jest.requireActual("@wpmedia/arc-themes-components"), | ||
useIdentity: jest.fn(() => ({ | ||
isInitialized: true, | ||
Identity: { | ||
...mockIdentity, | ||
}, | ||
})), | ||
useSales: jest.fn(() => ({ | ||
isInitialized: true, | ||
Sales: { | ||
...mockSales, | ||
}, | ||
})), | ||
})); | ||
|
||
describe("Bot challenge protection", () => { | ||
it("renders with required items", () => { | ||
render(<BotChallengeProtection challengeIn="signin" />); | ||
|
||
expect(screen.getByTestId("bot-challege-protection-container")).not.toBeNull(); | ||
}); | ||
it("it does not render if identity is not initialized", () => { | ||
useIdentity.mockImplementation(() => ({ | ||
isInitialized: false, | ||
Identity: { | ||
...mockIdentity, | ||
}, | ||
})) | ||
render(<BotChallengeProtection challengeIn="test" />); | ||
expect(screen.queryByTestId("bot-challege-protection-container")).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import React from "react"; | ||
import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
import Login from "./default"; | ||
import { useIdentity } from "@wpmedia/arc-themes-components"; | ||
import Login from "./default"; | ||
|
||
const defaultCustomFields = { | ||
redirectURL: "", | ||
redirectToPreviousPage: true, | ||
signUpURL: "" | ||
}; | ||
|
||
const mockLogin = jest.fn(() => Promise.resolve()); | ||
|
@@ -15,6 +16,10 @@ const mockIdentity = { | |
login: mockLogin, | ||
}; | ||
|
||
const mockSales = { | ||
getConfig: jest.fn(() => {}) | ||
} | ||
|
||
jest.mock("@wpmedia/arc-themes-components", () => ({ | ||
...jest.requireActual("@wpmedia/arc-themes-components"), | ||
useIdentity: jest.fn(() => ({ | ||
|
@@ -23,27 +28,33 @@ jest.mock("@wpmedia/arc-themes-components", () => ({ | |
...mockIdentity, | ||
}, | ||
})), | ||
useSales: jest.fn(() => ({ | ||
isInitialized: true, | ||
Sales: { | ||
...mockSales, | ||
}, | ||
})), | ||
})); | ||
jest.mock("fusion:properties", () => jest.fn(() => ({}))); | ||
|
||
describe("Identity Login Feature", () => { | ||
it("renders", () => { | ||
render(<Login customFields={defaultCustomFields} />); | ||
expect(screen.queryByRole("form")).not.toBeNull(); | ||
expect(screen.getByRole("form")).not.toBeNull(); | ||
}); | ||
|
||
it("shows login form", () => { | ||
render(<Login customFields={defaultCustomFields} />); | ||
expect(screen.queryByRole("form")).not.toBeNull(); | ||
expect(screen.getByRole("form")).not.toBeNull(); | ||
expect(screen.getByLabelText("identity-block.password")).not.toBeNull(); | ||
expect(screen.getByLabelText("identity-block.email")).not.toBeNull(); | ||
expect(screen.getByLabelText("identity-block.email-label")).not.toBeNull(); | ||
}); | ||
|
||
it("submits the login form", async () => { | ||
render(<Login customFields={defaultCustomFields} />); | ||
|
||
await waitFor(() => expect(screen.getByLabelText("identity-block.email"))); | ||
fireEvent.change(screen.getByLabelText("identity-block.email"), { | ||
await waitFor(() => expect(screen.getByLabelText("identity-block.email-label"))); | ||
fireEvent.change(screen.getByLabelText("identity-block.email-label"), { | ||
target: { value: "[email protected]" }, | ||
}); | ||
await waitFor(() => expect(screen.getByLabelText("identity-block.password"))); | ||
|
@@ -60,6 +71,9 @@ describe("Identity Login Feature", () => { | |
describe("Identity Login Feature - rejected Login", () => { | ||
beforeEach(() => { | ||
mockLogin.mockImplementation(() => Promise.reject()); | ||
global.grecaptcha = { | ||
reset: jest.fn() | ||
} | ||
}); | ||
|
||
afterEach(() => { | ||
|
@@ -69,8 +83,8 @@ describe("Identity Login Feature - rejected Login", () => { | |
it("rejects the login", async () => { | ||
render(<Login customFields={defaultCustomFields} />); | ||
|
||
await waitFor(() => expect(screen.getByLabelText("identity-block.email"))); | ||
fireEvent.change(screen.getByLabelText("identity-block.email"), { | ||
await waitFor(() => expect(screen.getByLabelText("identity-block.email-label"))); | ||
fireEvent.change(screen.getByLabelText("identity-block.email-label"), { | ||
target: { value: "[email protected]" }, | ||
}); | ||
|
||
|
@@ -83,7 +97,7 @@ describe("Identity Login Feature - rejected Login", () => { | |
fireEvent.click(screen.getByRole("button")); | ||
|
||
await waitFor(() => expect(mockLogin).toHaveBeenCalled()); | ||
await waitFor(() => screen.getByText("identity-block.login-form-error")); | ||
await screen.findByText("identity-block.login-form-error"); | ||
}); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.