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

[IAMRISK-3554] hcaptcha bug fix #2566

Merged
merged 4 commits into from
Jun 26, 2024
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 @@ -6,6 +6,7 @@ exports[`Arkose should match the snapshot 1`] = `
>
<div
className="auth0-lock-arkose"
id=""
/>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports[`Auth0 V2 should match the snapshot 1`] = `
>
<div
className="auth0-lock-auth0-v2"
id=""
/>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports[`friendly captcha should match the snapshot 1`] = `
>
<div
className="auth0-lock-friendly-captcha"
id=""
/>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports[`hCaptcha should match the snapshot 1`] = `
>
<div
className="auth0-lock-hcaptcha"
id="h-captcha"
/>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports[`Recaptcha Enterprise should match the snapshot 1`] = `
>
<div
className="auth0-lock-recaptchav2"
id=""
/>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports[`Recaptcha v2 should match the snapshot 1`] = `
>
<div
className="auth0-lock-recaptchav2"
id=""
/>
</div>
`;
12 changes: 11 additions & 1 deletion src/__tests__/field/captcha/third_party_captcha.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,24 @@ describe('ThirdPartyCaptcha', () => {
});

it('should call render with the correct renderParams', () => {
const renderParams = global.window.hcaptcha.render.mock.calls[0][1];
const renderCalls = global.window.hcaptcha.render.mock.calls;
const renderParams = renderCalls[0][1];
expect(renderParams).toEqual({
sitekey: 'mySiteKey',
callback: expect.any(Function),
'expired-callback': expect.any(Function),
'error-callback': expect.any(Function)
});
expect(renderCalls.length).toEqual(1);
});

it('should call render on update', () => {
act(() => {
wrapper.setState();
const renderCalls = global.window.hcaptcha.render.mock.calls;
expect(renderCalls.length).toEqual(1);
})
})
});

describe('auth0_v2', () => {
Expand Down
14 changes: 13 additions & 1 deletion src/field/captcha/third_party_captcha.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ const providerDomPrefix = (provider) => {
}
};

const providerComponentId = (provider) => {
if (provider === HCAPTCHA_PROVIDER) {
return 'h-captcha';
} else {
return '';
}
};

const loadScript = (url, attributes) => {
var script = document.createElement('script');
for (var attr in attributes) {
Expand Down Expand Up @@ -302,7 +310,7 @@ export class ThirdPartyCaptcha extends React.Component {
: `auth0-lock-${providerDomPrefix(this.props.provider)}-block auth0-lock-${providerDomPrefix(this.props.provider)}-block-error`
}
>
<div className={`auth0-lock-${providerDomPrefix(this.props.provider) === 'recaptcha' ? 'recaptchav2' : providerDomPrefix(this.props.provider)}`} ref={this.ref} />
<div className={`auth0-lock-${providerDomPrefix(this.props.provider) === 'recaptcha' ? 'recaptchav2' : providerDomPrefix(this.props.provider)}`} id={providerComponentId(this.props.provider)} ref={this.ref} />
</div>
);
}
Expand All @@ -316,9 +324,13 @@ export class ThirdPartyCaptcha extends React.Component {
}

componentDidUpdate(prevProps, prevState) {
let hCaptchaComponent = document.getElementById("h-captcha");
if (prevProps.value !== this.props.value && this.props.value === '') {
this.reset();
}
if (this.props.provider === HCAPTCHA_PROVIDER && hCaptchaComponent && window[this.props.provider]) {
window[this.props.provider].render('h-captcha', this.getRenderParams());
}
}
}

Expand Down