fix code review issues

This commit is contained in:
aonomike 2019-09-18 22:29:14 +03:00
parent ca970eb30a
commit bdfc2127d5

View File

@ -51,362 +51,364 @@ beforeEach(async () => {
afterEach(async () => { afterEach(async () => {
await factory.cleanDatabase() await factory.cleanDatabase()
}) })
describe('moderate resources', () => {
describe('disable', () => {
const mutation = gql`
mutation($id: ID!) {
disable(id: $id)
}
`
let variables
describe('disable', () => { beforeEach(() => {
const mutation = gql` variables = {
mutation($id: ID!) { id: 'some-resource',
disable(id: $id) }
}
`
let variables
beforeEach(() => {
// our defaul set of variables
variables = {
id: 'some-resource',
}
})
describe('unauthenticated', () => {
beforeEach(async () => {
authenticatedUser = await currentUser.toJson()
}) })
it('throws authorization error', async () => { describe('unauthenticated', () => {
const { errors } = await mutate({ mutation, variables })
expect(errors[0]).toHaveProperty('message', 'Not Authorised!')
})
})
describe('authenticated', () => {
describe('i am not a moderator', () => {
let nonModerator
beforeEach(async () => { beforeEach(async () => {
nonModerator = await factory.create('User', { authenticatedUser = await currentUser.toJson()
id: 'non-moderator',
name: 'Non Moderator',
email: 'non.moderator@example.org',
password: '1234',
})
authenticatedUser = await nonModerator.toJson()
}) })
it('throws authorization error', async () => { it('throws authorization error', async () => {
const { errors } = await mutate({ mutation, variables }) await expect(mutate({ mutation, variables })).resolves.toMatchObject({
expect(errors[0]).toHaveProperty('message', 'Not Authorised!') errors: [{ message: 'Not Authorised!' }],
})
}) })
}) })
describe('authenticated', () => {
describe('i am not a moderator', () => {
let nonModerator
describe('I am a moderator', () => {
let moderator
beforeEach(async () => {
moderator = await factory.create('User', {
id: 'moderator-id',
name: 'Moderator',
email: 'moderator@example.org',
password: '1234',
role: 'moderator',
})
authenticatedUser = await moderator.toJson()
})
describe('moderate a resource that is not a (Comment|Post|User) ', () => {
beforeEach(async () => { beforeEach(async () => {
variables = { nonModerator = await factory.create('User', {
id: 'sample-tag-id', id: 'non-moderator',
} name: 'Non Moderator',
await Promise.all([factory.create('Tag', { id: 'sample-tag-id' })]) email: 'non.moderator@example.org',
password: '1234',
})
authenticatedUser = await nonModerator.toJson()
}) })
it('throws authorization error', async () => {
it('returns null', async () => { await expect(mutate({ mutation, variables })).resolves.toMatchObject({
const expected = { data: { disable: null } } errors: [{ message: 'Not Authorised!' }],
await expect(mutate({ mutation, variables })).resolves.toMatchObject(expected) })
}) })
}) })
describe('moderate a comment', () => { describe('I am a moderator', () => {
const commentQuery = gql` let moderator
query($id: ID!) { beforeEach(async () => {
Comment(id: $id) { moderator = await factory.create('User', {
id id: 'moderator-id',
disabled name: 'Moderator',
disabledBy { email: 'moderator@example.org',
password: '1234',
role: 'moderator',
})
authenticatedUser = await moderator.toJson()
})
describe('moderate a resource that is not a (Comment|Post|User) ', () => {
beforeEach(async () => {
variables = {
id: 'sample-tag-id',
}
await Promise.all([factory.create('Tag', { id: 'sample-tag-id' })])
})
it('returns null', async () => {
await expect(mutate({ mutation, variables })).resolves.toMatchObject({
data: { disable: null },
})
})
})
describe('moderate a comment', () => {
const commentQuery = gql`
query($id: ID!) {
Comment(id: $id) {
id id
disabled
disabledBy {
id
}
} }
} }
} `
` beforeEach(async () => {
beforeEach(async () => { variables = {}
variables = {} await factory.create('Comment', {
await factory.create('Comment', { id: 'comment-id',
id: 'comment-id', })
})
it('returns disabled resource id', async () => {
variables = { id: 'comment-id' }
await expect(mutate({ mutation, variables })).resolves.toMatchObject({
data: { disable: 'comment-id' },
})
})
it('changes .disabledBy', async () => {
variables = { id: 'comment-id' }
const before = { data: { Comment: [{ id: 'comment-id', disabledBy: null }] } }
const expected = {
data: { Comment: [{ id: 'comment-id', disabledBy: { id: 'moderator-id' } }] },
}
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before)
await expect(mutate({ mutation, variables })).resolves.toMatchObject({
data: { disable: 'comment-id' },
})
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected)
})
it('updates .disabled on comment', async () => {
variables = { id: 'comment-id' }
const before = { data: { Comment: [{ id: 'comment-id', disabled: false }] } }
const expected = { data: { Comment: [{ id: 'comment-id', disabled: true }] } }
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before)
await expect(mutate({ mutation, variables })).resolves.toMatchObject({
data: { disable: 'comment-id' },
})
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected)
}) })
}) })
it('returns disabled resource id', async () => { describe('moderate a post', () => {
variables = { id: 'comment-id' } beforeEach(async () => {
const expected = { data: { disable: 'comment-id' } } variables = {}
await expect(mutate({ mutation, variables })).resolves.toMatchObject(expected) await factory.create('Post', {
}) id: 'sample-post-id',
})
it('changes .disabledBy', async () => {
variables = { id: 'comment-id' }
const before = { data: { Comment: [{ id: 'comment-id', disabledBy: null }] } }
const expected = {
data: { Comment: [{ id: 'comment-id', disabledBy: { id: 'moderator-id' } }] },
}
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before)
await expect(mutate({ mutation, variables })).resolves.toMatchObject({
data: { disable: 'comment-id' },
}) })
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) const postQuery = gql`
}) query($id: ID) {
Post(id: $id) {
it('updates .disabled on comment', async () => {
variables = { id: 'comment-id' }
const before = { data: { Comment: [{ id: 'comment-id', disabled: false }] } }
const expected = { data: { Comment: [{ id: 'comment-id', disabled: true }] } }
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before)
await expect(mutate({ mutation, variables })).resolves.toMatchObject({
data: { disable: 'comment-id' },
})
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected)
})
})
describe('moderate a post', () => {
beforeEach(async () => {
variables = {}
await factory.create('Post', {
id: 'sample-post-id',
})
})
const postQuery = gql`
query($id: ID) {
Post(id: $id) {
id
disabled
disabledBy {
id id
disabled
disabledBy {
id
}
} }
} }
} `
` it('returns disabled resource id', async () => {
it('returns disabled resource id', async () => { variables = { id: 'sample-post-id' }
variables = { id: 'sample-post-id' } await expect(mutate({ mutation, variables })).resolves.toMatchObject({
await expect(mutate({ mutation, variables })).resolves.toMatchObject({ data: { disable: 'sample-post-id' },
data: { disable: 'sample-post-id' }, })
}) })
})
it('changes .disabledBy', async () => { it('changes .disabledBy', async () => {
variables = { id: 'sample-post-id' } variables = { id: 'sample-post-id' }
const before = { data: { Post: [{ id: 'sample-post-id', disabledBy: null }] } } const before = { data: { Post: [{ id: 'sample-post-id', disabledBy: null }] } }
const expected = { const expected = {
data: { Post: [{ id: 'sample-post-id', disabledBy: { id: 'moderator-id' } }] }, data: { Post: [{ id: 'sample-post-id', disabledBy: { id: 'moderator-id' } }] },
} }
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before) await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before)
await expect(mutate({ mutation, variables })).resolves.toMatchObject({ await expect(mutate({ mutation, variables })).resolves.toMatchObject({
data: { disable: 'sample-post-id' }, data: { disable: 'sample-post-id' },
})
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
}) })
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
})
it('updates .disabled on post', async () => { it('updates .disabled on post', async () => {
const before = { data: { Post: [{ id: 'sample-post-id', disabled: false }] } } const before = { data: { Post: [{ id: 'sample-post-id', disabled: false }] } }
const expected = { data: { Post: [{ id: 'sample-post-id', disabled: true }] } } const expected = { data: { Post: [{ id: 'sample-post-id', disabled: true }] } }
variables = { id: 'sample-post-id' } variables = { id: 'sample-post-id' }
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before) await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before)
await expect(mutate({ mutation, variables })).resolves.toMatchObject({ await expect(mutate({ mutation, variables })).resolves.toMatchObject({
data: { disable: 'sample-post-id' }, data: { disable: 'sample-post-id' },
})
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
}) })
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
}) })
}) })
}) })
}) })
// describe('authenticated', () => { describe('enable', () => {
// beforeEach(() => { // describe('authenticated', () => {
// authenticateClient = setupAuthenticateClient({ // beforeEach(() => {
// email: 'user@example.org', // authenticateClient = setupAuthenticateClient({
// password: '1234', // email: 'user@example.org',
// }) // password: '1234',
// }) // })
// })
// describe('enable', () => { // describe('enable', () => {
// const mutation = gql` // const mutation = gql`
// mutation($id: ID!) { // mutation($id: ID!) {
// enable(id: $id) // enable(id: $id)
// } // }
// ` // `
// const action = async () => { // const action = async () => {
// return client.request(mutation, variables) // return client.request(mutation, variables)
// } // }
// beforeEach(() => {
// beforeEach(() => { // our default set of variables
// our default set of variables // variables = {
// variables = { // id: 'blabla',
// id: 'blabla', // }
// } // })
// }) // it('throws authorization error', async () => {
// await setup()
// it('throws authorization error', async () => { // await expect(action()).rejects.toThrow('Not Authorised')
// await setup() // })
// await expect(action()).rejects.toThrow('Not Authorised') // describe('authenticated', () => {
// }) // beforeEach(() => {
// authenticateClient = setupAuthenticateClient({
// describe('authenticated', () => { // email: 'user@example.org',
// beforeEach(() => { // password: '1234',
// authenticateClient = setupAuthenticateClient({ // })
// email: 'user@example.org', // })
// password: '1234', // it('throws authorization error', async () => {
// }) // await setup()
// }) // await expect(action()).rejects.toThrow('Not Authorised')
// it('throws authorization error', async () => { // })
// await setup() // describe('as moderator', () => {
// await expect(action()).rejects.toThrow('Not Authorised') // beforeEach(async () => {
// }) // authenticateClient = setupAuthenticateClient({
// describe('as moderator', () => { // role: 'moderator',
// beforeEach(async () => { // email: 'someuser@example.org',
// authenticateClient = setupAuthenticateClient({ // password: '1234',
// role: 'moderator', // })
// email: 'someuser@example.org', // })
// password: '1234', // describe('on something that is not a (Comment|Post|User) ', () => {
// }) // beforeEach(async () => {
// }) // variables = {
// describe('on something that is not a (Comment|Post|User) ', () => { // id: 't23',
// beforeEach(async () => { // }
// variables = { // createResource = () => {
// id: 't23', // // we cannot create a :DISABLED relationship here
// } // return Promise.all([factory.create('Tag', { id: 't23' })])
// createResource = () => { // }
// // we cannot create a :DISABLED relationship here // })
// return Promise.all([factory.create('Tag', { id: 't23' })]) // it('returns null', async () => {
// } // const expected = { enable: null }
// }) // await setup()
// it('returns null', async () => { // await expect(action()).resolves.toEqual(expected)
// const expected = { enable: null } // })
// await setup() // })
// await expect(action()).resolves.toEqual(expected) // describe('on a comment', () => {
// }) // beforeEach(async () => {
// }) // variables = {
// describe('on a comment', () => { // id: 'c456',
// beforeEach(async () => { // }
// variables = { // createPostVariables = {
// id: 'c456', // id: 'p9',
// } // title: 'post to comment on',
// createPostVariables = { // content: 'please comment on me',
// id: 'p9', // categoryIds,
// title: 'post to comment on', // }
// content: 'please comment on me', // createCommentVariables = {
// categoryIds, // id: 'c456',
// } // postId: 'p9',
// createCommentVariables = { // content: 'this comment was created for this post',
// id: 'c456', // }
// postId: 'p9', // createResource = async () => {
// content: 'this comment was created for this post', // await factory.create('User', {
// } // id: 'u123',
// createResource = async () => { // email: 'author@example.org',
// await factory.create('User', { // password: '1234',
// id: 'u123', // })
// email: 'author@example.org', // const asAuthenticatedUser = await factory.authenticateAs({
// password: '1234', // email: 'author@example.org',
// }) // password: '1234',
// const asAuthenticatedUser = await factory.authenticateAs({ // })
// email: 'author@example.org', // await asAuthenticatedUser.create('Post', createPostVariables)
// password: '1234', // await asAuthenticatedUser.create('Comment', createCommentVariables)
// }) // const disableMutation = gql`
// await asAuthenticatedUser.create('Post', createPostVariables) // mutation {
// await asAuthenticatedUser.create('Comment', createCommentVariables) // disable(id: "c456")
// const disableMutation = gql` // }
// mutation { // `
// disable(id: "c456") // await factory.mutate(disableMutation) // that's we want to delete
// } // }
// ` // })
// await factory.mutate(disableMutation) // that's we want to delete // it('returns disabled resource id', async () => {
// } // const expected = { enable: 'c456' }
// }) // await setup()
// it('returns disabled resource id', async () => { // await expect(action()).resolves.toEqual(expected)
// const expected = { enable: 'c456' } // })
// await setup() // it('changes .disabledBy', async () => {
// await expect(action()).resolves.toEqual(expected) // const before = { Comment: [{ id: 'c456', disabledBy: { id: 'u123' } }] }
// }) // const expected = { Comment: [{ id: 'c456', disabledBy: null }] }
// it('changes .disabledBy', async () => { // await setup()
// const before = { Comment: [{ id: 'c456', disabledBy: { id: 'u123' } }] } // await expect(
// const expected = { Comment: [{ id: 'c456', disabledBy: null }] } // client.request('{ Comment(disabled: true) { id, disabledBy { id } } }'),
// await setup() // ).resolves.toEqual(before)
// await expect( // await action()
// client.request('{ Comment(disabled: true) { id, disabledBy { id } } }'), // await expect(client.request('{ Comment { id, disabledBy { id } } }')).resolves.toEqual(
// ).resolves.toEqual(before) // expected,
// 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 }] }
// it('updates .disabled on post', async () => { // await setup()
// const before = { Comment: [{ id: 'c456', disabled: true }] } // await expect(
// const expected = { Comment: [{ id: 'c456', disabled: false }] } // client.request('{ Comment(disabled: true) { id disabled } }'),
// await setup() // ).resolves.toEqual(before)
// await expect( // await action() // this updates .disabled
// client.request('{ Comment(disabled: true) { id disabled } }'), // await expect(client.request('{ Comment { id disabled } }')).resolves.toEqual(expected)
// ).resolves.toEqual(before) // })
// await action() // this updates .disabled // })
// await expect(client.request('{ Comment { id disabled } }')).resolves.toEqual(expected) // describe('on a post', () => {
// }) // beforeEach(async () => {
// }) // variables = {
// describe('on a post', () => { // id: 'p9',
// beforeEach(async () => { // }
// variables = { // createResource = async () => {
// id: 'p9', // await factory.create('User', {
// } // id: 'u123',
// createResource = async () => { // email: 'author@example.org',
// await factory.create('User', { // password: '1234',
// id: 'u123', // })
// email: 'author@example.org', // await factory.authenticateAs({ email: 'author@example.org', password: '1234' })
// password: '1234', // await factory.create('Post', {
// }) // id: 'p9', // that's the ID we will look for
// await factory.authenticateAs({ email: 'author@example.org', password: '1234' }) // categoryIds,
// await factory.create('Post', { // })
// id: 'p9', // that's the ID we will look for // const disableMutation = gql`
// categoryIds, // mutation {
// }) // disable(id: "p9")
// const disableMutation = gql` // }
// mutation { // `
// disable(id: "p9") // await factory.mutate(disableMutation) // that's we want to delete
// } // }
// ` // })
// await factory.mutate(disableMutation) // that's we want to delete // it('returns disabled resource id', async () => {
// } // const expected = { enable: 'p9' }
// }) // await setup()
// it('returns disabled resource id', async () => { // await expect(action()).resolves.toEqual(expected)
// const expected = { enable: 'p9' } // })
// await setup() // it('changes .disabledBy', async () => {
// await expect(action()).resolves.toEqual(expected) // const before = { Post: [{ id: 'p9', disabledBy: { id: 'u123' } }] }
// }) // const expected = { Post: [{ id: 'p9', disabledBy: null }] }
// it('changes .disabledBy', async () => { // await setup()
// const before = { Post: [{ id: 'p9', disabledBy: { id: 'u123' } }] } // await expect(
// const expected = { Post: [{ id: 'p9', disabledBy: null }] } // client.request('{ Post(disabled: true) { id, disabledBy { id } } }'),
// await setup() // ).resolves.toEqual(before)
// await expect( // await action()
// client.request('{ Post(disabled: true) { id, disabledBy { id } } }'), // await expect(client.request('{ Post { id, disabledBy { id } } }')).resolves.toEqual(
// ).resolves.toEqual(before) // expected,
// 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 }] }
// it('updates .disabled on post', async () => { // await setup()
// const before = { Post: [{ id: 'p9', disabled: true }] } // await expect(client.request('{ Post(disabled: true) { id disabled } }')).resolves.toEqual(
// const expected = { Post: [{ id: 'p9', disabled: false }] } // before,
// await setup() // )
// await expect(client.request('{ Post(disabled: true) { id disabled } }')).resolves.toEqual( // await action() // this updates .disabled
// before, // await expect(client.request('{ Post { id disabled } }')).resolves.toEqual(expected)
// ) // })
// await action() // this updates .disabled // })
// await expect(client.request('{ Post { id disabled } }')).resolves.toEqual(expected) // })
// }) // })
// }) })
// })
// })
}) })