@owlmeans/web-captcha-recaptcha 0.0.0-stage → 0.2.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/README.md +87 -2
- package/agent-meta/manifest.json +16 -0
- package/agent-meta/skills/web-captcha-recaptcha/SKILL.md +78 -0
- package/build/append.d.ts +8 -0
- package/build/append.d.ts.map +1 -0
- package/build/append.js +8 -0
- package/build/append.js.map +1 -0
- package/build/client.d.ts +8 -0
- package/build/client.d.ts.map +1 -0
- package/build/client.js +20 -0
- package/build/client.js.map +1 -0
- package/build/consts.d.ts +34 -0
- package/build/consts.d.ts.map +1 -0
- package/build/consts.js +76 -0
- package/build/consts.js.map +1 -0
- package/build/index.d.ts +6 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +5 -0
- package/build/index.js.map +1 -0
- package/build/loader.d.ts +9 -0
- package/build/loader.d.ts.map +1 -0
- package/build/loader.js +105 -0
- package/build/loader.js.map +1 -0
- package/build/react/checkbox.d.ts +8 -0
- package/build/react/checkbox.d.ts.map +1 -0
- package/build/react/checkbox.js +9 -0
- package/build/react/checkbox.js.map +1 -0
- package/build/react.d.ts +2 -0
- package/build/react.d.ts.map +1 -0
- package/build/react.js +2 -0
- package/build/react.js.map +1 -0
- package/build/types.d.ts +58 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/package.json +58 -4
- package/src/append.ts +12 -0
- package/src/client.ts +22 -0
- package/src/consts.ts +87 -0
- package/src/index.ts +5 -0
- package/src/loader.ts +123 -0
- package/src/react/checkbox.tsx +17 -0
- package/src/react.ts +1 -0
- package/src/types.ts +60 -0
- package/tests/client.spec.ts +63 -0
- package/tests/context.ts +73 -0
- package/tests/loader.spec.ts +120 -0
- package/tests/react.spec.ts +36 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { CaptchaLoadError } from '@owlmeans/web-captcha'
|
|
3
|
+
import {
|
|
4
|
+
RECAPTCHA_BADGE_CSS, RECAPTCHA_EXECUTE_TIMEOUT, RECAPTCHA_LOAD_TIMEOUT, RECAPTCHA_SCRIPT_ATTRIBUTE,
|
|
5
|
+
RECAPTCHA_SCRIPT_URL, RECAPTCHA_STYLE_ATTRIBUTE,
|
|
6
|
+
} from '../src/consts.js'
|
|
7
|
+
import { createRecaptchaLoader, recaptchaLoader } from '../src/loader.js'
|
|
8
|
+
import type { RecaptchaLoaderOptions } from '../src/types.js'
|
|
9
|
+
import { makeTestPage } from './context.js'
|
|
10
|
+
|
|
11
|
+
const loaderOn = (page = makeTestPage(), options: RecaptchaLoaderOptions = {}) =>
|
|
12
|
+
({ page, loader: createRecaptchaLoader({ document: page.document, window: page.window, ...options }) })
|
|
13
|
+
|
|
14
|
+
const failure = async (promise: Promise<unknown>): Promise<CaptchaLoadError> => {
|
|
15
|
+
try {
|
|
16
|
+
await promise
|
|
17
|
+
} catch (error) {
|
|
18
|
+
expect(error).toBeInstanceOf(CaptchaLoadError)
|
|
19
|
+
return error as CaptchaLoadError
|
|
20
|
+
}
|
|
21
|
+
throw new Error('expected a failure')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('@owlmeans/web-captcha-recaptcha — loader', () => {
|
|
25
|
+
test('keeps Google\'s script, the badge rule and the timeouts', () => {
|
|
26
|
+
expect(RECAPTCHA_SCRIPT_URL).toBe('https://www.google.com/recaptcha/api.js')
|
|
27
|
+
expect(RECAPTCHA_BADGE_CSS).toBe('.grecaptcha-badge{visibility:hidden}')
|
|
28
|
+
expect(RECAPTCHA_LOAD_TIMEOUT).toBe(15_000)
|
|
29
|
+
expect(RECAPTCHA_EXECUTE_TIMEOUT).toBe(20_000)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('injects one script per site key, with the badge style once, and resolves when grecaptcha is ready', async () => {
|
|
33
|
+
const { page, loader } = loaderOn()
|
|
34
|
+
|
|
35
|
+
const first = loader.prepare('key one')
|
|
36
|
+
const again = loader.prepare('key one')
|
|
37
|
+
expect(page.scripts()).toHaveLength(1)
|
|
38
|
+
const [script] = page.scripts()
|
|
39
|
+
expect(script.src).toBe(`${RECAPTCHA_SCRIPT_URL}?render=key%20one`)
|
|
40
|
+
expect(script.async).toBe(true)
|
|
41
|
+
expect(script.attributes[RECAPTCHA_SCRIPT_ATTRIBUTE]).toBe('key one')
|
|
42
|
+
|
|
43
|
+
page.arrive(script)
|
|
44
|
+
await Promise.all([first, again])
|
|
45
|
+
await loader.prepare('key one')
|
|
46
|
+
expect(page.scripts()).toHaveLength(1)
|
|
47
|
+
|
|
48
|
+
const second = loader.prepare('key-two')
|
|
49
|
+
page.arrive(page.scripts()[1])
|
|
50
|
+
await second
|
|
51
|
+
expect(page.scripts()).toHaveLength(2)
|
|
52
|
+
const styles = page.head.filter(element => element.tag === 'style')
|
|
53
|
+
expect(styles).toHaveLength(1)
|
|
54
|
+
expect(styles[0].attributes).toHaveProperty(RECAPTCHA_STYLE_ATTRIBUTE)
|
|
55
|
+
expect(styles[0].textContent).toBe(RECAPTCHA_BADGE_CSS)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('a load that fails or never becomes ready is refused with its reason, and forgotten', async () => {
|
|
59
|
+
const { page, loader } = loaderOn(makeTestPage(), { loadTimeout: 5 })
|
|
60
|
+
|
|
61
|
+
const timedOut = await failure(loader.prepare('k'))
|
|
62
|
+
expect(timedOut.reason).toBe('timeout')
|
|
63
|
+
expect(page.scripts()).toHaveLength(0)
|
|
64
|
+
|
|
65
|
+
const erroring = loader.prepare('k')
|
|
66
|
+
page.scripts()[0].fire('error')
|
|
67
|
+
expect((await failure(erroring)).reason).toBe('load')
|
|
68
|
+
|
|
69
|
+
const bare = loader.prepare('k')
|
|
70
|
+
page.arrive(page.scripts()[0], null)
|
|
71
|
+
expect((await failure(bare)).reason).toBe('unavailable')
|
|
72
|
+
|
|
73
|
+
const retried = loader.prepare('k')
|
|
74
|
+
page.arrive(page.scripts()[0])
|
|
75
|
+
await retried
|
|
76
|
+
expect(page.head.filter(element => element.tag === 'script')).toHaveLength(4)
|
|
77
|
+
expect(page.scripts()).toHaveLength(1)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('without a page nothing is touched at import, and a call is unavailable', async () => {
|
|
81
|
+
expect(typeof (globalThis as { document?: unknown }).document).toBe('undefined')
|
|
82
|
+
expect((await failure(recaptchaLoader.prepare('k'))).reason).toBe('unavailable')
|
|
83
|
+
expect((await failure(createRecaptchaLoader().token('k', 'a'))).reason).toBe('unavailable')
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('hideBadge: false adds no document style; a script url with a query keeps it', async () => {
|
|
87
|
+
const { page, loader } = loaderOn(makeTestPage(), { hideBadge: false, scriptUrl: 'https://www.recaptcha.net/recaptcha/api.js?hl=pl' })
|
|
88
|
+
|
|
89
|
+
const ready = loader.prepare('k')
|
|
90
|
+
page.arrive(page.scripts()[0])
|
|
91
|
+
await ready
|
|
92
|
+
expect(page.head.map(element => element.tag)).toEqual(['script'])
|
|
93
|
+
expect(page.scripts()[0].src).toBe('https://www.recaptcha.net/recaptcha/api.js?hl=pl&render=k')
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
describe('@owlmeans/web-captcha-recaptcha — token', () => {
|
|
98
|
+
test('loads on demand and executes the action', async () => {
|
|
99
|
+
const { page, loader } = loaderOn()
|
|
100
|
+
|
|
101
|
+
const token = loader.token('k', 'email_pin')
|
|
102
|
+
page.arrive(page.scripts()[0])
|
|
103
|
+
expect(await token).toBe('token:k:email_pin')
|
|
104
|
+
expect(await loader.token('k', 'email_password')).toBe('token:k:email_password')
|
|
105
|
+
expect(page.scripts()).toHaveLength(1)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test('no token, a throwing or a hanging execute is an execute failure', async () => {
|
|
109
|
+
for (const execute of [
|
|
110
|
+
async () => '',
|
|
111
|
+
() => { throw new Error('denied') },
|
|
112
|
+
() => new Promise<string>(() => {}),
|
|
113
|
+
]) {
|
|
114
|
+
const { page, loader } = loaderOn(makeTestPage(), { executeTimeout: 5 })
|
|
115
|
+
const token = loader.token('k', 'email_pin')
|
|
116
|
+
page.arrive(page.scripts()[0], { ready: callback => { callback() }, execute })
|
|
117
|
+
expect((await failure(token)).reason).toBe('execute')
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import type { ReactElement } from 'react'
|
|
3
|
+
import ReCAPTCHA from 'react-google-recaptcha'
|
|
4
|
+
import { RecaptchaCheckbox } from '../src/react.js'
|
|
5
|
+
|
|
6
|
+
interface CheckboxProps {
|
|
7
|
+
sitekey: string
|
|
8
|
+
hl?: string
|
|
9
|
+
onChange: (token: string | null) => void
|
|
10
|
+
onExpired: () => void
|
|
11
|
+
onErrored: () => void
|
|
12
|
+
asyncScriptOnLoad: () => void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('@owlmeans/web-captcha-recaptcha/react — the v2 checkbox widget', () => {
|
|
16
|
+
test('renders Google\'s checkbox with the site key and reports tokens, expiry, readiness and errors', () => {
|
|
17
|
+
const tokens: (string | null)[] = []
|
|
18
|
+
const events: string[] = []
|
|
19
|
+
const element = (RecaptchaCheckbox as (props: unknown) => ReactElement<CheckboxProps>)({
|
|
20
|
+
siteKey: 'v2-key', language: 'pl',
|
|
21
|
+
onToken: (token: string | null) => { tokens.push(token) },
|
|
22
|
+
onReady: () => { events.push('ready') },
|
|
23
|
+
onError: (error: Error) => { events.push(error.message) },
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
expect(element.type).toBe(ReCAPTCHA)
|
|
27
|
+
expect(element.props.sitekey).toBe('v2-key')
|
|
28
|
+
expect(element.props.hl).toBe('pl')
|
|
29
|
+
element.props.onChange('token')
|
|
30
|
+
element.props.onExpired()
|
|
31
|
+
element.props.asyncScriptOnLoad()
|
|
32
|
+
element.props.onErrored()
|
|
33
|
+
expect(tokens).toEqual(['token', null])
|
|
34
|
+
expect(events).toEqual(['ready', 'reCAPTCHA could not load'])
|
|
35
|
+
})
|
|
36
|
+
})
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"@owlmeans/dep-config/tsconfig.base.json",
|
|
4
|
+
"@owlmeans/dep-config/tsconfig.react.json"
|
|
5
|
+
],
|
|
6
|
+
"compilerOptions": {
|
|
7
|
+
"rootDir": "./src/",
|
|
8
|
+
"outDir": "./build/"
|
|
9
|
+
},
|
|
10
|
+
"exclude": [
|
|
11
|
+
"./dist/**/*",
|
|
12
|
+
"./build/**/*",
|
|
13
|
+
"./tests/**/*",
|
|
14
|
+
"./*.ts"
|
|
15
|
+
]
|
|
16
|
+
}
|