refactor: 统一代码风格并迁移至 ESLint 新配置
style(backend): 格式化模型文件代码 style(frontend): 调整组件代码格式 chore: 删除旧 ESLint 配置并添加新配置 refactor(backend): 重构模型定义语法 style: 统一箭头函数和对象属性简写
This commit is contained in:
@@ -28,14 +28,14 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
username: 'admin',
|
||||
password: '$2a$10$test',
|
||||
realName: '管理员',
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
testRoom = await Room.create({
|
||||
roomId: 'ROOM_INT_TEST',
|
||||
name: '测试机房',
|
||||
location: '测试位置',
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
testRack = await Rack.create({
|
||||
@@ -46,12 +46,17 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
currentPower: 0,
|
||||
totalUnits: 48,
|
||||
usedUnits: 0,
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
app = createTestApp();
|
||||
authToken = jwt.sign(
|
||||
{ userId: adminUser.userId, username: adminUser.username, realName: adminUser.realName, roleName: '管理员' },
|
||||
{
|
||||
userId: adminUser.userId,
|
||||
username: adminUser.username,
|
||||
realName: adminUser.realName,
|
||||
roleName: '管理员',
|
||||
},
|
||||
JWT_SECRET,
|
||||
{ expiresIn: '24h' }
|
||||
);
|
||||
@@ -101,7 +106,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
position: 1,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'running'
|
||||
status: 'running',
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
@@ -116,8 +121,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'device',
|
||||
operationType: 'create',
|
||||
targetId: response.body.deviceId
|
||||
}
|
||||
targetId: response.body.deviceId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -135,7 +140,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
position: 5,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'offline'
|
||||
status: 'offline',
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
@@ -150,8 +155,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'device',
|
||||
operationType: 'update',
|
||||
targetId: device.deviceId
|
||||
}
|
||||
targetId: device.deviceId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -168,7 +173,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
position: 10,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'running'
|
||||
status: 'running',
|
||||
});
|
||||
|
||||
await request(app)
|
||||
@@ -180,8 +185,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'device',
|
||||
operationType: 'delete',
|
||||
targetId: device.deviceId
|
||||
}
|
||||
targetId: device.deviceId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -197,7 +202,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
position: 15,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'running'
|
||||
status: 'running',
|
||||
});
|
||||
|
||||
const device2 = await Device.create({
|
||||
@@ -208,7 +213,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
position: 20,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'running'
|
||||
status: 'running',
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
@@ -218,7 +223,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { operationType: 'batch_delete' }
|
||||
where: { operationType: 'batch_delete' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -234,7 +239,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
position: 25,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'offline'
|
||||
status: 'offline',
|
||||
});
|
||||
|
||||
const device2 = await Device.create({
|
||||
@@ -245,7 +250,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
position: 30,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'offline'
|
||||
status: 'offline',
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
@@ -255,7 +260,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
.expect(200);
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { operationType: 'status_change' }
|
||||
where: { operationType: 'status_change' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -273,7 +278,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
roleName: '测试角色',
|
||||
roleCode: `test_role_${Date.now()}`,
|
||||
permissions: ['read', 'write'],
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
});
|
||||
});
|
||||
|
||||
@@ -283,7 +288,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
password: 'Password123!',
|
||||
realName: '集成测试用户',
|
||||
email: `test_${Date.now()}@example.com`,
|
||||
roleIds: [testRole.roleId]
|
||||
roleIds: [testRole.roleId],
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
@@ -296,8 +301,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'user',
|
||||
operationType: 'create',
|
||||
targetName: userData.username
|
||||
}
|
||||
targetName: userData.username,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -314,7 +319,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
password: 'Password123!',
|
||||
realName: '旧名称用户',
|
||||
email: `old_${Date.now()}@example.com`,
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
@@ -327,8 +332,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'user',
|
||||
operationType: 'update',
|
||||
targetId: user.userId
|
||||
}
|
||||
targetId: user.userId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -345,7 +350,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
password: 'Password123!',
|
||||
realName: '角色测试用户',
|
||||
email: `role_${Date.now()}@example.com`,
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
const newRole = await Role.create({
|
||||
@@ -353,12 +358,12 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
roleName: '新测试角色',
|
||||
roleCode: `new_role_${Date.now()}`,
|
||||
permissions: ['admin'],
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
await UserRole.create({
|
||||
UserId: user.userId,
|
||||
RoleId: testRole.roleId
|
||||
RoleId: testRole.roleId,
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
@@ -371,8 +376,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'user',
|
||||
operationType: 'permission_change',
|
||||
targetId: user.userId
|
||||
}
|
||||
targetId: user.userId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -391,7 +396,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
password: 'Password123!',
|
||||
realName: '删除测试用户',
|
||||
email: `del_${Date.now()}@example.com`,
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
await request(app)
|
||||
@@ -403,8 +408,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'user',
|
||||
operationType: 'delete',
|
||||
targetId: user.userId
|
||||
}
|
||||
targetId: user.userId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -419,7 +424,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
roleCode: `int_test_role_${Date.now()}`,
|
||||
description: '集成测试用角色',
|
||||
permissions: ['read', 'write'],
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
};
|
||||
|
||||
const response = await request(app)
|
||||
@@ -432,8 +437,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'role',
|
||||
operationType: 'create',
|
||||
targetId: response.body.roleId
|
||||
}
|
||||
targetId: response.body.roleId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -450,7 +455,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
roleCode: `old_role_${Date.now()}`,
|
||||
description: '旧描述',
|
||||
permissions: ['read'],
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
@@ -458,7 +463,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
.set('Authorization', `Bearer ${authToken}`)
|
||||
.send({
|
||||
roleName: `新角色名_${Date.now()}`,
|
||||
permissions: ['read', 'write', 'delete']
|
||||
permissions: ['read', 'write', 'delete'],
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
@@ -466,8 +471,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'role',
|
||||
operationType: 'update',
|
||||
targetId: role.roleId
|
||||
}
|
||||
targetId: role.roleId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -484,7 +489,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
roleCode: `del_role_${Date.now()}`,
|
||||
description: '待删除',
|
||||
permissions: ['read'],
|
||||
status: 'active'
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
await request(app)
|
||||
@@ -496,8 +501,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'role',
|
||||
operationType: 'delete',
|
||||
targetId: role.roleId
|
||||
}
|
||||
targetId: role.roleId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -515,7 +520,7 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
position: 40,
|
||||
height: 2,
|
||||
powerConsumption: 500,
|
||||
status: 'running'
|
||||
status: 'running',
|
||||
});
|
||||
|
||||
await request(app)
|
||||
@@ -533,8 +538,8 @@ describe('设备/用户/角色操作日志集成测试', () => {
|
||||
where: {
|
||||
module: 'device',
|
||||
operationType: 'update',
|
||||
targetId: device.deviceId
|
||||
}
|
||||
targetId: device.deviceId,
|
||||
},
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
|
||||
@@ -36,7 +36,7 @@ describe('OperationLog 模型测试', () => {
|
||||
operatorId: 'user_001',
|
||||
operatorName: '测试用户',
|
||||
operatorRole: '管理员',
|
||||
result: 'success'
|
||||
result: 'success',
|
||||
};
|
||||
|
||||
const log = await OperationLog.create(logData);
|
||||
@@ -69,7 +69,7 @@ describe('OperationLog 模型测试', () => {
|
||||
operatorName: '测试用户',
|
||||
beforeState,
|
||||
afterState,
|
||||
result: 'success'
|
||||
result: 'success',
|
||||
});
|
||||
|
||||
expect(log.beforeState).toEqual(beforeState);
|
||||
@@ -80,7 +80,7 @@ describe('OperationLog 模型测试', () => {
|
||||
const metadata = {
|
||||
count: 5,
|
||||
source: 'batch_operation',
|
||||
extraInfo: '额外信息'
|
||||
extraInfo: '额外信息',
|
||||
};
|
||||
|
||||
const log = await OperationLog.create({
|
||||
@@ -93,7 +93,7 @@ describe('OperationLog 模型测试', () => {
|
||||
operatorId: 'user_001',
|
||||
operatorName: '测试用户',
|
||||
metadata,
|
||||
result: 'success'
|
||||
result: 'success',
|
||||
});
|
||||
|
||||
expect(log.metadata).toEqual(metadata);
|
||||
@@ -111,7 +111,7 @@ describe('OperationLog 模型测试', () => {
|
||||
operatorName: '管理员',
|
||||
ipAddress: '192.168.1.100',
|
||||
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
|
||||
result: 'success'
|
||||
result: 'success',
|
||||
});
|
||||
|
||||
expect(log.ipAddress).toBe('192.168.1.100');
|
||||
@@ -127,7 +127,7 @@ describe('OperationLog 模型测试', () => {
|
||||
targetId: 'DEV005',
|
||||
targetName: 'TEST_DEVICE',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '测试用户'
|
||||
operatorName: '测试用户',
|
||||
});
|
||||
|
||||
expect(log.result).toBe('success');
|
||||
@@ -142,7 +142,7 @@ describe('OperationLog 模型测试', () => {
|
||||
targetId: 'role_test',
|
||||
targetName: '测试角色',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '测试用户'
|
||||
operatorName: '测试用户',
|
||||
});
|
||||
|
||||
expect(log.metadata).toEqual({});
|
||||
@@ -161,7 +161,7 @@ describe('OperationLog 模型测试', () => {
|
||||
targetName: '设备A',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '用户A',
|
||||
result: 'success'
|
||||
result: 'success',
|
||||
},
|
||||
{
|
||||
recordId: 'OPLOG_QUERY_002',
|
||||
@@ -172,7 +172,7 @@ describe('OperationLog 模型测试', () => {
|
||||
targetName: '设备B',
|
||||
operatorId: 'user_002',
|
||||
operatorName: '用户B',
|
||||
result: 'success'
|
||||
result: 'success',
|
||||
},
|
||||
{
|
||||
recordId: 'OPLOG_QUERY_003',
|
||||
@@ -183,7 +183,7 @@ describe('OperationLog 模型测试', () => {
|
||||
targetName: '用户C',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '用户A',
|
||||
result: 'success'
|
||||
result: 'success',
|
||||
},
|
||||
{
|
||||
recordId: 'OPLOG_QUERY_004',
|
||||
@@ -194,35 +194,35 @@ describe('OperationLog 模型测试', () => {
|
||||
targetName: '设备D',
|
||||
operatorId: 'user_001',
|
||||
operatorName: '用户A',
|
||||
result: 'failed'
|
||||
}
|
||||
result: 'failed',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('应该能够按 module 查询', async () => {
|
||||
const deviceLogs = await OperationLog.findAll({
|
||||
where: { module: 'device' }
|
||||
where: { module: 'device' },
|
||||
});
|
||||
expect(deviceLogs.length).toBe(3);
|
||||
});
|
||||
|
||||
test('应该能够按 operationType 查询', async () => {
|
||||
const createLogs = await OperationLog.findAll({
|
||||
where: { operationType: 'create' }
|
||||
where: { operationType: 'create' },
|
||||
});
|
||||
expect(createLogs.length).toBe(2);
|
||||
});
|
||||
|
||||
test('应该能够按 operatorId 查询', async () => {
|
||||
const user001Logs = await OperationLog.findAll({
|
||||
where: { operatorId: 'user_001' }
|
||||
where: { operatorId: 'user_001' },
|
||||
});
|
||||
expect(user001Logs.length).toBe(3);
|
||||
});
|
||||
|
||||
test('应该能够按 result 查询', async () => {
|
||||
const failedLogs = await OperationLog.findAll({
|
||||
where: { result: 'failed' }
|
||||
where: { result: 'failed' },
|
||||
});
|
||||
expect(failedLogs.length).toBe(1);
|
||||
});
|
||||
@@ -231,8 +231,8 @@ describe('OperationLog 模型测试', () => {
|
||||
const { Op } = require('sequelize');
|
||||
const logs = await OperationLog.findAll({
|
||||
where: {
|
||||
targetId: { [Op.like]: '%DEV%' }
|
||||
}
|
||||
targetId: { [Op.like]: '%DEV%' },
|
||||
},
|
||||
});
|
||||
expect(logs.length).toBe(3);
|
||||
});
|
||||
@@ -241,7 +241,7 @@ describe('OperationLog 模型测试', () => {
|
||||
const { count, rows } = await OperationLog.findAndCountAll({
|
||||
limit: 2,
|
||||
offset: 0,
|
||||
order: [['createdAt', 'DESC']]
|
||||
order: [['createdAt', 'DESC']],
|
||||
});
|
||||
expect(count).toBe(4);
|
||||
expect(rows.length).toBe(2);
|
||||
|
||||
@@ -4,7 +4,7 @@ const {
|
||||
logOperation,
|
||||
logDeviceOperation,
|
||||
logUserOperation,
|
||||
logRoleOperation
|
||||
logRoleOperation,
|
||||
} = require('../utils/operationLogger');
|
||||
|
||||
describe('operationLogger 工具函数测试', () => {
|
||||
@@ -22,12 +22,12 @@ describe('operationLogger 工具函数测试', () => {
|
||||
user: {
|
||||
userId: 'user_test_001',
|
||||
realName: '测试用户',
|
||||
roleName: '管理员'
|
||||
roleName: '管理员',
|
||||
},
|
||||
headers: {
|
||||
'x-forwarded-for': '192.168.1.100',
|
||||
'user-agent': 'Mozilla/5.0 Test Browser'
|
||||
}
|
||||
'user-agent': 'Mozilla/5.0 Test Browser',
|
||||
},
|
||||
};
|
||||
|
||||
await logOperation({
|
||||
@@ -39,11 +39,11 @@ describe('operationLogger 工具函数测试', () => {
|
||||
beforeState: null,
|
||||
afterState: { name: '测试设备', status: 'running' },
|
||||
result: 'success',
|
||||
req: mockReq
|
||||
req: mockReq,
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV_TEST_001' }
|
||||
where: { targetId: 'DEV_TEST_001' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -63,7 +63,7 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该能够处理没有用户信息的请求', async () => {
|
||||
const mockReq = {
|
||||
user: null,
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
await logOperation({
|
||||
@@ -73,11 +73,11 @@ describe('operationLogger 工具函数测试', () => {
|
||||
targetId: 'SYSTEM',
|
||||
targetName: '系统',
|
||||
result: 'success',
|
||||
req: mockReq
|
||||
req: mockReq,
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'SYSTEM' }
|
||||
where: { targetId: 'SYSTEM' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -93,11 +93,11 @@ describe('operationLogger 工具函数测试', () => {
|
||||
targetId: 'DEV_NO_REQ',
|
||||
targetName: '无请求设备',
|
||||
result: 'success',
|
||||
req: null
|
||||
req: null,
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV_NO_REQ' }
|
||||
where: { targetId: 'DEV_NO_REQ' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -108,7 +108,7 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该能够记录失败的操作', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_fail', realName: '失败用户' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
await logOperation({
|
||||
@@ -119,11 +119,11 @@ describe('operationLogger 工具函数测试', () => {
|
||||
targetName: '失败设备',
|
||||
result: 'failed',
|
||||
req: mockReq,
|
||||
metadata: { errorMessage: '设备不存在' }
|
||||
metadata: { errorMessage: '设备不存在' },
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV_FAIL' }
|
||||
where: { targetId: 'DEV_FAIL' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -133,13 +133,13 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该使用提供的 metadata', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_meta', realName: '元数据用户' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
const customMetadata = {
|
||||
batchCount: 10,
|
||||
source: 'import',
|
||||
duration: 5000
|
||||
duration: 5000,
|
||||
};
|
||||
|
||||
await logOperation({
|
||||
@@ -150,11 +150,11 @@ describe('operationLogger 工具函数测试', () => {
|
||||
targetName: '批量设备',
|
||||
result: 'success',
|
||||
req: mockReq,
|
||||
metadata: customMetadata
|
||||
metadata: customMetadata,
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV_BATCH' }
|
||||
where: { targetId: 'DEV_BATCH' },
|
||||
});
|
||||
|
||||
expect(logs[0].metadata).toEqual(customMetadata);
|
||||
@@ -165,22 +165,18 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录设备创建日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_dev', realName: '设备管理员' },
|
||||
headers: { 'x-forwarded-for': '10.0.0.1' }
|
||||
headers: { 'x-forwarded-for': '10.0.0.1' },
|
||||
};
|
||||
|
||||
await logDeviceOperation(
|
||||
'create',
|
||||
'创建设备 测试服务器 (DEV001)',
|
||||
{
|
||||
targetId: 'DEV001',
|
||||
targetName: '测试服务器',
|
||||
afterState: { deviceId: 'DEV001', name: '测试服务器', status: 'running' },
|
||||
req: mockReq
|
||||
}
|
||||
);
|
||||
await logDeviceOperation('create', '创建设备 测试服务器 (DEV001)', {
|
||||
targetId: 'DEV001',
|
||||
targetName: '测试服务器',
|
||||
afterState: { deviceId: 'DEV001', name: '测试服务器', status: 'running' },
|
||||
req: mockReq,
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'device', operationType: 'create' }
|
||||
where: { module: 'device', operationType: 'create' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -193,26 +189,22 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录设备更新日志并包含状态变更', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_upd', realName: '更新操作员' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
const beforeState = { name: '旧名称', status: 'offline' };
|
||||
const afterState = { name: '新名称', status: 'running' };
|
||||
|
||||
await logDeviceOperation(
|
||||
'update',
|
||||
'更新设备 DEV002',
|
||||
{
|
||||
targetId: 'DEV002',
|
||||
targetName: 'DEV002',
|
||||
beforeState,
|
||||
afterState,
|
||||
req: mockReq
|
||||
}
|
||||
);
|
||||
await logDeviceOperation('update', '更新设备 DEV002', {
|
||||
targetId: 'DEV002',
|
||||
targetName: 'DEV002',
|
||||
beforeState,
|
||||
afterState,
|
||||
req: mockReq,
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV002', operationType: 'update' }
|
||||
where: { targetId: 'DEV002', operationType: 'update' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -223,22 +215,18 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录设备删除日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_del', realName: '删除操作员' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
await logDeviceOperation(
|
||||
'delete',
|
||||
'删除设备 DEV003 (测试服务器)',
|
||||
{
|
||||
targetId: 'DEV003',
|
||||
targetName: '测试服务器',
|
||||
beforeState: { deviceId: 'DEV003', name: '测试服务器' },
|
||||
req: mockReq
|
||||
}
|
||||
);
|
||||
await logDeviceOperation('delete', '删除设备 DEV003 (测试服务器)', {
|
||||
targetId: 'DEV003',
|
||||
targetName: '测试服务器',
|
||||
beforeState: { deviceId: 'DEV003', name: '测试服务器' },
|
||||
req: mockReq,
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { targetId: 'DEV003', operationType: 'delete' }
|
||||
where: { targetId: 'DEV003', operationType: 'delete' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -247,27 +235,23 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录批量删除日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_batch', realName: '批量操作员' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
await logDeviceOperation(
|
||||
'batch_delete',
|
||||
'批量删除设备 3台 (DEV_A,DEV_B,DEV_C)',
|
||||
{
|
||||
targetId: 'DEV_A,DEV_B,DEV_C',
|
||||
targetName: '3台设备',
|
||||
beforeState: [
|
||||
{ deviceId: 'DEV_A', name: '设备A' },
|
||||
{ deviceId: 'DEV_B', name: '设备B' },
|
||||
{ deviceId: 'DEV_C', name: '设备C' }
|
||||
],
|
||||
req: mockReq,
|
||||
metadata: { count: 3 }
|
||||
}
|
||||
);
|
||||
await logDeviceOperation('batch_delete', '批量删除设备 3台 (DEV_A,DEV_B,DEV_C)', {
|
||||
targetId: 'DEV_A,DEV_B,DEV_C',
|
||||
targetName: '3台设备',
|
||||
beforeState: [
|
||||
{ deviceId: 'DEV_A', name: '设备A' },
|
||||
{ deviceId: 'DEV_B', name: '设备B' },
|
||||
{ deviceId: 'DEV_C', name: '设备C' },
|
||||
],
|
||||
req: mockReq,
|
||||
metadata: { count: 3 },
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { operationType: 'batch_delete' }
|
||||
where: { operationType: 'batch_delete' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -277,30 +261,26 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录状态变更日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'user_status', realName: '状态管理员' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
await logDeviceOperation(
|
||||
'status_change',
|
||||
'批量变更设备状态为"运行中"',
|
||||
{
|
||||
targetId: 'DEV_STATUS_1,DEV_STATUS_2',
|
||||
targetName: '2台设备',
|
||||
beforeState: [
|
||||
{ deviceId: 'DEV_STATUS_1', status: 'offline' },
|
||||
{ deviceId: 'DEV_STATUS_2', status: 'maintenance' }
|
||||
],
|
||||
afterState: [
|
||||
{ deviceId: 'DEV_STATUS_1', status: 'running' },
|
||||
{ deviceId: 'DEV_STATUS_2', status: 'running' }
|
||||
],
|
||||
req: mockReq,
|
||||
metadata: { status: 'running', count: 2 }
|
||||
}
|
||||
);
|
||||
await logDeviceOperation('status_change', '批量变更设备状态为"运行中"', {
|
||||
targetId: 'DEV_STATUS_1,DEV_STATUS_2',
|
||||
targetName: '2台设备',
|
||||
beforeState: [
|
||||
{ deviceId: 'DEV_STATUS_1', status: 'offline' },
|
||||
{ deviceId: 'DEV_STATUS_2', status: 'maintenance' },
|
||||
],
|
||||
afterState: [
|
||||
{ deviceId: 'DEV_STATUS_1', status: 'running' },
|
||||
{ deviceId: 'DEV_STATUS_2', status: 'running' },
|
||||
],
|
||||
req: mockReq,
|
||||
metadata: { status: 'running', count: 2 },
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { operationType: 'status_change' }
|
||||
where: { operationType: 'status_change' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -312,23 +292,19 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录用户创建日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '系统管理员' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
await logUserOperation(
|
||||
'create',
|
||||
'创建用户 new_user',
|
||||
{
|
||||
targetId: 'user_new',
|
||||
targetName: 'new_user',
|
||||
afterState: { username: 'new_user', email: 'new@example.com' },
|
||||
req: mockReq,
|
||||
metadata: { roleIds: ['role_admin'] }
|
||||
}
|
||||
);
|
||||
await logUserOperation('create', '创建用户 new_user', {
|
||||
targetId: 'user_new',
|
||||
targetName: 'new_user',
|
||||
afterState: { username: 'new_user', email: 'new@example.com' },
|
||||
req: mockReq,
|
||||
metadata: { roleIds: ['role_admin'] },
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'user', operationType: 'create' }
|
||||
where: { module: 'user', operationType: 'create' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -339,24 +315,20 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录权限变更日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '管理员' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
await logUserOperation(
|
||||
'permission_change',
|
||||
'变更用户 test_user 的角色',
|
||||
{
|
||||
targetId: 'user_test',
|
||||
targetName: 'test_user',
|
||||
beforeState: { roleIds: ['role_viewer'] },
|
||||
afterState: { roleIds: ['role_admin'] },
|
||||
req: mockReq,
|
||||
metadata: { oldRoleIds: ['role_viewer'], newRoleIds: ['role_admin'] }
|
||||
}
|
||||
);
|
||||
await logUserOperation('permission_change', '变更用户 test_user 的角色', {
|
||||
targetId: 'user_test',
|
||||
targetName: 'test_user',
|
||||
beforeState: { roleIds: ['role_viewer'] },
|
||||
afterState: { roleIds: ['role_admin'] },
|
||||
req: mockReq,
|
||||
metadata: { oldRoleIds: ['role_viewer'], newRoleIds: ['role_admin'] },
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { operationType: 'permission_change' }
|
||||
where: { operationType: 'permission_change' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -367,22 +339,18 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录用户删除日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '管理员' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
await logUserOperation(
|
||||
'delete',
|
||||
'删除用户 deleted_user',
|
||||
{
|
||||
targetId: 'user_deleted',
|
||||
targetName: 'deleted_user',
|
||||
beforeState: { username: 'deleted_user', email: 'deleted@example.com' },
|
||||
req: mockReq
|
||||
}
|
||||
);
|
||||
await logUserOperation('delete', '删除用户 deleted_user', {
|
||||
targetId: 'user_deleted',
|
||||
targetName: 'deleted_user',
|
||||
beforeState: { username: 'deleted_user', email: 'deleted@example.com' },
|
||||
req: mockReq,
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'user', operationType: 'delete' }
|
||||
where: { module: 'user', operationType: 'delete' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -393,23 +361,19 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录角色创建日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '管理员' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
await logRoleOperation(
|
||||
'create',
|
||||
'创建角色 测试角色',
|
||||
{
|
||||
targetId: 'role_test',
|
||||
targetName: '测试角色',
|
||||
afterState: { roleName: '测试角色', permissions: ['read', 'write'] },
|
||||
req: mockReq,
|
||||
metadata: { roleCode: 'test_role', permissions: ['read', 'write'] }
|
||||
}
|
||||
);
|
||||
await logRoleOperation('create', '创建角色 测试角色', {
|
||||
targetId: 'role_test',
|
||||
targetName: '测试角色',
|
||||
afterState: { roleName: '测试角色', permissions: ['read', 'write'] },
|
||||
req: mockReq,
|
||||
metadata: { roleCode: 'test_role', permissions: ['read', 'write'] },
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'role', operationType: 'create' }
|
||||
where: { module: 'role', operationType: 'create' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -420,27 +384,23 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录角色更新日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '管理员' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
const beforeState = { roleName: '旧角色', permissions: ['read'] };
|
||||
const afterState = { roleName: '新角色', permissions: ['read', 'write', 'delete'] };
|
||||
|
||||
await logRoleOperation(
|
||||
'update',
|
||||
'更新角色 角色A',
|
||||
{
|
||||
targetId: 'role_a',
|
||||
targetName: '角色A',
|
||||
beforeState,
|
||||
afterState,
|
||||
req: mockReq,
|
||||
metadata: { oldRoleName: '旧角色', oldPermissions: ['read'] }
|
||||
}
|
||||
);
|
||||
await logRoleOperation('update', '更新角色 角色A', {
|
||||
targetId: 'role_a',
|
||||
targetName: '角色A',
|
||||
beforeState,
|
||||
afterState,
|
||||
req: mockReq,
|
||||
metadata: { oldRoleName: '旧角色', oldPermissions: ['read'] },
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'role', operationType: 'update' }
|
||||
where: { module: 'role', operationType: 'update' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
@@ -451,23 +411,19 @@ describe('operationLogger 工具函数测试', () => {
|
||||
test('应该记录角色删除日志', async () => {
|
||||
const mockReq = {
|
||||
user: { userId: 'admin', realName: '管理员' },
|
||||
headers: {}
|
||||
headers: {},
|
||||
};
|
||||
|
||||
await logRoleOperation(
|
||||
'delete',
|
||||
'删除角色 测试角色B',
|
||||
{
|
||||
targetId: 'role_b',
|
||||
targetName: '测试角色B',
|
||||
beforeState: { roleName: '测试角色B', userCount: 0 },
|
||||
req: mockReq,
|
||||
metadata: { userCount: 0 }
|
||||
}
|
||||
);
|
||||
await logRoleOperation('delete', '删除角色 测试角色B', {
|
||||
targetId: 'role_b',
|
||||
targetName: '测试角色B',
|
||||
beforeState: { roleName: '测试角色B', userCount: 0 },
|
||||
req: mockReq,
|
||||
metadata: { userCount: 0 },
|
||||
});
|
||||
|
||||
const logs = await OperationLog.findAll({
|
||||
where: { module: 'role', operationType: 'delete' }
|
||||
where: { module: 'role', operationType: 'delete' },
|
||||
});
|
||||
|
||||
expect(logs.length).toBe(1);
|
||||
|
||||
@@ -44,7 +44,7 @@ const createTestApp = () => {
|
||||
keyword,
|
||||
startDate,
|
||||
endDate,
|
||||
result
|
||||
result,
|
||||
} = req.query;
|
||||
|
||||
const offset = (parseInt(page) - 1) * parseInt(pageSize);
|
||||
@@ -72,7 +72,7 @@ const createTestApp = () => {
|
||||
where[Op.or] = [
|
||||
{ operationDescription: { [Op.like]: `%${keyword}%` } },
|
||||
{ targetName: { [Op.like]: `%${keyword}%` } },
|
||||
{ operatorName: { [Op.like]: `%${keyword}%` } }
|
||||
{ operatorName: { [Op.like]: `%${keyword}%` } },
|
||||
];
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ const createTestApp = () => {
|
||||
where,
|
||||
order: [['createdAt', 'DESC']],
|
||||
offset,
|
||||
limit
|
||||
limit,
|
||||
});
|
||||
|
||||
res.json({
|
||||
@@ -105,14 +105,14 @@ const createTestApp = () => {
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
pageSize: parseInt(pageSize),
|
||||
logs
|
||||
}
|
||||
logs,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取操作日志失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取操作日志失败'
|
||||
message: '获取操作日志失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -124,19 +124,19 @@ const createTestApp = () => {
|
||||
if (!log) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: '日志记录不存在'
|
||||
message: '日志记录不存在',
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: log
|
||||
data: log,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('获取操作日志详情失败:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: '获取操作日志详情失败'
|
||||
message: '获取操作日志详情失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -153,7 +153,7 @@ describe('OperationLogs API 路由测试', () => {
|
||||
userId: 'test_user_001',
|
||||
username: 'testuser',
|
||||
realName: '测试用户',
|
||||
roleName: '管理员'
|
||||
roleName: '管理员',
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -181,16 +181,14 @@ describe('OperationLogs API 路由测试', () => {
|
||||
operatorId: testUser.userId,
|
||||
operatorName: testUser.realName,
|
||||
operatorRole: testUser.roleName,
|
||||
result: 'success'
|
||||
result: 'success',
|
||||
};
|
||||
return await OperationLog.create({ ...defaultData, ...data });
|
||||
};
|
||||
|
||||
describe('GET /api/operation-logs', () => {
|
||||
test('未授权访问应该返回 401', async () => {
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs')
|
||||
.expect(401);
|
||||
const response = await request(app).get('/api/operation-logs').expect(401);
|
||||
|
||||
expect(response.body.success).toBe(false);
|
||||
});
|
||||
@@ -258,9 +256,21 @@ describe('OperationLogs API 路由测试', () => {
|
||||
});
|
||||
|
||||
test('应该支持按 keyword 搜索', async () => {
|
||||
await createTestLog({ operationDescription: '创建设备 SERVER_A', recordId: 'OPLOG_KW_1', targetName: '服务器A' });
|
||||
await createTestLog({ operationDescription: '更新设备 SERVER_B', recordId: 'OPLOG_KW_2', targetName: '服务器B' });
|
||||
await createTestLog({ operationDescription: '删除用户 USER_C', recordId: 'OPLOG_KW_3', targetName: '用户C' });
|
||||
await createTestLog({
|
||||
operationDescription: '创建设备 SERVER_A',
|
||||
recordId: 'OPLOG_KW_1',
|
||||
targetName: '服务器A',
|
||||
});
|
||||
await createTestLog({
|
||||
operationDescription: '更新设备 SERVER_B',
|
||||
recordId: 'OPLOG_KW_2',
|
||||
targetName: '服务器B',
|
||||
});
|
||||
await createTestLog({
|
||||
operationDescription: '删除用户 USER_C',
|
||||
recordId: 'OPLOG_KW_3',
|
||||
targetName: '用户C',
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.get('/api/operation-logs')
|
||||
@@ -269,7 +279,9 @@ describe('OperationLogs API 路由测试', () => {
|
||||
.expect(200);
|
||||
|
||||
expect(response.body.data.logs).toHaveLength(2);
|
||||
expect(response.body.data.logs.every(log => log.operationDescription.includes('SERVER'))).toBe(true);
|
||||
expect(
|
||||
response.body.data.logs.every(log => log.operationDescription.includes('SERVER'))
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test('应该支持按 result 筛选', async () => {
|
||||
@@ -291,19 +303,19 @@ describe('OperationLogs API 路由测试', () => {
|
||||
module: 'device',
|
||||
operationType: 'create',
|
||||
result: 'success',
|
||||
recordId: 'OPLOG_COMB_1'
|
||||
recordId: 'OPLOG_COMB_1',
|
||||
});
|
||||
await createTestLog({
|
||||
module: 'device',
|
||||
operationType: 'update',
|
||||
result: 'success',
|
||||
recordId: 'OPLOG_COMB_2'
|
||||
recordId: 'OPLOG_COMB_2',
|
||||
});
|
||||
await createTestLog({
|
||||
module: 'user',
|
||||
operationType: 'create',
|
||||
result: 'success',
|
||||
recordId: 'OPLOG_COMB_3'
|
||||
recordId: 'OPLOG_COMB_3',
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
@@ -324,7 +336,7 @@ describe('OperationLogs API 路由测试', () => {
|
||||
recordId: 'OPLOG_DETAIL_001',
|
||||
beforeState: { name: '旧名称' },
|
||||
afterState: { name: '新名称' },
|
||||
metadata: { customField: '自定义值' }
|
||||
metadata: { customField: '自定义值' },
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
|
||||
Reference in New Issue
Block a user