Files
FitOS/api/test/integration/exercises-gyms.test.ts

78 lines
3.1 KiB
TypeScript

import { describe, it, expect, beforeAll } from 'vitest';
import { prisma } from '../../src/lib/prisma.js';
import { buildApp, createUser, createExercise, loginViaApi, rand, registerViaApi, PASSWORD } from '../helpers.js';
describe('exercises routes', () => {
let token: string;
let exerciseId: string;
let slug: string;
beforeAll(async () => {
token = (await registerViaApi()).token;
slug = `test-bench-${rand()}`;
const exercise = await createExercise({ slug, name: 'Test Bench' });
exerciseId = exercise.id;
});
it('lists the exercise pool (including created exercise)', async () => {
const app = buildApp();
const res = await app.get('/api/v1/exercises').set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(200);
const exercise = res.body.exercises.find((p: { id: string }) => p.id === exerciseId);
expect(exercise).toMatchObject({
slug,
name: 'Test Bench',
primaryMuscle: 'chest',
secondaryMuscles: ['triceps'],
equipment: [],
source: 'BUILTIN',
});
});
it('returns exercise details', async () => {
const app = buildApp();
const res = await app.get(`/api/v1/exercises/${exerciseId}`).set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(200);
expect(res.body.exercise.id).toBe(exerciseId);
expect(res.body.exercise.description).toBe('Test exercise');
});
it('returns 404 for an unknown exercise', async () => {
const app = buildApp();
const res = await app.get('/api/v1/exercises/unknown').set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(404);
});
it('rejects /exercises without a token with 401', async () => {
const app = buildApp();
const res = await app.get('/api/v1/exercises');
expect(res.status).toBe(401);
});
});
describe('gyms routes', () => {
it('lists only the current user gym memberships with roles', async () => {
const app = buildApp();
const { user } = await createUser();
const { user: otherUser } = await createUser();
const gym = await prisma.gym.create({ data: { name: `My Gym ${Math.random().toString(36).slice(2)}` } });
const otherGym = await prisma.gym.create({ data: { name: `Other Gym ${Math.random().toString(36).slice(2)}` } });
await prisma.gymMembership.create({ data: { userId: user.id, gymId: gym.id, roles: ['MEMBER'] } });
await prisma.gymMembership.create({ data: { userId: otherUser.id, gymId: otherGym.id, roles: ['OWNER'] } });
const token = (await loginViaApi(user.email, PASSWORD)).token;
const res = await app.get('/api/v1/gyms').set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(200);
expect(res.body.gyms).toHaveLength(1);
expect(res.body.gyms[0]).toMatchObject({ id: gym.id, name: gym.name, roles: ['MEMBER'] });
});
it('returns an empty list for a user without memberships', async () => {
const app = buildApp();
const token = (await registerViaApi()).token;
const res = await app.get('/api/v1/gyms').set('Authorization', `Bearer ${token}`);
expect(res.status).toBe(200);
expect(res.body.gyms).toEqual([]);
});
});