Files
FitOS/api/test/integration/gyms.test.ts
2026-09-04 19:06:19 +02:00

362 lines
14 KiB
TypeScript

import { describe, it, expect, beforeAll } from 'vitest';
import { prisma } from '../../src/lib/prisma.js';
import { buildApp, createEquipment, createUser, loginViaApi, rand, PASSWORD } from '../helpers.js';
describe('gyms — selection and management', () => {
let token: string;
let userId: string;
beforeAll(async () => {
// gym creation is admin-only — the main user is an admin
const { user } = await createUser({ isAdmin: true });
userId = user.id;
token = (await loginViaApi(user.email, PASSWORD)).token;
});
it('lists all gyms (for selection)', async () => {
const app = buildApp();
const gym = await prisma.gym.create({ data: { name: `All Gym ${rand()}` } });
const res = await app.get('/api/v1/gyms/all').set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(200);
const found = res.body.gyms.find((g: { id: string }) => g.id === gym.id);
expect(found).toMatchObject({ id: gym.id, name: gym.name, equipment: [] });
});
it('rejects /gyms/all without a token with 401', async () => {
const app = buildApp();
const res = await app.get('/api/v1/gyms/all');
expect(res.status).toBe(401);
});
it('joins an existing gym (MEMBER membership)', async () => {
const app = buildApp();
const gym = await prisma.gym.create({ data: { name: `Join Gym ${rand()}` } });
const res = await app
.post(`/api/v1/gyms/${gym.id}/join`)
.set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(201);
expect(res.body.membership).toEqual({ gymId: gym.id, name: gym.name, roles: ['MEMBER'] });
const membership = await prisma.gymMembership.findUnique({
where: { userId_gymId: { userId, gymId: gym.id } },
});
expect(membership?.roles).toEqual(['MEMBER']);
});
it('rejects joining the same gym twice with 409', async () => {
const app = buildApp();
const gym = await prisma.gym.create({ data: { name: `Join Twice ${rand()}` } });
await app.post(`/api/v1/gyms/${gym.id}/join`).set('Authorization', `Bearer ${token}`);
const res = await app.post(`/api/v1/gyms/${gym.id}/join`).set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(409);
});
it('rejects joining an unknown gym with 404', async () => {
const app = buildApp();
const res = await app
.post('/api/v1/gyms/unknown-gym/join')
.set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(404);
});
it('rejects creating a REAL gym by a non-admin user with 403', async () => {
const app = buildApp();
const plain = await createUser();
const plainToken = (await loginViaApi(plain.user.email, PASSWORD)).token;
const res = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${plainToken}`)
.send({ name: `Forbidden Gym ${rand()}`, location: 'Warsaw', kind: 'REAL' });
expect(res.status).toBe(403);
expect(res.body.error.code).toBe('FORBIDDEN');
});
it('creates a gym and makes the creator its OWNER', async () => {
const app = buildApp();
const res = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `My New Gym ${rand()}`, location: 'Warsaw' });
expect(res.status).toBe(201);
const gym = res.body.gym as { id: string; roles: string[] };
expect(gym.roles).toEqual(['OWNER']);
const membership = await prisma.gymMembership.findUnique({
where: { userId_gymId: { userId, gymId: gym.id } },
});
expect(membership?.roles).toEqual(['OWNER']);
});
it('rejects creating a gym without a name with 400', async () => {
const app = buildApp();
const res = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ location: 'Warsaw' });
expect(res.status).toBe(400);
});
it('lets the owner add equipment with a quantity', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Equip Gym ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const barbell = await createEquipment('Sztanga');
const res = await app
.post(`/api/v1/gyms/${gymId}/equipment`)
.set('Authorization', `Bearer ${token}`)
.send({ equipmentId: barbell.id, quantity: 2 });
expect(res.status).toBe(200);
expect(res.body.gym.equipment).toEqual([
expect.objectContaining({
equipmentId: barbell.id,
quantity: 2,
equipment: expect.objectContaining({ id: barbell.id, name: 'Sztanga' }),
}),
]);
});
it('lets the owner change the equipment quantity', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Qty Gym ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const dumbbells = await createEquipment('Hantle');
await app
.post(`/api/v1/gyms/${gymId}/equipment`)
.set('Authorization', `Bearer ${token}`)
.send({ equipmentId: dumbbells.id, quantity: 3 });
const res = await app
.patch(`/api/v1/gyms/${gymId}/equipment/${dumbbells.id}`)
.set('Authorization', `Bearer ${token}`)
.send({ quantity: 5 });
expect(res.status).toBe(200);
expect(res.body.gym.equipment).toEqual([
expect.objectContaining({ equipmentId: dumbbells.id, quantity: 5 }),
]);
});
it('rejects an invalid quantity with 400', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Bad Qty Gym ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const res = await app
.post(`/api/v1/gyms/${gymId}/equipment`)
.set('Authorization', `Bearer ${token}`)
.send({ equipmentId: 'eq-1', quantity: 0 });
expect(res.status).toBe(400);
});
it('rejects duplicate equipment with 409', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Dup Gym ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const barbell = await createEquipment('Sztanga');
await app
.post(`/api/v1/gyms/${gymId}/equipment`)
.set('Authorization', `Bearer ${token}`)
.send({ equipmentId: barbell.id });
const res = await app
.post(`/api/v1/gyms/${gymId}/equipment`)
.set('Authorization', `Bearer ${token}`)
.send({ equipmentId: barbell.id });
expect(res.status).toBe(409);
});
it('rejects adding unknown equipment with 404', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Unknown Equip ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const res = await app
.post(`/api/v1/gyms/${gymId}/equipment`)
.set('Authorization', `Bearer ${token}`)
.send({ equipmentId: 'nonexistent-equipment' });
expect(res.status).toBe(404);
});
it('rejects equipment management by a non-owner with 403', async () => {
const app = buildApp();
const owner = await createUser({ isAdmin: true });
const ownerToken = (await loginViaApi(owner.user.email, PASSWORD)).token;
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${ownerToken}`)
.send({ name: `Protected Gym ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const treadmill = await createEquipment('Bieżnia');
const res = await app
.post(`/api/v1/gyms/${gymId}/equipment`)
.set('Authorization', `Bearer ${token}`)
.send({ equipmentId: treadmill.id });
expect(res.status).toBe(403);
});
it('lets the owner remove equipment', async () => {
const app = buildApp();
// REAL kind: /gyms/all lists only real gyms
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Remove Gym ${rand()}`, location: 'Warsaw', kind: 'REAL' });
const gymId = created.body.gym.id as string;
const kettlebell = await createEquipment('Kettlebell');
await app
.post(`/api/v1/gyms/${gymId}/equipment`)
.set('Authorization', `Bearer ${token}`)
.send({ equipmentId: kettlebell.id });
const res = await app
.delete(`/api/v1/gyms/${gymId}/equipment/${kettlebell.id}`)
.set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(204);
const after = await app.get(`/api/v1/gyms/all`).set('Authorization', `Bearer ${token}`);
const gym = after.body.gyms.find((g: { id: string }) => g.id === gymId);
expect(gym.equipment).toEqual([]);
});
it('rejects removing equipment not in the gym with 400', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Missing Equip ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const kettlebell = await createEquipment('Kettlebell');
const res = await app
.delete(`/api/v1/gyms/${gymId}/equipment/${kettlebell.id}`)
.set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(400);
});
it('includes the user gym (with role) in /me memberships', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Me Gym ${rand()}`, location: 'Warsaw' });
const res = await app.get('/api/v1/me').set('Authorization', `Bearer ${token}`);
const membership = res.body.memberships.find(
(m: { gymId: string }) => m.gymId === created.body.gym.id,
);
expect(membership).toMatchObject({ gymId: created.body.gym.id, roles: ['OWNER'] });
expect(membership.gym.equipment).toEqual([]);
});
it('lets the owner update a gym', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Update Gym ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const res = await app
.patch(`/api/v1/gyms/${gymId}`)
.set('Authorization', `Bearer ${token}`)
.send({ name: 'Renamed Gym', location: 'Kraków', capacity: 40 });
expect(res.status).toBe(200);
expect(res.body.gym).toMatchObject({ name: 'Renamed Gym', location: 'Kraków', capacity: 40 });
});
it('rejects updating a gym by a non-owner with 403', async () => {
const app = buildApp();
const owner = await createUser({ isAdmin: true });
const ownerToken = (await loginViaApi(owner.user.email, PASSWORD)).token;
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${ownerToken}`)
.send({ name: `Protected Update ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const res = await app
.patch(`/api/v1/gyms/${gymId}`)
.set('Authorization', `Bearer ${token}`)
.send({ name: 'Hacked' });
expect(res.status).toBe(403);
});
it('lets the owner delete a gym', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Delete Gym ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const res = await app
.delete(`/api/v1/gyms/${gymId}`)
.set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(204);
const gone = await prisma.gym.findUnique({ where: { id: gymId } });
expect(gone).toBeNull();
});
it('rejects deleting a gym by a non-owner with 403', async () => {
const app = buildApp();
const owner = await createUser({ isAdmin: true });
const ownerToken = (await loginViaApi(owner.user.email, PASSWORD)).token;
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${ownerToken}`)
.send({ name: `Protected Delete ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const res = await app
.delete(`/api/v1/gyms/${gymId}`)
.set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(403);
});
it('lets the owner manage schedule exceptions', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${token}`)
.send({ name: `Exception Gym ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const addRes = await app
.post(`/api/v1/gyms/${gymId}/exceptions`)
.set('Authorization', `Bearer ${token}`)
.send({ date: '2026-09-01', availability: null });
expect(addRes.status).toBe(201);
const exceptionId = addRes.body.exception.id as string;
const listRes = await app
.get(`/api/v1/gyms/${gymId}/exceptions`)
.set('Authorization', `Bearer ${token}`);
expect(listRes.status).toBe(200);
expect(listRes.body.exceptions).toEqual([
expect.objectContaining({ id: exceptionId, date: expect.anything() }),
]);
const delRes = await app
.delete(`/api/v1/gyms/${gymId}/exceptions/${exceptionId}`)
.set('Authorization', `Bearer ${token}`);
expect(delRes.status).toBe(204);
});
it('rejects schedule exceptions by a non-owner with 403', async () => {
const app = buildApp();
const owner = await createUser({ isAdmin: true });
const ownerToken = (await loginViaApi(owner.user.email, PASSWORD)).token;
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${ownerToken}`)
.send({ name: `Protected Exceptions ${rand()}`, location: 'Warsaw' });
const gymId = created.body.gym.id as string;
const res = await app
.post(`/api/v1/gyms/${gymId}/exceptions`)
.set('Authorization', `Bearer ${token}`)
.send({ date: '2026-09-01', availability: null });
expect(res.status).toBe(403);
});
});