@prosopo/procaptcha-frictionless 2.18.7 → 2.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build$colon$cjs.log +18 -17
- package/.turbo/turbo-build$colon$tsc.log +32 -29
- package/.turbo/turbo-build.log +19 -21
- package/CHANGELOG.md +87 -0
- package/LICENSE +201 -0
- package/dist/cjs/customDetectBot.cjs +6 -4
- package/dist/cjs/procaptchaFrictionless.cjs +79 -40
- package/dist/customDetectBot.d.ts.map +1 -1
- package/dist/customDetectBot.js +6 -4
- package/dist/customDetectBot.js.map +1 -1
- package/dist/procaptchaFrictionless.d.ts.map +1 -1
- package/dist/procaptchaFrictionless.js +80 -41
- package/dist/procaptchaFrictionless.js.map +1 -1
- package/dist/tests/clickWhileSolverLoads.test.d.ts +2 -0
- package/dist/tests/clickWhileSolverLoads.test.d.ts.map +1 -0
- package/dist/tests/clickWhileSolverLoads.test.js +93 -0
- package/dist/tests/clickWhileSolverLoads.test.js.map +1 -0
- package/dist/tests/procaptchaFrictionless.test.js +104 -1
- package/dist/tests/procaptchaFrictionless.test.js.map +1 -1
- package/dist/tests/sessionInvalidatedRemint.test.js +26 -0
- package/dist/tests/sessionInvalidatedRemint.test.js.map +1 -1
- package/dist/tests/startMode.test.d.ts +2 -0
- package/dist/tests/startMode.test.d.ts.map +1 -0
- package/dist/tests/{manualStart.test.js → startMode.test.js} +68 -1
- package/dist/tests/startMode.test.js.map +1 -0
- package/package.json +72 -69
- package/dist/tests/manualStart.test.d.ts +0 -2
- package/dist/tests/manualStart.test.d.ts.map +0 -1
- package/dist/tests/manualStart.test.js.map +0 -1
- package/src/authenticatedBadge.ts +0 -170
- package/src/customDetectBot.ts +0 -392
- package/src/detectorLoader.ts +0 -133
- package/src/detectorPrefetch.ts +0 -115
- package/src/frictionlessResultGuard.ts +0 -110
- package/src/index.ts +0 -15
- package/src/procaptchaFrictionless.ts +0 -626
- package/src/sessionInvalidatedRecovery.ts +0 -110
- package/src/tests/authenticatedBadge.unit.test.ts +0 -101
- package/src/tests/customDetectBot.test.ts +0 -77
- package/src/tests/customDetectBotAssignTimeout.test.ts +0 -185
- package/src/tests/customDetectBotSimd.test.ts +0 -173
- package/src/tests/detectorLoader.test.ts +0 -108
- package/src/tests/detectorPrefetch.test.ts +0 -114
- package/src/tests/escalationClientSession.test.ts +0 -216
- package/src/tests/escalationHandoff.integration.test.ts +0 -356
- package/src/tests/executeBeforeMount.test.ts +0 -233
- package/src/tests/frictionlessResultGuard.test.ts +0 -179
- package/src/tests/manualStart.test.ts +0 -391
- package/src/tests/procaptchaFrictionless.test.ts +0 -468
- package/src/tests/sessionInvalidatedRecovery.test.ts +0 -181
- package/src/tests/sessionInvalidatedRemint.test.ts +0 -272
- package/tsconfig.cjs.json +0 -41
- package/tsconfig.json +0 -49
- package/tsconfig.tsbuildinfo +0 -1
- package/tsconfig.types.json +0 -9
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import { describe, expect, it } from "vitest";
|
|
16
|
-
import {
|
|
17
|
-
MISSING_CAPTCHA_TYPE_MESSAGE,
|
|
18
|
-
evaluateFrictionlessResult,
|
|
19
|
-
} from "../frictionlessResultGuard.js";
|
|
20
|
-
|
|
21
|
-
// This guard exists so a `/frictionless` response with no `captchaType`
|
|
22
|
-
// (the shape emitted by request-time short-circuits — access-policy
|
|
23
|
-
// hard-block, decision-machine autoBan, domain / header middleware
|
|
24
|
-
// rejections — whose `{ error: "..." }` bare-string bodies slip past
|
|
25
|
-
// `error.message` inspection) cannot fall through into
|
|
26
|
-
// `renderForCaptchaType(undefined, ...)` and mount ProcaptchaPow with an
|
|
27
|
-
// undefined sessionId, which the provider then rejects as
|
|
28
|
-
// API.INCORRECT_CAPTCHA_TYPE for a frictionless sitekey.
|
|
29
|
-
|
|
30
|
-
describe("evaluateFrictionlessResult", () => {
|
|
31
|
-
it("passes a well-formed response with a captchaType", () => {
|
|
32
|
-
expect(
|
|
33
|
-
evaluateFrictionlessResult({
|
|
34
|
-
captchaType: "pow",
|
|
35
|
-
}),
|
|
36
|
-
).toEqual({ kind: "pass" });
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("passes when a captchaType is present even if error is a bare undefined", () => {
|
|
40
|
-
expect(
|
|
41
|
-
evaluateFrictionlessResult({
|
|
42
|
-
captchaType: "image",
|
|
43
|
-
error: undefined,
|
|
44
|
-
}),
|
|
45
|
-
).toEqual({ kind: "pass" });
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("returns a structured error when the server emits `{ error: { message } }`", () => {
|
|
49
|
-
expect(
|
|
50
|
-
evaluateFrictionlessResult({
|
|
51
|
-
captchaType: "pow",
|
|
52
|
-
error: { message: "Boom", key: "API.SOMETHING" },
|
|
53
|
-
}),
|
|
54
|
-
).toEqual({
|
|
55
|
-
kind: "error",
|
|
56
|
-
message: "Boom",
|
|
57
|
-
key: "API.SOMETHING",
|
|
58
|
-
retryable: true,
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it("omits key from the outcome when the server error carries no key", () => {
|
|
63
|
-
const outcome = evaluateFrictionlessResult({
|
|
64
|
-
captchaType: "pow",
|
|
65
|
-
error: { message: "Boom" },
|
|
66
|
-
});
|
|
67
|
-
expect(outcome.kind).toBe("error");
|
|
68
|
-
if (outcome.kind !== "error") throw new Error("expected error outcome");
|
|
69
|
-
expect(outcome.message).toBe("Boom");
|
|
70
|
-
expect("key" in outcome).toBe(false);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("halts when captchaType is missing (bare-string 401 body shape from access-policy / autoBan / middleware)", () => {
|
|
74
|
-
// Access-policy hard-block / decision-machine autoBan / domain
|
|
75
|
-
// middleware all return `{"error":"Unauthorized"}` — a bare
|
|
76
|
-
// string, not `{message}`, so `error.message` is undefined and
|
|
77
|
-
// would slip past the earlier error check. HttpClientBase does
|
|
78
|
-
// not throw on 4xx JSON so the widget receives this as a
|
|
79
|
-
// valid-looking result.
|
|
80
|
-
expect(
|
|
81
|
-
evaluateFrictionlessResult({
|
|
82
|
-
// Simulating the JSON parse: `error` came in as a string on
|
|
83
|
-
// the wire but is not the object shape the guard reads.
|
|
84
|
-
error: undefined,
|
|
85
|
-
}),
|
|
86
|
-
).toEqual({
|
|
87
|
-
kind: "error",
|
|
88
|
-
message: MISSING_CAPTCHA_TYPE_MESSAGE,
|
|
89
|
-
retryable: false,
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it("halts when both captchaType and error are absent (opaque parse failure)", () => {
|
|
94
|
-
expect(evaluateFrictionlessResult({})).toEqual({
|
|
95
|
-
kind: "error",
|
|
96
|
-
message: MISSING_CAPTCHA_TYPE_MESSAGE,
|
|
97
|
-
retryable: false,
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it("prefers the structured error over the missing-captchaType message when both apply", () => {
|
|
102
|
-
// If the server ever emits BOTH a structured error and no captchaType,
|
|
103
|
-
// the structured error is the more informative signal.
|
|
104
|
-
expect(
|
|
105
|
-
evaluateFrictionlessResult({
|
|
106
|
-
error: { message: "Server error", key: "API.SERVER_ERROR" },
|
|
107
|
-
}),
|
|
108
|
-
).toEqual({
|
|
109
|
-
kind: "error",
|
|
110
|
-
message: "Server error",
|
|
111
|
-
key: "API.SERVER_ERROR",
|
|
112
|
-
retryable: true,
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
describe("retryable classification", () => {
|
|
117
|
-
const retryableOf = (key?: string): boolean => {
|
|
118
|
-
const outcome = evaluateFrictionlessResult({
|
|
119
|
-
error: { message: "msg", ...(key !== undefined && { key }) },
|
|
120
|
-
});
|
|
121
|
-
if (outcome.kind !== "error") throw new Error("expected error outcome");
|
|
122
|
-
return outcome.retryable;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
it.each([
|
|
126
|
-
"API.SITE_KEY_NOT_REGISTERED",
|
|
127
|
-
"API.INVALID_SITE_KEY",
|
|
128
|
-
"API.UNAUTHORIZED_ORIGIN_URL",
|
|
129
|
-
"API.INCORRECT_CAPTCHA_TYPE",
|
|
130
|
-
])("shows integration fault %s rather than re-rolling", (key: string) => {
|
|
131
|
-
// Another provider returns the same answer, and the text is what
|
|
132
|
-
// tells the site owner what to change.
|
|
133
|
-
expect(retryableOf(key)).toBe(false);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it.each([
|
|
137
|
-
"API.ACCESS_POLICY_BLOCK",
|
|
138
|
-
"API.ABUSER_BLOCKED",
|
|
139
|
-
"API.CRAWLER_BLOCKED",
|
|
140
|
-
"API.DATACENTER_BLOCKED",
|
|
141
|
-
"API.DISALLOWED_WEBVIEW",
|
|
142
|
-
"API.MOBILE_BLOCKED",
|
|
143
|
-
"API.PROXY_BLOCKED",
|
|
144
|
-
"API.SATELLITE_BLOCKED",
|
|
145
|
-
"API.TOR_BLOCKED",
|
|
146
|
-
"API.VPN_BLOCKED",
|
|
147
|
-
"API.FORBIDDEN",
|
|
148
|
-
"API.UNAUTHORIZED",
|
|
149
|
-
])("does not re-roll policy denial %s", (key: string) => {
|
|
150
|
-
// Every provider shares the decision; retrying would hammer the
|
|
151
|
-
// fleet on behalf of traffic already refused.
|
|
152
|
-
expect(retryableOf(key)).toBe(false);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it("leaves CAPTCHA.NO_SESSION_FOUND to its own restart timer", () => {
|
|
156
|
-
expect(retryableOf("CAPTCHA.NO_SESSION_FOUND")).toBe(false);
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it("re-rolls API.BAD_REQUEST, which is what an unhandled provider throw becomes", () => {
|
|
160
|
-
// An unhandled throw in a handler serialises to this with a 400, and
|
|
161
|
-
// the client does not throw on a 400 with a JSON body — so without
|
|
162
|
-
// re-rolling, one unhealthy provider stranded the user on the first
|
|
163
|
-
// response while the rest of the fleet was up.
|
|
164
|
-
expect(retryableOf("API.BAD_REQUEST")).toBe(true);
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
it.each(["API.UNKNOWN", "API.INTERNAL_SERVER_ERROR", "DATABASE.UNKNOWN"])(
|
|
168
|
-
"re-rolls unrecognised provider fault %s",
|
|
169
|
-
(key: string) => {
|
|
170
|
-
expect(retryableOf(key)).toBe(true);
|
|
171
|
-
},
|
|
172
|
-
);
|
|
173
|
-
|
|
174
|
-
it("does not re-roll an error with no key at all", () => {
|
|
175
|
-
// Hard blocks arrive as a bare `{ error: "..." }` string with no key.
|
|
176
|
-
expect(retryableOf(undefined)).toBe(false);
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
});
|
|
@@ -1,391 +0,0 @@
|
|
|
1
|
-
// Copyright 2021-2026 Prosopo (UK) Ltd.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { Ti18n } from "@prosopo/locale";
|
|
16
|
-
import {
|
|
17
|
-
type Account,
|
|
18
|
-
type BotDetectionFunction,
|
|
19
|
-
type BotDetectionFunctionResult,
|
|
20
|
-
CaptchaType,
|
|
21
|
-
ModeEnum,
|
|
22
|
-
PROCAPTCHA_START_EVENT,
|
|
23
|
-
type ProcaptchaClientConfigOutput,
|
|
24
|
-
type ProcaptchaFrictionlessProps,
|
|
25
|
-
type ProcaptchaProps,
|
|
26
|
-
type ProcaptchaStartEventDetail,
|
|
27
|
-
type RandomProvider,
|
|
28
|
-
StartModeEnum,
|
|
29
|
-
} from "@prosopo/types";
|
|
30
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
31
|
-
|
|
32
|
-
type InnerWidget = "pow" | "image" | "puzzle";
|
|
33
|
-
|
|
34
|
-
const mocks = vi.hoisted(() => {
|
|
35
|
-
const mounts: {
|
|
36
|
-
widget: "pow" | "image" | "puzzle";
|
|
37
|
-
props: ProcaptchaProps;
|
|
38
|
-
}[] = [];
|
|
39
|
-
const stub =
|
|
40
|
-
(widget: "pow" | "image" | "puzzle") =>
|
|
41
|
-
(container: HTMLElement, props: ProcaptchaProps) => {
|
|
42
|
-
mounts.push({ widget, props });
|
|
43
|
-
const element = container.ownerDocument.createElement("div");
|
|
44
|
-
element.setAttribute("data-widget", widget);
|
|
45
|
-
container.appendChild(element);
|
|
46
|
-
return { destroy: () => element.remove() };
|
|
47
|
-
};
|
|
48
|
-
return { mounts, stub };
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
vi.mock("@prosopo/procaptcha-pow", () => ({
|
|
52
|
-
mountProcaptchaPowWidget: mocks.stub("pow"),
|
|
53
|
-
}));
|
|
54
|
-
vi.mock("@prosopo/procaptcha-react", () => ({
|
|
55
|
-
mountProcaptchaImageWidget: mocks.stub("image"),
|
|
56
|
-
}));
|
|
57
|
-
vi.mock("@prosopo/procaptcha-puzzle", () => ({
|
|
58
|
-
mountProcaptchaPuzzleWidget: mocks.stub("puzzle"),
|
|
59
|
-
}));
|
|
60
|
-
|
|
61
|
-
// jsdom serves the suite over http, so the widget's own HTTPS guard would
|
|
62
|
-
// short-circuit every test before detection ran.
|
|
63
|
-
vi.mock("@prosopo/procaptcha-common", async (importOriginal) => ({
|
|
64
|
-
...(await importOriginal<typeof import("@prosopo/procaptcha-common")>()),
|
|
65
|
-
isSecureBrowserContext: () => true,
|
|
66
|
-
}));
|
|
67
|
-
|
|
68
|
-
const { mountProcaptchaFrictionless } = await import(
|
|
69
|
-
"../procaptchaFrictionless.js"
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
const SITE_KEY = "5siteKey";
|
|
73
|
-
const SESSION_ID = "provider-session";
|
|
74
|
-
|
|
75
|
-
const config = (
|
|
76
|
-
overrides: Partial<ProcaptchaClientConfigOutput> = {},
|
|
77
|
-
): ProcaptchaClientConfigOutput =>
|
|
78
|
-
({
|
|
79
|
-
account: { address: SITE_KEY },
|
|
80
|
-
userAccountAddress: "",
|
|
81
|
-
web2: true,
|
|
82
|
-
theme: "light",
|
|
83
|
-
mode: ModeEnum.visible,
|
|
84
|
-
startMode: StartModeEnum.manual,
|
|
85
|
-
...overrides,
|
|
86
|
-
}) as unknown as ProcaptchaClientConfigOutput;
|
|
87
|
-
|
|
88
|
-
const i18nStub = {
|
|
89
|
-
isInitialized: true,
|
|
90
|
-
language: "en",
|
|
91
|
-
t: (key: string) => key,
|
|
92
|
-
changeLanguage: vi.fn(),
|
|
93
|
-
} as unknown as Ti18n;
|
|
94
|
-
|
|
95
|
-
const detectionResult = (): BotDetectionFunctionResult =>
|
|
96
|
-
({
|
|
97
|
-
status: "ok",
|
|
98
|
-
captchaType: CaptchaType.image,
|
|
99
|
-
sessionId: SESSION_ID,
|
|
100
|
-
provider: { provider: { url: "https://provider.test" } } as RandomProvider,
|
|
101
|
-
userAccount: { account: { address: "5FakeUserAccountAddress" } } as Account,
|
|
102
|
-
}) as unknown as BotDetectionFunctionResult;
|
|
103
|
-
|
|
104
|
-
const detectBotResolving = () =>
|
|
105
|
-
vi.fn<BotDetectionFunction>().mockResolvedValue(detectionResult());
|
|
106
|
-
|
|
107
|
-
let host: HTMLDivElement;
|
|
108
|
-
let widget: { destroy: () => void } | undefined;
|
|
109
|
-
|
|
110
|
-
const settle = async (): Promise<void> => {
|
|
111
|
-
await new Promise<void>((resolve: () => void) => setTimeout(resolve, 0));
|
|
112
|
-
await Promise.resolve();
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
interface MountOptions {
|
|
116
|
-
config?: ProcaptchaClientConfigOutput;
|
|
117
|
-
detectBot?: ReturnType<typeof detectBotResolving>;
|
|
118
|
-
container?: HTMLElement;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const mountWrapper = async (
|
|
122
|
-
options: MountOptions = {},
|
|
123
|
-
): Promise<ReturnType<typeof detectBotResolving>> => {
|
|
124
|
-
const detectBot = options.detectBot ?? detectBotResolving();
|
|
125
|
-
const props: ProcaptchaFrictionlessProps = {
|
|
126
|
-
config: options.config ?? config(),
|
|
127
|
-
callbacks: {},
|
|
128
|
-
restart: vi.fn(),
|
|
129
|
-
i18n: i18nStub,
|
|
130
|
-
detectBot,
|
|
131
|
-
container: options.container,
|
|
132
|
-
};
|
|
133
|
-
widget = mountProcaptchaFrictionless(host, props);
|
|
134
|
-
await settle();
|
|
135
|
-
return detectBot;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const checkbox = (): HTMLInputElement => {
|
|
139
|
-
const element = host.querySelector<HTMLInputElement>(
|
|
140
|
-
'[data-cy="captcha-checkbox"]',
|
|
141
|
-
);
|
|
142
|
-
if (!element) throw new Error("expected the checkbox to be on the page");
|
|
143
|
-
return element;
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
const spinner = (): Element | null => host.querySelector('[role="status"]');
|
|
147
|
-
|
|
148
|
-
const lastMountOf = (target: InnerWidget) => {
|
|
149
|
-
const mount = mocks.mounts.filter((m) => m.widget === target).at(-1);
|
|
150
|
-
if (!mount) throw new Error(`expected the ${target} widget to have mounted`);
|
|
151
|
-
return mount;
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
// jsdom exposes `isTrusted` as a non-configurable accessor on its internal
|
|
155
|
-
// implementation object, so the flag has to be pinned there.
|
|
156
|
-
const setTrusted = (event: Event, trusted: boolean): void => {
|
|
157
|
-
for (const symbol of Object.getOwnPropertySymbols(event)) {
|
|
158
|
-
const impl: unknown = Reflect.get(event, symbol);
|
|
159
|
-
if (impl && typeof impl === "object" && "isTrusted" in impl) {
|
|
160
|
-
Object.defineProperty(impl, "isTrusted", {
|
|
161
|
-
configurable: true,
|
|
162
|
-
get: () => trusted,
|
|
163
|
-
set: () => undefined,
|
|
164
|
-
});
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
throw new Error("could not reach the jsdom event implementation");
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
interface ClickOptions {
|
|
172
|
-
trusted?: boolean;
|
|
173
|
-
clientX?: number;
|
|
174
|
-
clientY?: number;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const click = async (
|
|
178
|
-
element: Element,
|
|
179
|
-
options: ClickOptions = {},
|
|
180
|
-
): Promise<void> => {
|
|
181
|
-
const event = new MouseEvent("click", {
|
|
182
|
-
bubbles: true,
|
|
183
|
-
cancelable: true,
|
|
184
|
-
clientX: options.clientX ?? 0,
|
|
185
|
-
clientY: options.clientY ?? 0,
|
|
186
|
-
});
|
|
187
|
-
setTrusted(event, options.trusted ?? true);
|
|
188
|
-
element.dispatchEvent(event);
|
|
189
|
-
await settle();
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
const dispatchStart = async (
|
|
193
|
-
detail?: ProcaptchaStartEventDetail,
|
|
194
|
-
): Promise<void> => {
|
|
195
|
-
document.dispatchEvent(
|
|
196
|
-
new CustomEvent<ProcaptchaStartEventDetail>(PROCAPTCHA_START_EVENT, {
|
|
197
|
-
detail,
|
|
198
|
-
bubbles: true,
|
|
199
|
-
}),
|
|
200
|
-
);
|
|
201
|
-
await settle();
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const dispatchExecute = async (): Promise<void> => {
|
|
205
|
-
document.dispatchEvent(new CustomEvent("procaptcha:execute"));
|
|
206
|
-
await settle();
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
beforeEach(() => {
|
|
210
|
-
mocks.mounts.length = 0;
|
|
211
|
-
widget = undefined;
|
|
212
|
-
host = document.createElement("div");
|
|
213
|
-
document.body.appendChild(host);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
afterEach(() => {
|
|
217
|
-
widget?.destroy();
|
|
218
|
-
widget = undefined;
|
|
219
|
-
host.remove();
|
|
220
|
-
vi.clearAllMocks();
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
describe("manual start mode", () => {
|
|
224
|
-
it("mounts a live checkbox without running detection", async () => {
|
|
225
|
-
const detectBot = await mountWrapper();
|
|
226
|
-
|
|
227
|
-
expect(detectBot).not.toHaveBeenCalled();
|
|
228
|
-
expect(mocks.mounts).toHaveLength(0);
|
|
229
|
-
expect(checkbox().disabled).toBe(false);
|
|
230
|
-
expect(spinner()).toBeNull();
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
it("still runs detection on mount in auto mode", async () => {
|
|
234
|
-
const detectBot = await mountWrapper({
|
|
235
|
-
config: config({ startMode: StartModeEnum.auto }),
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
239
|
-
expect(lastMountOf("image").props.autoStart).toBe(false);
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
it("defaults to auto when no start mode is given", async () => {
|
|
243
|
-
const detectBot = await mountWrapper({
|
|
244
|
-
config: config({ startMode: undefined }),
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
describe("started by the site", () => {
|
|
251
|
-
it("runs detection on procaptcha:start and leaves the widget waiting for a click", async () => {
|
|
252
|
-
const detectBot = await mountWrapper();
|
|
253
|
-
|
|
254
|
-
await dispatchStart();
|
|
255
|
-
|
|
256
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
257
|
-
const mount = lastMountOf("image").props;
|
|
258
|
-
expect(mount.autoStart).toBe(false);
|
|
259
|
-
expect(mount.startCoords).toBeUndefined();
|
|
260
|
-
expect(mount.frictionlessState?.sessionId).toBe(SESSION_ID);
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
it("honours an event addressed to its own element", async () => {
|
|
264
|
-
const widgetElement = document.createElement("div");
|
|
265
|
-
const container = document.createElement("div");
|
|
266
|
-
widgetElement.appendChild(container);
|
|
267
|
-
const detectBot = await mountWrapper({ container });
|
|
268
|
-
|
|
269
|
-
await dispatchStart({ element: widgetElement });
|
|
270
|
-
|
|
271
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
it("ignores an event addressed to another widget's element", async () => {
|
|
275
|
-
const container = document.createElement("div");
|
|
276
|
-
document.createElement("div").appendChild(container);
|
|
277
|
-
const detectBot = await mountWrapper({ container });
|
|
278
|
-
|
|
279
|
-
await dispatchStart({ element: document.createElement("div") });
|
|
280
|
-
|
|
281
|
-
expect(detectBot).not.toHaveBeenCalled();
|
|
282
|
-
expect(checkbox().disabled).toBe(false);
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
it("receives every event when mounted without a container", async () => {
|
|
286
|
-
const detectBot = await mountWrapper();
|
|
287
|
-
|
|
288
|
-
await dispatchStart({ element: document.createElement("div") });
|
|
289
|
-
|
|
290
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
it("opens the challenge straight away on procaptcha:execute", async () => {
|
|
294
|
-
const detectBot = await mountWrapper({
|
|
295
|
-
config: config({ mode: ModeEnum.invisible }),
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
await dispatchExecute();
|
|
299
|
-
|
|
300
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
301
|
-
expect(lastMountOf("image").props.autoStart).toBe(true);
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
it("ignores the events once started", async () => {
|
|
305
|
-
const detectBot = await mountWrapper();
|
|
306
|
-
|
|
307
|
-
await dispatchStart();
|
|
308
|
-
await dispatchStart();
|
|
309
|
-
await dispatchExecute();
|
|
310
|
-
|
|
311
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
312
|
-
expect(mocks.mounts).toHaveLength(1);
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
it("does not react to the events in auto mode", async () => {
|
|
316
|
-
const detectBot = await mountWrapper({
|
|
317
|
-
config: config({ startMode: StartModeEnum.auto }),
|
|
318
|
-
});
|
|
319
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
320
|
-
|
|
321
|
-
await dispatchStart();
|
|
322
|
-
|
|
323
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
324
|
-
});
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
describe("started by the user", () => {
|
|
328
|
-
it("runs detection on a checkbox click and opens the challenge where the user clicked", async () => {
|
|
329
|
-
const detectBot = await mountWrapper();
|
|
330
|
-
|
|
331
|
-
await click(checkbox(), { clientX: 11, clientY: 22 });
|
|
332
|
-
|
|
333
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
334
|
-
const mount = lastMountOf("image").props;
|
|
335
|
-
expect(mount.autoStart).toBe(true);
|
|
336
|
-
expect(mount.startCoords).toEqual({ x: 11, y: 22 });
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
it("opens the challenge after a keyboard activation with no position", async () => {
|
|
340
|
-
const detectBot = await mountWrapper();
|
|
341
|
-
|
|
342
|
-
await click(checkbox());
|
|
343
|
-
|
|
344
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
345
|
-
const mount = lastMountOf("image").props;
|
|
346
|
-
expect(mount.autoStart).toBe(true);
|
|
347
|
-
expect(mount.startCoords).toBeUndefined();
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it("shows the spinner while the deferred flow runs", async () => {
|
|
351
|
-
let finish: ((result: BotDetectionFunctionResult) => void) | undefined;
|
|
352
|
-
const detectBot = vi.fn<BotDetectionFunction>().mockImplementation(
|
|
353
|
-
() =>
|
|
354
|
-
new Promise<BotDetectionFunctionResult>((resolve) => {
|
|
355
|
-
finish = resolve;
|
|
356
|
-
}),
|
|
357
|
-
);
|
|
358
|
-
await mountWrapper({ detectBot });
|
|
359
|
-
|
|
360
|
-
await click(checkbox(), { clientX: 1, clientY: 1 });
|
|
361
|
-
|
|
362
|
-
expect(spinner()).not.toBeNull();
|
|
363
|
-
expect(mocks.mounts).toHaveLength(0);
|
|
364
|
-
|
|
365
|
-
finish?.(detectionResult());
|
|
366
|
-
await settle();
|
|
367
|
-
|
|
368
|
-
expect(spinner()).toBeNull();
|
|
369
|
-
expect(lastMountOf("image").props.autoStart).toBe(true);
|
|
370
|
-
});
|
|
371
|
-
|
|
372
|
-
it("ignores a synthetic click", async () => {
|
|
373
|
-
const detectBot = await mountWrapper();
|
|
374
|
-
|
|
375
|
-
await click(checkbox(), { trusted: false, clientX: 1, clientY: 1 });
|
|
376
|
-
|
|
377
|
-
expect(detectBot).not.toHaveBeenCalled();
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
it("starts once even if the site also asks", async () => {
|
|
381
|
-
const detectBot = await mountWrapper();
|
|
382
|
-
|
|
383
|
-
await click(checkbox(), { clientX: 3, clientY: 4 });
|
|
384
|
-
await dispatchStart();
|
|
385
|
-
|
|
386
|
-
expect(detectBot).toHaveBeenCalledTimes(1);
|
|
387
|
-
expect(mocks.mounts).toHaveLength(1);
|
|
388
|
-
expect(lastMountOf("image").props.startCoords).toEqual({ x: 3, y: 4 });
|
|
389
|
-
});
|
|
390
|
-
});
|
|
391
|
-
});
|