mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Add tests, create comments only when authenticated
This commit is contained in:
parent
dfef4fe05f
commit
9bc0c0f92c
@ -86,7 +86,8 @@ const permissions = shield({
|
|||||||
unshout: isAuthenticated,
|
unshout: isAuthenticated,
|
||||||
changePassword: isAuthenticated,
|
changePassword: isAuthenticated,
|
||||||
enable: isModerator,
|
enable: isModerator,
|
||||||
disable: isModerator
|
disable: isModerator,
|
||||||
|
CreateComment: isAuthenticated
|
||||||
// CreateUser: allow,
|
// CreateUser: allow,
|
||||||
},
|
},
|
||||||
User: {
|
User: {
|
||||||
|
|||||||
@ -5,7 +5,7 @@ export default {
|
|||||||
CreateComment: async (object, params, context, resolveInfo) => {
|
CreateComment: async (object, params, context, resolveInfo) => {
|
||||||
const { postId } = params
|
const { postId } = params
|
||||||
|
|
||||||
const result = await neo4jgraphql(object, params, context, resolveInfo, true)
|
const comment = await neo4jgraphql(object, params, context, resolveInfo, true)
|
||||||
|
|
||||||
const session = context.driver.session()
|
const session = context.driver.session()
|
||||||
const transactionRes = await session.run(`
|
const transactionRes = await session.run(`
|
||||||
@ -13,12 +13,9 @@ export default {
|
|||||||
MERGE (post)<-[:COMMENTS]-(comment)
|
MERGE (post)<-[:COMMENTS]-(comment)
|
||||||
RETURN comment {.id, .content}`, {
|
RETURN comment {.id, .content}`, {
|
||||||
postId,
|
postId,
|
||||||
commentId: result.id
|
commentId: comment.id
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
const [comment] = transactionRes.records.map(record => {
|
|
||||||
return record.get('comment')
|
|
||||||
})
|
|
||||||
|
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
|
|||||||
64
backend/src/resolvers/comments.spec.js
Normal file
64
backend/src/resolvers/comments.spec.js
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
import Factory from '../seed/factories'
|
||||||
|
import { GraphQLClient } from 'graphql-request'
|
||||||
|
import { host, login } from '../jest/helpers'
|
||||||
|
|
||||||
|
const factory = Factory()
|
||||||
|
let client
|
||||||
|
let variables
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
await factory.create('User', {
|
||||||
|
email: 'test@example.org',
|
||||||
|
password: '1234'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await factory.cleanDatabase()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('CreateComment', () => {
|
||||||
|
const mutation = `
|
||||||
|
mutation($id: ID!, $postId: ID!, $content: String!) {
|
||||||
|
CreateComment(id: $id, postId: $postId, content: $content) {
|
||||||
|
id
|
||||||
|
content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
describe('unauthenticated', () => {
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
variables = {
|
||||||
|
id: 'c1',
|
||||||
|
postId: 'p1',
|
||||||
|
content: "I'm not authorised to comment"
|
||||||
|
}
|
||||||
|
client = new GraphQLClient(host)
|
||||||
|
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated', () => {
|
||||||
|
let headers
|
||||||
|
beforeEach(async () => {
|
||||||
|
headers = await login({ email: 'test@example.org', password: '1234' })
|
||||||
|
client = new GraphQLClient(host, { headers })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('creates a post', async () => {
|
||||||
|
variables = {
|
||||||
|
id: 'c1',
|
||||||
|
postId: 'p1',
|
||||||
|
content: "I'm authorised to comment"
|
||||||
|
}
|
||||||
|
const expected = {
|
||||||
|
CreateComment: {
|
||||||
|
id: 'c1',
|
||||||
|
content: "I'm authorised to comment"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await expect(client.request(mutation, variables)).resolves.toMatchObject(expected)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
Loading…
x
Reference in New Issue
Block a user