add backend managecomments tests

This commit is contained in:
ALau2088 2019-06-13 13:00:20 -07:00
parent d91221fe7e
commit 3cc05c0916
4 changed files with 147 additions and 72 deletions

View File

@ -113,6 +113,7 @@ const permissions = shield({
enable: isModerator, enable: isModerator,
disable: isModerator, disable: isModerator,
CreateComment: isAuthenticated, CreateComment: isAuthenticated,
UpdateComment: isAuthor,
DeleteComment: isAuthor, DeleteComment: isAuthor,
// CreateUser: allow, // CreateUser: allow,
}, },

View File

@ -22,17 +22,12 @@ const validateUrl = async (resolve, root, args, context, info) => {
} }
} }
const validateComment = async (resolve, root, args, context, info) => { const validateUpdateComment = async (resolve, root, args, context, info) => {
const COMMENT_MIN_LENGTH = 1 const COMMENT_MIN_LENGTH = 1
const content = args.content.replace(/<(?:.|\n)*?>/gm, '').trim() const content = args.content.replace(/<(?:.|\n)*?>/gm, '').trim()
if (!args.content || content.length < COMMENT_MIN_LENGTH) { if (!args.content || content.length < COMMENT_MIN_LENGTH) {
throw new UserInputError(`Comment must be at least ${COMMENT_MIN_LENGTH} character long!`) throw new UserInputError(`Comment must be at least ${COMMENT_MIN_LENGTH} character long!`)
} }
const NO_POST_ERR_MESSAGE = 'Comment cannot be created without a post!'
const { postId } = args
if (!postId) {
throw new UserInputError(NO_POST_ERR_MESSAGE)
}
/* eslint-disable-next-line no-return-await */ /* eslint-disable-next-line no-return-await */
return await resolve(root, args, context, info) return await resolve(root, args, context, info)
} }
@ -42,7 +37,6 @@ export default {
CreateUser: validateUsername, CreateUser: validateUsername,
UpdateUser: validateUsername, UpdateUser: validateUsername,
CreateSocialMedia: validateUrl, CreateSocialMedia: validateUrl,
CreateComment: validateComment, UpdateComment: validateUpdateComment,
UpdateComment: validateComment,
}, },
} }

View File

@ -1,11 +1,13 @@
import { neo4jgraphql } from 'neo4j-graphql-js' import { neo4jgraphql } from 'neo4j-graphql-js'
import { UserInputError } from 'apollo-server' import { UserInputError } from 'apollo-server'
const COMMENT_MIN_LENGTH = 1
const NO_POST_ERR_MESSAGE = 'Comment cannot be created without a post!' const NO_POST_ERR_MESSAGE = 'Comment cannot be created without a post!'
export default { export default {
Mutation: { Mutation: {
CreateComment: async (object, params, context, resolveInfo) => { CreateComment: async (object, params, context, resolveInfo) => {
const content = params.content.replace(/<(?:.|\n)*?>/gm, '').trim()
const { postId } = params const { postId } = params
// Adding relationship from comment to post by passing in the postId, // Adding relationship from comment to post by passing in the postId,
// but we do not want to create the comment with postId as an attribute // but we do not want to create the comment with postId as an attribute
@ -13,6 +15,13 @@ export default {
// before comment creation. // before comment creation.
delete params.postId delete params.postId
if (!params.content || content.length < COMMENT_MIN_LENGTH) {
throw new UserInputError(`Comment must be at least ${COMMENT_MIN_LENGTH} character long!`)
}
if (!postId.trim()) {
throw new UserInputError(NO_POST_ERR_MESSAGE)
}
const session = context.driver.session() const session = context.driver.session()
const postQueryRes = await session.run( const postQueryRes = await session.run(
` `

View File

@ -9,9 +9,10 @@ let createCommentVariables
let createPostVariables let createPostVariables
let createCommentVariablesSansPostId let createCommentVariablesSansPostId
let createCommentVariablesWithNonExistentPost let createCommentVariablesWithNonExistentPost
let asAuthor
beforeEach(async () => { beforeEach(async () => {
await factory.create('User', { asAuthor = await factory.create('User', {
email: 'test@example.org', email: 'test@example.org',
password: '1234', password: '1234',
}) })
@ -211,22 +212,9 @@ describe('CreateComment', () => {
}) })
}) })
describe('DeleteComment', () => { describe('ManageComments', () => {
const deleteCommentMutation = gql`
mutation($id: ID!) {
DeleteComment(id: $id) {
id
}
}
`
let deleteCommentVariables = {
id: 'c1',
}
beforeEach(async () => { beforeEach(async () => {
const asAuthor = Factory() asAuthor = await factory.create('User', {
await asAuthor.create('User', {
email: 'author@example.org', email: 'author@example.org',
password: '1234', password: '1234',
}) })
@ -245,55 +233,138 @@ describe('DeleteComment', () => {
}) })
}) })
describe('unauthenticated', () => { describe('UpdateComment', () => {
it('throws authorization error', async () => { const updateCommentMutation = gql`
client = new GraphQLClient(host) mutation($content: String!, $id: ID!) {
await expect(client.request(deleteCommentMutation, deleteCommentVariables)).rejects.toThrow( UpdateComment(content: $content, id: $id) {
'Not Authorised', id
) content
}) }
})
describe('authenticated but not the author', () => {
beforeEach(async () => {
let headers
headers = await login({
email: 'test@example.org',
password: '1234',
})
client = new GraphQLClient(host, {
headers,
})
})
it('throws authorization error', async () => {
await expect(client.request(deleteCommentMutation, deleteCommentVariables)).rejects.toThrow(
'Not Authorised',
)
})
})
describe('authenticated as author', () => {
beforeEach(async () => {
let headers
headers = await login({
email: 'author@example.org',
password: '1234',
})
client = new GraphQLClient(host, {
headers,
})
})
it('deletes the comment', async () => {
const expected = {
DeleteComment: {
id: 'c1',
},
} }
await expect(client.request(deleteCommentMutation, deleteCommentVariables)).resolves.toEqual( `
expected,
) let updateCommentVariables = {
id: 'c1',
content: 'The comment is updated',
}
describe('unauthenticated', () => {
it('throws authorization error', async () => {
client = new GraphQLClient(host)
await expect(client.request(updateCommentMutation, updateCommentVariables)).rejects.toThrow(
'Not Authorised',
)
})
})
describe('authenticated but not the author', () => {
beforeEach(async () => {
let headers
headers = await login({
email: 'test@example.org',
password: '1234',
})
client = new GraphQLClient(host, {
headers,
})
})
it('throws authorization error', async () => {
await expect(client.request(updateCommentMutation, updateCommentVariables)).rejects.toThrow(
'Not Authorised',
)
})
})
describe('authenticated as author', () => {
beforeEach(async () => {
let headers
headers = await login({
email: 'author@example.org',
password: '1234',
})
client = new GraphQLClient(host, {
headers,
})
})
it('updates the comment', async () => {
const expected = {
UpdateComment: {
id: 'c1',
content: 'The comment is updated',
},
}
await expect(
client.request(updateCommentMutation, updateCommentVariables),
).resolves.toEqual(expected)
})
})
})
describe('DeleteComment', () => {
const deleteCommentMutation = gql`
mutation($id: ID!) {
DeleteComment(id: $id) {
id
}
}
`
let deleteCommentVariables = {
id: 'c1',
}
describe('unauthenticated', () => {
it('throws authorization error', async () => {
client = new GraphQLClient(host)
await expect(client.request(deleteCommentMutation, deleteCommentVariables)).rejects.toThrow(
'Not Authorised',
)
})
})
describe('authenticated but not the author', () => {
beforeEach(async () => {
let headers
headers = await login({
email: 'test@example.org',
password: '1234',
})
client = new GraphQLClient(host, {
headers,
})
})
it('throws authorization error', async () => {
await expect(client.request(deleteCommentMutation, deleteCommentVariables)).rejects.toThrow(
'Not Authorised',
)
})
})
describe('authenticated as author', () => {
beforeEach(async () => {
let headers
headers = await login({
email: 'author@example.org',
password: '1234',
})
client = new GraphQLClient(host, {
headers,
})
})
it('deletes the comment', async () => {
const expected = {
DeleteComment: {
id: 'c1',
},
}
await expect(
client.request(deleteCommentMutation, deleteCommentVariables),
).resolves.toEqual(expected)
})
}) })
}) })
}) })