@prosopo/procaptcha-frictionless 2.17.0 → 2.18.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 +6 -6
- package/.turbo/turbo-build$colon$tsc.log +24 -24
- package/.turbo/turbo-build.log +12 -12
- package/CHANGELOG.md +47 -0
- package/dist/{AuthenticatedBadge.d.ts → authenticatedBadge.d.ts} +3 -3
- package/dist/authenticatedBadge.d.ts.map +1 -0
- package/dist/authenticatedBadge.js +79 -0
- package/dist/authenticatedBadge.js.map +1 -0
- package/dist/cjs/authenticatedBadge.cjs +79 -0
- package/dist/cjs/detectorPrefetch.cjs +2 -2
- package/dist/cjs/index.cjs +2 -2
- package/dist/cjs/procaptchaFrictionless.cjs +269 -0
- package/dist/detectorPrefetch.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/procaptchaFrictionless.d.ts +6 -0
- package/dist/procaptchaFrictionless.d.ts.map +1 -0
- package/dist/procaptchaFrictionless.js +269 -0
- package/dist/procaptchaFrictionless.js.map +1 -0
- package/dist/tests/escalationClientSession.test.d.ts +0 -3
- package/dist/tests/escalationClientSession.test.d.ts.map +1 -1
- package/dist/tests/escalationClientSession.test.js +52 -61
- package/dist/tests/escalationClientSession.test.js.map +1 -1
- package/dist/tests/escalationHandoff.integration.test.d.ts +0 -3
- package/dist/tests/escalationHandoff.integration.test.d.ts.map +1 -1
- package/dist/tests/escalationHandoff.integration.test.js +62 -61
- package/dist/tests/escalationHandoff.integration.test.js.map +1 -1
- package/dist/tests/executeBeforeMount.test.d.ts +0 -3
- package/dist/tests/executeBeforeMount.test.d.ts.map +1 -1
- package/dist/tests/executeBeforeMount.test.js +53 -49
- package/dist/tests/executeBeforeMount.test.js.map +1 -1
- package/dist/tests/manualStart.test.d.ts +0 -3
- package/dist/tests/manualStart.test.d.ts.map +1 -1
- package/dist/tests/manualStart.test.js +55 -52
- package/dist/tests/manualStart.test.js.map +1 -1
- package/dist/tests/procaptchaFrictionless.test.d.ts +2 -0
- package/dist/tests/procaptchaFrictionless.test.d.ts.map +1 -0
- package/dist/tests/procaptchaFrictionless.test.js +244 -0
- package/dist/tests/procaptchaFrictionless.test.js.map +1 -0
- package/dist/tests/sessionInvalidatedRemint.test.d.ts +0 -3
- package/dist/tests/sessionInvalidatedRemint.test.d.ts.map +1 -1
- package/dist/tests/sessionInvalidatedRemint.test.js +49 -43
- package/dist/tests/sessionInvalidatedRemint.test.js.map +1 -1
- package/package.json +11 -13
- package/src/authenticatedBadge.ts +151 -0
- package/src/customDetectBot.ts +2 -2
- package/src/detectorPrefetch.ts +2 -2
- package/src/index.ts +1 -1
- package/src/{ProcaptchaFrictionless.tsx → procaptchaFrictionless.ts} +285 -314
- package/src/tests/{escalationClientSession.test.tsx → escalationClientSession.test.ts} +83 -92
- package/src/tests/{escalationHandoff.integration.test.tsx → escalationHandoff.integration.test.ts} +110 -108
- package/src/tests/{executeBeforeMount.test.tsx → executeBeforeMount.test.ts} +79 -77
- package/src/tests/{manualStart.test.tsx → manualStart.test.ts} +87 -83
- package/src/tests/procaptchaFrictionless.test.ts +408 -0
- package/src/tests/{sessionInvalidatedRemint.test.tsx → sessionInvalidatedRemint.test.ts} +67 -64
- package/tsconfig.cjs.json +1 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/AuthenticatedBadge.d.ts.map +0 -1
- package/dist/AuthenticatedBadge.js +0 -75
- package/dist/AuthenticatedBadge.js.map +0 -1
- package/dist/ProcaptchaFrictionless.d.ts +0 -3
- package/dist/ProcaptchaFrictionless.d.ts.map +0 -1
- package/dist/ProcaptchaFrictionless.js +0 -287
- package/dist/ProcaptchaFrictionless.js.map +0 -1
- package/dist/cjs/AuthenticatedBadge.cjs +0 -75
- package/dist/cjs/ProcaptchaFrictionless.cjs +0 -287
- package/src/AuthenticatedBadge.tsx +0 -141
|
@@ -14,73 +14,75 @@
|
|
|
14
14
|
|
|
15
15
|
import type { Ti18n } from "@prosopo/locale";
|
|
16
16
|
import {
|
|
17
|
+
type BotDetectionFunction,
|
|
17
18
|
CaptchaType,
|
|
18
19
|
type FrictionlessState,
|
|
19
20
|
ModeEnum,
|
|
20
|
-
type
|
|
21
|
+
type ProcaptchaClientConfigOutput,
|
|
21
22
|
type ProcaptchaProps,
|
|
22
23
|
type RandomProvider,
|
|
23
24
|
} from "@prosopo/types";
|
|
24
|
-
import { type ReactElement, act, createElement } from "react";
|
|
25
|
-
import { type Root, createRoot } from "react-dom/client";
|
|
26
25
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
27
26
|
|
|
28
|
-
declare global {
|
|
29
|
-
var IS_REACT_ACT_ENVIRONMENT: boolean;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// React only lets act() drive its scheduler when the environment says so;
|
|
33
|
-
// without this every render warns and updates flush unpredictably.
|
|
34
|
-
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
|
|
35
|
-
|
|
36
27
|
// Every widget the frictionless wrapper can mount records the props it was
|
|
37
28
|
// handed, so a test can assert on the config that reached the *second* mount
|
|
38
29
|
// after an escalation without rendering a real captcha.
|
|
39
|
-
const mocks = vi.hoisted(() =>
|
|
40
|
-
mounts:
|
|
30
|
+
const mocks = vi.hoisted(() => {
|
|
31
|
+
const mounts: {
|
|
41
32
|
widget: "pow" | "image" | "puzzle";
|
|
42
33
|
props: ProcaptchaProps;
|
|
43
|
-
}[]
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
34
|
+
}[] = [];
|
|
35
|
+
const stub =
|
|
36
|
+
(widget: "pow" | "image" | "puzzle") =>
|
|
37
|
+
(host: HTMLElement, props: ProcaptchaProps) => {
|
|
38
|
+
mounts.push({ widget, props });
|
|
39
|
+
const element = host.ownerDocument.createElement("div");
|
|
40
|
+
element.setAttribute("data-widget", widget);
|
|
41
|
+
host.appendChild(element);
|
|
42
|
+
return { destroy: () => element.remove() };
|
|
43
|
+
};
|
|
44
|
+
return { mounts, stub };
|
|
45
|
+
});
|
|
51
46
|
|
|
52
|
-
vi.mock("@prosopo/procaptcha-pow", () => ({
|
|
53
|
-
|
|
47
|
+
vi.mock("@prosopo/procaptcha-pow", () => ({
|
|
48
|
+
mountProcaptchaPowWidget: mocks.stub("pow"),
|
|
49
|
+
}));
|
|
50
|
+
vi.mock("@prosopo/procaptcha-react", () => ({
|
|
51
|
+
mountProcaptchaImageWidget: mocks.stub("image"),
|
|
52
|
+
}));
|
|
54
53
|
vi.mock("@prosopo/procaptcha-puzzle", () => ({
|
|
55
|
-
|
|
54
|
+
mountProcaptchaPuzzleWidget: mocks.stub("puzzle"),
|
|
56
55
|
}));
|
|
57
56
|
|
|
58
57
|
// The wrapper refuses to run outside a secure context, and jsdom's
|
|
59
58
|
// `isSecureContext` is not reliably true across environments. Everything else
|
|
60
|
-
// from procaptcha-common (
|
|
61
|
-
// real implementation.
|
|
62
|
-
vi.mock("@prosopo/procaptcha-common", async (importOriginal) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
});
|
|
59
|
+
// from procaptcha-common (the checkbox, getDefaultEvents, providerRetry) is
|
|
60
|
+
// the real implementation.
|
|
61
|
+
vi.mock("@prosopo/procaptcha-common", async (importOriginal) => ({
|
|
62
|
+
...(await importOriginal<typeof import("@prosopo/procaptcha-common")>()),
|
|
63
|
+
isSecureBrowserContext: () => true,
|
|
64
|
+
}));
|
|
67
65
|
|
|
68
66
|
// Imported after the mocks so the lazy loaders inside resolve to the stubs.
|
|
69
|
-
const {
|
|
67
|
+
const { mountProcaptchaFrictionless } = await import(
|
|
68
|
+
"../procaptchaFrictionless.js"
|
|
69
|
+
);
|
|
70
70
|
|
|
71
71
|
const SITE_KEY = "5siteKey";
|
|
72
72
|
const CLIENT_SESSION_ID = "jti-from-the-site";
|
|
73
73
|
const FIRST_SESSION_ID = "provider-session-1";
|
|
74
74
|
const ESCALATED_SESSION_ID = "provider-session-2";
|
|
75
75
|
|
|
76
|
-
const config = ():
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
76
|
+
const config = (): ProcaptchaClientConfigOutput =>
|
|
77
|
+
({
|
|
78
|
+
account: { address: SITE_KEY },
|
|
79
|
+
userAccountAddress: "",
|
|
80
|
+
web2: true,
|
|
81
|
+
theme: "light",
|
|
82
|
+
mode: ModeEnum.visible,
|
|
83
|
+
// What the site set via data-sessionid / renderOptions.sessionId.
|
|
84
|
+
clientSessionId: CLIENT_SESSION_ID,
|
|
85
|
+
}) as unknown as ProcaptchaClientConfigOutput;
|
|
84
86
|
|
|
85
87
|
const i18nStub = {
|
|
86
88
|
isInitialized: true,
|
|
@@ -92,49 +94,60 @@ const i18nStub = {
|
|
|
92
94
|
// Minimal shape of what customDetectBot resolves to; only the fields the
|
|
93
95
|
// wrapper reads on the happy path need to be present.
|
|
94
96
|
const detectBotReturning = (captchaType: CaptchaType) =>
|
|
95
|
-
vi.fn().mockResolvedValue({
|
|
97
|
+
vi.fn<BotDetectionFunction>().mockResolvedValue({
|
|
96
98
|
captchaType,
|
|
97
99
|
sessionId: FIRST_SESSION_ID,
|
|
98
100
|
provider: { provider: { url: "https://provider.test" } } as RandomProvider,
|
|
99
101
|
userAccount: "userAccount",
|
|
100
|
-
});
|
|
102
|
+
} as unknown as Awaited<ReturnType<BotDetectionFunction>>);
|
|
101
103
|
|
|
102
104
|
let container: HTMLDivElement;
|
|
103
|
-
let
|
|
104
|
-
|
|
105
|
-
const
|
|
106
|
-
await
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
)
|
|
105
|
+
let widget: { destroy: () => void } | undefined;
|
|
106
|
+
|
|
107
|
+
const settle = async (): Promise<void> => {
|
|
108
|
+
await new Promise<void>((resolve: () => void) => setTimeout(resolve, 0));
|
|
109
|
+
await Promise.resolve();
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const mountWrapper = async (
|
|
113
|
+
captchaType: CaptchaType,
|
|
114
|
+
configOverrides: Partial<ProcaptchaClientConfigOutput> = {},
|
|
115
|
+
): Promise<void> => {
|
|
116
|
+
widget = mountProcaptchaFrictionless(container, {
|
|
117
|
+
config: { ...config(), ...configOverrides },
|
|
118
|
+
callbacks: {},
|
|
119
|
+
restart: vi.fn(),
|
|
120
|
+
i18n: i18nStub,
|
|
121
|
+
detectBot: detectBotReturning(captchaType),
|
|
116
122
|
});
|
|
123
|
+
await settle();
|
|
117
124
|
};
|
|
118
125
|
|
|
119
|
-
const lastMountOf = (
|
|
120
|
-
const mount = mocks.mounts.filter((m) => m.widget ===
|
|
121
|
-
if (!mount) throw new Error(`expected the ${
|
|
126
|
+
const lastMountOf = (target: "pow" | "image" | "puzzle") => {
|
|
127
|
+
const mount = mocks.mounts.filter((m) => m.widget === target).at(-1);
|
|
128
|
+
if (!mount) throw new Error(`expected the ${target} widget to have mounted`);
|
|
122
129
|
return mount;
|
|
123
130
|
};
|
|
124
131
|
|
|
132
|
+
const escalate = async (to: CaptchaType): Promise<void> => {
|
|
133
|
+
const { onEscalate } = lastMountOf("pow").props;
|
|
134
|
+
onEscalate?.(
|
|
135
|
+
to as CaptchaType.image | CaptchaType.puzzle,
|
|
136
|
+
ESCALATED_SESSION_ID,
|
|
137
|
+
);
|
|
138
|
+
await settle();
|
|
139
|
+
};
|
|
140
|
+
|
|
125
141
|
beforeEach(() => {
|
|
126
142
|
mocks.mounts.length = 0;
|
|
143
|
+
widget = undefined;
|
|
127
144
|
container = document.createElement("div");
|
|
128
145
|
document.body.appendChild(container);
|
|
129
|
-
act(() => {
|
|
130
|
-
root = createRoot(container);
|
|
131
|
-
});
|
|
132
146
|
});
|
|
133
147
|
|
|
134
148
|
afterEach(() => {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
});
|
|
149
|
+
widget?.destroy();
|
|
150
|
+
widget = undefined;
|
|
138
151
|
container.remove();
|
|
139
152
|
vi.clearAllMocks();
|
|
140
153
|
});
|
|
@@ -158,10 +171,7 @@ describe("client session id across a PoW escalation", () => {
|
|
|
158
171
|
|
|
159
172
|
// The provider accepted the PoW but wants a visual challenge; the PoW
|
|
160
173
|
// widget calls back with the replacement session the provider minted.
|
|
161
|
-
|
|
162
|
-
await act(async () => {
|
|
163
|
-
onEscalate?.(CaptchaType.image, ESCALATED_SESSION_ID);
|
|
164
|
-
});
|
|
174
|
+
await escalate(CaptchaType.image);
|
|
165
175
|
|
|
166
176
|
expect(lastMountOf("image").props.config.clientSessionId).toBe(
|
|
167
177
|
CLIENT_SESSION_ID,
|
|
@@ -171,10 +181,7 @@ describe("client session id across a PoW escalation", () => {
|
|
|
171
181
|
it("survives the handoff to the escalated puzzle widget", async () => {
|
|
172
182
|
await mountWrapper(CaptchaType.pow);
|
|
173
183
|
|
|
174
|
-
|
|
175
|
-
await act(async () => {
|
|
176
|
-
onEscalate?.(CaptchaType.puzzle, ESCALATED_SESSION_ID);
|
|
177
|
-
});
|
|
184
|
+
await escalate(CaptchaType.puzzle);
|
|
178
185
|
|
|
179
186
|
expect(lastMountOf("puzzle").props.config.clientSessionId).toBe(
|
|
180
187
|
CLIENT_SESSION_ID,
|
|
@@ -188,10 +195,7 @@ describe("client session id across a PoW escalation", () => {
|
|
|
188
195
|
.frictionlessState as FrictionlessState;
|
|
189
196
|
expect(powState.sessionId).toBe(FIRST_SESSION_ID);
|
|
190
197
|
|
|
191
|
-
|
|
192
|
-
await act(async () => {
|
|
193
|
-
onEscalate?.(CaptchaType.image, ESCALATED_SESSION_ID);
|
|
194
|
-
});
|
|
198
|
+
await escalate(CaptchaType.image);
|
|
195
199
|
|
|
196
200
|
const imageMount = lastMountOf("image").props;
|
|
197
201
|
const imageState = imageMount.frictionlessState as FrictionlessState;
|
|
@@ -203,22 +207,9 @@ describe("client session id across a PoW escalation", () => {
|
|
|
203
207
|
});
|
|
204
208
|
|
|
205
209
|
it("carries no client session id when the site rendered without one", async () => {
|
|
206
|
-
await
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
config: { ...config(), clientSessionId: undefined },
|
|
210
|
-
callbacks: {},
|
|
211
|
-
restart: vi.fn(),
|
|
212
|
-
i18n: i18nStub,
|
|
213
|
-
detectBot: detectBotReturning(CaptchaType.pow),
|
|
214
|
-
}) as ReactElement,
|
|
215
|
-
);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
const { onEscalate } = lastMountOf("pow").props;
|
|
219
|
-
await act(async () => {
|
|
220
|
-
onEscalate?.(CaptchaType.image, ESCALATED_SESSION_ID);
|
|
221
|
-
});
|
|
210
|
+
await mountWrapper(CaptchaType.pow, { clientSessionId: undefined });
|
|
211
|
+
|
|
212
|
+
await escalate(CaptchaType.image);
|
|
222
213
|
|
|
223
214
|
expect(lastMountOf("image").props.config.clientSessionId).toBeUndefined();
|
|
224
215
|
});
|
package/src/tests/{escalationHandoff.integration.test.tsx → escalationHandoff.integration.test.ts}
RENAMED
|
@@ -13,12 +13,12 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* Post-PoW escalation, end to end on the client: the real
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
16
|
+
* Post-PoW escalation, end to end on the client: the real frictionless wrapper
|
|
17
|
+
* hands off to the real image widget and its real Manager. Only the network is
|
|
18
|
+
* stubbed, and the stub enforces the provider's actual contract —
|
|
19
|
+
* `checkAndRemoveSession` consumes a session the moment it issues a challenge,
|
|
20
|
+
* so a second `/captcha/image` on the same sessionId is answered with 400
|
|
21
|
+
* CAPTCHA.NO_SESSION_FOUND.
|
|
22
22
|
*
|
|
23
23
|
* Observed in production on provider 3.8.5:
|
|
24
24
|
*
|
|
@@ -39,12 +39,10 @@ import {
|
|
|
39
39
|
type CaptchaResponseBody,
|
|
40
40
|
CaptchaType,
|
|
41
41
|
ModeEnum,
|
|
42
|
-
type
|
|
42
|
+
type ProcaptchaClientConfigOutput,
|
|
43
43
|
type ProcaptchaProps,
|
|
44
44
|
type RandomProvider,
|
|
45
45
|
} from "@prosopo/types";
|
|
46
|
-
import { type ReactElement, act, createElement } from "react";
|
|
47
|
-
import { type Root, createRoot } from "react-dom/client";
|
|
48
46
|
import {
|
|
49
47
|
type Mock,
|
|
50
48
|
afterEach,
|
|
@@ -55,12 +53,6 @@ import {
|
|
|
55
53
|
vi,
|
|
56
54
|
} from "vitest";
|
|
57
55
|
|
|
58
|
-
declare global {
|
|
59
|
-
var IS_REACT_ACT_ENVIRONMENT: boolean;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
|
|
63
|
-
|
|
64
56
|
const ESCALATION_SESSION_ID = "provider-session-escalated";
|
|
65
57
|
const FIRST_SESSION_ID = "provider-session-1";
|
|
66
58
|
|
|
@@ -71,22 +63,23 @@ const mocks = vi.hoisted(() => {
|
|
|
71
63
|
const challengeRequests: (string | undefined)[] = [];
|
|
72
64
|
const consumed = new Set<string>();
|
|
73
65
|
|
|
74
|
-
const body = (): CaptchaResponseBody =>
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
66
|
+
const body = (): CaptchaResponseBody =>
|
|
67
|
+
({
|
|
68
|
+
captchas: [
|
|
69
|
+
{
|
|
70
|
+
captchaId: "captcha-id-1",
|
|
71
|
+
captchaContentId: "captcha-content-id-1",
|
|
72
|
+
datasetId: "dataset-id",
|
|
73
|
+
salt: "0xsalt",
|
|
74
|
+
target: "cars",
|
|
75
|
+
items: [],
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
requestHash: "0xrequest-hash",
|
|
79
|
+
timestamp: "1700000000000",
|
|
80
|
+
signature: { provider: { requestHash: "0xprovider-request-hash" } },
|
|
81
|
+
status: "ok",
|
|
82
|
+
}) as unknown as CaptchaResponseBody;
|
|
90
83
|
|
|
91
84
|
const getCaptchaChallenge = async (
|
|
92
85
|
_userAccount: string,
|
|
@@ -107,7 +100,7 @@ const mocks = vi.hoisted(() => {
|
|
|
107
100
|
message: "No session found",
|
|
108
101
|
code: 400,
|
|
109
102
|
},
|
|
110
|
-
};
|
|
103
|
+
} as unknown as CaptchaResponseBody;
|
|
111
104
|
}
|
|
112
105
|
if (sessionId) consumed.add(sessionId);
|
|
113
106
|
return body();
|
|
@@ -117,98 +110,119 @@ const mocks = vi.hoisted(() => {
|
|
|
117
110
|
public getCaptchaChallenge = getCaptchaChallenge;
|
|
118
111
|
}
|
|
119
112
|
|
|
120
|
-
|
|
121
|
-
|
|
113
|
+
const powMounts: { props: ProcaptchaProps }[] = [];
|
|
114
|
+
const powStub = (host: HTMLElement, props: ProcaptchaProps) => {
|
|
115
|
+
powMounts.push({ props });
|
|
116
|
+
const element = host.ownerDocument.createElement("div");
|
|
117
|
+
element.setAttribute("data-widget", "pow");
|
|
118
|
+
host.appendChild(element);
|
|
119
|
+
return { destroy: () => element.remove() };
|
|
120
|
+
};
|
|
121
|
+
const puzzleStub = (host: HTMLElement) => {
|
|
122
|
+
const element = host.ownerDocument.createElement("div");
|
|
123
|
+
element.setAttribute("data-widget", "puzzle");
|
|
124
|
+
host.appendChild(element);
|
|
125
|
+
return { destroy: () => element.remove() };
|
|
126
|
+
};
|
|
122
127
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
128
|
+
return {
|
|
129
|
+
challengeRequests,
|
|
130
|
+
consumed,
|
|
131
|
+
ProviderApiMock,
|
|
132
|
+
powMounts,
|
|
133
|
+
powStub,
|
|
134
|
+
puzzleStub,
|
|
135
|
+
};
|
|
126
136
|
});
|
|
127
137
|
|
|
138
|
+
vi.mock("@prosopo/api", async (importOriginal) => ({
|
|
139
|
+
...(await importOriginal<typeof import("@prosopo/api")>()),
|
|
140
|
+
ProviderApi: mocks.ProviderApiMock,
|
|
141
|
+
}));
|
|
142
|
+
|
|
128
143
|
// The image Manager constructs an extension before it can ask for a challenge
|
|
129
144
|
// (it then reads the account off the frictionless state, and nothing in this
|
|
130
145
|
// flow signs anything). `ExtensionLoader` resolves to the class, so the mock
|
|
131
146
|
// has to as well.
|
|
132
147
|
class ExtensionMock {
|
|
133
|
-
public getAccount = async (): Promise<Account> =>
|
|
134
|
-
account: { address: "user-address" }
|
|
135
|
-
});
|
|
148
|
+
public getAccount = async (): Promise<Account> =>
|
|
149
|
+
({ account: { address: "user-address" } }) as unknown as Account;
|
|
136
150
|
}
|
|
137
151
|
|
|
138
|
-
vi.mock("@prosopo/procaptcha-common", async (importOriginal) => {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
isSecureBrowserContext: () => true,
|
|
144
|
-
ExtensionLoader: async (_web2: boolean) => ExtensionMock,
|
|
145
|
-
};
|
|
146
|
-
});
|
|
152
|
+
vi.mock("@prosopo/procaptcha-common", async (importOriginal) => ({
|
|
153
|
+
...(await importOriginal<typeof import("@prosopo/procaptcha-common")>()),
|
|
154
|
+
isSecureBrowserContext: () => true,
|
|
155
|
+
ExtensionLoader: async (_web2: boolean) => ExtensionMock,
|
|
156
|
+
}));
|
|
147
157
|
|
|
148
|
-
vi.mock("@prosopo/locale", async (importOriginal) => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
useTranslation: () => ({ t: (key: string) => key, ready: true }),
|
|
153
|
-
loadI18next: async () => undefined,
|
|
154
|
-
};
|
|
155
|
-
});
|
|
158
|
+
vi.mock("@prosopo/locale", async (importOriginal) => ({
|
|
159
|
+
...(await importOriginal<typeof import("@prosopo/locale")>()),
|
|
160
|
+
loadI18next: async () => undefined,
|
|
161
|
+
}));
|
|
156
162
|
|
|
157
163
|
// The PoW widget is the escalation *source*, so it stays a stub — the test
|
|
158
164
|
// drives its `onEscalate` callback directly. The image widget below it is real.
|
|
159
|
-
const powMounts: { props: ProcaptchaProps }[] = [];
|
|
160
165
|
vi.mock("@prosopo/procaptcha-pow", () => ({
|
|
161
|
-
|
|
162
|
-
powMounts.push({ props });
|
|
163
|
-
return createElement("div", { "data-widget": "pow" });
|
|
164
|
-
},
|
|
166
|
+
mountProcaptchaPowWidget: mocks.powStub,
|
|
165
167
|
}));
|
|
166
168
|
vi.mock("@prosopo/procaptcha-puzzle", () => ({
|
|
167
|
-
|
|
169
|
+
mountProcaptchaPuzzleWidget: mocks.puzzleStub,
|
|
168
170
|
}));
|
|
169
171
|
|
|
170
|
-
const {
|
|
172
|
+
const { mountProcaptchaFrictionless } = await import(
|
|
173
|
+
"../procaptchaFrictionless.js"
|
|
174
|
+
);
|
|
171
175
|
|
|
172
176
|
const provider: RandomProvider = {
|
|
173
177
|
providerAccount: "provider-account",
|
|
174
178
|
provider: { url: "https://provider.test" },
|
|
175
|
-
};
|
|
179
|
+
} as unknown as RandomProvider;
|
|
176
180
|
|
|
177
|
-
const userAccount: Account = {
|
|
181
|
+
const userAccount: Account = {
|
|
182
|
+
account: { address: "user-address" },
|
|
183
|
+
} as unknown as Account;
|
|
178
184
|
|
|
179
|
-
const config = ():
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
+
const config = (): ProcaptchaClientConfigOutput =>
|
|
186
|
+
({
|
|
187
|
+
account: { address: "5siteKey" },
|
|
188
|
+
userAccountAddress: "",
|
|
189
|
+
web2: true,
|
|
190
|
+
theme: "light",
|
|
191
|
+
mode: ModeEnum.visible,
|
|
192
|
+
}) as unknown as ProcaptchaClientConfigOutput;
|
|
185
193
|
|
|
186
194
|
const i18nStub = {
|
|
187
195
|
isInitialized: true,
|
|
188
196
|
language: "en",
|
|
189
197
|
t: (key: string) => key,
|
|
190
198
|
changeLanguage: vi.fn(),
|
|
199
|
+
hasLoadedNamespace: () => true,
|
|
200
|
+
on: () => undefined,
|
|
201
|
+
off: () => undefined,
|
|
191
202
|
} as unknown as Ti18n;
|
|
192
203
|
|
|
193
|
-
const detectBot: Mock<BotDetectionFunction> = vi.fn(
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
204
|
+
const detectBot: Mock<BotDetectionFunction> = vi.fn(
|
|
205
|
+
async () =>
|
|
206
|
+
({
|
|
207
|
+
captchaType: CaptchaType.pow,
|
|
208
|
+
sessionId: FIRST_SESSION_ID,
|
|
209
|
+
status: "ok",
|
|
210
|
+
provider,
|
|
211
|
+
userAccount,
|
|
212
|
+
}) as unknown as Awaited<ReturnType<BotDetectionFunction>>,
|
|
213
|
+
);
|
|
200
214
|
|
|
201
215
|
let container: HTMLDivElement;
|
|
202
|
-
let
|
|
216
|
+
let widget: { destroy: () => void } | undefined;
|
|
203
217
|
|
|
204
218
|
const lastPowMount = () => {
|
|
205
|
-
const mount = powMounts.at(-1);
|
|
219
|
+
const mount = mocks.powMounts.at(-1);
|
|
206
220
|
if (!mount) throw new Error("expected the pow widget to have mounted");
|
|
207
221
|
return mount;
|
|
208
222
|
};
|
|
209
223
|
|
|
210
224
|
/**
|
|
211
|
-
* Flush
|
|
225
|
+
* Flush pending work until the challenge traffic has been quiet for four
|
|
212
226
|
* consecutive polls, so the negative assertions know nothing further is on its
|
|
213
227
|
* way. Paired with `waitForChallenge` below rather than used alone: starting
|
|
214
228
|
* from an already-quiet count, a pure quiescence check can declare "settled"
|
|
@@ -218,9 +232,7 @@ const settle = async (): Promise<void> => {
|
|
|
218
232
|
let quiet = 0;
|
|
219
233
|
let previous = mocks.challengeRequests.length;
|
|
220
234
|
for (let poll = 0; poll < 40 && quiet < 4; poll++) {
|
|
221
|
-
await
|
|
222
|
-
await new Promise((resolve) => setTimeout(resolve, 60));
|
|
223
|
-
});
|
|
235
|
+
await new Promise((resolve) => setTimeout(resolve, 60));
|
|
224
236
|
const current = mocks.challengeRequests.length;
|
|
225
237
|
quiet = current === previous ? quiet + 1 : 0;
|
|
226
238
|
previous = current;
|
|
@@ -237,9 +249,7 @@ const waitForChallenge = async (sessionId: string): Promise<void> => {
|
|
|
237
249
|
poll < 60 && challengeRequestCount(sessionId) === 0;
|
|
238
250
|
poll++
|
|
239
251
|
) {
|
|
240
|
-
await
|
|
241
|
-
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
242
|
-
});
|
|
252
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
243
253
|
}
|
|
244
254
|
};
|
|
245
255
|
|
|
@@ -254,40 +264,32 @@ const escalate = async (
|
|
|
254
264
|
expectRequest = true,
|
|
255
265
|
): Promise<void> => {
|
|
256
266
|
const { onEscalate } = lastPowMount().props;
|
|
257
|
-
|
|
258
|
-
onEscalate?.(CaptchaType.image, sessionId, { x: 120, y: 340 });
|
|
259
|
-
});
|
|
267
|
+
onEscalate?.(CaptchaType.image, sessionId, { x: 120, y: 340 });
|
|
260
268
|
if (expectRequest) await waitForChallenge(sessionId);
|
|
261
269
|
await settle();
|
|
262
270
|
};
|
|
263
271
|
|
|
264
272
|
beforeEach(async () => {
|
|
265
|
-
powMounts.length = 0;
|
|
273
|
+
mocks.powMounts.length = 0;
|
|
266
274
|
mocks.challengeRequests.length = 0;
|
|
267
275
|
mocks.consumed.clear();
|
|
268
276
|
detectBot.mockClear();
|
|
277
|
+
widget = undefined;
|
|
269
278
|
container = document.createElement("div");
|
|
270
279
|
document.body.appendChild(container);
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
config: config(),
|
|
278
|
-
callbacks: {},
|
|
279
|
-
restart: vi.fn<() => void>(),
|
|
280
|
-
i18n: i18nStub,
|
|
281
|
-
detectBot,
|
|
282
|
-
}) as ReactElement,
|
|
283
|
-
);
|
|
280
|
+
widget = mountProcaptchaFrictionless(container, {
|
|
281
|
+
config: config(),
|
|
282
|
+
callbacks: {},
|
|
283
|
+
restart: vi.fn<() => void>(),
|
|
284
|
+
i18n: i18nStub,
|
|
285
|
+
detectBot,
|
|
284
286
|
});
|
|
287
|
+
await settle();
|
|
285
288
|
});
|
|
286
289
|
|
|
287
290
|
afterEach(() => {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
});
|
|
291
|
+
widget?.destroy();
|
|
292
|
+
widget = undefined;
|
|
291
293
|
container.remove();
|
|
292
294
|
vi.clearAllMocks();
|
|
293
295
|
});
|