feat(backend): Aufgaben-Modul (Modul 6)
TasksModule mit CRUD, Status-/Prioritätsfilter und Erledigen-Endpunkt für polymorph verknüpfte Aufgaben.
This commit is contained in:
1 parent
829753ec90
commit
7bba168bd6
9 files changed
+408
-1
No files matched your search
@@ -0,0 +1,102 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import request from 'supertest';
|
||||
import { App } from 'supertest/types';
|
||||
import { PrismaService } from '../src/prisma/prisma.service';
|
||||
import { createTestApp } from './utils/create-test-app';
|
||||
import { login } from './utils/api';
|
||||
|
||||
interface TaskBody {
|
||||
id: string;
|
||||
status: string;
|
||||
completedAt: string | null;
|
||||
}
|
||||
|
||||
describe('Tasks (e2e)', () => {
|
||||
let app: INestApplication<App>;
|
||||
let prisma: PrismaService;
|
||||
const staffEmail = `tasks-e2e-staff-${Date.now()}@example.com`;
|
||||
const readonlyEmail = `tasks-e2e-readonly-${Date.now()}@example.com`;
|
||||
const password = 'SuperSecret123!';
|
||||
let staffId: string;
|
||||
let readonlyId: string;
|
||||
let staffToken: string;
|
||||
let readonlyToken: string;
|
||||
let taskId: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await createTestApp();
|
||||
prisma = app.get(PrismaService);
|
||||
|
||||
const passwordHash = await bcrypt.hash(password, 12);
|
||||
const staff = await prisma.user.create({
|
||||
data: { email: staffEmail, passwordHash, name: 'Staff', role: 'STAFF' },
|
||||
});
|
||||
staffId = staff.id;
|
||||
const readonly = await prisma.user.create({
|
||||
data: {
|
||||
email: readonlyEmail,
|
||||
passwordHash,
|
||||
name: 'Readonly',
|
||||
role: 'READONLY',
|
||||
},
|
||||
});
|
||||
readonlyId = readonly.id;
|
||||
staffToken = (await login(app, staffEmail, password)).accessToken;
|
||||
readonlyToken = (await login(app, readonlyEmail, password)).accessToken;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
if (taskId) {
|
||||
await prisma.task.delete({ where: { id: taskId } });
|
||||
}
|
||||
await prisma.refreshToken.deleteMany({
|
||||
where: { userId: { in: [staffId, readonlyId] } },
|
||||
});
|
||||
await prisma.auditLog.deleteMany({
|
||||
where: { userId: { in: [staffId, readonlyId] } },
|
||||
});
|
||||
await prisma.user.deleteMany({
|
||||
where: { id: { in: [staffId, readonlyId] } },
|
||||
});
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('rejects create for a READONLY user', () => {
|
||||
return request(app.getHttpServer())
|
||||
.post('/api/v1/tasks')
|
||||
.set('Authorization', `Bearer ${readonlyToken}`)
|
||||
.send({ title: 'Beute prüfen' })
|
||||
.expect(403);
|
||||
});
|
||||
|
||||
it('lets a STAFF user create a task', async () => {
|
||||
const response = await request(app.getHttpServer())
|
||||
.post('/api/v1/tasks')
|
||||
.set('Authorization', `Bearer ${staffToken}`)
|
||||
.send({ title: 'Beute prüfen', priority: 'HIGH' })
|
||||
.expect(201);
|
||||
const task = response.body as TaskBody;
|
||||
taskId = task.id;
|
||||
expect(task.status).toBe('OPEN');
|
||||
});
|
||||
|
||||
it('lists tasks filtered by status', async () => {
|
||||
const response = await request(app.getHttpServer())
|
||||
.get('/api/v1/tasks?status=OPEN')
|
||||
.set('Authorization', `Bearer ${readonlyToken}`)
|
||||
.expect(200);
|
||||
const tasks = response.body as TaskBody[];
|
||||
expect(tasks.some((entry) => entry.id === taskId)).toBe(true);
|
||||
});
|
||||
|
||||
it('completes a task', async () => {
|
||||
const response = await request(app.getHttpServer())
|
||||
.post(`/api/v1/tasks/${taskId}/complete`)
|
||||
.set('Authorization', `Bearer ${staffToken}`)
|
||||
.expect(201);
|
||||
const task = response.body as TaskBody;
|
||||
expect(task.status).toBe('DONE');
|
||||
expect(task.completedAt).not.toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user