diff --git a/backend/src/resolvers/comments.js b/backend/src/resolvers/comments.js index a4b3c21b1..7f3b11da4 100644 --- a/backend/src/resolvers/comments.js +++ b/backend/src/resolvers/comments.js @@ -56,7 +56,7 @@ export default { return comment }, DeleteComment: async (object, params, context, resolveInfo) => { - const socialMedia = await neo4jgraphql(object, params, context, resolveInfo, false) + const comment = await neo4jgraphql(object, params, context, resolveInfo, false) return comment }, diff --git a/backend/src/resolvers/comments.spec.js b/backend/src/resolvers/comments.spec.js index 4d86d2d58..e3bf00528 100644 --- a/backend/src/resolvers/comments.spec.js +++ b/backend/src/resolvers/comments.spec.js @@ -95,13 +95,15 @@ describe('CreateComment', () => { it('assigns the authenticated user as author', async () => { await client.request(createCommentMutation, createCommentVariables) - const { User } = await client.request(`{ + const { User } = await client.request(gql` + { User(email: "test@example.org") { comments { content } } - }`) + } + `) expect(User).toEqual([ { @@ -210,195 +212,83 @@ describe('CreateComment', () => { }) }) -// describe('DeleteComment', () => { -// const createCommentMutation = gql ` -// mutation($postId: ID, $content: String!) { -// CreateComment(postId: $postId, content: $content) { -// id -// content -// } -// } -// ` -// const deleteCommentMutation = gql ` -// mutation($id: ID!) { -// DeleteComment(id: $id) { -// id -// content -// } -// } -// ` -// const createPostMutation = gql ` -// mutation($id: ID!, $title: String!, $content: String!) { -// CreatePost(id: $id, title: $title, content: $content) { -// id -// } -// } -// ` -// const commentQueryForPostId = gql ` -// query($content: String) { -// Comment(content: $content) { -// postId -// } -// } -// ` -// describe('unauthenticated', () => { -// it('throws authorization error', async () => { -// deleteCommentVariables = { -// id: 'c1', -// } -// client = new GraphQLClient(host) -// await expect(client.request(deleteCommentMutation, deleteCommentVariables)).rejects.toThrow( -// 'Not Authorised', -// ) -// }) -// }) +describe('DeleteComment', () => { + const createCommentMutation = gql` + mutation($postId: ID, $content: String!) { + CreateComment(postId: $postId, content: $content) { + id + content + } + } + ` + const deleteCommentMutation = gql` + mutation($id: ID!) { + DeleteComment(id: $id) { + id + } + } + ` + const createPostMutation = gql` + mutation($id: ID!, $title: String!, $content: String!) { + CreatePost(id: $id, title: $title, content: $content) { + id + } + } + ` + describe('unauthenticated', () => { + it('throws authorization error', async () => { + deleteCommentVariables = { + id: 'c1', + } + client = new GraphQLClient(host) + await expect(client.request(deleteCommentMutation, deleteCommentVariables)).rejects.toThrow( + 'Not Authorised', + ) + }) + }) -// // describe('authenticated', () => { -// // let headers -// // beforeEach(async () => { -// // headers = await login({ -// // email: 'test@example.org', -// // password: '1234' -// // }) -// // client = new GraphQLClient(host, { -// // headers -// // }) -// // createCommentVariables = { -// // postId: 'p1', -// // content: "I'm authorised to comment", -// // } -// // createPostVariables = { -// // id: 'p1', -// // title: 'post to comment on', -// // content: 'please comment on me', -// // } -// // await client.request(createPostMutation, createPostVariables) -// // }) + describe('authenticated', () => { + let headers + beforeEach(async () => { + headers = await login({ + email: 'test@example.org', + password: '1234', + }) + client = new GraphQLClient(host, { + headers, + }) + createCommentVariables = { + id: 'c1', + postId: 'p1', + content: "I'm authorised to comment", + } + deleteCommentVariables = { + id: 'c1', + } + createPostVariables = { + id: 'p1', + title: 'post to comment on', + content: 'please comment on me', + } + await client.request(createPostMutation, createPostVariables) + }) -// // it('creates a comment', async () => { -// // const expected = { -// // CreateComment: { -// // content: "I'm authorised to comment", -// // }, -// // } + it('deletes the authors comment', async () => { + const { CreateComment } = await client.request(createCommentMutation, createCommentVariables) -// // await expect( -// // client.request(createCommentMutation, createCommentVariables), -// // ).resolves.toMatchObject(expected) -// // }) + deleteCommentVariables = { + id: CreateComment.id, + } + const expected = { + DeleteComment: { + id: CreateComment.id, + }, + } + await expect( + client.request(deleteCommentMutation, deleteCommentVariables), + ).resolves.toMatchObject(expected) + }) -// // it('assigns the authenticated user as author', async () => { -// // await client.request(createCommentMutation, createCommentVariables) - -// // 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 from the editor as content', async () => { -// // createCommentVariables = { -// // postId: 'p1', -// // content: '
', -// // } - -// // await expect(client.request(createCommentMutation, createCommentVariables)).rejects.toThrow( -// // 'Comment must be at least 1 character long!', -// // ) -// // }) - -// // it('throws an error if a comment sent from the editor does not contain a single character', async () => { -// // createCommentVariables = { -// // postId: 'p1', -// // content: '', -// // } - -// // await expect(client.request(createCommentMutation, createCommentVariables)).rejects.toThrow( -// // 'Comment must be at least 1 character long!', -// // ) -// // }) - -// // it('throws an error if postId is sent as an empty string', async () => { -// // createCommentVariables = { -// // postId: 'p1', -// // content: '', -// // } - -// // await expect(client.request(createCommentMutation, createCommentVariables)).rejects.toThrow( -// // 'Comment must be at least 1 character long!', -// // ) -// // }) - -// // it('throws an error if content is sent as an string of empty characters', async () => { -// // createCommentVariables = { -// // postId: 'p1', -// // content: ' ', -// // } - -// // await expect(client.request(createCommentMutation, createCommentVariables)).rejects.toThrow( -// // 'Comment must be at least 1 character long!', -// // ) -// // }) - -// // it('throws an error if postId is sent as an empty string', async () => { -// // createCommentVariablesSansPostId = { -// // postId: '', -// // content: 'this comment should not be created', -// // } - -// // await expect( -// // client.request(createCommentMutation, createCommentVariablesSansPostId), -// // ).rejects.toThrow('Comment cannot be created without a post!') -// // }) - -// // it('throws an error if postId is sent as an string of empty characters', async () => { -// // createCommentVariablesSansPostId = { -// // postId: ' ', -// // content: 'this comment should not be created', -// // } - -// // await expect( -// // client.request(createCommentMutation, createCommentVariablesSansPostId), -// // ).rejects.toThrow('Comment cannot be created without a post!') -// // }) - -// // it('throws an error if the post does not exist in the database', async () => { -// // createCommentVariablesWithNonExistentPost = { -// // postId: 'p2', -// // content: "comment should not be created cause the post doesn't exist", -// // } - -// // await expect( -// // client.request(createCommentMutation, createCommentVariablesWithNonExistentPost), -// // ).rejects.toThrow('Comment cannot be created without a post!') -// // }) - -// // it('does not create the comment with the postId as an attribute', async () => { -// // const commentQueryVariablesByContent = { -// // content: "I'm authorised to comment", -// // } - -// // await client.request(createCommentMutation, createCommentVariables) -// // const { -// // Comment -// // } = await client.request( -// // commentQueryForPostId, -// // commentQueryVariablesByContent, -// // ) -// // expect(Comment).toEqual([{ -// // postId: null -// // }]) -// // }) -// // }) -// }) + it.todo('throws an error if it tries to delete a comment not from this author') + }) +}) diff --git a/backend/src/resolvers/socialMedia.spec.js b/backend/src/resolvers/socialMedia.spec.js index 5143587b1..4e298e783 100644 --- a/backend/src/resolvers/socialMedia.spec.js +++ b/backend/src/resolvers/socialMedia.spec.js @@ -1,13 +1,14 @@ +import gql from 'graphql-tag' import Factory from '../seed/factories' import { GraphQLClient } from 'graphql-request' import { host, login } from '../jest/helpers' const factory = Factory() -describe('CreateSocialMedia', () => { +describe('SocialMedia', () => { let client let headers - const mutationC = ` + const mutationC = gql` mutation($url: String!) { CreateSocialMedia(url: $url) { id @@ -15,7 +16,7 @@ describe('CreateSocialMedia', () => { } } ` - const mutationD = ` + const mutationD = gql` mutation($id: ID!) { DeleteSocialMedia(id: $id) { id @@ -42,19 +43,28 @@ describe('CreateSocialMedia', () => { describe('unauthenticated', () => { it('throws authorization error', async () => { client = new GraphQLClient(host) - const variables = { url: 'http://nsosp.org' } + const variables = { + url: 'http://nsosp.org', + } await expect(client.request(mutationC, variables)).rejects.toThrow('Not Authorised') }) }) describe('authenticated', () => { beforeEach(async () => { - headers = await login({ email: 'test@example.org', password: '1234' }) - client = new GraphQLClient(host, { headers }) + headers = await login({ + email: 'test@example.org', + password: '1234', + }) + client = new GraphQLClient(host, { + headers, + }) }) it('creates social media with correct URL', async () => { - const variables = { url: 'http://nsosp.org' } + const variables = { + url: 'http://nsosp.org', + } await expect(client.request(mutationC, variables)).resolves.toEqual( expect.objectContaining({ CreateSocialMedia: { @@ -66,11 +76,15 @@ describe('CreateSocialMedia', () => { }) it('deletes social media', async () => { - const creationVariables = { url: 'http://nsosp.org' } + const creationVariables = { + url: 'http://nsosp.org', + } const { CreateSocialMedia } = await client.request(mutationC, creationVariables) const { id } = CreateSocialMedia - const deletionVariables = { id } + const deletionVariables = { + id, + } const expected = { DeleteSocialMedia: { id: id, @@ -81,12 +95,16 @@ describe('CreateSocialMedia', () => { }) it('rejects empty string', async () => { - const variables = { url: '' } + const variables = { + url: '', + } await expect(client.request(mutationC, variables)).rejects.toThrow('Input is not a URL') }) it('validates URLs', async () => { - const variables = { url: 'not-a-url' } + const variables = { + url: 'not-a-url', + } await expect(client.request(mutationC, variables)).rejects.toThrow('Input is not a URL') }) }) diff --git a/webapp/components/Comment.vue b/webapp/components/Comment.vue index ce5433332..7c85192d8 100644 --- a/webapp/components/Comment.vue +++ b/webapp/components/Comment.vue @@ -74,7 +74,6 @@ export default { }, async deleteCommentCallback() { try { - // XXX Make custom mutation and tests in the Backend !!! var gqlMutation = gql` mutation($id: ID!) { DeleteComment(id: $id) { diff --git a/webapp/pages/index.vue b/webapp/pages/index.vue index 6d07388dc..42b0f3eb4 100644 --- a/webapp/pages/index.vue +++ b/webapp/pages/index.vue @@ -1,7 +1,10 @@
Hier findest du weitere infos zum Thema.