From e96b9426d1c3353175c97f309c592d082b1e0b6c Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 14 Jun 2023 15:38:05 +0200 Subject: [PATCH] test roomms resolver --- backend/src/graphql/rooms.ts | 28 +++ backend/src/schema/resolvers/rooms.spec.ts | 198 +++++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 backend/src/graphql/rooms.ts create mode 100644 backend/src/schema/resolvers/rooms.spec.ts diff --git a/backend/src/graphql/rooms.ts b/backend/src/graphql/rooms.ts new file mode 100644 index 000000000..38d10a1d8 --- /dev/null +++ b/backend/src/graphql/rooms.ts @@ -0,0 +1,28 @@ +import gql from 'graphql-tag' + +export const createRoomMutation = () => { + return gql` + mutation ( + $userId: ID! + ) { + CreateRoom( + userId: $userId + ) { + id + } + } + ` +} + +export const roomQuery = () => { + return gql` + query { + Room { + id + users { + id + } + } + } + ` +} diff --git a/backend/src/schema/resolvers/rooms.spec.ts b/backend/src/schema/resolvers/rooms.spec.ts new file mode 100644 index 000000000..26c95920b --- /dev/null +++ b/backend/src/schema/resolvers/rooms.spec.ts @@ -0,0 +1,198 @@ +import { createTestClient } from 'apollo-server-testing' +import Factory, { cleanDatabase } from '../../db/factories' +import { getNeode, getDriver } from '../../db/neo4j' +import { createRoomMutation, roomQuery } from '../../graphql/rooms' +import createServer from '../../server' + +const driver = getDriver() +const neode = getNeode() + +let query +let mutate +let authenticatedUser +let chattingUser, otherChattingUser, notChattingUser + +beforeAll(async () => { + await cleanDatabase() + + const { server } = createServer({ + context: () => { + return { + driver, + neode, + user: authenticatedUser, + } + }, + }) + query = createTestClient(server).query + mutate = createTestClient(server).mutate +}) + +afterAll(async () => { + // await cleanDatabase() + driver.close() +}) + +describe('Room', () => { + beforeAll(async () => { + ;[chattingUser, otherChattingUser, notChattingUser] = await Promise.all([ + Factory.build( + 'user', + { + id: 'chatting-user', + name: 'Chatting User', + }, + ), + Factory.build( + 'user', + { + id: 'other-chatting-user', + name: 'Other Chatting User', + }, + ), + Factory.build( + 'user', + { + id: 'not-chatting-user', + name: 'Not Chatting User', + }, + ), + ]) + }) + + describe('create room', () => { + describe('unauthenticated', () => { + it('throws authorization error', async () => { + await expect(mutate({ mutation: createRoomMutation(), variables: { + userId: 'some-id' } })).resolves.toMatchObject({ + errors: [{ message: 'Not Authorized!' }], + }) + }) + }) + + describe('authenticated', () => { + beforeAll(async () => { + authenticatedUser = await chattingUser.toJson() + }) + + describe('user id does not exist', () => { + it('returns null', async () => { + await expect(mutate({ + mutation: createRoomMutation(), + variables: { + userId: 'not-existing-user', + }, + })).resolves.toMatchObject({ + errors: undefined, + data: { + CreateRoom: null, + }, + }) + }) + }) + + describe('user id exists', () => { + it('returns the id of the room', async () => { + await expect(mutate({ + mutation: createRoomMutation(), + variables: { + userId: 'other-chatting-user', + }, + })).resolves.toMatchObject({ + errors: undefined, + data: { + CreateRoom: { + id: expect.any(String), + }, + }, + }) + }) + }) + }) + }) + + describe('query room', () => { + describe('unauthenticated', () => { + beforeAll(() => { + authenticatedUser = null + }) + + it('throws authorization error', async () => { + await expect(query({ query: roomQuery() })).resolves.toMatchObject({ + errors: [{ message: 'Not Authorized!' }], + }) + }) + }) + + describe('authenticated', () => { + describe('as creater of room', () => { + beforeAll(async () => { + authenticatedUser = await chattingUser.toJson() + }) + + it('returns the room', async () => { + await expect(query({ query: roomQuery() })).resolves.toMatchObject({ + errors: undefined, + data: { + Room: [ + { + id: expect.any(String), + users: expect.arrayContaining([ + { + id: 'chatting-user', + }, + { + id: 'other-chatting-user', + }, + ]), + }, + ], + }, + }) + }) + }) + + describe('as chatter of room', () => { + beforeAll(async () => { + authenticatedUser = await otherChattingUser.toJson() + }) + + it('returns the room', async () => { + await expect(query({ query: roomQuery() })).resolves.toMatchObject({ + errors: undefined, + data: { + Room: [ + { + id: expect.any(String), + users: expect.arrayContaining([ + { + id: 'chatting-user', + }, + { + id: 'other-chatting-user', + }, + ]), + }, + ], + }, + }) + }) + }) + + describe('as not chatter of room', () => { + beforeAll(async () => { + authenticatedUser = await notChattingUser.toJson() + }) + + it('returns no rooms', async () => { + await expect(query({ query: roomQuery() })).resolves.toMatchObject({ + errors: undefined, + data: { + Room: [], + }, + }) + }) + }) + }) + }) +})