fix permissions and teacher attendance workflows

This commit is contained in:
2026-07-10 20:40:52 +08:00
parent 247879f276
commit 8ed1682b90
95 changed files with 4745 additions and 1237 deletions

View File

@@ -0,0 +1,48 @@
import { UnauthorizedException } from '@nestjs/common';
import { JwtStrategy } from './jwt.strategy';
describe('JwtStrategy', () => {
const config = { get: jest.fn().mockReturnValue('secret') };
it('refreshes permissions from the database instead of trusting stale JWT permissions', async () => {
const userRepo = {
findOne: jest.fn().mockResolvedValue({
id: 7,
username: 'teacher',
isActive: true,
isArchived: false,
roles: [
{
name: '老师',
status: 1,
permissions: [{ code: 'class:view' }, { code: 'attendance:view' }],
},
],
}),
};
const strategy = new JwtStrategy(config as never, userRepo as never);
await expect(
strategy.validate({ sub: 7, username: 'teacher', permissions: ['user:delete'] }),
).resolves.toEqual({
id: 7,
username: 'teacher',
permissions: ['class:view', 'attendance:view'],
isSuperAdmin: false,
roles: ['老师'],
});
});
it.each([
[{ id: 7, isActive: false, isArchived: false, roles: [] }],
[{ id: 7, isActive: true, isArchived: true, roles: [] }],
[null],
])('rejects disabled, archived, or deleted users', async (user) => {
const userRepo = { findOne: jest.fn().mockResolvedValue(user) };
const strategy = new JwtStrategy(config as never, userRepo as never);
await expect(strategy.validate({ sub: 7, username: 'teacher' })).rejects.toBeInstanceOf(
UnauthorizedException,
);
});
});