246 lines
10 KiB
TypeScript
246 lines
10 KiB
TypeScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
import { prisma } from '../../src/lib/prisma.js';
|
|
import { buildApp, createUserWithToken, rand } from '../helpers.js';
|
|
import { photoUploadDir, removeGymPhotoFiles } from '../../src/lib/uploads.js';
|
|
|
|
/** Minimal 1x1 transparent PNG. */
|
|
const PNG_BUFFER = Buffer.from(
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
|
|
'base64',
|
|
);
|
|
|
|
describe('gym photos — gallery of REAL gyms', () => {
|
|
let ownerToken: string;
|
|
let ownerId: string;
|
|
let strangerToken: string;
|
|
let strangerId: string;
|
|
let adminToken: string;
|
|
|
|
const gymIds: string[] = [];
|
|
|
|
beforeAll(async () => {
|
|
const owner = await createUserWithToken();
|
|
ownerId = owner.user.id;
|
|
ownerToken = owner.token;
|
|
const stranger = await createUserWithToken();
|
|
strangerId = stranger.user.id;
|
|
strangerToken = stranger.token;
|
|
const admin = await createUserWithToken({ isAdmin: true });
|
|
adminToken = admin.token;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
for (const id of gymIds) removeGymPhotoFiles(id);
|
|
await prisma.gymPhoto.deleteMany({ where: { gymId: { in: gymIds } } }).catch(() => undefined);
|
|
await prisma.gymMembership.deleteMany({ where: { gymId: { in: gymIds } } }).catch(() => undefined);
|
|
await prisma.gym.deleteMany({ where: { id: { in: gymIds } } }).catch(() => undefined);
|
|
});
|
|
|
|
async function createRealGym(ownerUserId: string | null, name?: string) {
|
|
const gym = await prisma.gym.create({
|
|
data: { name: name ?? `Real Gym ${rand()}`, kind: 'REAL' },
|
|
});
|
|
gymIds.push(gym.id);
|
|
if (ownerUserId) {
|
|
await prisma.gymMembership.create({
|
|
data: { userId: ownerUserId, gymId: gym.id, roles: ['OWNER'] },
|
|
});
|
|
}
|
|
return gym;
|
|
}
|
|
|
|
it('GET /gyms/:id returns a REAL gym to a stranger (with empty gallery)', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(ownerId);
|
|
const res = await app.get(`/api/v1/gyms/${gym.id}`).set('Authorization', `Bearer ${strangerToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.gym).toMatchObject({ id: gym.id, kind: 'REAL', photos: [], coverPhoto: null });
|
|
expect(res.body.roles).toEqual([]);
|
|
expect(res.body.isAdmin).toBe(false);
|
|
});
|
|
|
|
it('GET /gyms/:id returns 403 for a VIRTUAL gym to a non-manager', async () => {
|
|
const app = buildApp();
|
|
const gym = await prisma.gym.create({
|
|
data: { name: `Virtual Gym ${rand()}`, kind: 'VIRTUAL', location: 'Warszawa' },
|
|
});
|
|
gymIds.push(gym.id);
|
|
const res = await app.get(`/api/v1/gyms/${gym.id}`).set('Authorization', `Bearer ${strangerToken}`);
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it('GET /gyms/:id returns a VIRTUAL gym to its OWNER', async () => {
|
|
const app = buildApp();
|
|
const gym = await prisma.gym.create({
|
|
data: { name: `Virtual Owned ${rand()}`, kind: 'VIRTUAL', location: 'Warszawa' },
|
|
});
|
|
gymIds.push(gym.id);
|
|
await prisma.gymMembership.create({
|
|
data: { userId: ownerId, gymId: gym.id, roles: ['OWNER'] },
|
|
});
|
|
const res = await app.get(`/api/v1/gyms/${gym.id}`).set('Authorization', `Bearer ${ownerToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.roles).toContain('OWNER');
|
|
});
|
|
|
|
it('GET /gyms/:id returns 404 for an unknown gym', async () => {
|
|
const app = buildApp();
|
|
const res = await app.get('/api/v1/gyms/unknown').set('Authorization', `Bearer ${ownerToken}`);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it('POST /gyms/:id/photos lets the OWNER add photos (multipart)', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(ownerId);
|
|
const res = await app
|
|
.post(`/api/v1/gyms/${gym.id}/photos`)
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.attach('photos', PNG_BUFFER, { filename: 'a.png', contentType: 'image/png' })
|
|
.attach('photos', PNG_BUFFER, { filename: 'b.png', contentType: 'image/png' });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.gym.photos).toHaveLength(2);
|
|
expect(res.body.gym.photos[0]).toMatchObject({ order: 0 });
|
|
expect(res.body.gym.coverPhoto).toBe(`/gyms/${gym.id}/photos/${res.body.gym.photos[0].id}`);
|
|
});
|
|
|
|
it('POST /gyms/:id/photos rejects a non-OWNER with 403', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(ownerId);
|
|
const res = await app
|
|
.post(`/api/v1/gyms/${gym.id}/photos`)
|
|
.set('Authorization', `Bearer ${strangerToken}`)
|
|
.attach('photos', PNG_BUFFER, { filename: 'a.png', contentType: 'image/png' });
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it('POST /gyms/:id/photos rejects a VIRTUAL gym with 409', async () => {
|
|
const app = buildApp();
|
|
const gym = await prisma.gym.create({
|
|
data: { name: `Virtual NoGallery ${rand()}`, kind: 'VIRTUAL', location: 'Warszawa' },
|
|
});
|
|
gymIds.push(gym.id);
|
|
await prisma.gymMembership.create({
|
|
data: { userId: ownerId, gymId: gym.id, roles: ['OWNER'] },
|
|
});
|
|
const res = await app
|
|
.post(`/api/v1/gyms/${gym.id}/photos`)
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.attach('photos', PNG_BUFFER, { filename: 'a.png', contentType: 'image/png' });
|
|
expect(res.status).toBe(409);
|
|
});
|
|
|
|
it('POST /gyms/:id/photos lets an admin add to a REAL gym without an OWNER', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(null);
|
|
const res = await app
|
|
.post(`/api/v1/gyms/${gym.id}/photos`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.attach('photos', PNG_BUFFER, { filename: 'a.png', contentType: 'image/png' });
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.gym.photos).toHaveLength(1);
|
|
});
|
|
|
|
it('POST /gyms/:id/photos rejects a non-image file with 400', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(ownerId);
|
|
const res = await app
|
|
.post(`/api/v1/gyms/${gym.id}/photos`)
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.attach('photos', Buffer.from('hello'), { filename: 'a.txt', contentType: 'text/plain' });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('DELETE /gyms/:id/photos/:photoId lets the OWNER remove a photo', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(ownerId);
|
|
const added = await app
|
|
.post(`/api/v1/gyms/${gym.id}/photos`)
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.attach('photos', PNG_BUFFER, { filename: 'a.png', contentType: 'image/png' });
|
|
const photoId = added.body.gym.photos[0].id as string;
|
|
const res = await app
|
|
.delete(`/api/v1/gyms/${gym.id}/photos/${photoId}`)
|
|
.set('Authorization', `Bearer ${ownerToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.body.gym.photos).toHaveLength(0);
|
|
const dir = photoUploadDir(gym.id);
|
|
expect(fs.existsSync(dir) ? fs.readdirSync(dir).length : 0).toBe(0);
|
|
});
|
|
|
|
it('DELETE /gyms/:id/photos/:photoId rejects a non-OWNER with 403', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(ownerId);
|
|
const added = await app
|
|
.post(`/api/v1/gyms/${gym.id}/photos`)
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.attach('photos', PNG_BUFFER, { filename: 'a.png', contentType: 'image/png' });
|
|
const photoId = added.body.gym.photos[0].id as string;
|
|
const res = await app
|
|
.delete(`/api/v1/gyms/${gym.id}/photos/${photoId}`)
|
|
.set('Authorization', `Bearer ${strangerToken}`);
|
|
expect(res.status).toBe(403);
|
|
});
|
|
|
|
it('PATCH /gyms/:id/photos/:photoId/cover makes the photo the cover (order 0)', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(ownerId);
|
|
const added = await app
|
|
.post(`/api/v1/gyms/${gym.id}/photos`)
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.attach('photos', PNG_BUFFER, { filename: 'a.png', contentType: 'image/png' })
|
|
.attach('photos', PNG_BUFFER, { filename: 'b.png', contentType: 'image/png' });
|
|
const photos = added.body.gym.photos as { id: string; order: number }[];
|
|
const second = photos[1].id;
|
|
const res = await app
|
|
.patch(`/api/v1/gyms/${gym.id}/photos/${second}/cover`)
|
|
.set('Authorization', `Bearer ${ownerToken}`);
|
|
expect(res.status).toBe(200);
|
|
const byId = new Map((res.body.gym.photos as { id: string; order: number }[]).map((p) => [p.id, p.order]));
|
|
expect(byId.get(second)).toBe(0);
|
|
expect(res.body.gym.coverPhoto).toBe(`/gyms/${gym.id}/photos/${second}`);
|
|
});
|
|
|
|
it('GET /gyms/:id/photos/:photoId serves the image with the stored Content-Type', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(ownerId);
|
|
const added = await app
|
|
.post(`/api/v1/gyms/${gym.id}/photos`)
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.attach('photos', PNG_BUFFER, { filename: 'a.png', contentType: 'image/png' });
|
|
const photoId = added.body.gym.photos[0].id as string;
|
|
const res = await app
|
|
.get(`/api/v1/gyms/${gym.id}/photos/${photoId}`)
|
|
.set('Authorization', `Bearer ${strangerToken}`);
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers['content-type']).toContain('image/png');
|
|
expect(res.body.length).toBe(PNG_BUFFER.length);
|
|
});
|
|
|
|
it('GET /gyms/:id/photos/:photoId returns 404 when the file is missing on disk', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(ownerId);
|
|
const photo = await prisma.gymPhoto.create({
|
|
data: { gymId: gym.id, storedName: 'missing.png', mimeType: 'image/png', sizeBytes: 1, order: 0 },
|
|
});
|
|
const res = await app
|
|
.get(`/api/v1/gyms/${gym.id}/photos/${photo.id}`)
|
|
.set('Authorization', `Bearer ${ownerToken}`);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it('deleting a gym removes its photo files', async () => {
|
|
const app = buildApp();
|
|
const gym = await createRealGym(ownerId);
|
|
await app
|
|
.post(`/api/v1/gyms/${gym.id}/photos`)
|
|
.set('Authorization', `Bearer ${ownerToken}`)
|
|
.attach('photos', PNG_BUFFER, { filename: 'a.png', contentType: 'image/png' });
|
|
expect(fs.existsSync(path.join(photoUploadDir(gym.id)))).toBe(true);
|
|
const res = await app.delete(`/api/v1/gyms/${gym.id}`).set('Authorization', `Bearer ${ownerToken}`);
|
|
expect(res.status).toBe(204);
|
|
expect(fs.existsSync(path.join(photoUploadDir(gym.id)))).toBe(false);
|
|
});
|
|
});
|