Skip to content

Commit 304e4db

Browse files
madblexbrianhall
andcommitted
feat(pir): captcha providers tests (#1560)
* feat(pir): captcha providers tests * feat: address PR comments * feat: amend tests * fix: add new tests and unroll for loops * Minor tweaks to wording and removal of hCaptcha mocks * fix: lint --------- Co-authored-by: Brian Hall <[email protected]>
1 parent 9a4d195 commit 304e4db

File tree

16 files changed

+470
-111
lines changed

16 files changed

+470
-111
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import { test as base } from '@playwright/test';
2+
import { createConfiguredDbpTest } from './fixtures';
3+
import { createGetRecaptchaInfoAction, createSolveRecaptchaAction } from '../mocks/broker-protection/captcha.js';
4+
import { BROKER_PROTECTION_CONFIGS } from './tests-config.js';
5+
6+
const test = createConfiguredDbpTest(base);
7+
8+
test.describe('Broker Protection Captcha', () => {
9+
test.describe('recaptcha2', () => {
10+
const recaptchaTargetPage = 're-captcha.html';
11+
const recaptchaResponseSelector = '#g-recaptcha-response';
12+
13+
test.describe('getCaptchaInfo', () => {
14+
test.describe('with useEnhancedCaptchaSystem: "enabled"', () => {
15+
test('returns the expected response for the correct action data', async ({ createConfiguredDbp }) => {
16+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemEnabled);
17+
await dbp.navigatesTo(recaptchaTargetPage);
18+
await dbp.receivesInlineAction(createGetRecaptchaInfoAction());
19+
const sucessResponse = await dbp.getSuccessResponse();
20+
21+
dbp.isCaptchaMatch(sucessResponse, { captchaType: 'recaptcha2', targetPage: recaptchaTargetPage });
22+
});
23+
24+
test('returns the expected response for the correct action data without the "captchaType" field', async ({
25+
createConfiguredDbp,
26+
}) => {
27+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemEnabled);
28+
await dbp.navigatesTo(recaptchaTargetPage);
29+
await dbp.receivesInlineAction(createGetRecaptchaInfoAction({ captchaType: undefined }));
30+
const sucessResponse = await dbp.getSuccessResponse();
31+
32+
dbp.isCaptchaMatch(sucessResponse, { captchaType: 'recaptcha2', targetPage: recaptchaTargetPage });
33+
});
34+
35+
test('returns the expected type when the "captchaType" field does not match the detected captcha type', async ({
36+
createConfiguredDbp,
37+
}) => {
38+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemEnabled);
39+
await dbp.navigatesTo(recaptchaTargetPage);
40+
await dbp.receivesInlineAction(createGetRecaptchaInfoAction({ captchaType: 'recaptchaEnterprise' }));
41+
const sucessResponse = await dbp.getSuccessResponse();
42+
43+
dbp.isCaptchaMatch(sucessResponse, { captchaType: 'recaptcha2', targetPage: recaptchaTargetPage });
44+
});
45+
46+
test('returns an error response for an action data with an invalid "captchaType" field', async ({
47+
createConfiguredDbp,
48+
}) => {
49+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemEnabled);
50+
await dbp.navigatesTo(recaptchaTargetPage);
51+
await dbp.receivesInlineAction(createGetRecaptchaInfoAction({ captchaType: 'invalid' }));
52+
53+
await dbp.isCaptchaError();
54+
});
55+
});
56+
57+
test.describe('with useEnhancedCaptchaSystem: "disabled"', () => {
58+
test('returns the expected response for the correct action data', async ({ createConfiguredDbp }) => {
59+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemDisabled);
60+
await dbp.navigatesTo(recaptchaTargetPage);
61+
await dbp.receivesInlineAction(createGetRecaptchaInfoAction());
62+
const sucessResponse = await dbp.getSuccessResponse();
63+
64+
dbp.isCaptchaMatch(sucessResponse, { captchaType: 'recaptcha2', targetPage: recaptchaTargetPage });
65+
});
66+
67+
test('returns the expected response for the correct action data without the "captchaType" field', async ({
68+
createConfiguredDbp,
69+
}) => {
70+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemDisabled);
71+
await dbp.navigatesTo(recaptchaTargetPage);
72+
await dbp.receivesInlineAction(createGetRecaptchaInfoAction({ captchaType: undefined }));
73+
const sucessResponse = await dbp.getSuccessResponse();
74+
75+
dbp.isCaptchaMatch(sucessResponse, { captchaType: 'recaptcha2', targetPage: recaptchaTargetPage });
76+
});
77+
78+
test('returns the expected type when the "captchaType" field does not match the detected captcha type', async ({
79+
createConfiguredDbp,
80+
}) => {
81+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemDisabled);
82+
await dbp.navigatesTo(recaptchaTargetPage);
83+
await dbp.receivesInlineAction(createGetRecaptchaInfoAction({ captchaType: 'recaptchaEnterprise' }));
84+
const sucessResponse = await dbp.getSuccessResponse();
85+
86+
dbp.isCaptchaMatch(sucessResponse, { captchaType: 'recaptcha2', targetPage: recaptchaTargetPage });
87+
});
88+
89+
test('returns the expected response for an action data with an invalid "captchaType" field', async ({
90+
createConfiguredDbp,
91+
}) => {
92+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemDisabled);
93+
await dbp.navigatesTo(recaptchaTargetPage);
94+
await dbp.receivesInlineAction(createGetRecaptchaInfoAction({ captchaType: 'invalid' }));
95+
const sucessResponse = await dbp.getSuccessResponse();
96+
97+
dbp.isCaptchaMatch(sucessResponse, { captchaType: 'recaptcha2', targetPage: recaptchaTargetPage });
98+
});
99+
});
100+
});
101+
102+
test.describe('solveCaptchaInfo', () => {
103+
test.describe('with useEnhancedCaptchaSystem: "enabled"', () => {
104+
test('solves the captcha for the correct action data', async ({ createConfiguredDbp }) => {
105+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemEnabled);
106+
await dbp.navigatesTo(recaptchaTargetPage);
107+
await dbp.receivesInlineAction(createSolveRecaptchaAction());
108+
dbp.getSuccessResponse();
109+
110+
await dbp.isCaptchaTokenFilled(recaptchaResponseSelector);
111+
});
112+
113+
test('solves the captcha for the correct action data without the "captchaType" field', async ({ createConfiguredDbp }) => {
114+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemEnabled);
115+
await dbp.navigatesTo(recaptchaTargetPage);
116+
await dbp.receivesInlineAction(createSolveRecaptchaAction({ captchaType: undefined }));
117+
dbp.getSuccessResponse();
118+
119+
await dbp.isCaptchaTokenFilled(recaptchaResponseSelector);
120+
});
121+
122+
test('solves the captcha for an action data when the "captchaType" field does not match the detected captcha type', async ({
123+
createConfiguredDbp,
124+
}) => {
125+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemEnabled);
126+
await dbp.navigatesTo(recaptchaTargetPage);
127+
await dbp.receivesInlineAction(createSolveRecaptchaAction({ captchaType: 'recaptchaEnterprise' }));
128+
dbp.getSuccessResponse();
129+
130+
await dbp.isCaptchaTokenFilled(recaptchaResponseSelector);
131+
});
132+
133+
test('returns an error response for an action data with an invalid "captchaType" field', async ({
134+
createConfiguredDbp,
135+
}) => {
136+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemEnabled);
137+
await dbp.navigatesTo(recaptchaTargetPage);
138+
await dbp.receivesInlineAction(createSolveRecaptchaAction({ captchaType: 'invalid' }));
139+
140+
await dbp.isCaptchaError();
141+
});
142+
});
143+
144+
test.describe('with useEnhancedCaptchaSystem: "disabled"', () => {
145+
test('solves the captcha for the correct action data', async ({ createConfiguredDbp }) => {
146+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemDisabled);
147+
await dbp.navigatesTo(recaptchaTargetPage);
148+
await dbp.receivesInlineAction(createSolveRecaptchaAction());
149+
dbp.getSuccessResponse();
150+
151+
await dbp.isCaptchaTokenFilled(recaptchaResponseSelector);
152+
});
153+
154+
test('solves the captcha for the correct action data without the "captchaType" field', async ({ createConfiguredDbp }) => {
155+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemDisabled);
156+
await dbp.navigatesTo(recaptchaTargetPage);
157+
await dbp.receivesInlineAction(createSolveRecaptchaAction({ captchaType: undefined }));
158+
dbp.getSuccessResponse();
159+
160+
await dbp.isCaptchaTokenFilled(recaptchaResponseSelector);
161+
});
162+
163+
test('solves the captcha for an action when the "captchaType" field does not match the detected captcha type', async ({
164+
createConfiguredDbp,
165+
}) => {
166+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemDisabled);
167+
await dbp.navigatesTo(recaptchaTargetPage);
168+
await dbp.receivesInlineAction(createSolveRecaptchaAction({ captchaType: 'recaptchaEnterprise' }));
169+
dbp.getSuccessResponse();
170+
171+
await dbp.isCaptchaTokenFilled(recaptchaResponseSelector);
172+
});
173+
174+
test('solves the captcha for an action data with an invalid "captchaType" field', async ({ createConfiguredDbp }) => {
175+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemDisabled);
176+
await dbp.navigatesTo(recaptchaTargetPage);
177+
await dbp.receivesInlineAction(createSolveRecaptchaAction({ captchaType: 'invalid' }));
178+
dbp.getSuccessResponse();
179+
180+
await dbp.isCaptchaTokenFilled(recaptchaResponseSelector);
181+
});
182+
});
183+
});
184+
185+
test('remove query params from captcha url', async ({ createConfiguredDbp }) => {
186+
const dbp = await createConfiguredDbp(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemEnabled);
187+
await dbp.navigatesTo('re-captcha.html?fname=john&lname=smith');
188+
await dbp.receivesInlineAction(createGetRecaptchaInfoAction());
189+
const sucessResponse = await dbp.getSuccessResponse();
190+
191+
dbp.isQueryParamRemoved(sucessResponse);
192+
});
193+
});
194+
});

injected/integration-test/broker-protection.spec.js renamed to injected/integration-test/broker-protection-tests/broker-protection.spec.js

+14-43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { test, expect } from '@playwright/test';
2-
import { BrokerProtectionPage } from './page-objects/broker-protection.js';
2+
import { BrokerProtectionPage } from '../page-objects/broker-protection.js';
3+
import { BROKER_PROTECTION_CONFIGS } from './tests-config.js';
34

45
test.describe('Broker Protection communications', () => {
56
test('sends an error when the action is not found', async ({ page }, workerInfo) => {
@@ -333,9 +334,19 @@ test.describe('Broker Protection communications', () => {
333334
});
334335
});
335336
test.describe('Executes action and sends success message', () => {
336-
test('buildUrl', async ({ page }, workerInfo) => {
337+
test('buildUrl with useEnhancedCaptchaSystem: "enabled"', async ({ page }, workerInfo) => {
337338
const dbp = BrokerProtectionPage.create(page, workerInfo.project.use);
338-
await dbp.enabled();
339+
await dbp.withFeatureConfig(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemEnabled);
340+
await dbp.navigatesTo('results.html');
341+
await dbp.receivesAction('navigate.json');
342+
const response = await dbp.collector.waitForMessage('actionCompleted');
343+
dbp.isSuccessMessage(response);
344+
dbp.isUrlMatch(response[0].payload.params.result.success.response);
345+
});
346+
347+
test('buildUrl with useEnhancedCaptchaSystem: "disabled"', async ({ page }, workerInfo) => {
348+
const dbp = BrokerProtectionPage.create(page, workerInfo.project.use);
349+
await dbp.withFeatureConfig(BROKER_PROTECTION_CONFIGS.enhancedCaptchaSystemDisabled);
339350
await dbp.navigatesTo('results.html');
340351
await dbp.receivesAction('navigate.json');
341352
const response = await dbp.collector.waitForMessage('actionCompleted');
@@ -509,46 +520,6 @@ test.describe('Broker Protection communications', () => {
509520
dbp.isSuccessMessage(response);
510521
});
511522

512-
test('getCaptchaInfo', async ({ page }, workerInfo) => {
513-
const dbp = BrokerProtectionPage.create(page, workerInfo.project.use);
514-
await dbp.enabled();
515-
await dbp.navigatesTo('captcha.html');
516-
await dbp.receivesAction('get-captcha.json');
517-
const response = await dbp.collector.waitForMessage('actionCompleted');
518-
dbp.isSuccessMessage(response);
519-
dbp.isCaptchaMatch(response[0].payload?.params.result.success.response);
520-
});
521-
522-
test('getCaptchaInfo (hcaptcha)', async ({ page }, workerInfo) => {
523-
const dbp = BrokerProtectionPage.create(page, workerInfo.project.use);
524-
await dbp.enabled();
525-
await dbp.navigatesTo('captcha2.html');
526-
await dbp.receivesAction('get-captcha.json');
527-
const response = await dbp.collector.waitForMessage('actionCompleted');
528-
dbp.isSuccessMessage(response);
529-
dbp.isHCaptchaMatch(response[0].payload?.params.result.success.response);
530-
});
531-
532-
test('remove query params from captcha url', async ({ page }, workerInfo) => {
533-
const dbp = BrokerProtectionPage.create(page, workerInfo.project.use);
534-
await dbp.enabled();
535-
await dbp.navigatesTo('captcha.html?fname=john&lname=smith');
536-
await dbp.receivesAction('get-captcha.json');
537-
const response = await dbp.collector.waitForMessage('actionCompleted');
538-
dbp.isSuccessMessage(response);
539-
dbp.isQueryParamRemoved(response[0].payload?.params.result.success.response);
540-
});
541-
542-
test('solveCaptcha', async ({ page }, workerInfo) => {
543-
const dbp = BrokerProtectionPage.create(page, workerInfo.project.use);
544-
await dbp.enabled();
545-
await dbp.navigatesTo('captcha.html');
546-
await dbp.receivesAction('solve-captcha.json');
547-
const response = await dbp.collector.waitForMessage('actionCompleted');
548-
dbp.isSuccessMessage(response);
549-
await dbp.isCaptchaTokenFilled();
550-
});
551-
552523
test('expectation', async ({ page }, workerInfo) => {
553524
const dbp = BrokerProtectionPage.create(page, workerInfo.project.use);
554525
await dbp.enabled();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { BrokerProtectionPage } from '../page-objects/broker-protection.js';
2+
/**
3+
* @import {PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, TestType} from "@playwright/test"
4+
*/
5+
6+
/**
7+
* @param {typeof import("@playwright/test").test} test
8+
* @return {TestType<PlaywrightTestArgs & PlaywrightTestOptions & PlaywrightWorkerArgs & PlaywrightWorkerOptions & { createConfiguredDbp: (config: Record<string, any>) => Promise<BrokerProtectionPage> }, {}>}
9+
*/
10+
export function createConfiguredDbpTest(test) {
11+
return test.extend({
12+
createConfiguredDbp: async ({ page }, use, workerInfo) => {
13+
/**
14+
* @param {Record<string, any>} config
15+
*/
16+
const createWithConfig = async (config) => {
17+
const dbp = BrokerProtectionPage.create(page, workerInfo.project.use);
18+
await dbp.withFeatureConfig(config);
19+
return dbp;
20+
};
21+
22+
await use(createWithConfig);
23+
},
24+
});
25+
}
26+
27+
/**
28+
* @param {typeof import("@playwright/test").test} test
29+
* @return {TestType<PlaywrightTestArgs & PlaywrightTestOptions & PlaywrightWorkerArgs & PlaywrightWorkerOptions & { createConfiguredDbp: (config: Record<string, any>) => Promise<BrokerProtectionPage> }, {}>}
30+
*/
31+
export function createConfiguredDbpTestWithNavigation(test) {
32+
return test.extend({
33+
createConfiguredDbp: async ({ page }, use, workerInfo) => {
34+
/**
35+
* @param {object} params
36+
* @param {string} params.targetPage
37+
* @param {string|object} params.action
38+
* @param {Record<string, any>} params.config
39+
*/
40+
const createWithConfig = async (params) => {
41+
const { config, targetPage, action } = params;
42+
const dbp = BrokerProtectionPage.create(page, workerInfo.project.use);
43+
await dbp.withFeatureConfig(config);
44+
await dbp.navigatesTo(targetPage);
45+
if (typeof action === 'string') {
46+
dbp.receivesAction(action);
47+
} else {
48+
await dbp.receivesInlineAction(action);
49+
}
50+
return dbp;
51+
};
52+
53+
await use(createWithConfig);
54+
},
55+
});
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createFeatureConfig } from '../mocks/broker-protection/feature-config';
2+
3+
export const BROKER_PROTECTION_CONFIGS = Object.freeze({
4+
enhancedCaptchaSystemEnabled: createFeatureConfig({
5+
state: 'enabled',
6+
settings: {
7+
useEnhancedCaptchaSystem: 'enabled',
8+
},
9+
}),
10+
enhancedCaptchaSystemDisabled: createFeatureConfig({
11+
state: 'enabled',
12+
settings: {
13+
useEnhancedCaptchaSystem: 'disabled',
14+
},
15+
}),
16+
});

0 commit comments

Comments
 (0)