275 lines
11 KiB
TypeScript
275 lines
11 KiB
TypeScript
import { describe, it, expect, beforeAll } from 'vitest';
|
|
import { prisma } from '../../src/lib/prisma.js';
|
|
import { buildApp, createUser, loginViaApi, rand, PASSWORD } from '../helpers.js';
|
|
|
|
describe('virtual gyms — creation, listing and ratings', () => {
|
|
let adminToken: string;
|
|
let adminId: string;
|
|
let userToken: string;
|
|
let userId: string;
|
|
|
|
beforeAll(async () => {
|
|
const admin = await createUser({ isAdmin: true });
|
|
adminId = admin.user.id;
|
|
adminToken = (await loginViaApi(admin.user.email, PASSWORD)).token;
|
|
const user = await createUser();
|
|
userId = user.user.id;
|
|
userToken = (await loginViaApi(user.user.email, PASSWORD)).token;
|
|
});
|
|
|
|
it('lets a non-admin create a VIRTUAL gym (creator = OWNER)', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.send({ name: `Virtual Gym ${rand()}`, location: 'Gdańsk' });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.gym).toMatchObject({ kind: 'VIRTUAL', isPrivate: false });
|
|
expect(res.body.gym.roles).toEqual(['OWNER']);
|
|
});
|
|
|
|
it('rejects a VIRTUAL gym without a location with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.send({ name: `No Location ${rand()}` });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects kind=REAL from a non-admin with 403', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.send({ name: `Real Try ${rand()}`, location: 'Gdańsk', kind: 'REAL' });
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it('lets an admin create a REAL gym', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Admin Real ${rand()}`, location: 'Wrocław', kind: 'REAL' });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.gym).toMatchObject({ kind: 'REAL', isPrivate: false });
|
|
});
|
|
|
|
it('lets an admin create a private VIRTUAL gym', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Admin Virtual ${rand()}`, location: 'Wrocław', kind: 'VIRTUAL', isPrivate: true });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.gym).toMatchObject({ kind: 'VIRTUAL', isPrivate: true });
|
|
});
|
|
|
|
it('rejects REAL + isPrivate=true with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Private Real ${rand()}`, location: 'Wrocław', kind: 'REAL', isPrivate: true });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('lets the owner toggle isPrivate via PATCH (VIRTUAL only)', async () => {
|
|
const app = buildApp();
|
|
const created = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.send({ name: `Toggle Gym ${rand()}`, location: 'Gdańsk' });
|
|
const gymId = created.body.gym.id as string;
|
|
const res = await app
|
|
.patch(`/api/v1/gyms/${gymId}`)
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.send({ isPrivate: true });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.gym.isPrivate).toBe(true);
|
|
});
|
|
|
|
it('rejects isPrivate=true on a REAL gym via PATCH with 400', async () => {
|
|
const app = buildApp();
|
|
const created = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ name: `Real Patch ${rand()}`, location: 'Wrocław', kind: 'REAL' });
|
|
const gymId = created.body.gym.id as string;
|
|
const res = await app
|
|
.patch(`/api/v1/gyms/${gymId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ isPrivate: true });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('hides a private VIRTUAL gym from /gyms/virtual for strangers, shows it to the owner', async () => {
|
|
const app = buildApp();
|
|
const created = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.send({ name: `Hidden Gym ${rand()}`, location: 'Gdańsk', isPrivate: true });
|
|
const gymId = created.body.gym.id as string;
|
|
|
|
const stranger = await createUser();
|
|
const strangerToken = (await loginViaApi(stranger.user.email, PASSWORD)).token;
|
|
const forStranger = await app
|
|
.get('/api/v1/gyms/virtual')
|
|
.set('Authorization', `Bearer ${strangerToken}`);
|
|
expect(forStranger.status).toBe(200);
|
|
expect(forStranger.body.gyms.some((g: { id: string }) => g.id === gymId)).toBe(false);
|
|
|
|
const forOwner = await app
|
|
.get('/api/v1/gyms/virtual')
|
|
.set('Authorization', `Bearer ${userToken}`);
|
|
const own = forOwner.body.gyms.find((g: { id: string }) => g.id === gymId);
|
|
expect(own).toMatchObject({ isPrivate: true });
|
|
expect(own.up).toBeUndefined();
|
|
expect(own.myVote).toBeUndefined();
|
|
});
|
|
|
|
it('lists only REAL gyms in /gyms/all', async () => {
|
|
const app = buildApp();
|
|
const virtual = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.send({ name: `All List Virtual ${rand()}`, location: 'Gdańsk' });
|
|
const res = await app.get('/api/v1/gyms/all').set('Authorization', `Bearer ${userToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.gyms.some((g: { id: string }) => g.id === virtual.body.gym.id)).toBe(false);
|
|
for (const g of res.body.gyms) {
|
|
expect(g.kind).toBe('REAL');
|
|
}
|
|
});
|
|
|
|
it('sorts /gyms/virtual by score desc then name asc; own private last', async () => {
|
|
const app = buildApp();
|
|
const owner = await createUser();
|
|
const ownerToken = (await loginViaApi(owner.user.email, PASSWORD)).token;
|
|
const voter1 = await createUser();
|
|
const voter1Token = (await loginViaApi(voter1.user.email, PASSWORD)).token;
|
|
const voter2 = await createUser();
|
|
const voter2Token = (await loginViaApi(voter2.user.email, PASSWORD)).token;
|
|
const voter3 = await createUser();
|
|
const voter3Token = (await loginViaApi(voter3.user.email, PASSWORD)).token;
|
|
|
|
const gA = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.send({ name: `Sort A ${rand()}`, location: 'X' });
|
|
const gB = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.send({ name: `Sort B ${rand()}`, location: 'X' });
|
|
const gPrivate = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.send({ name: `Sort Private ${rand()}`, location: 'X', isPrivate: true });
|
|
|
|
// gB: 2 up, 1 down (score 1); gA: 1 up (score 1) → same score, name asc
|
|
await app.post(`/api/v1/gyms/${gA.body.gym.id}/rate`).set('Authorization', `Bearer ${voter1Token}`).send({ vote: 'UP' });
|
|
await app.post(`/api/v1/gyms/${gB.body.gym.id}/rate`).set('Authorization', `Bearer ${voter1Token}`).send({ vote: 'UP' });
|
|
await app.post(`/api/v1/gyms/${gB.body.gym.id}/rate`).set('Authorization', `Bearer ${voter2Token}`).send({ vote: 'UP' });
|
|
await app.post(`/api/v1/gyms/${gB.body.gym.id}/rate`).set('Authorization', `Bearer ${voter3Token}`).send({ vote: 'UP' });
|
|
await app.post(`/api/v1/gyms/${gB.body.gym.id}/rate`).set('Authorization', `Bearer ${voter1Token}`).send({ vote: 'DOWN' });
|
|
|
|
const res = await app
|
|
.get('/api/v1/gyms/virtual')
|
|
.set('Authorization', `Bearer ${ownerToken}`);
|
|
const ids = res.body.gyms.map((g: { id: string }) => g.id);
|
|
const ia = ids.indexOf(gA.body.gym.id);
|
|
const ib = ids.indexOf(gB.body.gym.id);
|
|
const ip = ids.indexOf(gPrivate.body.gym.id);
|
|
expect(ia).toBeGreaterThanOrEqual(0);
|
|
expect(ib).toBeGreaterThanOrEqual(0);
|
|
expect(ip).toBe(ids.length - 1);
|
|
// equal scores → name ascending
|
|
const nameA = res.body.gyms[ia].name;
|
|
const nameB = res.body.gyms[ib].name;
|
|
expect(nameA.localeCompare(nameB, 'pl')).toBeLessThanOrEqual(0);
|
|
expect(res.body.gyms[ib]).toMatchObject({ up: 2, down: 1, myVote: null });
|
|
});
|
|
|
|
it('rate: set, change and toggle (same vote removes)', async () => {
|
|
const app = buildApp();
|
|
const owner = await createUser();
|
|
const ownerToken = (await loginViaApi(owner.user.email, PASSWORD)).token;
|
|
const voter = await createUser();
|
|
const voterToken = (await loginViaApi(voter.user.email, PASSWORD)).token;
|
|
const gym = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.send({ name: `Rate Gym ${rand()}`, location: 'X' });
|
|
const gymId = gym.body.gym.id as string;
|
|
|
|
const up = await app
|
|
.post(`/api/v1/gyms/${gymId}/rate`)
|
|
.set('Authorization', `Bearer ${voterToken}`)
|
|
.send({ vote: 'UP' });
|
|
expect(up.status).toBe(200);
|
|
expect(up.body).toEqual({ vote: 'UP', up: 1, down: 0 });
|
|
|
|
const change = await app
|
|
.post(`/api/v1/gyms/${gymId}/rate`)
|
|
.set('Authorization', `Bearer ${voterToken}`)
|
|
.send({ vote: 'DOWN' });
|
|
expect(change.body).toEqual({ vote: 'DOWN', up: 0, down: 1 });
|
|
|
|
const toggle = await app
|
|
.post(`/api/v1/gyms/${gymId}/rate`)
|
|
.set('Authorization', `Bearer ${voterToken}`)
|
|
.send({ vote: 'DOWN' });
|
|
expect(toggle.body).toEqual({ vote: null, up: 0, down: 0 });
|
|
});
|
|
|
|
it('rate: the owner cannot rate their own gym (403)', async () => {
|
|
const app = buildApp();
|
|
const owner = await createUser();
|
|
const ownerToken = (await loginViaApi(owner.user.email, PASSWORD)).token;
|
|
const gym = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.send({ name: `Owner Rate ${rand()}`, location: 'X' });
|
|
const res = await app
|
|
.post(`/api/v1/gyms/${gym.body.gym.id}/rate`)
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.send({ vote: 'UP' });
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it('rate: REAL and private VIRTUAL gyms are rejected with 409', async () => {
|
|
const app = buildApp();
|
|
const voter = await createUser();
|
|
const voterToken = (await loginViaApi(voter.user.email, PASSWORD)).token;
|
|
|
|
const real = await prisma.gym.create({ data: { name: `Rate Real ${rand()}`, kind: 'REAL' } });
|
|
const realRes = await app
|
|
.post(`/api/v1/gyms/${real.id}/rate`)
|
|
.set('Authorization', `Bearer ${voterToken}`)
|
|
.send({ vote: 'UP' });
|
|
expect(realRes.status).toBe(409);
|
|
|
|
const owner = await createUser();
|
|
const ownerToken = (await loginViaApi(owner.user.email, PASSWORD)).token;
|
|
const privateGym = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.send({ name: `Rate Private ${rand()}`, location: 'X', isPrivate: true });
|
|
const privateRes = await app
|
|
.post(`/api/v1/gyms/${privateGym.body.gym.id}/rate`)
|
|
.set('Authorization', `Bearer ${voterToken}`)
|
|
.send({ vote: 'UP' });
|
|
expect(privateRes.status).toBe(409);
|
|
});
|
|
|
|
it('rate: unknown gym → 404', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms/unknown-gym/rate')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.send({ vote: 'UP' });
|
|
expect(res.status).toBe(404);
|
|
});
|
|
});
|