63 lines
2.6 KiB
TypeScript
63 lines
2.6 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { JwtService } from '@nestjs/jwt';
|
|
import { ForbiddenException, UnauthorizedException } from '@nestjs/common';
|
|
import { AuthService } from '../src/auth/auth.service.js';
|
|
import type { JwtUser } from '../src/common/auth.types.js';
|
|
import { KeyResultsService } from '../src/key-results/key-results.service.js';
|
|
import { ObjectivesService } from '../src/objectives/objectives.service.js';
|
|
import { PrismaService } from '../src/prisma/prisma.service.js';
|
|
|
|
const employeeUser: JwtUser = {
|
|
sub: 3,
|
|
email: 'employee@okr.local',
|
|
role: 'EMPLOYEE',
|
|
name: 'Nguyen Van A',
|
|
};
|
|
|
|
const managerUser: JwtUser = {
|
|
sub: 2,
|
|
email: 'manager@okr.local',
|
|
role: 'MANAGER',
|
|
name: 'Nguyen Van Manager',
|
|
};
|
|
|
|
test('AuthService rejects invalid passwords and signs valid users', async () => {
|
|
const prisma = new PrismaService();
|
|
await prisma.$connect();
|
|
const service = new AuthService(prisma, new JwtService({ secret: 'test-secret' }));
|
|
await assert.rejects(() => service.login('employee', 'wrong-password'), UnauthorizedException);
|
|
const result = await service.login('employee', 'Password@123');
|
|
assert.equal(result.user.email, 'employee@okr.local');
|
|
assert.ok(result.token.length > 20);
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
test('ObjectivesService applies employee role filtering', async () => {
|
|
const prisma = new PrismaService();
|
|
await prisma.$connect();
|
|
const service = new ObjectivesService(prisma);
|
|
const employeeObjectives = await service.list(employeeUser);
|
|
const managerObjectives = await service.list(managerUser);
|
|
assert.deepEqual(employeeObjectives.map((objective) => objective.ownerId), [employeeUser.sub]);
|
|
assert.ok(managerObjectives.length > employeeObjectives.length);
|
|
await prisma.$disconnect();
|
|
});
|
|
|
|
test('KeyResultsService blocks employee updates to another owner and recalculates owned progress', async () => {
|
|
const prisma = new PrismaService();
|
|
await prisma.$connect();
|
|
const service = new KeyResultsService(prisma);
|
|
await assert.rejects(
|
|
() => service.updateProgress(3, { progress: 50, comment: 'Not mine' }, employeeUser),
|
|
ForbiddenException,
|
|
);
|
|
const updated = await service.updateProgress(1, { progress: 100, comment: 'Completed' }, employeeUser);
|
|
assert.equal(updated.progress, 100);
|
|
const objective = await prisma.objective.findUniqueOrThrow({ where: { id: updated.objectiveId } });
|
|
assert.equal(objective.status, 'IN_PROGRESS');
|
|
await prisma.keyResult.update({ where: { id: 1 }, data: { progress: 33 } });
|
|
await prisma.progressUpdate.deleteMany({ where: { keyResultId: 1 } });
|
|
await prisma.$disconnect();
|
|
});
|