824 lines
30 KiB
TypeScript
824 lines
30 KiB
TypeScript
import { describe, it, expect, beforeAll } from 'vitest';
|
|
import { prisma } from '../../src/lib/prisma.js';
|
|
import { buildApp, createUser, loginViaApi, registerViaApi, PASSWORD } from '../helpers.js';
|
|
|
|
describe('admin routes', () => {
|
|
let adminToken: string;
|
|
let adminEmail: string;
|
|
let memberToken: string;
|
|
let gymId: string;
|
|
let targetEmail: string;
|
|
let targetId: string;
|
|
// Osobne konta na właścicieli tworzonych siłowni — bez zatruwania członkostw `target`.
|
|
let ownerId: string;
|
|
let ownerId2: string;
|
|
|
|
beforeAll(async () => {
|
|
const { user: admin } = await createUser({ isAdmin: true });
|
|
adminEmail = admin.email;
|
|
const { user: target } = await createUser();
|
|
targetEmail = target.email;
|
|
targetId = target.id;
|
|
ownerId = (await createUser()).user.id;
|
|
ownerId2 = (await createUser()).user.id;
|
|
adminToken = (await loginViaApi(admin.email, PASSWORD)).token;
|
|
memberToken = (await registerViaApi()).token;
|
|
const gym = await prisma.gym.create({
|
|
data: { name: `Gym ${Math.random().toString(36).slice(2)}`, location: 'Warszawa' },
|
|
});
|
|
gymId = gym.id;
|
|
});
|
|
|
|
it('rejects a non-admin user with 403', async () => {
|
|
const app = buildApp();
|
|
for (const path of ['/api/v1/admin/users', '/api/v1/admin/gyms']) {
|
|
const res = await app.get(path).set('Authorization', `Bearer ${memberToken}`);
|
|
expect(res.status).toBe(403);
|
|
expect(res.body.error.code).toBe('FORBIDDEN');
|
|
}
|
|
});
|
|
|
|
it('rejects missing token with 401', async () => {
|
|
const app = buildApp();
|
|
const res = await app.get('/api/v1/admin/users');
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it('lists gyms with membership counts', async () => {
|
|
const app = buildApp();
|
|
const res = await app.get('/api/v1/admin/gyms').set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(200);
|
|
const gym = res.body.gyms.find((g: { id: string }) => g.id === gymId);
|
|
expect(gym).toBeDefined();
|
|
expect(gym._count.memberships).toBe(0);
|
|
});
|
|
|
|
it('creates a gym and makes the designated owner OWNER', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
name: 'Nowa siłownia',
|
|
addressLine1: 'Mokotowska 12',
|
|
postalCode: '00-900',
|
|
city: 'Warszawa',
|
|
capacity: 50,
|
|
ownerId,
|
|
});
|
|
expect(res.status).toBe(201);
|
|
const newGymId = res.body.gym.id as string;
|
|
expect(res.body.gym).toMatchObject({
|
|
name: 'Nowa siłownia',
|
|
// display string derivowany z adresu strukturalnego
|
|
location: 'Mokotowska 12, 00-900 Warszawa',
|
|
countryCode: 'PL',
|
|
capacity: 50,
|
|
});
|
|
const membership = await prisma.gymMembership.findUnique({
|
|
where: { userId_gymId: { userId: ownerId, gymId: newGymId } },
|
|
});
|
|
expect(membership?.roles).toEqual(['OWNER']);
|
|
const detail = await app
|
|
.get(`/api/v1/admin/gyms/${newGymId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(detail.body.owners).toEqual([expect.objectContaining({ id: ownerId })]);
|
|
});
|
|
|
|
it('rejects a gym without a name with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: '' });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects creating a gym without a designated owner with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
name: 'Bez właściciela',
|
|
addressLine1: 'Polna 1',
|
|
postalCode: '00-001',
|
|
city: 'Warszawa',
|
|
});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects creating a REAL gym without a structured address with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: 'Bez adresu', ownerId });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects an invalid postal code with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
name: 'Zły kod',
|
|
addressLine1: 'Polna 1',
|
|
postalCode: '00001',
|
|
city: 'Warszawa',
|
|
ownerId,
|
|
});
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects creating a gym with an unknown owner with 404', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
name: 'Nieistniejący właściciel',
|
|
addressLine1: 'Polna 1',
|
|
postalCode: '00-001',
|
|
city: 'Warszawa',
|
|
ownerId: 'no-such-user',
|
|
});
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it('grants and revokes the OWNER role for a user in a gym', async () => {
|
|
const app = buildApp();
|
|
const add = await app
|
|
.post(`/api/v1/admin/gyms/${gymId}/owners`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ userId: targetId });
|
|
expect(add.status).toBe(200);
|
|
expect(add.body.membership.roles).toEqual(['OWNER']);
|
|
|
|
// ponowne nadanie jest idempotentne (bez duplikatu roli)
|
|
const again = await app
|
|
.post(`/api/v1/admin/gyms/${gymId}/owners`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ userId: targetId });
|
|
expect(again.status).toBe(200);
|
|
expect(again.body.membership.roles).toEqual(['OWNER']);
|
|
|
|
const detail = await app
|
|
.get(`/api/v1/admin/gyms/${gymId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(detail.body.owners.map((o: { id: string }) => o.id)).toContain(targetId);
|
|
|
|
const del = await app
|
|
.delete(`/api/v1/admin/gyms/${gymId}/owners/${targetId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(del.status).toBe(204);
|
|
const membership = await prisma.gymMembership.findUnique({
|
|
where: { userId_gymId: { userId: targetId, gymId } },
|
|
});
|
|
expect(membership).toBeNull();
|
|
|
|
const delAgain = await app
|
|
.delete(`/api/v1/admin/gyms/${gymId}/owners/${targetId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(delAgain.status).toBe(404);
|
|
});
|
|
|
|
it('updates a gym and recomputes location from partial address changes', async () => {
|
|
const app = buildApp();
|
|
const created = await app
|
|
.post('/api/v1/admin/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
name: 'Do edycji',
|
|
addressLine1: 'Długa 1',
|
|
postalCode: '80-001',
|
|
city: 'Gdańsk',
|
|
ownerId: ownerId2,
|
|
});
|
|
const id = created.body.gym.id;
|
|
const res = await app
|
|
.patch(`/api/v1/admin/gyms/${id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ postalCode: '80-100', capacity: null });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.gym).toMatchObject({
|
|
// scalenie z resztą adresu w bazie + przeliczenie display stringa
|
|
location: 'Długa 1, 80-100 Gdańsk',
|
|
capacity: null,
|
|
});
|
|
});
|
|
|
|
it('returns 404 when updating an unknown gym', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.patch('/api/v1/admin/gyms/unknown-gym')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: 'X' });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it('deletes a gym', async () => {
|
|
const app = buildApp();
|
|
const created = await app
|
|
.post('/api/v1/admin/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
name: 'Do usunięcia',
|
|
addressLine1: 'Krótka 3',
|
|
postalCode: '02-001',
|
|
city: 'Warszawa',
|
|
ownerId: ownerId2,
|
|
});
|
|
const id = created.body.gym.id;
|
|
const res = await app.delete(`/api/v1/admin/gyms/${id}`).set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(204);
|
|
const again = await app.delete(`/api/v1/admin/gyms/${id}`).set('Authorization', `Bearer ${adminToken}`);
|
|
expect(again.status).toBe(404);
|
|
});
|
|
|
|
it('creates a gym schedule exception (closed day)', async () => {
|
|
const app = buildApp();
|
|
const date = '2030-01-01';
|
|
const res = await app
|
|
.post(`/api/v1/admin/gyms/${gymId}/exceptions`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ date, availability: null });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.exception).toMatchObject({ gymId, availability: null });
|
|
});
|
|
|
|
it('lists a gym schedule exceptions', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.get(`/api/v1/admin/gyms/${gymId}/exceptions`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.exceptions.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('rejects a duplicate exception for the same date with 409', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post(`/api/v1/admin/gyms/${gymId}/exceptions`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ date: '2030-01-01', availability: { start: '08:00', end: '20:00' } });
|
|
expect(res.status).toBe(409);
|
|
});
|
|
|
|
it('deletes a gym schedule exception', async () => {
|
|
const app = buildApp();
|
|
const list = await app
|
|
.get(`/api/v1/admin/gyms/${gymId}/exceptions`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
const ex = list.body.exceptions[0];
|
|
const res = await app
|
|
.delete(`/api/v1/admin/gyms/${gymId}/exceptions/${ex.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(204);
|
|
});
|
|
|
|
it('lists users and filters by search', async () => {
|
|
const app = buildApp();
|
|
const all = await app.get('/api/v1/admin/users').set('Authorization', `Bearer ${adminToken}`);
|
|
expect(all.status).toBe(200);
|
|
expect(all.body.users.some((u: { email: string }) => u.email === targetEmail)).toBe(true);
|
|
|
|
const searched = await app
|
|
.get(`/api/v1/admin/users?search=${encodeURIComponent(targetEmail)}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(searched.status).toBe(200);
|
|
expect(searched.body.users).toHaveLength(1);
|
|
expect(searched.body.users[0].email).toBe(targetEmail);
|
|
});
|
|
|
|
it('creates a user', async () => {
|
|
const app = buildApp();
|
|
const email = `created-${Math.random().toString(36).slice(2)}@test.local`;
|
|
const res = await app
|
|
.post('/api/v1/admin/users')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ email, password: 'Password123!', firstName: 'Nowy', age: 28 });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.user).toMatchObject({ email, firstName: 'Nowy', age: 28, isAdmin: false });
|
|
expect(res.body.user).not.toHaveProperty('passwordHash');
|
|
});
|
|
|
|
it('rejects a duplicate user email with 409', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/users')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ email: targetEmail, password: 'Password123!' });
|
|
expect(res.status).toBe(409);
|
|
});
|
|
|
|
it('rejects an invalid user payload with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/users')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ email: 'not-an-email', password: 'x' });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('promotes a user to admin and revokes their old token', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
const targetToken = (await loginViaApi(targetEmail, PASSWORD)).token;
|
|
|
|
const res = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ isAdmin: true });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.user.isAdmin).toBe(true);
|
|
|
|
const oldToken = await app.get('/api/v1/me').set('Authorization', `Bearer ${targetToken}`);
|
|
expect(oldToken.status).toBe(401);
|
|
});
|
|
|
|
it('updates user profile fields', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
const res = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
firstName: 'Zmieniony',
|
|
heightCm: 175,
|
|
age: 30,
|
|
availability: [{ start: '18:00', end: '20:00' }, null, null, null, null, null, null],
|
|
priorities: 'Siła, hipertrofia',
|
|
exclusions: 'Kontuzja kolana',
|
|
});
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.user).toMatchObject({
|
|
firstName: 'Zmieniony',
|
|
heightCm: 175,
|
|
age: 30,
|
|
availability: [{ start: '18:00', end: '20:00' }, null, null, null, null, null, null],
|
|
priorities: 'Siła, hipertrofia',
|
|
exclusions: 'Kontuzja kolana',
|
|
});
|
|
});
|
|
|
|
it('clears training profile fields with null', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
const res = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ availability: null, priorities: null, exclusions: null });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.user).toMatchObject({ availability: null, priorities: null, exclusions: null });
|
|
});
|
|
|
|
it('returns 404 when updating an unknown user', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.patch('/api/v1/admin/users/unknown-user')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ firstName: 'X' });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it('updates a user email', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
const newEmail = `renamed-${Math.random().toString(36).slice(2)}@test.local`;
|
|
const res = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ email: newEmail });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.user.email).toBe(newEmail);
|
|
targetEmail = newEmail;
|
|
});
|
|
|
|
it('rejects changing email to a taken one with 409', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
const res = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ email: adminEmail });
|
|
expect(res.status).toBe(409);
|
|
expect(res.body.error.code).toBe('CONFLICT');
|
|
});
|
|
|
|
it('rejects an invalid email with 400', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
const res = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ email: 'not-an-email' });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('assigns and removes a gym membership', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
|
|
const add = await app
|
|
.post(`/api/v1/admin/users/${target!.id}/memberships`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ gymId, roles: ['TRAINER', 'MEMBER'] });
|
|
expect(add.status).toBe(201);
|
|
expect(add.body.membership).toMatchObject({ gymId, roles: ['TRAINER', 'MEMBER'] });
|
|
|
|
const dup = await app
|
|
.post(`/api/v1/admin/users/${target!.id}/memberships`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ gymId, roles: ['MEMBER'] });
|
|
expect(dup.status).toBe(409);
|
|
|
|
const badRole = await app
|
|
.post(`/api/v1/admin/users/${target!.id}/memberships`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ gymId, roles: ['SUPERUSER'] });
|
|
expect(badRole.status).toBe(400);
|
|
|
|
const emptyRoles = await app
|
|
.post(`/api/v1/admin/users/${target!.id}/memberships`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ gymId, roles: [] });
|
|
expect(emptyRoles.status).toBe(400);
|
|
|
|
const unknownGym = await app
|
|
.post(`/api/v1/admin/users/${target!.id}/memberships`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ gymId: 'no-such-gym', roles: ['MEMBER'] });
|
|
expect(unknownGym.status).toBe(404);
|
|
|
|
const freshToken = (await loginViaApi(targetEmail, PASSWORD)).token;
|
|
const me = await app.get('/api/v1/me').set('Authorization', `Bearer ${freshToken}`);
|
|
expect(me.body.memberships).toHaveLength(1);
|
|
expect(me.body.memberships[0]).toMatchObject({ gymId, roles: ['TRAINER', 'MEMBER'] });
|
|
|
|
const remove = await app
|
|
.delete(`/api/v1/admin/users/${target!.id}/memberships/${gymId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(remove.status).toBe(204);
|
|
|
|
const again = await app
|
|
.delete(`/api/v1/admin/users/${target!.id}/memberships/${gymId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(again.status).toBe(400);
|
|
});
|
|
|
|
it('changes a membership roles', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
|
|
const add = await app
|
|
.post(`/api/v1/admin/users/${target!.id}/memberships`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ gymId, roles: ['MEMBER'] });
|
|
expect(add.status).toBe(201);
|
|
|
|
const update = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}/memberships/${gymId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ roles: ['OWNER', 'TRAINER'] });
|
|
expect(update.status).toBe(200);
|
|
expect(update.body.membership).toMatchObject({ gymId, roles: ['OWNER', 'TRAINER'] });
|
|
|
|
const badRole = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}/memberships/${gymId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ roles: ['SUPERUSER'] });
|
|
expect(badRole.status).toBe(400);
|
|
|
|
const emptyRoles = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}/memberships/${gymId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ roles: [] });
|
|
expect(emptyRoles.status).toBe(400);
|
|
|
|
const unknownGym = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}/memberships/no-such-gym`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ roles: ['MEMBER'] });
|
|
expect(unknownGym.status).toBe(404);
|
|
|
|
const cleanup = await app
|
|
.delete(`/api/v1/admin/users/${target!.id}/memberships/${gymId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(cleanup.status).toBe(204);
|
|
});
|
|
|
|
it('deletes a user', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const res = await app.delete(`/api/v1/admin/users/${user.id}`).set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(204);
|
|
const again = await app.delete(`/api/v1/admin/users/${user.id}`).set('Authorization', `Bearer ${adminToken}`);
|
|
expect(again.status).toBe(404);
|
|
});
|
|
|
|
it('creates a focus area', async () => {
|
|
const app = buildApp();
|
|
const suffix = Math.random().toString(36).slice(2);
|
|
const muscle = `muscle-${suffix}`;
|
|
const name = `Puzel ${suffix}`;
|
|
const res = await app
|
|
.post('/api/v1/admin/focus-areas')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name, muscle, description: 'opis' });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.focusArea).toMatchObject({ name, muscle, description: 'opis' });
|
|
});
|
|
|
|
it('rejects a duplicate focus area muscle with 409', async () => {
|
|
const app = buildApp();
|
|
const suffix = Math.random().toString(36).slice(2);
|
|
const muscle = `dup-muscle-${suffix}`;
|
|
const first = await app
|
|
.post('/api/v1/admin/focus-areas')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Dup ${suffix}`, muscle });
|
|
expect(first.status).toBe(201);
|
|
const res = await app
|
|
.post('/api/v1/admin/focus-areas')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: 'Klatka piersiowa', muscle });
|
|
expect(res.status).toBe(409);
|
|
});
|
|
|
|
it('updates and deletes a focus area', async () => {
|
|
const app = buildApp();
|
|
const suffix = Math.random().toString(36).slice(2);
|
|
const muscle = `muscle-${suffix}`;
|
|
const created = await app
|
|
.post('/api/v1/admin/focus-areas')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Do edycji ${suffix}`, muscle });
|
|
const id = created.body.focusArea.id;
|
|
|
|
const updated = await app
|
|
.patch(`/api/v1/admin/focus-areas/${id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: 'Zmieniony', description: null });
|
|
expect(updated.status).toBe(200);
|
|
expect(updated.body.focusArea).toMatchObject({ name: 'Zmieniony', description: null });
|
|
|
|
const deleted = await app
|
|
.delete(`/api/v1/admin/focus-areas/${id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(deleted.status).toBe(204);
|
|
const again = await app
|
|
.delete(`/api/v1/admin/focus-areas/${id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(again.status).toBe(404);
|
|
});
|
|
|
|
it('rejects a focus area with setsMin > setsMax with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/admin/focus-areas')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: 'Zly zakres', muscle: `bad-range-${Math.random().toString(36).slice(2)}`, setsMin: 20, setsMax: 10 });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a focus-area patch that would invert the sets range with 400', async () => {
|
|
const app = buildApp();
|
|
const muscle = `range-${Math.random().toString(36).slice(2)}`;
|
|
const created = await app
|
|
.post('/api/v1/admin/focus-areas')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Zakres ${muscle}`, muscle, setsMin: 10, setsMax: 20 });
|
|
const id = created.body.focusArea.id;
|
|
|
|
const raisedMin = await app
|
|
.patch(`/api/v1/admin/focus-areas/${id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ setsMin: 30 });
|
|
expect(raisedMin.status).toBe(400);
|
|
|
|
const loweredMax = await app
|
|
.patch(`/api/v1/admin/focus-areas/${id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ setsMax: 5 });
|
|
expect(loweredMax.status).toBe(400);
|
|
});
|
|
|
|
it('lists focus areas with exercisesCount', async () => {
|
|
const app = buildApp();
|
|
const muscle = `cnt-${Math.random().toString(36).slice(2)}`;
|
|
const created = await app
|
|
.post('/api/v1/admin/focus-areas')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Licznik ${muscle}`, muscle });
|
|
const areaId = created.body.focusArea.id;
|
|
expect(created.body.focusArea).not.toHaveProperty('exercisesCount');
|
|
|
|
const exercise = await app
|
|
.post('/api/v1/admin/exercises')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Licznik CW ${muscle}`, primaryMuscle: muscle });
|
|
expect(exercise.status).toBe(201);
|
|
|
|
const res = await app.get('/api/v1/admin/focus-areas').set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(200);
|
|
const area = res.body.focusAreas.find((a: { id: string }) => a.id === areaId);
|
|
expect(area.exercisesCount).toBe(1);
|
|
|
|
await app
|
|
.delete(`/api/v1/admin/exercises/${exercise.body.exercise.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
});
|
|
|
|
it('creates and updates an exercise with custom has* flags', async () => {
|
|
const app = buildApp();
|
|
const suffix = Math.random().toString(36).slice(2);
|
|
const res = await app
|
|
.post('/api/v1/admin/exercises')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
name: `Cardio ${suffix}`,
|
|
primaryMuscle: 'quads',
|
|
hasReps: false,
|
|
hasLoad: false,
|
|
hasRir: false,
|
|
hasCardio: true,
|
|
hasTime: true,
|
|
description: 'Bieganie.',
|
|
});
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.exercise).toMatchObject({
|
|
hasReps: false,
|
|
hasLoad: false,
|
|
hasRir: false,
|
|
hasCardio: true,
|
|
hasTime: true,
|
|
hasSpeed: false,
|
|
description: 'Bieganie.',
|
|
planExercisesCount: 0,
|
|
});
|
|
|
|
const id = res.body.exercise.id;
|
|
const patched = await app
|
|
.patch(`/api/v1/admin/exercises/${id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ hasSpeed: true });
|
|
expect(patched.status).toBe(200);
|
|
expect(patched.body.exercise).toMatchObject({ hasSpeed: true, hasCardio: true, hasTime: true });
|
|
|
|
const listed = await app.get('/api/v1/admin/exercises').set('Authorization', `Bearer ${adminToken}`);
|
|
const item = listed.body.exercises.find((e: { id: string }) => e.id === id);
|
|
expect(item).toMatchObject({ hasSpeed: true, planExercisesCount: 0 });
|
|
|
|
const del = await app
|
|
.delete(`/api/v1/admin/exercises/${id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(del.status).toBe(204);
|
|
});
|
|
|
|
it('sets the 4 muscle groups on a user', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
const res = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
developMuscles: ['chest', 'legs'],
|
|
canDoMuscles: ['chest'],
|
|
difficultMuscles: ['back'],
|
|
avoidMuscles: ['shoulders'],
|
|
});
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.user).toMatchObject({
|
|
developMuscles: ['chest', 'legs'],
|
|
canDoMuscles: ['chest'],
|
|
difficultMuscles: ['back'],
|
|
avoidMuscles: ['shoulders'],
|
|
});
|
|
});
|
|
|
|
it('resolves develop/avoid and canDo/difficult conflicts', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
// chest is both developed and avoided; back is both can-do and difficult
|
|
const res = await app
|
|
.patch(`/api/v1/admin/users/${target!.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({
|
|
developMuscles: ['chest', 'legs'],
|
|
avoidMuscles: ['chest', 'shoulders'],
|
|
canDoMuscles: ['back'],
|
|
difficultMuscles: ['back'],
|
|
});
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.user).toMatchObject({
|
|
developMuscles: ['legs'],
|
|
avoidMuscles: ['chest', 'shoulders'],
|
|
canDoMuscles: [],
|
|
difficultMuscles: ['back'],
|
|
});
|
|
});
|
|
|
|
it('returns the focus chain for a user', async () => {
|
|
const app = buildApp();
|
|
const target = await prisma.user.findUnique({ where: { email: targetEmail } });
|
|
const res = await app
|
|
.get(`/api/v1/admin/users/${target!.id}/focus-chain`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toHaveProperty('develop');
|
|
expect(res.body).toHaveProperty('canDo');
|
|
expect(res.body).toHaveProperty('difficult');
|
|
expect(res.body).toHaveProperty('avoid');
|
|
});
|
|
});
|
|
|
|
describe('admin: kary na koncie (status)', () => {
|
|
let adminToken: string;
|
|
const futureIso = () => new Date(Date.now() + 24 * 3600 * 1000).toISOString();
|
|
const pastIso = () => new Date(Date.now() - 24 * 3600 * 1000).toISOString();
|
|
|
|
beforeAll(async () => {
|
|
const { user } = await createUser({ isAdmin: true });
|
|
adminToken = (await loginViaApi(user.email, PASSWORD)).token;
|
|
});
|
|
|
|
it('ban bez powodu → 400', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const res = await app
|
|
.patch(`/api/v1/admin/users/${user.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ status: 'BANNED' });
|
|
expect(res.status).toBe(400);
|
|
expect(res.body.error.code).toBe('BAD_REQUEST');
|
|
});
|
|
|
|
it('zawieszenie bez terminu lub z terminem w przeszłości → 400', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const noUntil = await app
|
|
.patch(`/api/v1/admin/users/${user.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ status: 'SUSPENDED' });
|
|
expect(noUntil.status).toBe(400);
|
|
const past = await app
|
|
.patch(`/api/v1/admin/users/${user.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ status: 'SUSPENDED', suspensionUntil: pastIso() });
|
|
expect(past.status).toBe(400);
|
|
});
|
|
|
|
it('nadanie bana: zapis statusu+powodu, unieważnienie starych tokenów', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const targetToken = (await loginViaApi(user.email, PASSWORD)).token;
|
|
|
|
const res = await app
|
|
.patch(`/api/v1/admin/users/${user.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ status: 'BANNED', restrictionReason: 'Udostępnianie kont osobom trzecim' });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.user).toMatchObject({
|
|
status: 'BANNED',
|
|
restrictionReason: 'Udostępnianie kont osobom trzecim',
|
|
});
|
|
|
|
const oldToken = await app.get('/api/v1/me').set('Authorization', `Bearer ${targetToken}`);
|
|
expect(oldToken.status).toBe(401);
|
|
});
|
|
|
|
it('cofnięcie kary (ACTIVE): czyści until/reason i podbija tokenVersion', async () => {
|
|
const app = buildApp();
|
|
const { user } = await createUser();
|
|
const tokenBefore = (await loginViaApi(user.email, PASSWORD)).token;
|
|
|
|
const ban = await app
|
|
.patch(`/api/v1/admin/users/${user.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ status: 'BANNED', restrictionReason: 'Powód' });
|
|
expect(ban.status).toBe(200);
|
|
|
|
const lift = await app
|
|
.patch(`/api/v1/admin/users/${user.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ status: 'ACTIVE' });
|
|
expect(lift.status).toBe(200);
|
|
expect(lift.body.user).toMatchObject({
|
|
status: 'ACTIVE',
|
|
suspensionUntil: null,
|
|
restrictionReason: null,
|
|
});
|
|
|
|
// token zalogowany przed banem wygasł (ban podbił tokenVersion), a po cofnięciu
|
|
// kary da się zalogować ponownie
|
|
const revoked = await app.get('/api/v1/me').set('Authorization', `Bearer ${tokenBefore}`);
|
|
expect(revoked.status).toBe(401);
|
|
const relogin = await app
|
|
.post('/api/v1/auth/login')
|
|
.send({ email: user.email, password: PASSWORD });
|
|
expect(relogin.status).toBe(200);
|
|
});
|
|
});
|