vona-module-test-vona 5.0.43 → 5.0.44

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.
@@ -0,0 +1,80 @@
1
+ import type { ICaptchaData } from 'vona-module-a-captcha';
2
+ import assert from 'node:assert';
3
+ import { describe, it } from 'node:test';
4
+ import { catchError } from '@cabloy/utils';
5
+ import { app } from 'vona-mock';
6
+
7
+ describe('captcha.test.ts', () => {
8
+ // scene/provider
9
+ const sceneName = 'a-captchasimple:simple';
10
+ const providerName = 'a-captchasimple:simple';
11
+ it('action:captcha', async () => {
12
+ await app.bean.executor.mockCtx(async () => {
13
+ // create
14
+ const captcha = await app.bean.captcha.create(sceneName);
15
+ assert.equal(captcha.provider, providerName);
16
+ // get
17
+ const captchaData = await app.bean.captcha.getCaptchaData(captcha.id);
18
+ assert.equal(captchaData?.provider, providerName);
19
+ // token
20
+ const token = captchaData?.token;
21
+ // verify: false
22
+ const verifiedFalse = await app.bean.captcha.verify(captcha.id, `${token}!`, sceneName);
23
+ assert.equal(verifiedFalse, false);
24
+ // verify: true
25
+ const verifiedTrue = await app.bean.captcha.verify(captcha.id, token, sceneName);
26
+ assert.equal(verifiedTrue, true);
27
+ });
28
+ });
29
+ it('action:captcha api', async () => {
30
+ await app.bean.executor.mockCtx(async () => {
31
+ // create
32
+ const captcha: ICaptchaData = await app.bean.executor.performAction('post', '/captcha/create', {
33
+ body: {
34
+ scene: sceneName,
35
+ },
36
+ });
37
+ assert.equal(captcha.provider, providerName);
38
+ // refresh
39
+ const captcha2: ICaptchaData = await app.bean.executor.performAction('post', '/captcha/refresh', {
40
+ body: {
41
+ id: captcha.id,
42
+ scene: sceneName,
43
+ },
44
+ });
45
+ assert.equal(captcha2.provider, providerName);
46
+ assert.notEqual(captcha2.id, captcha.id);
47
+ // get token
48
+ const captchaData = await app.bean.captcha.getCaptchaData(captcha2.id);
49
+ // verifyImmediate: error
50
+ const [_, error] = await catchError(() => {
51
+ return app.bean.executor.performAction('post', '/captcha/verifyImmediate', {
52
+ body: {
53
+ id: captcha2.id,
54
+ token: `${captchaData?.token}!`,
55
+ },
56
+ });
57
+ });
58
+ assert.equal(error?.code, 403);
59
+ // verifyImmediate: ok
60
+ const tokenSecondary = await app.bean.executor.performAction('post', '/captcha/verifyImmediate', {
61
+ body: {
62
+ id: captcha2.id,
63
+ token: captchaData?.token,
64
+ },
65
+ });
66
+ assert.equal(!!tokenSecondary, true);
67
+ // verify: ok
68
+ await app.bean.executor.performAction('post', '/test/vona/captcha/signin', {
69
+ body: {
70
+ username: 'xxx',
71
+ password: 'xxx',
72
+ captcha: {
73
+ id: captcha2.id,
74
+ token: tokenSecondary,
75
+ },
76
+ },
77
+ });
78
+ });
79
+ });
80
+ });