487 lines
19 KiB
TypeScript
487 lines
19 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';
|
|
|
|
const PDF_BUFFER = Buffer.from('%PDF-1.4 test', 'utf8');
|
|
|
|
function makePdf(sizeBytes: number): Buffer {
|
|
const buf = Buffer.alloc(sizeBytes, 0x20);
|
|
buf.write('%PDF-1.4', 0, 'utf8');
|
|
return buf;
|
|
}
|
|
|
|
describe('gym claims — verification and new real gyms', () => {
|
|
let adminToken: string;
|
|
let userToken: string;
|
|
let userId: string;
|
|
|
|
beforeAll(async () => {
|
|
const admin = await createUser({ isAdmin: true });
|
|
adminToken = (await loginViaApi(admin.user.email, PASSWORD)).token;
|
|
const user = await createUser();
|
|
userId = user.user.id;
|
|
userToken = (await loginViaApi(user.user.email, PASSWORD)).token;
|
|
});
|
|
|
|
async function createVirtualGym(token: string, name?: string) {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms')
|
|
.set('Authorization', `Bearer ${token}`)
|
|
.send({ name: name ?? `Claim Gym ${rand()}`, location: 'Katowice' });
|
|
expect(res.status).toBe(201);
|
|
return res.body.gym.id as string;
|
|
}
|
|
|
|
describe('POST /gyms/claims (verification)', () => {
|
|
it('creates a claim with an uploaded document (201)', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'To moja prawdziwa siłownia')
|
|
.field('companyName', 'Fit Sp. z o.o.')
|
|
.field('nip', '1234567890')
|
|
.field('phone', '+48 600 000 000')
|
|
.field('email', 'owner@fit.local')
|
|
.field('address', 'ul. Żelazna 1, Katowice')
|
|
.attach('files', PDF_BUFFER, { filename: 'umowa.pdf', contentType: 'application/pdf' });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.claim).toMatchObject({
|
|
gymId,
|
|
type: 'verification',
|
|
status: 'PENDING',
|
|
description: 'To moja prawdziwa siłownia',
|
|
});
|
|
expect(res.body.claim.gym).toMatchObject({ id: gymId, kind: 'VIRTUAL' });
|
|
expect(res.body.claim.documents).toEqual([
|
|
expect.objectContaining({ originalName: 'umowa.pdf', mimeType: 'application/pdf' }),
|
|
]);
|
|
});
|
|
|
|
it('rejects a second PENDING claim of the same user for the same gym with 409', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'pierwszy');
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'drugi');
|
|
expect(res.status).toBe(409);
|
|
});
|
|
|
|
it('rejects a claim for a REAL gym with 409', async () => {
|
|
const app = buildApp();
|
|
const gym = await prisma.gym.create({ data: { name: `Claim Real ${rand()}`, kind: 'REAL' } });
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gym.id)
|
|
.field('description', 'nie jest wirtualna');
|
|
expect(res.status).toBe(409);
|
|
});
|
|
|
|
it('rejects an unknown gym with 404', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', 'unknown-gym')
|
|
.field('description', 'x');
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it('rejects a NIP that is not 10 digits with 400', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'x')
|
|
.field('nip', '123');
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects an invalid email with 400', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'x')
|
|
.field('email', 'not-an-email');
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects more than 5 files with 400', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const req = app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'x');
|
|
for (let i = 0; i < 6; i++) {
|
|
req.attach('files', PDF_BUFFER, { filename: `f${i}.pdf`, contentType: 'application/pdf' });
|
|
}
|
|
const res = await req;
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a file over 5 MB with 400', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'x')
|
|
.attach('files', makePdf(6 * 1024 * 1024), { filename: 'big.pdf', contentType: 'application/pdf' });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects an unsupported MIME type with 400', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'x')
|
|
.attach('files', Buffer.from('x'), { filename: 'a.exe', contentType: 'application/x-msdownload' });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
});
|
|
|
|
describe('POST /gyms/claims (new real gym)', () => {
|
|
it('creates a claim without a gymId (201)', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymName', `Nowa Realna ${rand()}`)
|
|
.field('gymLocation', 'ul. Dźwignia 2, Łódź')
|
|
.field('gymCapacity', '30')
|
|
.field('description', 'Prowadzę tę siłownię od 2020')
|
|
.field('companyName', 'Nowa Fit Sp. z o.o.')
|
|
.field('nip', '1234567890');
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.claim).toMatchObject({
|
|
gymId: null,
|
|
type: 'new',
|
|
status: 'PENDING',
|
|
gymName: expect.stringContaining('Nowa Realna'),
|
|
gymLocation: 'ul. Dźwignia 2, Łódź',
|
|
gymCapacity: 30,
|
|
});
|
|
});
|
|
|
|
it('rejects a new-real claim without gymName with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymLocation', 'Łódź')
|
|
.field('description', 'x');
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('rejects a new-real claim without gymLocation with 400', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymName', 'Bez Lokalizacji')
|
|
.field('description', 'x');
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('allows multiple PENDING new-real claims for one user', async () => {
|
|
const app = buildApp();
|
|
for (let i = 0; i < 2; i++) {
|
|
const res = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymName', `Wiele ${rand()} ${i}`)
|
|
.field('gymLocation', 'Łódź')
|
|
.field('description', 'x');
|
|
expect(res.status).toBe(201);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('GET /gyms/claims', () => {
|
|
it('lists the user\'s own claims (all statuses, newest first)', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'do listy');
|
|
const res = await app.get('/api/v1/gyms/claims').set('Authorization', `Bearer ${userToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.claims.length).toBeGreaterThan(0);
|
|
for (const c of res.body.claims) {
|
|
expect(['PENDING', 'APPROVED', 'REJECTED']).toContain(c.status);
|
|
}
|
|
const dates = res.body.claims.map((c: { createdAt: string }) => c.createdAt);
|
|
expect([...dates].sort((a, b) => b.localeCompare(a))).toEqual(dates);
|
|
});
|
|
|
|
it('does not leak other users\' claims', async () => {
|
|
const app = buildApp();
|
|
const other = await createUser();
|
|
const otherToken = (await loginViaApi(other.user.email, PASSWORD)).token;
|
|
const gymId = await createVirtualGym(otherToken);
|
|
const created = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${otherToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'czyjś wniosek');
|
|
const mine = await app.get('/api/v1/gyms/claims').set('Authorization', `Bearer ${userToken}`);
|
|
expect(mine.body.claims.some((c: { id: string }) => c.id === created.body.claim.id)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('DELETE /gyms/claims/:id', () => {
|
|
it('withdraws the user\'s own PENDING claim (204)', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const created = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'do wycofania');
|
|
const claimId = created.body.claim.id as string;
|
|
const res = await app
|
|
.delete(`/api/v1/gyms/claims/${claimId}`)
|
|
.set('Authorization', `Bearer ${userToken}`);
|
|
expect(res.status).toBe(204);
|
|
const gone = await prisma.gymClaim.findUnique({ where: { id: claimId } });
|
|
expect(gone).toBeNull();
|
|
});
|
|
|
|
it('rejects withdrawing another user\'s claim with 403', async () => {
|
|
const app = buildApp();
|
|
const other = await createUser();
|
|
const otherToken = (await loginViaApi(other.user.email, PASSWORD)).token;
|
|
const gymId = await createVirtualGym(otherToken);
|
|
const created = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${otherToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'nie mój');
|
|
const res = await app
|
|
.delete(`/api/v1/gyms/claims/${created.body.claim.id}`)
|
|
.set('Authorization', `Bearer ${userToken}`);
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it('rejects withdrawing a non-PENDING claim with 409', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const created = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'już rozstrzygnięty');
|
|
const claimId = created.body.claim.id as string;
|
|
await prisma.gymClaim.update({ where: { id: claimId }, data: { status: 'REJECTED' } });
|
|
const res = await app
|
|
.delete(`/api/v1/gyms/claims/${claimId}`)
|
|
.set('Authorization', `Bearer ${userToken}`);
|
|
expect(res.status).toBe(409);
|
|
});
|
|
});
|
|
|
|
describe('admin — approve / reject', () => {
|
|
it('approve (verification): gym → REAL + public, applicant → OWNER, other owners demoted, other claims rejected, location overridden', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
// a second owner (admin-assigned role) to be demoted
|
|
const other = await createUser();
|
|
await prisma.gymMembership.create({
|
|
data: { userId: other.user.id, gymId, roles: ['OWNER'] },
|
|
});
|
|
const otherToken = (await loginViaApi(other.user.email, PASSWORD)).token;
|
|
const otherClaim = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${otherToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'konkurencyjny wniosek');
|
|
|
|
const myClaim = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'mój wniosek')
|
|
.field('address', 'ul. Nowa Adres 99, Katowice');
|
|
const claimId = myClaim.body.claim.id as string;
|
|
|
|
const res = await app
|
|
.post(`/api/v1/admin/gyms/claims/${claimId}/approve`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(200);
|
|
|
|
const gym = await prisma.gym.findUnique({ where: { id: gymId } });
|
|
expect(gym).toMatchObject({ kind: 'REAL', isPrivate: false, location: 'ul. Nowa Adres 99, Katowice' });
|
|
|
|
const myMembership = await prisma.gymMembership.findUnique({
|
|
where: { userId_gymId: { userId, gymId } },
|
|
});
|
|
expect(myMembership?.roles).toContain('OWNER');
|
|
|
|
const otherMembership = await prisma.gymMembership.findUnique({
|
|
where: { userId_gymId: { userId: other.user.id, gymId } },
|
|
});
|
|
expect(otherMembership?.roles).toEqual(['MEMBER']);
|
|
|
|
const otherClaimRow = await prisma.gymClaim.findUnique({ where: { id: otherClaim.body.claim.id } });
|
|
expect(otherClaimRow).toMatchObject({ status: 'REJECTED', rejectReason: 'Zatwierdzono inny wniosek' });
|
|
|
|
const claimRow = await prisma.gymClaim.findUnique({ where: { id: claimId } });
|
|
expect(claimRow).toMatchObject({ status: 'APPROVED' });
|
|
expect(claimRow?.decidedAt).toBeInstanceOf(Date);
|
|
});
|
|
|
|
it('approve (new real): creates a REAL gym, applicant → OWNER, claim.gymId set', async () => {
|
|
const app = buildApp();
|
|
const created = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymName', `Zatwierdzona Realna ${rand()}`)
|
|
.field('gymLocation', 'ul. Tarcza 5, Poznań')
|
|
.field('description', 'wniosek o nową realną');
|
|
const claimId = created.body.claim.id as string;
|
|
|
|
const res = await app
|
|
.post(`/api/v1/admin/gyms/claims/${claimId}/approve`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(200);
|
|
|
|
const claimRow = await prisma.gymClaim.findUnique({ where: { id: claimId } });
|
|
expect(claimRow).toMatchObject({ status: 'APPROVED' });
|
|
expect(claimRow?.gymId).toBeTruthy();
|
|
const gym = await prisma.gym.findUnique({ where: { id: claimRow!.gymId! } });
|
|
expect(gym).toMatchObject({
|
|
name: expect.stringContaining('Zatwierdzona Realna'),
|
|
location: 'ul. Tarcza 5, Poznań',
|
|
kind: 'REAL',
|
|
isPrivate: false,
|
|
});
|
|
const membership = await prisma.gymMembership.findUnique({
|
|
where: { userId_gymId: { userId, gymId: claimRow!.gymId! } },
|
|
});
|
|
expect(membership?.roles).toEqual(['OWNER']);
|
|
});
|
|
|
|
it('reject with a reason', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const created = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'do odrzucenia');
|
|
const res = await app
|
|
.post(`/api/v1/admin/gyms/claims/${created.body.claim.id}/reject`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.send({ reason: 'Brak dowodu własności' });
|
|
expect(res.status).toBe(200);
|
|
const row = await prisma.gymClaim.findUnique({ where: { id: created.body.claim.id } });
|
|
expect(row).toMatchObject({ status: 'REJECTED', rejectReason: 'Brak dowodu własności' });
|
|
});
|
|
|
|
it('approve of a non-PENDING claim → 409', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const created = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'rozstrzygnięty');
|
|
await prisma.gymClaim.update({ where: { id: created.body.claim.id }, data: { status: 'REJECTED' } });
|
|
const res = await app
|
|
.post(`/api/v1/admin/gyms/claims/${created.body.claim.id}/approve`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(409);
|
|
});
|
|
|
|
it('GET /admin/gyms/claims lists claims with gym/user data (PENDING first)', async () => {
|
|
const app = buildApp();
|
|
const res = await app
|
|
.get('/api/v1/admin/gyms/claims')
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.claims.length).toBeGreaterThan(0);
|
|
const firstPendingIndex = res.body.claims.findIndex((c: { status: string }) => c.status === 'PENDING');
|
|
const firstDecidedIndex = res.body.claims.findIndex(
|
|
(c: { status: string }) => c.status !== 'PENDING',
|
|
);
|
|
if (firstPendingIndex >= 0 && firstDecidedIndex >= 0) {
|
|
expect(firstPendingIndex).toBeLessThan(firstDecidedIndex);
|
|
}
|
|
for (const c of res.body.claims) {
|
|
expect(['verification', 'new']).toContain(c.type);
|
|
expect(c.user).toMatchObject({ id: expect.any(String), email: expect.any(String) });
|
|
}
|
|
});
|
|
|
|
it('GET /admin/gyms/claims/:id returns the full claim with documents', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const created = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'z dokumentem')
|
|
.attach('files', PDF_BUFFER, { filename: 'dokument.pdf', contentType: 'application/pdf' });
|
|
const res = await app
|
|
.get(`/api/v1/admin/gyms/claims/${created.body.claim.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.claim).toMatchObject({
|
|
description: 'z dokumentem',
|
|
user: { id: userId },
|
|
});
|
|
expect(res.body.claim.documents).toEqual([
|
|
expect.objectContaining({ originalName: 'dokument.pdf' }),
|
|
]);
|
|
});
|
|
|
|
it('GET /admin/gyms/claims/:id/documents/:docId downloads the file', async () => {
|
|
const app = buildApp();
|
|
const gymId = await createVirtualGym(userToken);
|
|
const created = await app
|
|
.post('/api/v1/gyms/claims')
|
|
.set('Authorization', `Bearer ${userToken}`)
|
|
.field('gymId', gymId)
|
|
.field('description', 'do pobrania')
|
|
.attach('files', PDF_BUFFER, { filename: 'pobierz.pdf', contentType: 'application/pdf' });
|
|
const docId = created.body.claim.documents[0].id as string;
|
|
const res = await app
|
|
.get(`/api/v1/admin/gyms/claims/${created.body.claim.id}/documents/${docId}`)
|
|
.set('Authorization', `Bearer ${adminToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers['content-disposition']).toContain('pobierz.pdf');
|
|
expect(res.body.equals(PDF_BUFFER)).toBe(true);
|
|
});
|
|
|
|
it('rejects a non-admin from the claims admin routes with 403', async () => {
|
|
const app = buildApp();
|
|
const res = await app.get('/api/v1/admin/gyms/claims').set('Authorization', `Bearer ${userToken}`);
|
|
expect(res.status).toBe(403);
|
|
});
|
|
});
|
|
});
|