- 已登录时 /home.htm 会 302 到 ERP 后台 /web/home,改为按「最终落地页」判断登录态 - 手动逐跳跟随重定向并合并 set-cookie:登录后会话 cookie 在 302 响应头里下发, fetch 的 redirect=follow 会丢失,导致后续请求仍是未登录会话 - checkSession 与登录后校验统一走 followGet;新下发的 cookie 一并落库 - 补单测覆盖 302 到后台 / 回登录页两种结果 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
190 lines
7.3 KiB
JavaScript
190 lines
7.3 KiB
JavaScript
'use strict';
|
||
|
||
const { assert } = require('egg-mock/bootstrap');
|
||
const crypto = require('crypto'); // eslint-disable-line no-restricted-modules
|
||
const DxmService = require('../../../app/lib/printPlatform/dxm');
|
||
|
||
/**
|
||
* 解出 encryptLoginField 的明文,验证加密格式与服务端约定一致
|
||
* @param {string} value 加密串
|
||
* @return {string} 明文
|
||
*/
|
||
function decryptLoginField(value) {
|
||
const body = String(value);
|
||
assert.strictEqual(body[0], '0');
|
||
const iv = Buffer.from(body.slice(5, 37), 'hex');
|
||
const key = Buffer.from(body.slice(37, 69), 'hex');
|
||
const data = Buffer.from(body.slice(69), 'hex');
|
||
const decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
|
||
return Buffer.concat([ decipher.update(data), decipher.final() ]).toString('utf8');
|
||
}
|
||
|
||
function createService(overrides = {}) {
|
||
const platform = { code: 'dxm', name: '店小秘', url: '', ...overrides.platform };
|
||
const account = { id: 1, account: 'user@example.com', password: 'secret', cookie: '' };
|
||
const saved = { cookie: '', status: null };
|
||
const service = new DxmService({
|
||
platform,
|
||
account: { ...account, ...overrides.account },
|
||
saveCookie: async cookie => {
|
||
saved.cookie = cookie;
|
||
},
|
||
saveLoginStatus: async status => {
|
||
saved.status = status;
|
||
},
|
||
});
|
||
return { service, saved };
|
||
}
|
||
|
||
describe('printPlatform/dxm encryptLoginField', () => {
|
||
it('格式为 0 + 随机串 + IV + Key + 密文,且可还原明文', () => {
|
||
const encrypted = DxmService.encryptLoginField('user@example.com');
|
||
assert.match(encrypted, /^0[0-9a-f]{4}[0-9a-f]{32}[0-9a-f]{32}[0-9a-f]+$/);
|
||
assert.strictEqual(decryptLoginField(encrypted), 'user@example.com');
|
||
});
|
||
});
|
||
|
||
describe('printPlatform/dxm getLoginCaptcha', () => {
|
||
it('返回 data URL,并带上验证码绑定会话', async () => {
|
||
const { service } = createService();
|
||
service.requestBinary = async () => ({
|
||
status: 200,
|
||
buffer: Buffer.from('jpeg-bytes'),
|
||
contentType: 'image/jpeg;charset=UTF-8',
|
||
setCookies: [ 'JSESSIONID=abc123; Path=/; HttpOnly', 'dxm_vc=token456; Path=/; HttpOnly' ],
|
||
});
|
||
|
||
const result = await service.getLoginCaptcha();
|
||
assert.strictEqual(result.image, `data:image/jpeg;base64,${Buffer.from('jpeg-bytes').toString('base64')}`);
|
||
assert.strictEqual(result.session, 'JSESSIONID=abc123; dxm_vc=token456');
|
||
});
|
||
|
||
it('验证码接口失败时抛错', async () => {
|
||
const { service } = createService();
|
||
service.requestBinary = async () => ({ status: 500, buffer: Buffer.alloc(0), contentType: '', setCookies: [] });
|
||
await assert.rejects(service.getLoginCaptcha(), /获取验证码失败/);
|
||
});
|
||
});
|
||
|
||
describe('printPlatform/dxm loginByPassword', () => {
|
||
it('没有验证码 / 验证码会话时直接提示', async () => {
|
||
const { service } = createService();
|
||
await assert.rejects(service.login({ session: 'JSESSIONID=abc' }), /需要图形验证码/);
|
||
await assert.rejects(service.login({ captcha: '1234' }), /验证码已过期/);
|
||
});
|
||
|
||
it('提交加密账号密码与验证码,成功后保存会话并校验登录态', async () => {
|
||
const { service, saved } = createService();
|
||
const calls = [];
|
||
service.request = async (url, options) => {
|
||
calls.push({ url, options });
|
||
if (url.endsWith('/user/userLoginNew2.json')) {
|
||
const form = new URLSearchParams(options.body);
|
||
assert.strictEqual(form.get('dxmVerify'), '1234');
|
||
assert.strictEqual(decryptLoginField(form.get('account')), 'user@example.com');
|
||
assert.strictEqual(decryptLoginField(form.get('password')), 'secret');
|
||
assert.match(options.headers.cookie, /dxm_vc=token456/);
|
||
return {
|
||
status: 200,
|
||
json: { code: 0, url: '/home.htm' },
|
||
text: '',
|
||
setCookies: [ 'JSESSIONID=logged-in; Path=/' ],
|
||
};
|
||
}
|
||
assert.match(url, /\/home\.htm$/);
|
||
assert.match(options.headers.cookie, /JSESSIONID=logged-in/);
|
||
return { status: 200, json: null, text: '', setCookies: [] };
|
||
};
|
||
// 登录后工作台会 302 到 ERP 后台 /web/home,由 followGet 跟随
|
||
service.followGet = async (url, cookie) => {
|
||
calls.push({ url, cookie });
|
||
return {
|
||
status: 200,
|
||
finalUrl: 'https://www.dianxiaomi.com/web/home',
|
||
html: '<title>店小秘--免费的跨境电商ERP</title>',
|
||
cookie: `${cookie}; dxm_i=user-1`,
|
||
};
|
||
};
|
||
|
||
const result = await service.login({ captcha: '1234', session: 'JSESSIONID=abc; dxm_vc=token456' });
|
||
|
||
assert.strictEqual(result.reused, false);
|
||
assert.strictEqual(result.cookie, 'JSESSIONID=logged-in; dxm_vc=token456; dxm_i=user-1');
|
||
assert.strictEqual(saved.cookie, result.cookie);
|
||
assert.strictEqual(saved.status, 1);
|
||
assert.strictEqual(calls.length, 2);
|
||
});
|
||
|
||
it('登录成功但工作台仍回到登录页时提示校验失败', async () => {
|
||
const { service } = createService();
|
||
service.request = async () => ({ status: 200, json: { code: 0, url: '/home.htm' }, text: '', setCookies: [] });
|
||
service.followGet = async () => ({
|
||
status: 200,
|
||
finalUrl: 'https://www.dianxiaomi.com/index.htm',
|
||
html: '<form id="loginForm">',
|
||
cookie: '',
|
||
});
|
||
|
||
await assert.rejects(
|
||
service.login({ captcha: '1234', session: 'JSESSIONID=abc; dxm_vc=token456' }),
|
||
/登录后校验失败/
|
||
);
|
||
});
|
||
|
||
it('平台返回验证码错误时抛出可读原因且不保存会话', async () => {
|
||
const { service, saved } = createService();
|
||
service.request = async () => ({
|
||
status: 200,
|
||
json: { code: -1, error: '验证码填写错误' },
|
||
text: '',
|
||
setCookies: [],
|
||
});
|
||
|
||
await assert.rejects(
|
||
service.login({ captcha: '0000', session: 'JSESSIONID=abc; dxm_vc=token456' }),
|
||
/店小秘登录失败:验证码填写错误/
|
||
);
|
||
assert.strictEqual(saved.cookie, '');
|
||
});
|
||
});
|
||
|
||
describe('printPlatform/dxm checkSession', () => {
|
||
it('工作台 302 到 ERP 后台时视为已登录,并保存新下发的 cookie', async () => {
|
||
const { service, saved } = createService({ account: { cookie: 'JSESSIONID=ok' } });
|
||
service.followGet = async () => ({
|
||
status: 200,
|
||
finalUrl: 'https://www.dianxiaomi.com/web/home',
|
||
html: '<title>店小秘--免费的跨境电商ERP</title>',
|
||
cookie: 'JSESSIONID=ok; dxm_i=user-1',
|
||
});
|
||
|
||
const result = await service.checkSession();
|
||
assert.strictEqual(result.loggedIn, true);
|
||
assert.strictEqual(saved.status, 1);
|
||
assert.strictEqual(saved.cookie, 'JSESSIONID=ok; dxm_i=user-1');
|
||
assert.strictEqual(result.url, 'https://www.dianxiaomi.com/web/home');
|
||
});
|
||
|
||
it('最终落到登录页时标记未登录', async () => {
|
||
const { service, saved } = createService({ account: { cookie: 'JSESSIONID=expired' } });
|
||
service.followGet = async () => ({
|
||
status: 200,
|
||
finalUrl: 'https://www.dianxiaomi.com/index.htm',
|
||
html: '<form id="loginForm">',
|
||
cookie: 'JSESSIONID=expired',
|
||
});
|
||
|
||
const result = await service.checkSession();
|
||
assert.strictEqual(result.loggedIn, false);
|
||
assert.strictEqual(saved.status, 0);
|
||
assert.match(result.message, /重定向到登录页/);
|
||
});
|
||
|
||
it('没有 cookie 时直接返回未登录', async () => {
|
||
const { service } = createService();
|
||
const result = await service.checkSession();
|
||
assert.strictEqual(result.loggedIn, false);
|
||
assert.match(result.message, /未保存会话/);
|
||
});
|
||
});
|