Skip to content

Add unit tests for handleRecaptchaFlow #7699

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

Merged
merged 35 commits into from
Nov 1, 2023
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
0e48d5b
Create handleRecaptchaFlow helper method
Sep 28, 2023
3307fe5
Revert "Create handleRecaptchaFlow helper method"
Oct 11, 2023
7174365
Add handleRecaptchaFlow
Oct 11, 2023
8185450
Revert tests
Oct 11, 2023
7bd54ae
Integration
Oct 11, 2023
d311ea4
Revert "Revert tests"
Oct 12, 2023
eeec05a
Update recaptcha_enterprise_verifier.test.ts
Oct 14, 2023
102f9fa
Update recaptcha_enterprise_verifier.test.ts
Oct 14, 2023
81f5662
Update recaptcha_enterprise_verifier.test.ts
Oct 14, 2023
eff15ef
Update recaptcha_enterprise_verifier.test.ts
Oct 14, 2023
0682586
Update recaptcha_enterprise_verifier.test.ts
Oct 14, 2023
5bb3cb4
Update recaptcha_enterprise_verifier.test.ts
Oct 15, 2023
1a582a8
Update recaptcha_enterprise_verifier.test.ts
Oct 15, 2023
fe18400
Update recaptcha_enterprise_verifier.test.ts
Oct 27, 2023
952b0af
Create flat-seals-bathe.md
Oct 27, 2023
b88cdb3
Create handleRecaptchaFlow helper method
Sep 28, 2023
90ea7f5
Revert "Create handleRecaptchaFlow helper method"
Oct 11, 2023
7e0cc32
Add handleRecaptchaFlow
Oct 11, 2023
c859c15
Revert tests
Oct 11, 2023
447dc94
Integration
Oct 11, 2023
f63dd72
Revert "Revert tests"
Oct 12, 2023
42b3360
Update recaptcha_enterprise_verifier.test.ts
Oct 14, 2023
a46cb7e
Update recaptcha_enterprise_verifier.test.ts
Oct 14, 2023
b337207
Update recaptcha_enterprise_verifier.test.ts
Oct 14, 2023
9d67729
Update recaptcha_enterprise_verifier.test.ts
Oct 14, 2023
97a758f
Update recaptcha_enterprise_verifier.test.ts
Oct 14, 2023
1f60c51
Update recaptcha_enterprise_verifier.test.ts
Oct 15, 2023
861db5e
Update recaptcha_enterprise_verifier.test.ts
Oct 15, 2023
28f14ed
Update recaptcha_enterprise_verifier.test.ts
Oct 27, 2023
3c85c72
Create flat-seals-bathe.md
Oct 27, 2023
9a97aab
Merge branch 're-tests' of https://github.com/firebase/firebase-js-sd…
Oct 30, 2023
ad5fb49
Update recaptcha_enterprise_verifier.test.ts
Oct 30, 2023
9065ee5
Delete lucky-dragons-juggle.md
Oct 30, 2023
ddb1cd4
Update recaptcha_enterprise_verifier.test.ts
Oct 30, 2023
6b71639
Revert "Create flat-seals-bathe.md"
Nov 1, 2023
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 @@ -20,17 +20,27 @@ import chaiAsPromised from 'chai-as-promised';
import * as sinon from 'sinon';
import sinonChai from 'sinon-chai';

import { Endpoint, RecaptchaClientType, RecaptchaVersion } from '../../api';
import {
Endpoint,
RecaptchaClientType,
RecaptchaVersion,
RecaptchaActionName
} from '../../api';
import { mockEndpointWithParams } from '../../../test/helpers/api/helper';
import { testAuth, TestAuth } from '../../../test/helpers/mock_auth';
import * as mockFetch from '../../../test/helpers/mock_fetch';
import { ServerError } from '../../api/errors';
import { AuthInternal } from '../../model/auth';

import { MockGreCAPTCHATopLevel } from './recaptcha_mock';
import {
RecaptchaEnterpriseVerifier,
FAKE_TOKEN
FAKE_TOKEN,
handleRecaptchaFlow
} from './recaptcha_enterprise_verifier';
import { RecaptchaConfig } from './recaptcha';
import { AuthErrorCode } from '../../core/errors';
import { _createError } from '../../core/util/assert';

use(chaiAsPromised);
use(sinonChai);
Expand All @@ -39,6 +49,29 @@ describe('platform_browser/recaptcha/recaptcha_enterprise_verifier', () => {
let auth: TestAuth;
let verifier: RecaptchaEnterpriseVerifier;

const recaptchaConfigResponseEnforce = {
recaptchaKey: 'foo/bar/to/site-key',
recaptchaEnforcementState: [
{
provider: 'EMAIL_PASSWORD_PROVIDER',
enforcementState: 'ENFORCE'
}
]
};
const recaptchaConfigEnforce = new RecaptchaConfig(
recaptchaConfigResponseEnforce
);
const recaptchaConfigResponseOff = {
recaptchaKey: 'foo/bar/to/site-key',
recaptchaEnforcementState: [
{
provider: 'EMAIL_PASSWORD_PROVIDER',
enforcementState: 'OFF'
}
]
};
const recaptchaConfigOff = new RecaptchaConfig(recaptchaConfigResponseOff);

beforeEach(async () => {
auth = await testAuth();
mockFetch.setUp();
Expand All @@ -51,16 +84,6 @@ describe('platform_browser/recaptcha/recaptcha_enterprise_verifier', () => {
});

context('#verify', () => {
const recaptchaConfigResponseEnforce = {
recaptchaKey: 'foo/bar/to/site-key',
recaptchaEnforcementState: [
{
provider: 'EMAIL_PASSWORD_PROVIDER',
enforcementState: 'ENFORCE'
}
]
};

const request = {
clientType: RecaptchaClientType.WEB,
version: RecaptchaVersion.ENTERPRISE
Expand Down Expand Up @@ -117,4 +140,110 @@ describe('platform_browser/recaptcha/recaptcha_enterprise_verifier', () => {
expect(await verifier.verify()).to.eq(FAKE_TOKEN);
});
});

context('handleRecaptchaFlow', () => {
let mockAuthInstance: AuthInternal;
let mockRequest: any;
let mockActionMethod: sinon.SinonStub;

beforeEach(async () => {
mockAuthInstance = await testAuth();
mockRequest = {};
mockActionMethod = sinon.stub();
});

afterEach(() => {
sinon.restore();
});

it('should call actionMethod with request if emailPasswordEnabled is true', async () => {
if (typeof window === 'undefined') {
return;
}
sinon
.stub(mockAuthInstance, '_getRecaptchaConfig')
.returns(recaptchaConfigEnforce);
sinon
.stub(RecaptchaEnterpriseVerifier.prototype, 'verify')
.resolves('recaptcha-response');
mockRequest = { foo: 'bar' };
mockActionMethod = sinon.stub().resolves('testResponse');
const response = await handleRecaptchaFlow(
mockAuthInstance,
mockRequest,
RecaptchaActionName.SIGN_IN_WITH_PASSWORD,
mockActionMethod
);
expect(mockActionMethod).to.have.been.calledOnce;
expect(response).to.equal('testResponse');
});

// "Errors like "MISSING_RECAPTCHA_TOKEN" will be handled irrespective of the enablement status of "emailPasswordEnabled", but this test verifies the more likely scenario where emailPasswordEnabled is false"
it('should handle MISSING_RECAPTCHA_TOKEN error when emailPasswordEnabled is false', async () => {
if (typeof window === 'undefined') {
return;
}
sinon
.stub(mockAuthInstance, '_getRecaptchaConfig')
.returns(recaptchaConfigOff);
sinon
.stub(RecaptchaEnterpriseVerifier.prototype, 'verify')
.resolves('recaptcha-response');
mockRequest = { foo: 'bar' };
let callCount = 0;
mockActionMethod = sinon.stub().callsFake(() => {
callCount++;
if (callCount === 1) {
return Promise.reject(
_createError(AuthErrorCode.MISSING_RECAPTCHA_TOKEN)
);
} else {
return Promise.resolve('testResponse');
}
});
const response = await handleRecaptchaFlow(
mockAuthInstance,
mockRequest,
RecaptchaActionName.SIGN_IN_WITH_PASSWORD,
mockActionMethod
);
expect(mockActionMethod).to.have.been.calledTwice;
expect(response).to.equal('testResponse');
});

it('should handle non MISSING_RECAPTCHA_TOKEN error when emailPasswordEnabled is false', async () => {
if (typeof window === 'undefined') {
return;
}
sinon
.stub(mockAuthInstance, '_getRecaptchaConfig')
.returns(recaptchaConfigOff);
sinon
.stub(RecaptchaEnterpriseVerifier.prototype, 'verify')
.resolves('recaptcha-response');
mockRequest = { foo: 'bar' };
let callCount = 0;
mockActionMethod = sinon.stub().callsFake(() => {
callCount++;
if (callCount === 1) {
return Promise.reject(
_createError(AuthErrorCode.RECAPTCHA_NOT_ENABLED)
);
} else {
return Promise.resolve('testResponse');
}
});

const response = handleRecaptchaFlow(
mockAuthInstance,
mockRequest,
RecaptchaActionName.SIGN_IN_WITH_PASSWORD,
mockActionMethod
);
await expect(response).to.be.rejectedWith(
AuthErrorCode.RECAPTCHA_NOT_ENABLED
);
expect(mockActionMethod).to.have.been.calledOnce;
});
});
});