Silownie: dolaczenie do prywatnej silowni odrzucane (403) - tylko zaproszenia

This commit is contained in:
tisachi
2026-10-11 16:17:29 +02:00
parent c626898cd5
commit 69435d899b
2 changed files with 17 additions and 1 deletions

View File

@@ -158,10 +158,11 @@ export async function rateGym(
return { vote: existing?.vote === vote ? null : vote, up, down };
}
/** Join an existing gym (creates a MEMBER membership). */
/** Join an existing gym (creates a MEMBER membership). Private gyms are invitation-only. */
export async function joinGym(prisma: PrismaClient, userId: string, gymId: string) {
const gym = await prisma.gym.findUnique({ where: { id: gymId } });
if (!gym) throw notFound('Gym not found');
if (gym.isPrivate) throw forbidden('Private gyms are invitation-only');
const existing = await prisma.gymMembership.findUnique({
where: { userId_gymId: { userId, gymId } },
});

View File

@@ -90,6 +90,21 @@ describe('virtual gyms — creation, listing and ratings', () => {
expect(res.body.gym.isPrivate).toBe(true);
});
it('rejects joining a private virtual gym with 403 (invitation only)', async () => {
const app = buildApp();
const created = await app
.post('/api/v1/gyms')
.set('Authorization', `Bearer ${userToken}`)
.send({ name: `Locked Gym ${rand()}`, location: 'Gdańsk', isPrivate: true });
expect(created.status).toBe(201);
const outsider = await createUser();
const outsiderToken = (await loginViaApi(outsider.user.email, PASSWORD)).token;
const res = await app
.post(`/api/v1/gyms/${created.body.gym.id}/join`)
.set('Authorization', `Bearer ${outsiderToken}`);
expect(res.status).toBe(403);
});
it('rejects isPrivate=true on a REAL gym via PATCH with 400', async () => {
const app = buildApp();
const created = await app