327 lines
11 KiB
TypeScript
327 lines
11 KiB
TypeScript
import type {
|
|
Conversation,
|
|
FriendsOverview,
|
|
Message,
|
|
MessagePage,
|
|
} from '@repo/contracts';
|
|
import request from 'supertest';
|
|
import {
|
|
befriend,
|
|
connected,
|
|
createTestApp,
|
|
listen,
|
|
nextEvent,
|
|
TestAccounts,
|
|
TestSockets,
|
|
type TestApp,
|
|
type TestUser,
|
|
} from './helpers';
|
|
|
|
describe('Friends, conversations and realtime (e2e)', () => {
|
|
let app: TestApp;
|
|
let accounts: TestAccounts;
|
|
let sockets: TestSockets;
|
|
let alice: TestUser;
|
|
let bob: TestUser;
|
|
let carol: TestUser;
|
|
|
|
async function overviewOf(account: TestUser): Promise<FriendsOverview> {
|
|
const response = await account.agent.get('/friends').expect(200);
|
|
return response.body;
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
app = await createTestApp();
|
|
sockets = new TestSockets(await listen(app));
|
|
accounts = new TestAccounts(app, '@chat-e2e.test');
|
|
await accounts.cleanup();
|
|
alice = await accounts.signUp('Alice');
|
|
bob = await accounts.signUp('Bob');
|
|
carol = await accounts.signUp('Carol');
|
|
});
|
|
|
|
afterAll(async () => {
|
|
sockets.closeAll();
|
|
await accounts.cleanup();
|
|
await app.close();
|
|
});
|
|
|
|
describe('friends', () => {
|
|
it('requires a session', () => {
|
|
return request(app.getHttpServer()).get('/friends').expect(401);
|
|
});
|
|
|
|
it('answers 404 for an unknown username and 400 for your own', async () => {
|
|
await alice.agent
|
|
.post('/friends/requests')
|
|
.send({ username: 'nobody_by_this_name' })
|
|
.expect(404);
|
|
await alice.agent
|
|
.post('/friends/requests')
|
|
.send({ username: alice.username })
|
|
.expect(400);
|
|
});
|
|
|
|
it('sends a request that shows up on both sides', async () => {
|
|
const response = await alice.agent
|
|
.post('/friends/requests')
|
|
.send({ username: bob.username })
|
|
.expect(201);
|
|
expect(response.body).toMatchObject({
|
|
status: 'pending',
|
|
user: { id: bob.id, username: bob.username, name: 'Bob' },
|
|
});
|
|
|
|
const aliceSide = await overviewOf(alice);
|
|
expect(aliceSide.outgoing.map((pending) => pending.user.id)).toEqual([
|
|
bob.id,
|
|
]);
|
|
expect(aliceSide.incoming).toEqual([]);
|
|
|
|
const bobSide = await overviewOf(bob);
|
|
expect(bobSide.incoming.map((pending) => pending.user.id)).toEqual([
|
|
alice.id,
|
|
]);
|
|
expect(bobSide.friends).toEqual([]);
|
|
});
|
|
|
|
it('answers 409 when the request was already sent', () => {
|
|
return alice.agent
|
|
.post('/friends/requests')
|
|
.send({ username: bob.username })
|
|
.expect(409);
|
|
});
|
|
|
|
it("doesn't let the sender or a third person accept it", async () => {
|
|
await alice.agent.post(`/friends/requests/${bob.id}/accept`).expect(404);
|
|
await carol.agent
|
|
.post(`/friends/requests/${alice.id}/accept`)
|
|
.expect(404);
|
|
});
|
|
|
|
it('makes the two friends once it is accepted', async () => {
|
|
await bob.agent.post(`/friends/requests/${alice.id}/accept`).expect(204);
|
|
|
|
const aliceSide = await overviewOf(alice);
|
|
expect(aliceSide.friends.map((friend) => friend.id)).toEqual([bob.id]);
|
|
expect(aliceSide.outgoing).toEqual([]);
|
|
const bobSide = await overviewOf(bob);
|
|
expect(bobSide.friends.map((friend) => friend.id)).toEqual([alice.id]);
|
|
expect(bobSide.incoming).toEqual([]);
|
|
|
|
await alice.agent
|
|
.post('/friends/requests')
|
|
.send({ username: bob.username })
|
|
.expect(409);
|
|
});
|
|
|
|
it('accepts straight away when both ask each other', async () => {
|
|
await carol.agent
|
|
.post('/friends/requests')
|
|
.send({ username: alice.username })
|
|
.expect(201);
|
|
const response = await alice.agent
|
|
.post('/friends/requests')
|
|
.send({ username: carol.username })
|
|
.expect(201);
|
|
expect(response.body.status).toBe('accepted');
|
|
expect((await overviewOf(carol)).friends.map((f) => f.id)).toEqual([
|
|
alice.id,
|
|
]);
|
|
});
|
|
|
|
it('removes a friend for both', async () => {
|
|
await alice.agent.delete(`/friends/${carol.id}`).expect(204);
|
|
expect((await overviewOf(carol)).friends).toEqual([]);
|
|
expect((await overviewOf(alice)).friends.map((f) => f.id)).toEqual([
|
|
bob.id,
|
|
]);
|
|
await alice.agent.delete(`/friends/${carol.id}`).expect(404);
|
|
});
|
|
});
|
|
|
|
describe('direct conversations', () => {
|
|
let conversation: Conversation;
|
|
|
|
it("doesn't open a chat with someone who isn't a friend", async () => {
|
|
await carol.agent
|
|
.post('/conversations/direct')
|
|
.send({ userId: alice.id })
|
|
.expect(403);
|
|
await alice.agent
|
|
.post('/conversations/direct')
|
|
.send({ userId: alice.id })
|
|
.expect(400);
|
|
});
|
|
|
|
it('opens one chat per pair of friends', async () => {
|
|
const first = await alice.agent
|
|
.post('/conversations/direct')
|
|
.send({ userId: bob.id })
|
|
.expect(200);
|
|
conversation = first.body;
|
|
expect(conversation.type).toBe('direct');
|
|
expect(conversation.lastMessage).toBeNull();
|
|
expect(conversation.members.map((member) => member.id).sort()).toEqual(
|
|
[alice.id, bob.id].sort(),
|
|
);
|
|
|
|
const again = await bob.agent
|
|
.post('/conversations/direct')
|
|
.send({ userId: alice.id })
|
|
.expect(200);
|
|
expect(again.body.id).toBe(conversation.id);
|
|
});
|
|
|
|
it('posts messages and pages through them, newest first', async () => {
|
|
const sent: Message[] = [];
|
|
for (const content of ['one', ' two ', 'three']) {
|
|
const author = sent.length === 1 ? bob : alice;
|
|
const response = await author.agent
|
|
.post(`/conversations/${conversation.id}/messages`)
|
|
.send({ content })
|
|
.expect(201);
|
|
sent.push(response.body);
|
|
}
|
|
expect(sent[1]).toMatchObject({
|
|
content: 'two',
|
|
author: { id: bob.id, name: 'Bob' },
|
|
});
|
|
|
|
const newest = await bob.agent
|
|
.get(`/conversations/${conversation.id}/messages?limit=2`)
|
|
.expect(200);
|
|
const firstPage: MessagePage = newest.body;
|
|
expect(firstPage.items.map((message) => message.content)).toEqual([
|
|
'three',
|
|
'two',
|
|
]);
|
|
expect(firstPage.nextCursor).toBe(sent[1].id);
|
|
|
|
const older = await bob.agent
|
|
.get(
|
|
`/conversations/${conversation.id}/messages?limit=2&before=${firstPage.nextCursor}`,
|
|
)
|
|
.expect(200);
|
|
expect(older.body).toEqual({ items: [sent[0]], nextCursor: null });
|
|
});
|
|
|
|
it('lists the conversation with its latest message', async () => {
|
|
const response = await bob.agent.get('/conversations').expect(200);
|
|
expect(response.body).toHaveLength(1);
|
|
expect(response.body[0]).toMatchObject({
|
|
id: conversation.id,
|
|
lastMessage: { content: 'three', author: { id: alice.id } },
|
|
});
|
|
});
|
|
|
|
it('rejects an empty or oversized message', async () => {
|
|
const path = `/conversations/${conversation.id}/messages`;
|
|
await alice.agent.post(path).send({ content: ' ' }).expect(400);
|
|
await alice.agent
|
|
.post(path)
|
|
.send({ content: 'x'.repeat(4001) })
|
|
.expect(400);
|
|
await alice.agent.get(`${path}?limit=0`).expect(400);
|
|
await alice.agent.get(`${path}?before=not-an-id`).expect(400);
|
|
});
|
|
|
|
it('hides the conversation from people who are not in it', async () => {
|
|
await carol.agent.get(`/conversations/${conversation.id}`).expect(404);
|
|
await carol.agent
|
|
.get(`/conversations/${conversation.id}/messages`)
|
|
.expect(404);
|
|
await carol.agent
|
|
.post(`/conversations/${conversation.id}/messages`)
|
|
.send({ content: 'let me in' })
|
|
.expect(404);
|
|
expect(
|
|
(await carol.agent.get('/conversations').expect(200)).body,
|
|
).toEqual([]);
|
|
});
|
|
|
|
describe('realtime', () => {
|
|
it('refuses a socket without a session', async () => {
|
|
await expect(connected(sockets.connect())).rejects.toThrow(
|
|
'Unauthorized',
|
|
);
|
|
});
|
|
|
|
it('refuses a socket opened from another site', async () => {
|
|
const socket = sockets.connect(bob.cookie, {
|
|
origin: 'https://evil.example',
|
|
});
|
|
await expect(connected(socket)).rejects.toThrow('Unauthorized');
|
|
});
|
|
|
|
it('pushes a new message to both members, and only to them', async () => {
|
|
const aliceSocket = sockets.connect(alice.cookie);
|
|
const bobSocket = sockets.connect(bob.cookie);
|
|
const carolSocket = sockets.connect(carol.cookie);
|
|
await Promise.all([aliceSocket, bobSocket, carolSocket].map(connected));
|
|
let carolGotIt = false;
|
|
carolSocket.on('message:created', () => {
|
|
carolGotIt = true;
|
|
});
|
|
|
|
const forAlice = nextEvent(aliceSocket, 'message:created');
|
|
const forBob = nextEvent(bobSocket, 'message:created');
|
|
const response = await alice.agent
|
|
.post(`/conversations/${conversation.id}/messages`)
|
|
.send({ content: 'live!' })
|
|
.expect(201);
|
|
|
|
expect(await forBob).toEqual(response.body);
|
|
expect(await forAlice).toEqual(response.body);
|
|
expect(carolGotIt).toBe(false);
|
|
});
|
|
|
|
it('tells both people when a friend request arrives', async () => {
|
|
const bobSocket = sockets.connect(bob.cookie);
|
|
const carolSocket = sockets.connect(carol.cookie);
|
|
await Promise.all([bobSocket, carolSocket].map(connected));
|
|
|
|
const forBob = nextEvent(bobSocket, 'friends:changed');
|
|
const forCarol = nextEvent(carolSocket, 'friends:changed');
|
|
await carol.agent
|
|
.post('/friends/requests')
|
|
.send({ username: bob.username })
|
|
.expect(201);
|
|
await Promise.all([forBob, forCarol]);
|
|
});
|
|
|
|
it('closes the socket when its session signs out', async () => {
|
|
const dave = await accounts.signUp('Dave');
|
|
const socket = sockets.connect(dave.cookie);
|
|
await connected(socket);
|
|
|
|
const disconnected = new Promise<string>((resolve) => {
|
|
socket.once('disconnect', resolve);
|
|
});
|
|
await accounts.signOut(dave).expect(200);
|
|
expect(await disconnected).toBe('io server disconnect');
|
|
});
|
|
});
|
|
|
|
it('keeps the history but stops new messages after unfriending', async () => {
|
|
await bob.agent.delete(`/friends/${alice.id}`).expect(204);
|
|
|
|
await alice.agent
|
|
.post(`/conversations/${conversation.id}/messages`)
|
|
.send({ content: 'still there?' })
|
|
.expect(403);
|
|
const history = await alice.agent
|
|
.get(`/conversations/${conversation.id}/messages`)
|
|
.expect(200);
|
|
expect(history.body.items).toHaveLength(4);
|
|
|
|
// Befriending again brings the same conversation back to life.
|
|
await befriend(alice, bob);
|
|
await alice.agent
|
|
.post(`/conversations/${conversation.id}/messages`)
|
|
.send({ content: 'back' })
|
|
.expect(201);
|
|
});
|
|
});
|
|
});
|