- 回放/下载要用的 GET /api/web/nvrs/:id/credential 不再挂 subjectAdmin: 普通成员可读(取本主体录像机凭据去拉流),范围由 credentialOf(id, subjectId) 限定当前主体, 别的主体一律 404;其余 /nvrs 管理接口仍然只给主体管理员 - 面单摄像头下发只看 cameras.status:cameras.enabled 是「导入通道那一刻」的快照, 通道后来才装上摄像头(预览正常)时它一直是 0,会把正常的一路挡在设备列表外 - 停用的工作台/摄像头不再静默丢弃,改为给 warning(设备端页面底部可见) - 补测试:nvr-credential 权限 5 条、label-scan 3 条 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
117 lines
4.0 KiB
JavaScript
117 lines
4.0 KiB
JavaScript
'use strict';
|
||
|
||
const jwt = require('jsonwebtoken');
|
||
const { app, mock, assert } = require('egg-mock/bootstrap');
|
||
|
||
/** 伪造的 sequelize 实例(toJSON 返回同一份数据) */
|
||
const instance = (data) => Object.assign({ toJSON: () => data }, data);
|
||
|
||
/**
|
||
* `GET /api/web/nvrs/:id/credential` 的权限
|
||
*
|
||
* 按运单回放/下载录像要录像机登录凭据,这是打包现场的日常动作:
|
||
* **普通成员也要能取自己主体的凭据**,但不能扩散到别的主体。
|
||
* 其余 `/api/web/nvrs` 管理接口仍然只给主体管理员。
|
||
*/
|
||
describe('test/app/controller/nvr-credential.test.js', () => {
|
||
const SUBJECT_ID = 5;
|
||
const OTHER_SUBJECT_ID = 99;
|
||
|
||
// 普通成员:不是主体创建者(adminUserId=1),role=2
|
||
const member = { id: 2, username: 'packer', nickname: '打包员', status: 1 };
|
||
const adminUserId = 1;
|
||
|
||
const token = () => {
|
||
const cfg = app.config.jwt;
|
||
const signed = jwt.sign({ sub: String(member.id), typ: 'user' }, cfg.secret, {
|
||
algorithm: 'HS256',
|
||
expiresIn: 3600,
|
||
issuer: cfg.issuer,
|
||
audience: cfg.audience,
|
||
});
|
||
return `Bearer ${signed}`;
|
||
};
|
||
|
||
const withSubject = (req, subjectId = SUBJECT_ID) => req
|
||
.set('Authorization', token())
|
||
.set('X-Subject-Id', String(subjectId));
|
||
|
||
function mockModels(models = {}) {
|
||
mock(app, 'model', Object.assign({
|
||
User: { async findByPk() { return instance(member); } },
|
||
SubjectMember: {
|
||
async findOne() {
|
||
return instance({ id: 21, subjectId: SUBJECT_ID, userId: member.id, name: '打包员', role: 2, status: 1 });
|
||
},
|
||
},
|
||
Subject: {
|
||
async findByPk() {
|
||
return instance({
|
||
id: SUBJECT_ID,
|
||
name: '测试主体',
|
||
adminUserId,
|
||
status: 1,
|
||
expireAt: new Date(Date.now() + 86400000),
|
||
});
|
||
},
|
||
},
|
||
}, models));
|
||
}
|
||
|
||
const nvrRow = (subjectId) => instance({
|
||
id: 9,
|
||
subjectId,
|
||
name: '仓库录像机',
|
||
host: '192.168.8.241',
|
||
port: 8000,
|
||
username: 'admin',
|
||
passwordEnc: 'x',
|
||
});
|
||
|
||
it('普通成员能取本主体录像机凭据(回放/下载要用)', async () => {
|
||
mockModels({
|
||
Nvr: { async findByPk() { return nvrRow(SUBJECT_ID); } },
|
||
});
|
||
app.mockService('nvr', 'passwordOf', () => 'p@ss');
|
||
|
||
const res = await withSubject(app.httpRequest().get('/api/web/nvrs/9/credential')).expect(200);
|
||
assert(res.body.data.host === '192.168.8.241', JSON.stringify(res.body));
|
||
assert(res.body.data.password === 'p@ss', '凭据要能拿到,不然桌面端拉不了流');
|
||
assert(res.body.data.hasPassword === true);
|
||
});
|
||
|
||
it('别的主体的录像机 → 404(范围只限当前主体)', async () => {
|
||
mockModels({
|
||
Nvr: { async findByPk() { return nvrRow(OTHER_SUBJECT_ID); } },
|
||
});
|
||
app.mockService('nvr', 'passwordOf', () => 'p@ss');
|
||
|
||
await withSubject(app.httpRequest().get('/api/web/nvrs/9/credential')).expect(404);
|
||
});
|
||
|
||
it('录像机不存在 → 404', async () => {
|
||
mockModels({ Nvr: { async findByPk() { return null; } } });
|
||
app.mockService('nvr', 'passwordOf', () => '');
|
||
|
||
await withSubject(app.httpRequest().get('/api/web/nvrs/9/credential')).expect(404);
|
||
});
|
||
|
||
it('没带主体 header → 400(不能靠"随便拿一台"绕过)', async () => {
|
||
mockModels({ Nvr: { async findByPk() { return nvrRow(SUBJECT_ID); } } });
|
||
app.mockService('nvr', 'passwordOf', () => 'p@ss');
|
||
|
||
const res = await app.httpRequest()
|
||
.get('/api/web/nvrs/9/credential')
|
||
.set('Authorization', token())
|
||
.expect(400);
|
||
assert(res.body.status === 'subject_required', JSON.stringify(res.body));
|
||
});
|
||
|
||
it('其余 NVR 管理接口仍然只给主体管理员(403 subject_forbidden)', async () => {
|
||
mockModels({ Nvr: { async findAll() { return []; } } });
|
||
|
||
const list = await withSubject(app.httpRequest().get('/api/web/nvrs')).expect(403);
|
||
assert(list.body.status === 'subject_forbidden', JSON.stringify(list.body));
|
||
});
|
||
});
|