Refactor shout spec

- Test unauthorized user path
This commit is contained in:
aonomike 2019-09-24 23:36:52 +03:00
parent 869a93f36d
commit 33134c1d4f

View File

@ -1,13 +1,13 @@
import { GraphQLClient } from 'graphql-request'
import { createTestClient } from 'apollo-server-testing'
import Factory from '../../seed/factories'
import { host, login, gql } from '../../jest/helpers'
import { neode } from '../../bootstrap/neo4j'
import { gql } from '../../jest/helpers'
import { neode as getNeode, getDriver } from '../../bootstrap/neo4j'
import createServer from '../../server'
let clientUser1, clientUser2
let headersUser1, headersUser2
let mutate, query, authenticatedUser, variables
const factory = Factory()
const instance = neode()
const categoryIds = ['cat9']
const instance = getNeode()
const driver = getDriver()
const mutationShoutPost = gql`
mutation($id: ID!) {
@ -28,132 +28,34 @@ const createPostMutation = gql`
}
}
`
const createPostVariables = {
id: 'p1234',
title: 'Post Title 1234',
content: 'Some Post Content 1234',
categoryIds,
}
beforeEach(async () => {
await factory.create('User', {
id: 'u1',
email: 'test@example.org',
password: '1234',
describe('shout and unshout posts', () => {
beforeAll(() => {
authenticatedUser = undefined
const { server } = createServer({
context: () => {
return {
driver,
neode: instance,
user: authenticatedUser,
}
},
})
mutate = createTestClient(server).mutate
query = createTestClient(server).query
})
await factory.create('User', {
id: 'u2',
email: 'test2@example.org',
password: '1234',
})
await instance.create('Category', {
id: 'cat9',
name: 'Democracy & Politics',
icon: 'university',
})
headersUser1 = await login({ email: 'test@example.org', password: '1234' })
headersUser2 = await login({ email: 'test2@example.org', password: '1234' })
clientUser1 = new GraphQLClient(host, { headers: headersUser1 })
clientUser2 = new GraphQLClient(host, { headers: headersUser2 })
await clientUser1.request(createPostMutation, createPostVariables)
await clientUser2.request(createPostMutation, {
id: 'p12345',
title: 'Post Title 12345',
content: 'Some Post Content 12345',
categoryIds,
afterEach(() => {
factory.cleanDatabase()
})
})
afterEach(async () => {
await factory.cleanDatabase()
})
describe('shout', () => {
describe('shout foreign post', () => {
describe('unauthenticated shout', () => {
describe('shout', () => {
describe('unauthenticated', () => {
it('throws authorization error', async () => {
const client = new GraphQLClient(host)
await expect(client.request(mutationShoutPost, { id: 'p1234' })).rejects.toThrow(
'Not Authorised',
)
variables = { id: 'post-to-shout-id' }
await expect(mutate({ mutation: mutationShoutPost, variables })).resolves.toMatchObject({
errors: [{ message: 'Not Authorised!' }],
})
})
})
it('I shout a post of another user', async () => {
const res = await clientUser1.request(mutationShoutPost, { id: 'p12345' })
const expected = {
shout: true,
}
expect(res).toMatchObject(expected)
const { Post } = await clientUser1.request(gql`
query {
Post(id: "p12345") {
shoutedByCurrentUser
}
}
`)
const expected2 = {
shoutedByCurrentUser: true,
}
expect(Post[0]).toMatchObject(expected2)
})
it('I can`t shout my own post', async () => {
const res = await clientUser1.request(mutationShoutPost, { id: 'p1234' })
const expected = {
shout: false,
}
expect(res).toMatchObject(expected)
const { Post } = await clientUser1.request(gql`
query {
Post(id: "p1234") {
shoutedByCurrentUser
}
}
`)
const expected2 = {
shoutedByCurrentUser: false,
}
expect(Post[0]).toMatchObject(expected2)
})
})
describe('unshout foreign post', () => {
describe('unauthenticated shout', () => {
it('throws authorization error', async () => {
// shout
await clientUser1.request(mutationShoutPost, { id: 'p12345' })
// unshout
const client = new GraphQLClient(host)
await expect(client.request(mutationUnshoutPost, { id: 'p12345' })).rejects.toThrow(
'Not Authorised',
)
})
})
it('I unshout a post of another user', async () => {
// shout
await clientUser1.request(mutationShoutPost, { id: 'p12345' })
const expected = {
unshout: true,
}
// unshout
const res = await clientUser1.request(mutationUnshoutPost, { id: 'p12345' })
expect(res).toMatchObject(expected)
const { Post } = await clientUser1.request(gql`
query {
Post(id: "p12345") {
shoutedByCurrentUser
}
}
`)
const expected2 = {
shoutedByCurrentUser: false,
}
expect(Post[0]).toMatchObject(expected2)
})
})
})