Add authorship to commments at creation

- Co-authored-by: Mike Aono <aonomike@gmail.com>
This commit is contained in:
Matt Rider 2019-05-02 15:54:44 -03:00
parent c3cfe90aa2
commit 154082a251
3 changed files with 45 additions and 32 deletions

View File

@ -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

View File

@ -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',

View File

@ -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) }'