Skip to content

Commit 39e3fa2

Browse files
author
renkelvin
committed
Update recaptcha_enterprise_verifier.test.ts
1 parent b2ac319 commit 39e3fa2

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

packages/auth/src/platform_browser/recaptcha/recaptcha_enterprise_verifier.test.ts

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,23 @@ import chaiAsPromised from 'chai-as-promised';
2020
import * as sinon from 'sinon';
2121
import sinonChai from 'sinon-chai';
2222

23-
import { Endpoint, RecaptchaClientType, RecaptchaVersion } from '../../api';
23+
import {
24+
Endpoint,
25+
RecaptchaClientType,
26+
RecaptchaVersion,
27+
RecaptchaActionName
28+
} from '../../api';
2429
import { mockEndpointWithParams } from '../../../test/helpers/api/helper';
2530
import { testAuth, TestAuth } from '../../../test/helpers/mock_auth';
2631
import * as mockFetch from '../../../test/helpers/mock_fetch';
2732
import { ServerError } from '../../api/errors';
33+
import { AuthInternal } from '../../model/auth';
2834

2935
import { MockGreCAPTCHATopLevel } from './recaptcha_mock';
3036
import {
3137
RecaptchaEnterpriseVerifier,
32-
FAKE_TOKEN
38+
FAKE_TOKEN,
39+
handleRecaptchaFlow
3340
} from './recaptcha_enterprise_verifier';
3441

3542
use(chaiAsPromised);
@@ -117,4 +124,79 @@ describe('platform_browser/recaptcha/recaptcha_enterprise_verifier', () => {
117124
expect(await verifier.verify()).to.eq(FAKE_TOKEN);
118125
});
119126
});
127+
128+
context('handleRecaptchaFlow', () => {
129+
let mockAuthInstance: AuthInternal;
130+
let mockRequest: any;
131+
let mockActionMethod: sinon.SinonStub;
132+
133+
beforeEach(async () => {
134+
mockAuthInstance = await testAuth();
135+
mockRequest = {};
136+
mockActionMethod = sinon.stub();
137+
});
138+
139+
afterEach(() => {
140+
sinon.restore();
141+
});
142+
143+
it('should handle recaptcha when emailPasswordEnabled is true', async () => {
144+
sinon.stub(mockAuthInstance, '_getRecaptchaConfig').returns({
145+
emailPasswordEnabled: true,
146+
siteKey: "mock_site_key"
147+
});
148+
mockActionMethod.resolves('success');
149+
150+
const result = await handleRecaptchaFlow(
151+
mockAuthInstance,
152+
mockRequest,
153+
RecaptchaActionName.GET_OOB_CODE,
154+
mockActionMethod
155+
);
156+
157+
expect(result).to.equal('success');
158+
expect(mockActionMethod).to.have.been.calledOnce;
159+
// Add more assertions as needed, e.g., checking if injectRecaptchaFields was called
160+
});
161+
162+
it('should handle action without recaptcha when emailPasswordEnabled is false and no error', async () => {
163+
sinon.stub(mockAuthInstance, '_getRecaptchaConfig').returns({
164+
emailPasswordEnabled: false,
165+
siteKey: 'mock_site_key'
166+
});
167+
mockActionMethod.resolves('success');
168+
169+
const result = await handleRecaptchaFlow(
170+
mockAuthInstance,
171+
mockRequest,
172+
RecaptchaActionName.GET_OOB_CODE,
173+
mockActionMethod
174+
);
175+
176+
expect(result).to.equal('success');
177+
expect(mockActionMethod).to.have.been.calledOnce;
178+
});
179+
180+
it('should handle MISSING_RECAPTCHA_TOKEN error when emailPasswordEnabled is false', async () => {
181+
sinon.stub(mockAuthInstance, '_getRecaptchaConfig').returns({
182+
emailPasswordEnabled: false,
183+
siteKey: 'mock_site_key'
184+
});
185+
mockActionMethod.onFirstCall().rejects({
186+
code: 'auth/MISSING_RECAPTCHA_TOKEN'
187+
});
188+
mockActionMethod.onSecondCall().resolves('success-after-recaptcha');
189+
190+
const result = await handleRecaptchaFlow(
191+
mockAuthInstance,
192+
mockRequest,
193+
RecaptchaActionName.GET_OOB_CODE,
194+
mockActionMethod
195+
);
196+
197+
expect(result).to.equal('success-after-recaptcha');
198+
expect(mockActionMethod).to.have.been.calledTwice;
199+
// Add more assertions as needed, e.g., checking if injectRecaptchaFields was called
200+
});
120201
});
202+
});

0 commit comments

Comments
 (0)