import { describe, it, expect } from 'vitest'; import { prisma } from '../../src/lib/prisma.js'; import { buildApp, createUserWithToken, rand } from '../helpers.js'; describe('gym invitations — private virtual gyms', () => { async function setup() { const { user: owner, token: ownerToken } = await createUserWithToken(); const app = buildApp(); const gym = await app .post('/api/v1/gyms') .set('Authorization', `Bearer ${ownerToken}`) .send({ name: `Invite Gym ${rand()}`, location: 'Bydgoszcz', isPrivate: true }); expect(gym.status).toBe(201); return { owner, ownerToken, gymId: gym.body.gym.id as string, app }; } it('owner of a private VIRTUAL gym can invite a user (201)', async () => { const { ownerToken, gymId, app } = await setup(); const target = await createUserWithToken(); const res = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); expect(res.status).toBe(201); expect(res.body.invitation).toMatchObject({ status: 'PENDING', gymId, userId: target.user.id }); expect(new Date(res.body.invitation.expiresAt).getTime()).toBeGreaterThan(Date.now()); }); it('rejects inviting to a public VIRTUAL gym with 409', async () => { const { ownerToken, app } = await setup(); const publicGym = await app .post('/api/v1/gyms') .set('Authorization', `Bearer ${ownerToken}`) .send({ name: `Public Invite ${rand()}`, location: 'Bydgoszcz' }); const target = await createUserWithToken(); const res = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId: publicGym.body.gym.id, userId: target.user.id }); expect(res.status).toBe(409); }); it('rejects inviting to a REAL gym with 409', async () => { const { ownerToken, app } = await setup(); const real = await prisma.gym.create({ data: { name: `Invite Real ${rand()}`, kind: 'REAL' } }); const target = await createUserWithToken(); const res = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId: real.id, userId: target.user.id }); expect(res.status).toBe(409); }); it('rejects inviting by a non-owner with 403', async () => { const { gymId, app } = await setup(); const stranger = await createUserWithToken(); const target = await createUserWithToken(); const res = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${stranger.token}`) .send({ gymId, userId: target.user.id }); expect(res.status).toBe(403); }); it('rejects inviting yourself with 400', async () => { const { owner, ownerToken, gymId, app } = await setup(); const res = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: owner.id }); expect(res.status).toBe(400); }); it('rejects inviting an unknown user with 404', async () => { const { ownerToken, gymId, app } = await setup(); const res = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: 'unknown-user' }); expect(res.status).toBe(404); }); it('rejects inviting an existing member with 409', async () => { const { ownerToken, gymId, app } = await setup(); const member = await createUserWithToken(); await prisma.gymMembership.create({ data: { userId: member.user.id, gymId, roles: ['MEMBER'] } }); const res = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: member.user.id }); expect(res.status).toBe(409); }); it('rejects a duplicate PENDING invitation with 409', async () => { const { ownerToken, gymId, app } = await setup(); const target = await createUserWithToken(); const first = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); expect(first.status).toBe(201); const second = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); expect(second.status).toBe(409); }); it('accept: creates a MEMBER membership and marks ACCEPTED', async () => { const { ownerToken, gymId, app } = await setup(); const target = await createUserWithToken(); const sent = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); const invId = sent.body.invitation.id as string; const res = await app .post(`/api/v1/gyms/invitations/${invId}/accept`) .set('Authorization', `Bearer ${target.token}`); expect(res.status).toBe(200); const membership = await prisma.gymMembership.findUnique({ where: { userId_gymId: { userId: target.user.id, gymId } }, }); expect(membership?.roles).toEqual(['MEMBER']); const row = await prisma.gymInvitation.findUnique({ where: { id: invId } }); expect(row?.status).toBe('ACCEPTED'); }); it('accept: the sender cannot accept their own invitation (403)', async () => { const { ownerToken, gymId, app } = await setup(); const target = await createUserWithToken(); const sent = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); const res = await app .post(`/api/v1/gyms/invitations/${sent.body.invitation.id}/accept`) .set('Authorization', `Bearer ${ownerToken}`); expect(res.status).toBe(403); }); it('decline: marks DECLINED', async () => { const { ownerToken, gymId, app } = await setup(); const target = await createUserWithToken(); const sent = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); const res = await app .post(`/api/v1/gyms/invitations/${sent.body.invitation.id}/decline`) .set('Authorization', `Bearer ${target.token}`); expect(res.status).toBe(200); const row = await prisma.gymInvitation.findUnique({ where: { id: sent.body.invitation.id } }); expect(row?.status).toBe('DECLINED'); }); it('re-invite after DECLINED updates the existing row', async () => { const { ownerToken, gymId, app } = await setup(); const target = await createUserWithToken(); const sent = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); const invId = sent.body.invitation.id as string; await app .post(`/api/v1/gyms/invitations/${invId}/decline`) .set('Authorization', `Bearer ${target.token}`); const resent = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); expect(resent.status).toBe(201); expect(resent.body.invitation.id).toBe(invId); const rows = await prisma.gymInvitation.findMany({ where: { gymId, userId: target.user.id } }); expect(rows).toHaveLength(1); expect(rows[0].status).toBe('PENDING'); }); it('withdraw: sender removes a PENDING invitation (204)', async () => { const { ownerToken, gymId, app } = await setup(); const target = await createUserWithToken(); const sent = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); const res = await app .delete(`/api/v1/gyms/invitations/${sent.body.invitation.id}`) .set('Authorization', `Bearer ${ownerToken}`); expect(res.status).toBe(204); const row = await prisma.gymInvitation.findUnique({ where: { id: sent.body.invitation.id } }); expect(row).toBeNull(); }); it('withdraw: the invited user cannot withdraw (403)', async () => { const { ownerToken, gymId, app } = await setup(); const target = await createUserWithToken(); const sent = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); const res = await app .delete(`/api/v1/gyms/invitations/${sent.body.invitation.id}`) .set('Authorization', `Bearer ${target.token}`); expect(res.status).toBe(403); }); it('expired invitation (7 days) → 410 on accept', async () => { const { ownerToken, gymId, app } = await setup(); const target = await createUserWithToken(); const sent = await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); const invId = sent.body.invitation.id as string; await prisma.gymInvitation.update({ where: { id: invId }, data: { expiresAt: new Date(Date.now() - 1000) }, }); const res = await app .post(`/api/v1/gyms/invitations/${invId}/accept`) .set('Authorization', `Bearer ${target.token}`); expect(res.status).toBe(410); }); it('GET /gyms/invitations lists received (gym + inviter) and sent (user + gym)', async () => { const { owner, ownerToken, gymId, app } = await setup(); const target = await createUserWithToken(); await app .post('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`) .send({ gymId, userId: target.user.id }); const received = await app .get('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${target.token}`); expect(received.status).toBe(200); const rec = received.body.invitations.find( (i: { gym: { id: string } }) => i.gym.id === gymId, ); expect(rec).toMatchObject({ direction: 'received', gym: { id: gymId, isPrivate: true }, invitedBy: { id: owner.id }, }); const sentList = await app .get('/api/v1/gyms/invitations') .set('Authorization', `Bearer ${ownerToken}`); const sent = sentList.body.invitations.find((i: { id: string }) => i.id === rec.id); expect(sent).toMatchObject({ direction: 'sent', gym: { id: gymId }, user: { id: target.user.id }, }); }); it('GET /gyms/invitations/search: owner only, excludes self and members', async () => { const { owner, ownerToken, gymId, app } = await setup(); const member = await createUserWithToken({ firstName: 'Jan', lastName: 'Kowalski' }); await prisma.gymMembership.create({ data: { userId: member.user.id, gymId, roles: ['MEMBER'] } }); const candidate = await createUserWithToken({ firstName: 'Jan', lastName: 'Kowalski' }); const res = await app .get(`/api/v1/gyms/invitations/search?gymId=${gymId}&firstName=Jan&lastName=Kowalski`) .set('Authorization', `Bearer ${ownerToken}`); expect(res.status).toBe(200); const ids = res.body.users.map((u: { id: string }) => u.id); expect(ids).toContain(candidate.user.id); expect(ids).not.toContain(member.user.id); expect(ids).not.toContain(owner.id); const stranger = await createUserWithToken(); const forbidden = await app .get(`/api/v1/gyms/invitations/search?gymId=${gymId}&firstName=Jan&lastName=Kowalski`) .set('Authorization', `Bearer ${stranger.token}`); expect(forbidden.status).toBe(403); }); });