Files
jiapu/tests/captcha-pages.test.js
T
2026-07-09 17:30:10 +08:00

106 lines
2.6 KiB
JavaScript

const assert = require('assert');
const CaptchaPages = require('../public/js/captcha-pages.js');
const fakeApi = {
clientId: 'client-x',
tenantId: 'tenant-x'
};
function run() {
assert.deepStrictEqual(CaptchaPages.buildChallengeBody({
sceneCode: 'WEB_H5_LOGIN',
subject: '13800000000'
}, fakeApi), {
tenantId: 'tenant-x',
clientId: 'client-x',
sceneCode: 'WEB_H5_LOGIN',
subject: '13800000000'
});
const challenge = CaptchaPages.adaptChallengeResponse({
code: 200,
data: {
required: true,
providerCode: 'tianai',
captchaType: 'SLIDER',
challengeId: 'challenge-1',
payload: {
id: 'tianai-1',
backgroundImage: 'bg.png',
templateImage: 'tpl.png',
backgroundImageWidth: 340
}
}
});
assert.strictEqual(challenge.code, 200);
assert.strictEqual(challenge.data.type, 'SLIDER');
assert.strictEqual(challenge.data.id, 'tianai-1');
assert.strictEqual(challenge.data.challengeId, 'challenge-1');
assert.strictEqual(challenge.data.backgroundImage, 'bg.png');
assert.strictEqual(challenge.data.templateImage, 'tpl.png');
assert.deepStrictEqual(CaptchaPages.buildVerifyBody({
id: 'tianai-1',
data: {
trackList: [{ x: 10, y: 2 }]
}
}, {
sceneCode: 'WEB_H5_LOGIN',
subject: '13800000000',
providerCode: 'tianai',
captchaType: 'SLIDER',
challengeId: 'challenge-1'
}, fakeApi), {
tenantId: 'tenant-x',
clientId: 'client-x',
sceneCode: 'WEB_H5_LOGIN',
subject: '13800000000',
challengeId: 'challenge-1',
providerCode: 'tianai',
captchaType: 'SLIDER',
payload: {
id: 'tianai-1',
data: {
trackList: [{ x: 10, y: 2 }]
}
}
});
assert.strictEqual(CaptchaPages.extractValidToken({
code: 200,
data: {
passed: true,
validToken: 'captcha-ticket'
}
}), 'captcha-ticket');
assert.strictEqual(CaptchaPages.shouldRequireCaptcha({
required: false
}), false);
assert.strictEqual(CaptchaPages.shouldRequireCaptcha({
required: true
}), true);
const modalBox = { nodeName: 'modal' };
const formBox = { nodeName: 'form-box' };
const originalDocument = global.document;
global.document = {
querySelector(selector) {
return selector === '.auth-captcha-modal[data-captcha-box]' ? modalBox : null;
}
};
assert.strictEqual(CaptchaPages.getCaptchaBox({
querySelector(selector) {
return selector === '[data-captcha-box]' ? formBox : null;
}
}), modalBox);
global.document = originalDocument;
console.log('captcha-pages tests passed');
}
run();