@internetarchive/recaptcha-manager 0.0.1-alpha.3 → 0.1.1
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 +30 -1
- package/demo/app-root.ts +5 -5
- package/dist/demo/app-root.js +2 -2
- package/dist/demo/app-root.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js.map +1 -1
- package/dist/src/recaptcha-manager.d.ts +3 -3
- package/dist/src/recaptcha-manager.js +1 -1
- package/dist/src/recaptcha-manager.js.map +1 -1
- package/dist/src/recaptcha-widget.d.ts +8 -2
- package/dist/src/recaptcha-widget.js +11 -9
- package/dist/src/recaptcha-widget.js.map +1 -1
- package/dist/test/mock-grecaptcha.d.ts +24 -0
- package/dist/test/mock-grecaptcha.js +61 -98
- package/dist/test/mock-grecaptcha.js.map +1 -1
- package/dist/test/mock-lazy-loader.d.ts +15 -0
- package/dist/test/mock-lazy-loader.js +17 -0
- package/dist/test/mock-lazy-loader.js.map +1 -0
- package/dist/test/recaptcha-manager.test.d.ts +1 -0
- package/dist/test/recaptcha-manager.test.js +136 -67
- package/dist/test/recaptcha-manager.test.js.map +1 -1
- package/index.ts +1 -0
- package/package.json +4 -3
- package/src/recaptcha-manager.ts +4 -4
- package/src/recaptcha-widget.ts +23 -9
- package/test/mock-grecaptcha.ts +87 -124
- package/test/mock-lazy-loader.ts +38 -0
- package/test/recaptcha-manager.test.ts +146 -66
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import {
|
|
3
|
+
BundleType,
|
|
4
|
+
LazyLoaderServiceEvents,
|
|
5
|
+
LazyLoaderServiceInterface,
|
|
6
|
+
} from '@internetarchive/lazy-loader-service';
|
|
7
|
+
import { Unsubscribe } from 'nanoevents';
|
|
8
|
+
import { MockGrecaptcha } from './mock-grecaptcha';
|
|
9
|
+
|
|
10
|
+
export class MockLazyLoaderService implements LazyLoaderServiceInterface {
|
|
11
|
+
loadScriptSrc?: string;
|
|
12
|
+
|
|
13
|
+
on<E extends keyof LazyLoaderServiceEvents>(
|
|
14
|
+
event: E,
|
|
15
|
+
callback: LazyLoaderServiceEvents[E]
|
|
16
|
+
): Unsubscribe {
|
|
17
|
+
throw new Error('Method not implemented.');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async loadBundle(bundle: {
|
|
21
|
+
module?: string;
|
|
22
|
+
nomodule?: string;
|
|
23
|
+
}): Promise<void> {
|
|
24
|
+
console.debug('loadBundle', bundle);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async loadScript(options: {
|
|
28
|
+
src: string;
|
|
29
|
+
bundleType?: BundleType;
|
|
30
|
+
attributes?: Record<string, string>;
|
|
31
|
+
}): Promise<void> {
|
|
32
|
+
this.loadScriptSrc = options.src;
|
|
33
|
+
window.grecaptcha = new MockGrecaptcha({
|
|
34
|
+
mode: 'success',
|
|
35
|
+
});
|
|
36
|
+
(window as any).grecaptchaLoadedCallback();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -1,71 +1,151 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { expect } from '@open-wc/testing';
|
|
2
|
+
import { MockGrecaptcha } from './mock-grecaptcha';
|
|
3
|
+
import { RecaptchaManager } from '../src/recaptcha-manager';
|
|
4
|
+
import { MockLazyLoaderService } from './mock-lazy-loader';
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
// it('can execute the recaptcha like a Promise', async () => {
|
|
7
|
-
// const recaptchaElement = (await fixture(html`
|
|
8
|
-
// <div></div>
|
|
9
|
-
// `)) as HTMLElement;
|
|
10
|
-
// const mockGrecaptcha = new MockGrecaptcha(MockGrecaptchaMode.Success);
|
|
11
|
-
// const recaptchaManager = new RecaptchaManager({
|
|
12
|
-
// grecaptchaLibrary: mockGrecaptcha,
|
|
13
|
-
// siteKey: '123',
|
|
14
|
-
// });
|
|
15
|
-
// recaptchaManager.setup(recaptchaElement, 1, 'dark', 'image');
|
|
16
|
-
// const result = await recaptchaManager.execute();
|
|
17
|
-
// expect(result).to.equal('foo');
|
|
18
|
-
// });
|
|
6
|
+
const mockLazyLoader = new MockLazyLoaderService();
|
|
19
7
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// });
|
|
8
|
+
describe('ReCaptcha Management', () => {
|
|
9
|
+
describe('ReCaptcha Manager', () => {
|
|
10
|
+
it('can return recaptchas', async () => {
|
|
11
|
+
const mockGrecaptcha = new MockGrecaptcha({
|
|
12
|
+
mode: 'success',
|
|
13
|
+
});
|
|
14
|
+
mockGrecaptcha.response = 'foo';
|
|
15
|
+
const recaptchaManager = new RecaptchaManager({
|
|
16
|
+
lazyLoader: mockLazyLoader,
|
|
17
|
+
grecaptchaLibrary: mockGrecaptcha,
|
|
18
|
+
defaultSiteKey: '123',
|
|
19
|
+
});
|
|
20
|
+
const recaptcha = await recaptchaManager.getRecaptchaWidget();
|
|
21
|
+
const result = await recaptcha.execute();
|
|
22
|
+
expect(result).to.equal('foo');
|
|
23
|
+
});
|
|
37
24
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
25
|
+
it('throws an error if no site key found', async () => {
|
|
26
|
+
const mockGrecaptcha = new MockGrecaptcha({
|
|
27
|
+
mode: 'success',
|
|
28
|
+
});
|
|
29
|
+
mockGrecaptcha.response = 'foo';
|
|
30
|
+
const recaptchaManager = new RecaptchaManager({
|
|
31
|
+
lazyLoader: mockLazyLoader,
|
|
32
|
+
grecaptchaLibrary: mockGrecaptcha,
|
|
33
|
+
});
|
|
34
|
+
try {
|
|
35
|
+
await recaptchaManager.getRecaptchaWidget();
|
|
36
|
+
expect.fail('should not get here');
|
|
37
|
+
} catch (e) {
|
|
38
|
+
expect((e as Error).message).to.equal(
|
|
39
|
+
'The reCaptcha widget requires a site key'
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
55
43
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
44
|
+
it('returns a cached version if one already exists for a given site key', async () => {
|
|
45
|
+
const mockGrecaptcha = new MockGrecaptcha({
|
|
46
|
+
mode: 'success',
|
|
47
|
+
});
|
|
48
|
+
mockGrecaptcha.response = 'foo';
|
|
49
|
+
const recaptchaManager = new RecaptchaManager({
|
|
50
|
+
lazyLoader: mockLazyLoader,
|
|
51
|
+
grecaptchaLibrary: mockGrecaptcha,
|
|
52
|
+
defaultSiteKey: '123',
|
|
53
|
+
});
|
|
54
|
+
const recaptcha1 = await recaptchaManager.getRecaptchaWidget();
|
|
55
|
+
const recaptcha2 = await recaptchaManager.getRecaptchaWidget();
|
|
56
|
+
expect(recaptcha1).to.equal(recaptcha2);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('loads the recaptcha library if needed', async () => {
|
|
60
|
+
const recaptchaManager = new RecaptchaManager({
|
|
61
|
+
lazyLoader: mockLazyLoader,
|
|
62
|
+
defaultSiteKey: '123',
|
|
63
|
+
});
|
|
64
|
+
await recaptchaManager.getRecaptchaWidget();
|
|
65
|
+
const loadUrl = mockLazyLoader.loadScriptSrc;
|
|
66
|
+
expect(
|
|
67
|
+
loadUrl?.includes('recaptcha/api.js?onload=grecaptchaLoadedCallback')
|
|
68
|
+
).to.be.true;
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('ReCaptcha Widget', () => {
|
|
73
|
+
it('can error properly like a Promise', async () => {
|
|
74
|
+
const mockGrecaptcha = new MockGrecaptcha({
|
|
75
|
+
mode: 'error',
|
|
76
|
+
});
|
|
77
|
+
const recaptchaManager = new RecaptchaManager({
|
|
78
|
+
lazyLoader: mockLazyLoader,
|
|
79
|
+
grecaptchaLibrary: mockGrecaptcha,
|
|
80
|
+
defaultSiteKey: '123',
|
|
81
|
+
});
|
|
82
|
+
const recaptcha = await recaptchaManager.getRecaptchaWidget();
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
await recaptcha.execute();
|
|
86
|
+
expect.fail('should not get here');
|
|
87
|
+
} catch (error) {
|
|
88
|
+
expect((error as Error).message).to.equal('error');
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('can expire properly like a Promise', async () => {
|
|
93
|
+
const mockGrecaptcha = new MockGrecaptcha({
|
|
94
|
+
mode: 'expired',
|
|
95
|
+
});
|
|
96
|
+
const recaptchaManager = new RecaptchaManager({
|
|
97
|
+
lazyLoader: mockLazyLoader,
|
|
98
|
+
grecaptchaLibrary: mockGrecaptcha,
|
|
99
|
+
defaultSiteKey: '123',
|
|
100
|
+
});
|
|
101
|
+
const recaptcha = await recaptchaManager.getRecaptchaWidget();
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
await recaptcha.execute();
|
|
105
|
+
expect.fail('should not get here');
|
|
106
|
+
} catch (error) {
|
|
107
|
+
expect((error as Error).message).to.equal('expired');
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('resets the captcha after execution', async () => {
|
|
112
|
+
const mockGrecaptcha = new MockGrecaptcha({
|
|
113
|
+
mode: 'success',
|
|
114
|
+
addDelay: true,
|
|
115
|
+
});
|
|
116
|
+
const recaptchaManager = new RecaptchaManager({
|
|
117
|
+
lazyLoader: mockLazyLoader,
|
|
118
|
+
grecaptchaLibrary: mockGrecaptcha,
|
|
119
|
+
defaultSiteKey: '123',
|
|
120
|
+
});
|
|
121
|
+
const recaptcha = await recaptchaManager.getRecaptchaWidget();
|
|
122
|
+
|
|
123
|
+
expect(mockGrecaptcha.resetCallCount).to.equal(0);
|
|
124
|
+
await recaptcha.execute();
|
|
125
|
+
expect(mockGrecaptcha.resetCallCount).to.equal(1);
|
|
126
|
+
await recaptcha.execute();
|
|
127
|
+
expect(mockGrecaptcha.resetCallCount).to.equal(2);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// there's no way to know if the user has dismissed the captcha so if another execution
|
|
131
|
+
// comes through while the captcha is still active, it will be reset
|
|
132
|
+
it('resets the captcha if another execution gets called', async () => {
|
|
133
|
+
const mockGrecaptcha = new MockGrecaptcha({
|
|
134
|
+
mode: 'success',
|
|
135
|
+
addDelay: true,
|
|
136
|
+
});
|
|
137
|
+
const recaptchaManager = new RecaptchaManager({
|
|
138
|
+
lazyLoader: mockLazyLoader,
|
|
139
|
+
grecaptchaLibrary: mockGrecaptcha,
|
|
140
|
+
defaultSiteKey: '123',
|
|
141
|
+
});
|
|
142
|
+
const recaptcha = await recaptchaManager.getRecaptchaWidget();
|
|
143
|
+
|
|
144
|
+
expect(mockGrecaptcha.resetCallCount).to.equal(0);
|
|
145
|
+
recaptcha.execute();
|
|
146
|
+
expect(mockGrecaptcha.resetCallCount).to.equal(0);
|
|
147
|
+
await recaptcha.execute();
|
|
148
|
+
expect(mockGrecaptcha.resetCallCount).to.equal(2);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
71
151
|
});
|