324 lines
12 KiB
TypeScript
324 lines
12 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
import { prisma } from '../../src/lib/prisma.js';
|
|
import { buildApp, createUserWithToken, rand } from '../helpers.js';
|
|
|
|
describe('gym contact — wątek członek ↔ właściciele', () => {
|
|
let ownerToken: string;
|
|
let ownerId: string;
|
|
let memberToken: string;
|
|
let memberId: string;
|
|
let strangerToken: string;
|
|
let adminToken: string;
|
|
|
|
const gymIds: string[] = [];
|
|
|
|
beforeAll(async () => {
|
|
const owner = await createUserWithToken();
|
|
ownerId = owner.user.id;
|
|
ownerToken = owner.token;
|
|
const member = await createUserWithToken();
|
|
memberId = member.user.id;
|
|
memberToken = member.token;
|
|
const stranger = await createUserWithToken();
|
|
strangerToken = stranger.token;
|
|
const admin = await createUserWithToken({ isAdmin: true });
|
|
adminToken = admin.token;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
const convos = await prisma.gymConversation.findMany({
|
|
where: { gymId: { in: gymIds } },
|
|
select: { id: true },
|
|
});
|
|
const ids = convos.map((c) => c.id);
|
|
await prisma.gymConversationRead
|
|
.deleteMany({ where: { conversationId: { in: ids } } })
|
|
.catch(() => undefined);
|
|
await prisma.gymMessage.deleteMany({ where: { conversationId: { in: ids } } }).catch(() => undefined);
|
|
await prisma.gymConversation
|
|
.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);
|
|
});
|
|
|
|
/** REAL z OWNER-em i MEMBER-em (dowolny typ gyma działa w kontakcie — test na REAL). */
|
|
async function createGymWithRoles() {
|
|
const gym = await prisma.gym.create({
|
|
data: { name: `Contact Gym ${rand()}`, kind: 'REAL', location: 'Warszawa' },
|
|
});
|
|
gymIds.push(gym.id);
|
|
await prisma.gymMembership.create({
|
|
data: { userId: ownerId, gymId: gym.id, roles: ['OWNER'] },
|
|
});
|
|
await prisma.gymMembership.create({
|
|
data: { userId: memberId, gymId: gym.id, roles: ['MEMBER'] },
|
|
});
|
|
return gym;
|
|
}
|
|
|
|
const auth = (token: string) => ({ Authorization: `Bearer ${token}` });
|
|
|
|
it('member: brak wątku → widok z conversation null i canReply', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
const res = await app.get(`/api/v1/gyms/${gym.id}/contact`).set(auth(memberToken));
|
|
expect(res.status).toBe(200);
|
|
expect(res.body).toMatchObject({ role: 'member', conversation: null, messages: [], canReply: true });
|
|
});
|
|
|
|
it('member wysyła pierwszą wiadomość → 201 (tworzy wątek)', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
const res = await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: 'Cześć, mam pytanie o sprzęt' });
|
|
expect(res.status).toBe(201);
|
|
expect(res.body.conversation).toMatchObject({ gymId: gym.id, userId: memberId });
|
|
|
|
const thread = await app.get(`/api/v1/gyms/${gym.id}/contact`).set(auth(memberToken));
|
|
expect(thread.status).toBe(200);
|
|
expect(thread.body.conversation).toMatchObject({ gymId: gym.id, memberName: expect.any(String) });
|
|
expect(thread.body.messages).toHaveLength(1);
|
|
expect(thread.body.messages[0]).toMatchObject({ senderId: memberId, body: 'Cześć, mam pytanie o sprzęt' });
|
|
});
|
|
|
|
it('stranger → 403 (widok i wysyłka)', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
const view = await app.get(`/api/v1/gyms/${gym.id}/contact`).set(auth(strangerToken));
|
|
expect(view.status).toBe(403);
|
|
const post = await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(strangerToken))
|
|
.send({ body: 'hej' });
|
|
expect(post.status).toBe(403);
|
|
});
|
|
|
|
it('pusty body → 400', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
const res = await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: '' });
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it('owner widzi wątek w liście, odpowiada; read-mark kasuje unreadCount', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: 'Pytanie' });
|
|
|
|
// inbox właściciela: 1 wątek, 1 nieprzeczytana
|
|
const inbox = await app.get('/api/v1/gym-contact/conversations').set(auth(ownerToken));
|
|
expect(inbox.status).toBe(200);
|
|
const conv = inbox.body.conversations.find((c: { gymId: string }) => c.gymId === gym.id);
|
|
expect(conv).toMatchObject({
|
|
gymId: gym.id,
|
|
userId: memberId,
|
|
lastMessageBody: 'Pytanie',
|
|
unreadCount: 1,
|
|
});
|
|
|
|
// lista wątków gyma (GET /gyms/:id/contact dla ownera)
|
|
const list = await app.get(`/api/v1/gyms/${gym.id}/contact`).set(auth(ownerToken));
|
|
expect(list.status).toBe(200);
|
|
expect(list.body.role).toBe('owner');
|
|
expect(list.body.threads).toHaveLength(1);
|
|
expect(list.body.threads[0]).toMatchObject({ id: conv.id, unreadCount: 1 });
|
|
|
|
// odczyt = read-mark → unreadCount 0
|
|
await app.get(`/api/v1/gym-contact/conversations/${conv.id}`).set(auth(ownerToken));
|
|
const inboxAfter = await app.get('/api/v1/gym-contact/conversations').set(auth(ownerToken));
|
|
const convAfter = inboxAfter.body.conversations.find((c: { id: string }) => c.id === conv.id);
|
|
expect(convAfter.unreadCount).toBe(0);
|
|
|
|
// odpowiedź ownera → member widzi ją w swoim wątku
|
|
const reply = await app
|
|
.post(`/api/v1/gym-contact/conversations/${conv.id}/messages`)
|
|
.set(auth(ownerToken))
|
|
.send({ body: 'Odpowiedź właściciela' });
|
|
expect(reply.status).toBe(201);
|
|
const memberView = await app.get(`/api/v1/gyms/${gym.id}/contact`).set(auth(memberToken));
|
|
expect(memberView.body.messages).toHaveLength(2);
|
|
expect(memberView.body.messages[1]).toMatchObject({
|
|
senderId: ownerId,
|
|
body: 'Odpowiedź właściciela',
|
|
});
|
|
|
|
// unread callera liczy tylko wiadomości innych — member po odczycie ma 0 w inboxie ownera (bo sam jest właścicielem? nie) — sprawdzenie przez GET wątku ownera
|
|
const ownerView = await app.get(`/api/v1/gym-contact/conversations/${conv.id}`).set(auth(ownerToken));
|
|
expect(ownerView.status).toBe(200);
|
|
expect(ownerView.body.messages).toHaveLength(2);
|
|
});
|
|
|
|
it('drugi OWNER gyma (niedynamiczny? — tak dynamiczny) dostaje dostęp po nadaniu roli', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
const second = await createUserWithToken();
|
|
await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: 'hello' });
|
|
|
|
// przed nadaniem roli: nieuczestnik → 404
|
|
const denied = await app
|
|
.get(`/api/v1/gym-contact/conversations`)
|
|
.set(auth(second.token));
|
|
expect(denied.status).toBe(200);
|
|
expect(
|
|
denied.body.conversations.some((c: { gymId: string }) => c.gymId === gym.id),
|
|
).toBe(false);
|
|
|
|
await prisma.gymMembership.create({
|
|
data: { userId: second.user.id, gymId: gym.id, roles: ['OWNER'] },
|
|
});
|
|
const inbox = await app.get('/api/v1/gym-contact/conversations').set(auth(second.token));
|
|
expect(
|
|
inbox.body.conversations.some((c: { gymId: string }) => c.gymId === gym.id),
|
|
).toBe(true);
|
|
});
|
|
|
|
it('delete: tylko autor (204), ktoś inny → 404', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: 'do usunięcia' });
|
|
const conv = await prisma.gymConversation.findUnique({
|
|
where: { gymId_userId: { gymId: gym.id, userId: memberId } },
|
|
});
|
|
const message = await prisma.gymMessage.findFirst({ where: { conversationId: conv!.id } });
|
|
|
|
const other = await app
|
|
.delete(`/api/v1/gym-contact/messages/${message!.id}`)
|
|
.set(auth(ownerToken));
|
|
expect(other.status).toBe(404);
|
|
|
|
const mine = await app
|
|
.delete(`/api/v1/gym-contact/messages/${message!.id}`)
|
|
.set(auth(memberToken));
|
|
expect(mine.status).toBe(204);
|
|
const still = await app.get(`/api/v1/gym-contact/conversations/${conv!.id}`).set(auth(memberToken));
|
|
expect(still.body.messages).toHaveLength(0);
|
|
});
|
|
|
|
it('usunięcie członkostwa → member traci dostęp do wątku (403)', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: 'jestem członkiem' });
|
|
const conv = await prisma.gymConversation.findUnique({
|
|
where: { gymId_userId: { gymId: gym.id, userId: memberId } },
|
|
});
|
|
|
|
await prisma.gymMembership.delete({
|
|
where: { userId_gymId: { userId: memberId, gymId: gym.id } },
|
|
});
|
|
const view = await app.get(`/api/v1/gyms/${gym.id}/contact`).set(auth(memberToken));
|
|
expect(view.status).toBe(403);
|
|
const single = await app
|
|
.get(`/api/v1/gym-contact/conversations/${conv!.id}`)
|
|
.set(auth(memberToken));
|
|
expect(single.status).toBe(403);
|
|
});
|
|
|
|
it('utrata roli OWNER → właściciel traci dostęp do wątku (404)', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: 'pytanie' });
|
|
const conv = await prisma.gymConversation.findUnique({
|
|
where: { gymId_userId: { gymId: gym.id, userId: memberId } },
|
|
});
|
|
|
|
await prisma.gymMembership.update({
|
|
where: { userId_gymId: { userId: ownerId, gymId: gym.id } },
|
|
data: { roles: ['MEMBER'] },
|
|
});
|
|
const single = await app
|
|
.get(`/api/v1/gym-contact/conversations/${conv!.id}`)
|
|
.set(auth(ownerToken));
|
|
expect(single.status).toBe(404);
|
|
const list = await app.get(`/api/v1/gyms/${gym.id}/contact`).set(auth(ownerToken));
|
|
// jako (były) OWNER zrolowany do MEMBER — member widok własnego wątku (pusty), nie threads
|
|
expect(list.status).toBe(200);
|
|
expect(list.body.role).toBe('member');
|
|
});
|
|
|
|
it('nieuczestnik → 404 na pojedynczym wątku', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: 'tajne' });
|
|
const conv = await prisma.gymConversation.findUnique({
|
|
where: { gymId_userId: { gymId: gym.id, userId: memberId } },
|
|
});
|
|
const res = await app
|
|
.get(`/api/v1/gym-contact/conversations/${conv!.id}`)
|
|
.set(auth(strangerToken));
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it('admin ma dostęp do każdego wątku', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: 'dla admina' });
|
|
const inbox = await app.get('/api/v1/gym-contact/conversations').set(auth(adminToken));
|
|
expect(inbox.status).toBe(200);
|
|
const conv = inbox.body.conversations.find((c: { gymId: string }) => c.gymId === gym.id);
|
|
expect(conv).toBeTruthy();
|
|
const single = await app
|
|
.get(`/api/v1/gym-contact/conversations/${conv.id}`)
|
|
.set(auth(adminToken));
|
|
expect(single.status).toBe(200);
|
|
expect(single.body.role).toBe('admin');
|
|
});
|
|
|
|
it('ten sam (gymId,userId) nie dubluje wątku (upsert)', async () => {
|
|
const app = buildApp();
|
|
const gym = await createGymWithRoles();
|
|
const first = await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: 'raz' });
|
|
const second = await app
|
|
.post(`/api/v1/gyms/${gym.id}/contact/messages`)
|
|
.set(auth(memberToken))
|
|
.send({ body: 'dwa' });
|
|
expect(second.body.conversation.id).toBe(first.body.conversation.id);
|
|
const count = await prisma.gymConversation.count({
|
|
where: { gymId: gym.id, userId: memberId },
|
|
});
|
|
expect(count).toBe(1);
|
|
});
|
|
|
|
it('404 dla nieistniejącej siłowni', async () => {
|
|
const app = buildApp();
|
|
const res = await app.get('/api/v1/gyms/does-not-exist/contact').set(auth(memberToken));
|
|
expect(res.status).toBe(404);
|
|
const post = await app
|
|
.post('/api/v1/gyms/does-not-exist/contact/messages')
|
|
.set(auth(memberToken))
|
|
.send({ body: 'x' });
|
|
expect(post.status).toBe(404);
|
|
});
|
|
});
|