Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions apps/api-e2e/src/api/project-activities.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,15 @@ describe('Project activity catalog', () => {
const inactiveConflict = await addOrder().expect(409);
expect(inactiveConflict.body.code).toBe('PROJECT_HAS_ACTIVITIES');

const item = await ctx.http.get(projectPath).expect(200);
const item = await ctx.http
.get(projectPath)
.auth(token, { type: 'bearer' })
.expect(200);
expect(item.body.activityCount).toBe(1);
const listed = await ctx.http.get('/api/projects').expect(200);
const listed = await ctx.http
.get('/api/projects')
.auth(token, { type: 'bearer' })
.expect(200);
expect(listed.body).toContainEqual(
expect.objectContaining({ id: project.id, activityCount: 1 }),
);
Expand All @@ -331,8 +337,14 @@ describe('Project activity catalog', () => {
.expect(200);
expect(edited.body.activityCount).toBe(1);

await ctx.http.delete(activityPath).auth(token, { type: 'bearer' }).expect(204);
const empty = await ctx.http.get(projectPath).expect(200);
await ctx.http
.delete(activityPath)
.auth(token, { type: 'bearer' })
.expect(204);
const empty = await ctx.http
.get(projectPath)
.auth(token, { type: 'bearer' })
.expect(200);
expect(empty.body.activityCount).toBe(0);
await addOrder().expect(201);
});
Expand All @@ -357,7 +369,10 @@ describe('Project activity catalog', () => {
} else {
expect(activity.body.code).toBe('PROJECT_HAS_SERVICE_ORDERS');
}
const saved = await ctx.http.get(`/api/projects/${project.id}`).expect(200);
const saved = await ctx.http
.get(`/api/projects/${project.id}`)
.auth(token, { type: 'bearer' })
.expect(200);
expect(saved.body.activityCount).toBe(activity.status === 201 ? 1 : 0);
expect(saved.body.serviceOrders).toHaveLength(order.status === 201 ? 1 : 0);
});
Expand All @@ -382,7 +397,10 @@ describe('Project activity catalog', () => {
},
});

const mixed = await ctx.http.get(projectPath).expect(200);
const mixed = await ctx.http
.get(projectPath)
.auth(token, { type: 'bearer' })
.expect(200);
expect(mixed.body).toMatchObject({
activityCount: 1,
serviceOrders: [expect.objectContaining({ id: legacyOrder.id })],
Expand Down Expand Up @@ -423,7 +441,10 @@ describe('Project activity catalog', () => {
.delete(`${path}/${activity.id}`)
.auth(token, { type: 'bearer' })
.expect(409);
await ctx.http.delete(orderPath).auth(token, { type: 'bearer' }).expect(204);
await ctx.http
.delete(orderPath)
.auth(token, { type: 'bearer' })
.expect(204);
expect(
await ctx.prisma.timeEntry.findUnique({ where: { id: entry.id } }),
).toEqual(entry);
Expand Down
43 changes: 34 additions & 9 deletions apps/api-e2e/src/api/project-booking-range.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,26 @@ describe('POST /timeentries/book-project — retroactive range booking', () => {
});

async function fixture() {
const manager = await seedEmployee(ctx.prisma, {
personalNo: '0010',
firstName: 'Mara',
lastName: 'Manager',
email: 'manager@test.local',
role: 'Manager',
});
const worker = await seedEmployee(ctx.prisma, {
personalNo: '1001',
firstName: 'Willi',
lastName: 'Worker',
email: 'worker@test.local',
managerId: manager.id,
});
const other = await seedEmployee(ctx.prisma, {
personalNo: '1002',
firstName: 'Olga',
lastName: 'Other',
email: 'other@test.local',
});
const manager = await seedEmployee(ctx.prisma, {
personalNo: '0010',
firstName: 'Mara',
lastName: 'Manager',
email: 'manager@test.local',
role: 'Manager',
});
const project = await seedProject(ctx.prisma, {
code: 'P-RANGE',
assigneeIds: [worker.id],
Expand All @@ -51,7 +52,24 @@ describe('POST /timeentries/book-project — retroactive range booking', () => {
serviceOrders: [{ orderNo: 'SA-1', title: 'Design' }],
});
const foreign = await seedProject(ctx.prisma, { code: 'P-FOREIGN' });
return { worker, other, manager, project, ordered, foreign };
return {
worker,
other,
manager,
project,
ordered,
foreign,
};
}

function seedUnrelatedManager() {
return seedEmployee(ctx.prisma, {
personalNo: '0011',
firstName: 'Uma',
lastName: 'Unrelated',
email: 'unrelated-manager@test.local',
role: 'Manager',
});
}

/** Today at the given UTC hour (fractions allowed, e.g. 12.5 = 12:30Z). */
Expand Down Expand Up @@ -294,10 +312,12 @@ describe('POST /timeentries/book-project — retroactive range booking', () => {
).toBe(true);
});

it('foreign ranges need Manager/HRAdmin', async () => {
it('foreign ranges need the direct Manager/HRAdmin', async () => {
const { worker, other, manager, project } = await fixture();
const unrelatedManager = await seedUnrelatedManager();
const otherToken = await login(ctx.http, other.email);
const managerToken = await login(ctx.http, manager.email);
const unrelatedManagerToken = await login(ctx.http, unrelatedManager.email);
await seedEntry(worker.id, 9, 15);

const payload = {
Expand All @@ -315,6 +335,11 @@ describe('POST /timeentries/book-project — retroactive range booking', () => {
.set('Authorization', `Bearer ${otherToken}`)
.send(payload)
.expect(403);
await ctx.http
.post('/api/timeentries/book-project')
.set('Authorization', `Bearer ${unrelatedManagerToken}`)
.send(payload)
.expect(403);
await ctx.http
.post('/api/timeentries/book-project')
.set('Authorization', `Bearer ${managerToken}`)
Expand Down
95 changes: 88 additions & 7 deletions apps/api-e2e/src/api/projects.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,46 @@ describe('Projects — CRUD, service orders, assignment matrix', () => {
firstName: 'Willi',
lastName: 'Worker',
email: 'worker@test.local',
managerId: manager.id,
});
return { hr, manager, worker };
}

it('GET /projects is readable without a token; mutations are role-guarded', async () => {
const { manager, worker } = await fixture();
it('project reads and mutations require authentication and the Manager/HRAdmin role', async () => {
const { hr, manager, worker } = await fixture();
const existing = await seedProject(ctx.prisma, { code: 'P-EXISTING' });

await ctx.http.get('/api/projects').expect(200);
await ctx.http.get('/api/projects').expect(401);
await ctx.http.get(`/api/projects/${existing.id}`).expect(401);
await ctx.http
.post('/api/projects')
.send({ code: 'P-1', name: 'One' })
.expect(401);

const workerToken = await login(ctx.http, worker.email);
await ctx.http
.get('/api/projects')
.set('Authorization', `Bearer ${workerToken}`)
.expect(403);
await ctx.http
.get(`/api/projects/${existing.id}`)
.set('Authorization', `Bearer ${workerToken}`)
.expect(403);
await ctx.http
.post('/api/projects')
.set('Authorization', `Bearer ${workerToken}`)
.send({ code: 'P-1', name: 'One' })
.expect(403);

const managerToken = await login(ctx.http, manager.email);
await ctx.http
.get('/api/projects')
.set('Authorization', `Bearer ${managerToken}`)
.expect(200);
await ctx.http
.get(`/api/projects/${existing.id}`)
.set('Authorization', `Bearer ${managerToken}`)
.expect(200);
const created = await ctx.http
.post('/api/projects')
.set('Authorization', `Bearer ${managerToken}`)
Expand All @@ -69,6 +88,16 @@ describe('Projects — CRUD, service orders, assignment matrix', () => {
expect(created.body.isActive).toBe(true);
expect(created.body.assignedEmployeeCount).toBe(0);
expect(created.body.activityCount).toBe(0);

const hrToken = await login(ctx.http, hr.email);
await ctx.http
.get('/api/projects')
.set('Authorization', `Bearer ${hrToken}`)
.expect(200);
await ctx.http
.get(`/api/projects/${existing.id}`)
.set('Authorization', `Bearer ${hrToken}`)
.expect(200);
});

it('HRAdmin may create; duplicate codes are rejected with 409', async () => {
Expand Down Expand Up @@ -98,10 +127,14 @@ describe('Projects — CRUD, service orders, assignment matrix', () => {
.expect(200);
expect(updated.body.isActive).toBe(false);

const activeOnly = await ctx.http.get('/api/projects').expect(200);
const activeOnly = await ctx.http
.get('/api/projects')
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(activeOnly.body.length).toBe(0);
const all = await ctx.http
.get('/api/projects?includeInactive=true')
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(all.body.length).toBe(1);
});
Expand Down Expand Up @@ -168,7 +201,10 @@ describe('Projects — CRUD, service orders, assignment matrix', () => {
.delete(`/api/projects/${p1.id}/service-orders/${created.body.id}`)
.set('Authorization', `Bearer ${token}`)
.expect(204);
const after = await ctx.http.get(`/api/projects/${p1.id}`).expect(200);
const after = await ctx.http
.get(`/api/projects/${p1.id}`)
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(after.body.serviceOrders.length).toBe(0);
});

Expand Down Expand Up @@ -223,6 +259,7 @@ describe('Projects — CRUD, service orders, assignment matrix', () => {

it('bookable returns only active, assigned projects with their active orders', async () => {
const { worker } = await fixture();
const token = await login(ctx.http, worker.email);
const active = await seedProject(ctx.prisma, {
code: 'P-ACTIVE',
assigneeIds: [worker.id],
Expand All @@ -240,6 +277,7 @@ describe('Projects — CRUD, service orders, assignment matrix', () => {

const res = await ctx.http
.get(`/api/projects/bookable?employeeId=${worker.id}`)
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(res.body).toEqual([
{
Expand All @@ -261,6 +299,45 @@ describe('Projects — CRUD, service orders, assignment matrix', () => {
]);
});

it('scopes bookable projects to self, direct reports, or HRAdmin', async () => {
const { hr, manager, worker } = await fixture();
const unrelatedManager = await seedEmployee(ctx.prisma, {
personalNo: '0011',
firstName: 'Una',
lastName: 'Related',
email: 'unrelated-manager@test.local',
role: 'Manager',
});
const otherEmployee = await seedEmployee(ctx.prisma, {
personalNo: '1002',
firstName: 'Other',
lastName: 'Employee',
email: 'other@test.local',
});
await seedProject(ctx.prisma, {
code: 'P-SCOPED',
assigneeIds: [worker.id],
});

await ctx.http
.get(`/api/projects/bookable?employeeId=${worker.id}`)
.expect(401);
for (const actor of [worker, manager, hr]) {
const token = await login(ctx.http, actor.email);
await ctx.http
.get(`/api/projects/bookable?employeeId=${worker.id}`)
.set('Authorization', `Bearer ${token}`)
.expect(200);
}
for (const actor of [unrelatedManager, otherEmployee]) {
const token = await login(ctx.http, actor.email);
await ctx.http
.get(`/api/projects/bookable?employeeId=${worker.id}`)
.set('Authorization', `Bearer ${token}`)
.expect(403);
}
});

it('service orders with booked time cannot be deleted (409), only deactivated', async () => {
const { hr, worker } = await fixture();
const token = await login(ctx.http, hr.email);
Expand Down Expand Up @@ -341,7 +418,8 @@ describe('Projects — CRUD, service orders, assignment matrix', () => {
});

it('reports gross booked minutes per project and order (closed, non-rejected)', async () => {
const { worker } = await fixture();
const { manager, worker } = await fixture();
const token = await login(ctx.http, manager.email);
const project = await seedProject(ctx.prisma, {
code: 'P-IST',
assigneeIds: [worker.id],
Expand Down Expand Up @@ -378,7 +456,10 @@ describe('Projects — CRUD, service orders, assignment matrix', () => {
},
}); // open → ignored

const res = await ctx.http.get(`/api/projects/${project.id}`).expect(200);
const res = await ctx.http
.get(`/api/projects/${project.id}`)
.set('Authorization', `Bearer ${token}`)
.expect(200);
expect(res.body.bookedMinutes).toBe(240);
expect(res.body.serviceOrders[0].bookedMinutes).toBe(120);
});
Expand Down
24 changes: 22 additions & 2 deletions apps/api-e2e/src/api/solo-customers-reports.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,32 @@ describe('Solo customers, project ownership and customer statements', () => {
).body as { id: string; name: string };
}

it('requires Solo ownership and keeps legacy Team project reads public', async () => {
it('requires authentication in Team mode and preserves Solo owner isolation', async () => {
const owner = await seedEmployee(ctx.prisma, {
personalNo: 'TEAM',
firstName: 'Team',
lastName: 'Admin',
email: 'team-admin@test.local',
role: 'HRAdmin',
});
const outsider = await seedEmployee(ctx.prisma, {
personalNo: 'OUTSIDE',
firstName: 'Other',
lastName: 'Employee',
email: 'other@test.local',
role: 'HRAdmin',
});
const token = await login(ctx.http, owner.email);
await ctx.http.get('/api/projects').expect(200);
const outsiderToken = await login(ctx.http, outsider.email);
await ctx.http.get('/api/projects').expect(401);
await ctx.http
.get('/api/projects')
.set('Authorization', `Bearer ${token}`)
.expect(200);
await ctx.http
.get('/api/projects')
.set('Authorization', `Bearer ${outsiderToken}`)
.expect(200);
await ctx.http
.get('/api/customers')
.set('Authorization', `Bearer ${token}`)
Expand All @@ -75,6 +91,10 @@ describe('Solo customers, project ownership and customer statements', () => {
.get('/api/projects')
.set('Authorization', `Bearer ${token}`)
.expect(200);
await ctx.http
.get('/api/projects')
.set('Authorization', `Bearer ${outsiderToken}`)
.expect(403);
});

it('creates minimal customers and assigns new projects to the owner atomically', async () => {
Expand Down
Loading