diff --git a/backend/src/schema/resolvers/messages.spec.ts b/backend/src/schema/resolvers/messages.spec.ts index b1fc0b208..e95d5d256 100644 --- a/backend/src/schema/resolvers/messages.spec.ts +++ b/backend/src/schema/resolvers/messages.spec.ts @@ -36,6 +36,8 @@ afterAll(async () => { describe('Message', () => { + let roomId: string + beforeAll(async () => { ;[chattingUser, otherChattingUser, notChattingUser] = await Promise.all([ Factory.build( @@ -88,6 +90,120 @@ describe('Message', () => { }) }) }) + + describe('room exists', () => { + beforeAll(async () => { + const room = await mutate({ + mutation: createRoomMutation(), + variables: { + userId: 'other-chatting-user', + }, + }) + roomId = room.data.CreateRoom.id + }) + + describe('user chats in room', () => { + it('returns the message', async () => { + await expect(mutate({ + mutation: createMessageMutation(), + variables: { + roomId, + content: 'Some nice message to other chatting user', + } })).resolves.toMatchObject({ + errors: undefined, + data: { + CreateMessage: { + id: expect.any(String), + content: 'Some nice message to other chatting user', + }, + }, + }) + }) + }) + + describe('user does not chat in room', () => { + beforeAll(async () => { + authenticatedUser = await notChattingUser.toJson() + }) + + it('returns null', async () => { + await expect(mutate({ + mutation: createMessageMutation(), + variables: { + roomId, + content: 'I have no access to this room!', + } })).resolves.toMatchObject({ + errors: undefined, + data: { + CreateMessage: null, + }, + }) + }) + }) + }) + }) + }) + + describe('message query', () => { + describe('unauthenticated', () => { + beforeAll(() => { + authenticatedUser = null + }) + + it('throws authorization error', async () => { + await expect(query({ + query: messageQuery(), + variables: { + roomId: 'some-id' } + })).resolves.toMatchObject({ + errors: [{ message: 'Not Authorized!' }], + }) + }) + }) + + describe('authenticated', () => { + beforeAll(async () => { + authenticatedUser = await otherChattingUser.toJson() + }) + + describe('room does not exists', () => { + it('returns null', async () => { + await expect(query({ + query: messageQuery(), + variables: { + roomId: 'some-id' + }, + })).resolves.toMatchObject({ + errors: undefined, + data: { + Message: [], + }, + }) + }) + }) + + describe('room exists with authenticated user chatting', () => { + it('returns null', async () => { + console.log(roomId) + await expect(query({ + query: messageQuery(), + variables: { + roomId, + }, + })).resolves.toMatchObject({ + errors: undefined, + data: { + Message: [{ + id: expect.any(String), + content: 'Some nice message to other chatting user', + author: { + id: 'chatting-user', + }, + }], + }, + }) + }) + }) }) }) }) diff --git a/backend/src/schema/resolvers/messages.ts b/backend/src/schema/resolvers/messages.ts index 44610fb83..ac090e78f 100644 --- a/backend/src/schema/resolvers/messages.ts +++ b/backend/src/schema/resolvers/messages.ts @@ -1,15 +1,22 @@ import { v4 as uuid } from 'uuid' import { neo4jgraphql } from 'neo4j-graphql-js' +import Resolver from './helpers/Resolver' export default { Query: { - Message: async (object, params, context, resolveInfo) => { - if (!params.filter) params.filter = {} + Message: async (object, params, context, resolveInfo) => { + console.log('message query', params) + const { roomId } = params + // if (!params.filter) params.filter = {} + /* params.filter.room = { + id_in: [roomId], users_some: { id: context.user.id, }, } + */ + console.log(params.filter) return neo4jgraphql(object, params, context, resolveInfo) }, }, @@ -47,4 +54,12 @@ export default { } }, }, + Message: { + ...Resolver('Message', { + hasOne: { + author: '<-[:CREATED]-(related:User)', + room: '-[:INSIDE]->(related:Room)', + } + }), + } } diff --git a/backend/src/schema/types/type/Room.gql b/backend/src/schema/types/type/Room.gql index b3b5ea913..0cff02a32 100644 --- a/backend/src/schema/types/type/Room.gql +++ b/backend/src/schema/types/type/Room.gql @@ -1,6 +1,8 @@ input _RoomFilter { AND: [_RoomFilter!] OR: [_RoomFilter!] + id: ID + id_in: [ID!] users_some: _UserFilter }