diff --git a/backend/src/resolvers/comments.js b/backend/src/resolvers/comments.js index 97b99f4ab..b2dc0fbcf 100644 --- a/backend/src/resolvers/comments.js +++ b/backend/src/resolvers/comments.js @@ -44,6 +44,16 @@ export default { commentId: comment.id } ) + + await session.run(` + MATCH (comment:Comment {id: $commentId}), (author:User {id: $userId}) + MERGE (comment)<-[:WROTE]-(author) + RETURN comment {.id, .content}`, { + commentId: comment.id, + userId: context.user.id + } + ) + session.close() return comment diff --git a/backend/src/resolvers/comments.spec.js b/backend/src/resolvers/comments.spec.js index 34a18d807..e572e206b 100644 --- a/backend/src/resolvers/comments.spec.js +++ b/backend/src/resolvers/comments.spec.js @@ -19,12 +19,12 @@ afterEach(async () => { describe('CreateComment', () => { const mutation = ` - mutation($postId: ID, $content: String!) { - CreateComment(postId: $postId, content: $content) { - id - content - } + mutation($postId: ID, $content: String!) { + CreateComment(postId: $postId, content: $content) { + id + content } + } ` describe('unauthenticated', () => { it('throws authorization error', async () => { @@ -58,6 +58,24 @@ describe('CreateComment', () => { await expect(client.request(mutation, variables)).resolves.toMatchObject(expected) }) + it('assigns the authenticated user as author', async () => { + variables = { + postId: 'p1', + content: 'I\'m authorised to comment' + } + await client.request(mutation, variables) + + const { User } = await client.request(`{ + User(email: "test@example.org") { + comments { + content + } + } + }`) + + expect(User).toEqual([ { comments: [ { content: 'I\'m authorised to comment' } ] } ]) + }) + it('throw an error if an empty string is sent as content', async () => { variables = { postId: 'p1', diff --git a/backend/src/seed/seed-db.js b/backend/src/seed/seed-db.js index f17c20315..8694a7948 100644 --- a/backend/src/seed/seed-db.js +++ b/backend/src/seed/seed-db.js @@ -189,33 +189,18 @@ import Factory from './factories' ]) await Promise.all([ - f.create('Comment', { id: 'c1', postId: 'p1' }), - f.create('Comment', { id: 'c2', postId: 'p1' }), - f.create('Comment', { id: 'c3', postId: 'p3' }), - f.create('Comment', { id: 'c4', postId: 'p2' }), - f.create('Comment', { id: 'c5', postId: 'p3' }), - f.create('Comment', { id: 'c6', postId: 'p4' }), - f.create('Comment', { id: 'c7', postId: 'p2' }), - f.create('Comment', { id: 'c8', postId: 'p15' }), - f.create('Comment', { id: 'c9', postId: 'p15' }), - f.create('Comment', { id: 'c10', postId: 'p15' }), - f.create('Comment', { id: 'c11', postId: 'p15' }), - f.create('Comment', { id: 'c12', postId: 'p15' }) - ]) - - await Promise.all([ - f.relate('Comment', 'Author', { from: 'u3', to: 'c1' }), - f.relate('Comment', 'Author', { from: 'u1', to: 'c2' }), - f.relate('Comment', 'Author', { from: 'u1', to: 'c3' }), - f.relate('Comment', 'Author', { from: 'u4', to: 'c4' }), - f.relate('Comment', 'Author', { from: 'u4', to: 'c5' }), - f.relate('Comment', 'Author', { from: 'u3', to: 'c6' }), - f.relate('Comment', 'Author', { from: 'u2', to: 'c7' }), - f.relate('Comment', 'Author', { from: 'u5', to: 'c8' }), - f.relate('Comment', 'Author', { from: 'u6', to: 'c9' }), - f.relate('Comment', 'Author', { from: 'u7', to: 'c10' }), - f.relate('Comment', 'Author', { from: 'u5', to: 'c11' }), - f.relate('Comment', 'Author', { from: 'u6', to: 'c12' }) + asUser.create('Comment', { id: 'c1', postId: 'p1' }), + asTick.create('Comment', { id: 'c2', postId: 'p1' }), + asTrack.create('Comment', { id: 'c3', postId: 'p3' }), + asTrick.create('Comment', { id: 'c4', postId: 'p2' }), + asModerator.create('Comment', { id: 'c5', postId: 'p3' }), + asAdmin.create('Comment', { id: 'c6', postId: 'p4' }), + asUser.create('Comment', { id: 'c7', postId: 'p2' }), + asTick.create('Comment', { id: 'c8', postId: 'p15' }), + asTrick.create('Comment', { id: 'c9', postId: 'p15' }), + asTrack.create('Comment', { id: 'c10', postId: 'p15' }), + asUser.create('Comment', { id: 'c11', postId: 'p15' }), + asUser.create('Comment', { id: 'c12', postId: 'p15' }) ]) const disableMutation = 'mutation($id: ID!) { disable(id: $id) }'