- 后端:新增 workstationConfigOf 与 GET /api/scanner/workstations,只下发绑定了本机通道的工作台及其摄像头与 NVR 凭据 - 桌面:新增 WorkstationOsdService/OsdText——扫码命中即写「摄像头名 + 单号」,单号不变不重写,连续 60s 未识别恢复工作台名;单线程写队列、失败退避、配置移除的摄像头还原一次 - 桌面:HikDeviceService.SetChannelOsdName 读-改-写 + 回读校验(GBK、31 字节、按字符边界截断) - 修复 build.ps1 / test-update.ps1 丢失 UTF-8 BOM,PowerShell 5.1 按 GBK 解析中文注释导致语法错误 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
294 lines
9.6 KiB
JavaScript
294 lines
9.6 KiB
JavaScript
'use strict';
|
||
|
||
const { app, mock, assert } = require('egg-mock/bootstrap');
|
||
const jwt = require('jsonwebtoken');
|
||
const createFakeRedis = require('../../utils/fake-redis');
|
||
const { HEADER: INTERNAL } = require('../../utils/internal');
|
||
|
||
/** 管理侧接口(绑定/列表/删除)要运维令牌;设备侧接口多带这个 header 也无副作用 */
|
||
const http = {
|
||
get: url => app.httpRequest().get(url).set(INTERNAL),
|
||
post: url => app.httpRequest().post(url).set(INTERNAL),
|
||
put: url => app.httpRequest().put(url).set(INTERNAL),
|
||
delete: url => app.httpRequest().delete(url).set(INTERNAL),
|
||
};
|
||
|
||
const instance = (data) => Object.assign(
|
||
{
|
||
toJSON: () => data,
|
||
update: async (fields) => Object.assign(data, fields),
|
||
},
|
||
data,
|
||
);
|
||
|
||
/** 假实例:update 时额外回调(用于断言写入的字段) */
|
||
const inst = (data, onUpdate) => Object.assign(
|
||
{
|
||
toJSON: () => data,
|
||
update: async fields => {
|
||
if (onUpdate) onUpdate(fields);
|
||
Object.assign(data, fields);
|
||
return data;
|
||
},
|
||
},
|
||
data,
|
||
);
|
||
|
||
/** 用与服务端一致的密钥/签发者造一个扫描器 JWT */
|
||
function signScannerToken(payload = {}, options = {}) {
|
||
return jwt.sign(
|
||
Object.assign({ sub: '1', sn: 'SN-001', typ: 'scanner', jti: 'jti-1' }, payload),
|
||
app.config.jwt.secret,
|
||
Object.assign({
|
||
algorithm: 'HS256',
|
||
expiresIn: '365d',
|
||
issuer: app.config.jwt.issuer,
|
||
audience: app.config.jwt.audience,
|
||
}, options),
|
||
);
|
||
}
|
||
|
||
describe('test/app/controller/scanner.test.js', () => {
|
||
beforeEach(() => {
|
||
mock(app, 'redis', createFakeRedis().client);
|
||
});
|
||
|
||
it('老的 sn + secret 注册/登录入口已下线(设备统一走扫码登录 GET /api/device/login)', async () => {
|
||
mock(app, 'model', { User: {}, Scanner: {} });
|
||
const reg = await http.post('/api/scanner/register').send({ sn: 'SN-001' }).expect(404);
|
||
const login = await http.post('/api/scanner/login').send({ sn: 'SN-001', secret: 'x' }).expect(404);
|
||
// 接口的 404 也要是 JSON(不然客户端按 JSON 解析会报错)
|
||
assert(reg.body.ok === false && reg.body.message === '接口不存在', JSON.stringify(reg.body));
|
||
assert(login.body.ok === false, JSON.stringify(login.body));
|
||
});
|
||
|
||
it('GET /api/scanner/me 必须带 JWT:无令牌 401 / 带令牌 200', async () => {
|
||
await http.get('/api/scanner/me').expect(401);
|
||
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: {
|
||
async findByPk(id) {
|
||
// 鉴权阶段用 payload.sub(字符串),控制器里又按主键(数字)取一次
|
||
assert(String(id) === '1');
|
||
return instance({ id: 1, sn: 'SN-001', status: 1, tokenJti: 'jti-1' });
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await http
|
||
.get('/api/scanner/me')
|
||
.set('Authorization', `Bearer ${signScannerToken()}`)
|
||
.expect(200);
|
||
|
||
assert(res.body.data.sn === 'SN-001');
|
||
assert(res.body.data.secretHash === undefined && res.body.data.tokenJti === undefined, '不应回传密钥/jti');
|
||
});
|
||
|
||
it('GET /api/scanner/me JWT 失效的各种情况', async () => {
|
||
const baseModel = {
|
||
User: {},
|
||
Scanner: {
|
||
async findByPk() {
|
||
return instance({ id: 1, sn: 'SN-001', status: 1, tokenJti: 'jti-1' });
|
||
},
|
||
},
|
||
};
|
||
|
||
// 签名不对
|
||
mock(app, 'model', baseModel);
|
||
const badSign = jwt.sign({ sub: '1', sn: 'SN-001', jti: 'jti-1' }, 'wrong-secret', {
|
||
issuer: app.config.jwt.issuer, audience: app.config.jwt.audience,
|
||
});
|
||
await http.get('/api/scanner/me').set('Authorization', `Bearer ${badSign}`).expect(401);
|
||
|
||
// 已过期
|
||
const expired = signScannerToken({}, { expiresIn: '-1s' });
|
||
await http.get('/api/scanner/me').set('Authorization', `Bearer ${expired}`).expect(401);
|
||
|
||
// jti 已被轮换
|
||
const stale = signScannerToken({ jti: 'old-jti' });
|
||
const staleRes = await http.get('/api/scanner/me').set('Authorization', `Bearer ${stale}`).expect(401);
|
||
assert(staleRes.body.message.includes('已失效'));
|
||
|
||
// 扫描器被禁用
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: { async findByPk() { return instance({ id: 1, sn: 'SN-001', status: 0, tokenJti: 'jti-1' }); } },
|
||
});
|
||
await http.get('/api/scanner/me').set('Authorization', `Bearer ${signScannerToken()}`).expect(403);
|
||
|
||
// 扫描器已删除
|
||
mock(app, 'model', { User: {}, Scanner: { async findByPk() { return null; } } });
|
||
await http.get('/api/scanner/me').set('Authorization', `Bearer ${signScannerToken()}`).expect(401);
|
||
});
|
||
|
||
it('POST /api/scanner/heartbeat 用 JWT 保活并刷新心跳时间', async () => {
|
||
let updated = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: {
|
||
async findByPk() {
|
||
return inst({ id: 1, sn: 'SN-001', status: 1, tokenJti: 'jti-1' }, fields => {
|
||
updated = fields;
|
||
});
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await http
|
||
.post('/api/scanner/heartbeat')
|
||
.set('Authorization', `Bearer ${signScannerToken()}`)
|
||
.expect(200);
|
||
|
||
assert(res.body.data.sn === 'SN-001');
|
||
assert(typeof res.body.data.serverTime === 'string');
|
||
assert(updated.lastSeenAt instanceof Date, '应刷新最近心跳时间');
|
||
});
|
||
|
||
it('POST /api/scanner/bind 支持 sn + username 绑定', async () => {
|
||
let updated = null;
|
||
mock(app, 'model', {
|
||
User: {
|
||
async findOne() {
|
||
return instance({ id: 7, username: 'admin' });
|
||
},
|
||
},
|
||
Scanner: {
|
||
async findOne() {
|
||
return inst({ id: 1, sn: 'SN-001' }, fields => {
|
||
updated = fields;
|
||
});
|
||
},
|
||
},
|
||
});
|
||
|
||
await http
|
||
.post('/api/scanner/bind')
|
||
.send({ sn: 'SN-001', username: 'admin' })
|
||
.expect(200);
|
||
|
||
assert(updated.userId === 7);
|
||
});
|
||
|
||
it('POST /api/scanner/bind 扫描器不存在 → 404,用户不存在 → 404', async () => {
|
||
mock(app, 'model', {
|
||
User: { async findOne() { return null; } },
|
||
Scanner: { async findOne() { return null; } },
|
||
});
|
||
await http.post('/api/scanner/bind').send({ sn: 'X', username: 'nobody' }).expect(404);
|
||
|
||
mock(app, 'model', {
|
||
User: { async findOne() { return null; } },
|
||
Scanner: { async findOne() { return instance({ id: 1, sn: 'SN-001' }); } },
|
||
});
|
||
const res = await http.post('/api/scanner/bind').send({ sn: 'SN-001', username: 'nobody' }).expect(404);
|
||
assert(res.body.message.includes('用户不存在'));
|
||
});
|
||
|
||
it('POST /api/scanner/unbind 解绑(清掉归属,保留扫描器)', async () => {
|
||
let updated = null;
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: {
|
||
async findOne() {
|
||
return inst({ id: 1, sn: 'SN-001', userId: 7 }, fields => {
|
||
updated = fields;
|
||
});
|
||
},
|
||
},
|
||
});
|
||
|
||
await http.post('/api/scanner/unbind').send({ sn: 'SN-001' }).expect(200);
|
||
assert(updated.userId === null);
|
||
});
|
||
|
||
it('GET /api/scanners 分页列表(带绑定账号、不含密钥与 jti)', async () => {
|
||
mock(app, 'model', {
|
||
User: { name: 'User' },
|
||
Scanner: {
|
||
async findAndCountAll(options) {
|
||
assert(options.limit === 5);
|
||
assert(options.offset === 5);
|
||
assert.deepStrictEqual(options.order, [[ 'id', 'DESC' ]]);
|
||
return {
|
||
rows: [ instance({ id: 1, sn: 'SN-001', name: '扫描枪', user: { id: 1, nickname: '管理员' } }) ],
|
||
count: 1,
|
||
};
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await http.get('/api/scanners?page=2&pageSize=5').expect(200);
|
||
assert(res.body.data.total === 1);
|
||
assert(res.body.data.page === 2);
|
||
assert(res.body.data.list[0].sn === 'SN-001');
|
||
assert(res.body.data.list[0].user.nickname === '管理员');
|
||
assert(!JSON.stringify(res.body.data).includes('tokenJti'));
|
||
});
|
||
|
||
it('GET /api/scanners/:id 详情 / 不存在 404 / id 非法 400', async () => {
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: {
|
||
async findByPk() {
|
||
return instance({ id: 1, sn: 'SN-001' });
|
||
},
|
||
},
|
||
});
|
||
await http.get('/api/scanners/1').expect(200);
|
||
|
||
mock(app, 'model', { User: {}, Scanner: { async findByPk() { return null; } } });
|
||
await http.get('/api/scanners/1').expect(404);
|
||
await http.get('/api/scanners/abc').expect(400);
|
||
});
|
||
|
||
it('DELETE /api/scanners/:id 软删除扫描器', async () => {
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: {
|
||
async destroy(options) {
|
||
assert.deepStrictEqual(options.where, { id: 1 });
|
||
return 1;
|
||
},
|
||
},
|
||
});
|
||
|
||
await http.delete('/api/scanners/1').expect(200);
|
||
});
|
||
|
||
it('GET /api/scanner/workstations 返回工作台 OSD 配置(需设备令牌)', async () => {
|
||
mock(app, 'model', {
|
||
User: {},
|
||
Scanner: {
|
||
async findByPk() {
|
||
return instance({ id: 1, sn: 'SN-001', status: 1, tokenJti: 'jti-1' });
|
||
},
|
||
},
|
||
});
|
||
app.mockService('labelScan', 'workstationConfigOf', async () => ({
|
||
revision: 'sha1:0123456789abcdef',
|
||
items: [{
|
||
workstationId: 2,
|
||
workstationCode: 'WS-2',
|
||
workstationName: '2号线',
|
||
wsType: 1,
|
||
channels: [{ channelId: 'CH1', channelName: '2号线扫码枪' }],
|
||
cameras: [{ cameraId: 11, cameraName: '2号线面单', cameraChannelNo: 45, streamType: 0, nvr: { id: 1 } }],
|
||
}],
|
||
warnings: [],
|
||
}));
|
||
|
||
const res = await http
|
||
.get('/api/scanner/workstations')
|
||
.set('Authorization', `Bearer ${signScannerToken()}`)
|
||
.expect(200);
|
||
|
||
assert(res.body.ok === true, JSON.stringify(res.body));
|
||
assert(res.body.data.items.length === 1 && res.body.data.items[0].cameras.length === 1);
|
||
});
|
||
|
||
it('GET /api/scanner/workstations 没令牌 401', async () => {
|
||
await http.get('/api/scanner/workstations').expect(401);
|
||
});});
|