2026-10-14 06:12:41 +02:00
|
|
|
import { describe, it, expect, beforeAll } from 'vitest';
|
|
|
|
|
import { prisma } from '../../src/lib/prisma.js';
|
|
|
|
|
import { buildApp, createUserWithToken, rand, PASSWORD } from '../helpers.js';
|
|
|
|
|
|
|
|
|
|
// Endpointy admina per-user (dawne dane użytkownika: ciało, historia, postępy, killstreak, wnioski)
|
|
|
|
|
describe('admin per-user data routes', () => {
|
|
|
|
|
let adminToken: string;
|
|
|
|
|
let memberToken: string;
|
|
|
|
|
let targetUserId: string;
|
|
|
|
|
let otherUserId: string;
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
const admin = await createUserWithToken({ isAdmin: true });
|
|
|
|
|
adminToken = admin.token;
|
|
|
|
|
const member = await createUserWithToken();
|
|
|
|
|
memberToken = member.token;
|
|
|
|
|
const target = await createUserWithToken({ firstName: 'Cel', lastName: 'Testowy' });
|
|
|
|
|
targetUserId = target.user.id;
|
|
|
|
|
const other = await createUserWithToken();
|
|
|
|
|
otherUserId = other.user.id;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const adminGet = (path: string, token = adminToken) =>
|
|
|
|
|
buildApp().get(`/api/v1/admin${path}`).set('Authorization', `Bearer ${token}`);
|
|
|
|
|
|
|
|
|
|
it('odmawia użytkownikowi bez roli admina (403)', async () => {
|
|
|
|
|
for (const path of [
|
|
|
|
|
`/users/${targetUserId}/killstreak`,
|
|
|
|
|
`/users/${targetUserId}/body-parameters`,
|
|
|
|
|
`/users/${targetUserId}/history`,
|
|
|
|
|
`/users/${targetUserId}/exercise-progress`,
|
|
|
|
|
]) {
|
|
|
|
|
const res = await adminGet(path, memberToken);
|
|
|
|
|
expect(res.status).toBe(403);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('zwraca 404 dla nieistniejącego użytkownika', async () => {
|
|
|
|
|
for (const path of [
|
|
|
|
|
'/users/nope/killstreak',
|
|
|
|
|
'/users/nope/body-parameters',
|
|
|
|
|
'/users/nope/history',
|
|
|
|
|
'/users/nope/exercise-progress',
|
|
|
|
|
]) {
|
|
|
|
|
const res = await adminGet(path);
|
|
|
|
|
expect(res.status).toBe(404);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('zwraca killstreak użytkownika', async () => {
|
|
|
|
|
const res = await adminGet(`/users/${targetUserId}/killstreak`);
|
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
expect(res.body).toEqual({ killstreak: 0 });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('dodaje i usuwa pomiar wagi użytkownika', async () => {
|
|
|
|
|
const app = buildApp();
|
|
|
|
|
const create = await app
|
|
|
|
|
.post(`/api/v1/admin/users/${targetUserId}/body-parameters/weight`)
|
|
|
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
|
|
|
.send({ value: 81.5, date: '2026-01-10T12:00:00Z' });
|
|
|
|
|
expect(create.status).toBe(201);
|
|
|
|
|
expect(create.body).toMatchObject({ value: 81.5 });
|
|
|
|
|
|
|
|
|
|
const list = await adminGet(`/users/${targetUserId}/body-parameters`);
|
|
|
|
|
expect(list.status).toBe(200);
|
|
|
|
|
expect(list.body.weight).toHaveLength(1);
|
|
|
|
|
expect(list.body.weight[0]).toMatchObject({ value: 81.5 });
|
|
|
|
|
|
|
|
|
|
const del = await app
|
|
|
|
|
.delete(`/api/v1/admin/users/${targetUserId}/body-parameters/weight/${create.body.id}`)
|
|
|
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
|
|
|
expect(del.status).toBe(204);
|
|
|
|
|
|
|
|
|
|
const after = await adminGet(`/users/${targetUserId}/body-parameters`);
|
|
|
|
|
expect(after.body.weight).toHaveLength(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('odrzuca nieprawidłowy pomiar wagi (400) i usunięcie z cudzego konta (404)', async () => {
|
|
|
|
|
const app = buildApp();
|
|
|
|
|
const bad = await app
|
|
|
|
|
.post(`/api/v1/admin/users/${targetUserId}/body-parameters/weight`)
|
|
|
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
|
|
|
.send({ value: -5 });
|
|
|
|
|
expect(bad.status).toBe(400);
|
|
|
|
|
|
|
|
|
|
const foreign = await prisma.bodyMeasurement.create({
|
|
|
|
|
data: { userId: otherUserId, value: 70 },
|
|
|
|
|
});
|
|
|
|
|
const del = await app
|
|
|
|
|
.delete(`/api/v1/admin/users/${targetUserId}/body-parameters/weight/${foreign.id}`)
|
|
|
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
|
|
|
expect(del.status).toBe(404);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('dodaje i usuwa obwód użytkownika', async () => {
|
|
|
|
|
const app = buildApp();
|
|
|
|
|
const create = await app
|
|
|
|
|
.post(`/api/v1/admin/users/${targetUserId}/body-parameters/circumference`)
|
|
|
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
2026-10-17 11:16:54 +02:00
|
|
|
.send({ typeId: 'biceps', side: 'right', value: 36.2 });
|
2026-10-14 06:12:41 +02:00
|
|
|
expect(create.status).toBe(201);
|
|
|
|
|
|
|
|
|
|
const list = await adminGet(`/users/${targetUserId}/body-parameters`);
|
2026-10-17 11:16:54 +02:00
|
|
|
const series = list.body.circumferences.find(
|
|
|
|
|
(s: { typeId: string; side: string }) => s.typeId === 'biceps' && s.side === 'right',
|
|
|
|
|
);
|
2026-10-14 06:12:41 +02:00
|
|
|
expect(series?.points[0]).toMatchObject({ value: 36.2 });
|
|
|
|
|
|
|
|
|
|
const del = await app
|
2026-10-17 11:16:54 +02:00
|
|
|
.delete(`/api/v1/admin/users/${targetUserId}/body-parameters/circumference/${create.body.id}`)
|
2026-10-14 06:12:41 +02:00
|
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
|
|
|
expect(del.status).toBe(204);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('zwraca historię i waliduje parametr month', async () => {
|
|
|
|
|
const ok = await adminGet(`/users/${targetUserId}/history`);
|
|
|
|
|
expect(ok.status).toBe(200);
|
|
|
|
|
expect(ok.body).toEqual({ sessions: [] });
|
|
|
|
|
|
|
|
|
|
const bad = await adminGet(`/users/${targetUserId}/history?month=2026-1`);
|
|
|
|
|
expect(bad.status).toBe(400);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('zwraca postępy ćwiczeń użytkownika', async () => {
|
|
|
|
|
const res = await adminGet(`/users/${targetUserId}/exercise-progress`);
|
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
expect(res.body).toEqual({ exercises: [] });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('filtruje wnioski siłowni po userId', async () => {
|
|
|
|
|
const gym = await prisma.gym.create({ data: { name: `Gym ${rand()}` } });
|
|
|
|
|
const claimOfTarget = await prisma.gymClaim.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: targetUserId,
|
|
|
|
|
gymId: gym.id,
|
|
|
|
|
description: 'Wniosek celu',
|
|
|
|
|
status: 'PENDING',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
await prisma.gymClaim.create({
|
|
|
|
|
data: {
|
|
|
|
|
userId: otherUserId,
|
|
|
|
|
gymId: gym.id,
|
|
|
|
|
description: 'Wniosek innego',
|
|
|
|
|
status: 'PENDING',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const res = await adminGet(`/gyms/claims?userId=${targetUserId}`);
|
|
|
|
|
expect(res.status).toBe(200);
|
|
|
|
|
const ids = res.body.claims.map((c: { id: string }) => c.id);
|
|
|
|
|
expect(ids).toContain(claimOfTarget.id);
|
|
|
|
|
for (const c of res.body.claims) {
|
|
|
|
|
expect(c.user.id).toBe(targetUserId);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|