Test refactoring: Check comments + posts

This commit is contained in:
Robert Schäfer 2019-03-06 15:14:08 +01:00
parent 5cff508bd6
commit 1c34f10f96

View File

@ -5,54 +5,76 @@ import { host, login } from '../jest/helpers'
const factory = Factory() const factory = Factory()
let client let client
const setupAuthenticateClient = (params) => {
const authenticateClient = async () => {
await factory.create('User', params)
const headers = await login(params)
client = new GraphQLClient(host, { headers })
}
return authenticateClient
}
let setup
const runSetup = async () => {
await setup.createResource()
await setup.authenticateClient()
}
beforeEach(() => {
setup = {
createResource: () => {
},
authenticateClient: () => {
client = new GraphQLClient(host)
}
}
})
afterEach(async () => { afterEach(async () => {
await factory.cleanDatabase() await factory.cleanDatabase()
}) })
describe('disable', () => { describe('disable', () => {
const setup = async (params = {}) => {
await factory.create('User', { email: 'author@example.org', password: '1234' })
await factory.authenticateAs({ email: 'author@example.org', password: '1234' })
await factory.create('Post', {
id: 'p9' // that's the ID we will look for
})
// create the user we use in the scenario below
let headers = {}
const { email, password } = params
if (email && password) {
await factory.create('User', params)
headers = await login({ email, password })
}
client = new GraphQLClient(host, { headers })
}
const mutation = ` const mutation = `
mutation { mutation($id: ID!, $type: ResourceEnum!) {
disable(resource: { disable(resource: { id: $id, type: $type })
id: "p9"
type: contribution
})
} }
` `
let variables
beforeEach(() => {
// our defaul set of variables
variables = {
id: 'blabla',
type: 'contribution'
}
})
const action = async () => {
return client.request(mutation, variables)
}
it('throws authorization error', async () => { it('throws authorization error', async () => {
await setup() await runSetup()
await expect(client.request(mutation)).rejects.toThrow('Not Authorised') await expect(action()).rejects.toThrow('Not Authorised')
}) })
describe('authenticated', () => { describe('authenticated', () => {
it('throws authorization error', async () => { beforeEach(() => {
await setup({ setup.authenticateClient = setupAuthenticateClient({
email: 'someUser@example.org', email: 'user@example.org',
password: '1234' password: '1234'
}) })
await expect(client.request(mutation)).rejects.toThrow('Not Authorised') })
it('throws authorization error', async () => {
await runSetup()
await expect(action()).rejects.toThrow('Not Authorised')
}) })
describe('as moderator', () => { describe('as moderator', () => {
beforeEach(async () => { beforeEach(() => {
await setup({ setup.authenticateClient = setupAuthenticateClient({
id: 'u7', id: 'u7',
email: 'moderator@example.org', email: 'moderator@example.org',
password: '1234', password: '1234',
@ -60,127 +82,293 @@ describe('disable', () => {
}) })
}) })
it('returns true', async () => { describe('on a comment', () => {
const expected = { disable: true } beforeEach(async () => {
await expect(client.request(mutation)).resolves.toEqual(expected) variables = {
id: 'c47',
type: 'comment'
}
setup.createResource = async () => {
await factory.create('User', { id: 'u45', email: 'commenter@example.org', password: '1234' })
await factory.authenticateAs({ email: 'commenter@example.org', password: '1234' })
await factory.create('Post', {
id: 'p3'
})
await factory.create('Comment', {
id: 'c47'
})
await Promise.all([
factory.relate('Comment', 'Author', { from: 'u45', to: 'c47' }),
factory.relate('Comment', 'Post', { from: 'c47', to: 'p3' })
])
}
})
it('returns true', async () => {
const expected = { disable: true }
await runSetup()
await expect(action()).resolves.toEqual(expected)
})
it('changes .disabledBy', async () => {
const before = { Comment: [{ id: 'c47', disabledBy: null }] }
const expected = { Comment: [{ id: 'c47', disabledBy: { id: 'u7' } }] }
await runSetup()
await expect(client.request(
'{ Comment { id, disabledBy { id } } }'
)).resolves.toEqual(before)
await action()
await expect(client.request(
'{ Comment(disabled: true) { id, disabledBy { id } } }'
)).resolves.toEqual(expected)
})
it('updates .disabled on comment', async () => {
const before = { Comment: [ { id: 'c47', disabled: false } ] }
const expected = { Comment: [ { id: 'c47', disabled: true } ] }
await runSetup()
await expect(client.request(
'{ Comment { id disabled } }'
)).resolves.toEqual(before)
await action()
await expect(client.request(
'{ Comment(disabled: true) { id disabled } }'
)).resolves.toEqual(expected)
})
}) })
it('changes .disabledBy', async () => { describe('on a post', () => {
const before = { Post: [{ id: 'p9', disabledBy: null }] } beforeEach(async () => {
const expected = { Post: [{ id: 'p9', disabledBy: { id: 'u7' } }] } variables = {
id: 'p9',
type: 'contribution'
}
await expect(client.request( setup.createResource = async () => {
'{ Post { id, disabledBy { id } } }' await factory.create('User', { email: 'author@example.org', password: '1234' })
)).resolves.toEqual(before) await factory.authenticateAs({ email: 'author@example.org', password: '1234' })
await client.request(mutation) await factory.create('Post', {
await expect(client.request( id: 'p9' // that's the ID we will look for
'{ Post(disabled: true) { id, disabledBy { id } } }' })
)).resolves.toEqual(expected) }
}) })
it('updates .disabled on post', async () => { it('returns true', async () => {
const before = { Post: [ { id: 'p9', disabled: false } ] } const expected = { disable: true }
const expected = { Post: [ { id: 'p9', disabled: true } ] } await runSetup()
await expect(action()).resolves.toEqual(expected)
})
await expect(client.request( it('changes .disabledBy', async () => {
'{ Post { id disabled } }' const before = { Post: [{ id: 'p9', disabledBy: null }] }
)).resolves.toEqual(before) const expected = { Post: [{ id: 'p9', disabledBy: { id: 'u7' } }] }
await client.request(mutation) // this updates .disabled
await expect(client.request( await runSetup()
'{ Post(disabled: true) { id disabled } }' await expect(client.request(
)).resolves.toEqual(expected) '{ Post { id, disabledBy { id } } }'
)).resolves.toEqual(before)
await action()
await expect(client.request(
'{ Post(disabled: true) { id, disabledBy { id } } }'
)).resolves.toEqual(expected)
})
it('updates .disabled on post', async () => {
const before = { Post: [ { id: 'p9', disabled: false } ] }
const expected = { Post: [ { id: 'p9', disabled: true } ] }
await runSetup()
await expect(client.request(
'{ Post { id disabled } }'
)).resolves.toEqual(before)
await action()
await expect(client.request(
'{ Post(disabled: true) { id disabled } }'
)).resolves.toEqual(expected)
})
}) })
}) })
}) })
}) })
describe('enable', () => { describe('enable', () => {
const setup = async (params = {}) => {
await factory.create('User', { email: 'anotherModerator@example.org', password: '1234', id: 'u123', role: 'moderator' })
await factory.authenticateAs({ email: 'anotherModerator@example.org', password: '1234' })
await factory.create('Post', {
id: 'p9' // that's the ID we will look for
})
const disableMutation = `
mutation {
disable(resource: {
id: "p9"
type: contribution
})
}
`
await factory.mutate(disableMutation) // that's we want to delete
let headers = {}
const { email, password } = params
if (email && password) {
await factory.create('User', params)
headers = await login({ email, password })
}
client = new GraphQLClient(host, { headers })
}
const mutation = ` const mutation = `
mutation { mutation($id: ID!, $type: ResourceEnum!) {
enable(resource: { enable(resource: { id: $id, type: $type })
id: "p9"
type: contribution
})
} }
` `
let variables
const action = async () => {
return client.request(mutation, variables)
}
beforeEach(() => {
// our defaul set of variables
variables = {
id: 'blabla',
type: 'contribution'
}
})
it('throws authorization error', async () => { it('throws authorization error', async () => {
await setup() await runSetup()
await expect(client.request(mutation)).rejects.toThrow('Not Authorised') await expect(action()).rejects.toThrow('Not Authorised')
}) })
describe('authenticated', () => { describe('authenticated', () => {
it('throws authorization error', async () => { beforeEach(() => {
await setup({ setup.authenticateClient = setupAuthenticateClient({
email: 'someUser@example.org', email: 'user@example.org',
password: '1234' password: '1234'
}) })
await expect(client.request(mutation)).rejects.toThrow('Not Authorised') })
it('throws authorization error', async () => {
await runSetup()
await expect(action()).rejects.toThrow('Not Authorised')
}) })
describe('as moderator', () => { describe('as moderator', () => {
beforeEach(async () => { beforeEach(async () => {
await setup({ setup.authenticateClient = setupAuthenticateClient({
role: 'moderator', role: 'moderator',
email: 'someUser@example.org', email: 'someUser@example.org',
password: '1234' password: '1234'
}) })
}) })
it('returns true', async () => { describe('on a comment', () => {
const expected = { enable: true } beforeEach(async () => {
await expect(client.request(mutation)).resolves.toEqual(expected) variables = {
id: 'c456',
type: 'comment'
}
setup.createResource = async () => {
await factory.create('User', { id: 'u123', email: 'author@example.org', password: '1234' })
await factory.authenticateAs({ email: 'author@example.org', password: '1234' })
await factory.create('Post', {
id: 'p9' // that's the ID we will look for
})
await factory.create('Comment', {
id: 'c456'
})
await Promise.all([
factory.relate('Comment', 'Author', { from: 'u123', to: 'c456' }),
factory.relate('Comment', 'Post', { from: 'c456', to: 'p9' })
])
const disableMutation = `
mutation {
disable(resource: {
id: "c456"
type: comment
})
}
`
await factory.mutate(disableMutation) // that's we want to delete
}
})
it('returns true', async () => {
const expected = { enable: true }
await runSetup()
await expect(action()).resolves.toEqual(expected)
})
it('changes .disabledBy', async () => {
const before = { Comment: [{ id: 'c456', disabledBy: { id: 'u123' } }] }
const expected = { Comment: [{ id: 'c456', disabledBy: null }] }
await runSetup()
await expect(client.request(
'{ Comment(disabled: true) { id, disabledBy { id } } }'
)).resolves.toEqual(before)
await action()
await expect(client.request(
'{ Comment { id, disabledBy { id } } }'
)).resolves.toEqual(expected)
})
it('updates .disabled on post', async () => {
const before = { Comment: [ { id: 'c456', disabled: true } ] }
const expected = { Comment: [ { id: 'c456', disabled: false } ] }
await runSetup()
await expect(client.request(
'{ Comment(disabled: true) { id disabled } }'
)).resolves.toEqual(before)
await action() // this updates .disabled
await expect(client.request(
'{ Comment { id disabled } }'
)).resolves.toEqual(expected)
})
}) })
it('changes .disabledBy', async () => { describe('on a post', () => {
const before = { Post: [{ id: 'p9', disabledBy: { id: 'u123' } }] } beforeEach(async () => {
const expected = { Post: [{ id: 'p9', disabledBy: null }] } variables = {
id: 'p9',
type: 'contribution'
}
await expect(client.request( setup.createResource = async () => {
'{ Post(disabled: true) { id, disabledBy { id } } }' await factory.create('User', { id: 'u123', email: 'author@example.org', password: '1234' })
)).resolves.toEqual(before) await factory.authenticateAs({ email: 'author@example.org', password: '1234' })
await client.request(mutation) await factory.create('Post', {
await expect(client.request( id: 'p9' // that's the ID we will look for
'{ Post { id, disabledBy { id } } }' })
)).resolves.toEqual(expected)
})
it('updates .disabled on post', async () => { const disableMutation = `
const before = { Post: [ { id: 'p9', disabled: true } ] } mutation {
const expected = { Post: [ { id: 'p9', disabled: false } ] } disable(resource: {
id: "p9"
type: contribution
})
}
`
await factory.mutate(disableMutation) // that's we want to delete
}
})
await expect(client.request( it('returns true', async () => {
'{ Post(disabled: true) { id disabled } }' const expected = { enable: true }
)).resolves.toEqual(before) await runSetup()
await client.request(mutation) // this updates .disabled await expect(action()).resolves.toEqual(expected)
await expect(client.request( })
'{ Post { id disabled } }'
)).resolves.toEqual(expected) it('changes .disabledBy', async () => {
const before = { Post: [{ id: 'p9', disabledBy: { id: 'u123' } }] }
const expected = { Post: [{ id: 'p9', disabledBy: null }] }
await runSetup()
await expect(client.request(
'{ Post(disabled: true) { id, disabledBy { id } } }'
)).resolves.toEqual(before)
await action()
await expect(client.request(
'{ Post { id, disabledBy { id } } }'
)).resolves.toEqual(expected)
})
it('updates .disabled on post', async () => {
const before = { Post: [ { id: 'p9', disabled: true } ] }
const expected = { Post: [ { id: 'p9', disabled: false } ] }
await runSetup()
await expect(client.request(
'{ Post(disabled: true) { id disabled } }'
)).resolves.toEqual(before)
await action() // this updates .disabled
await expect(client.request(
'{ Post { id disabled } }'
)).resolves.toEqual(expected)
})
}) })
}) })
}) })