mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Refactor badges test
CC @Tirokk @grenzfrequence * the top level block should correspond with the name of the resolver * the block below should be `CreatePost` or `UpdatePost` * the arguments of client.request are `query/mutation`, `variables` but you passed in the `headers` which should go into `new GraphQlClient(host, options)` * re-use the very same mutation to avoid bugs in the tests * use `await expect(someAsyncMethod).resolves.toEqual(expected)` style for extra test assurance
This commit is contained in:
parent
6937c60ef8
commit
f25708875a
@ -4,7 +4,7 @@ import { host, login } from '../jest/helpers'
|
||||
|
||||
const factory = Factory()
|
||||
|
||||
describe('Badge', () => {
|
||||
describe('badges', () => {
|
||||
beforeEach(async () => {
|
||||
await factory.create('User', {
|
||||
email: 'user@example.org',
|
||||
@ -27,6 +27,7 @@ describe('Badge', () => {
|
||||
await factory.cleanDatabase()
|
||||
})
|
||||
|
||||
describe('CreateBadge', () => {
|
||||
const params = {
|
||||
id: 'b1',
|
||||
key: 'indiegogo_en_racoon',
|
||||
@ -35,74 +36,59 @@ describe('Badge', () => {
|
||||
icon: '/img/badges/indiegogo_en_racoon.svg'
|
||||
}
|
||||
|
||||
const mutation = `
|
||||
mutation(
|
||||
$id: ID
|
||||
$key: String!
|
||||
$type: BadgeTypeEnum!
|
||||
$status: BadgeStatusEnum!
|
||||
$icon: String!
|
||||
) {
|
||||
CreateBadge(id: $id, key: $key, type: $type, status: $status, icon: $icon) {
|
||||
id
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
describe('unauthenticated', () => {
|
||||
let client
|
||||
|
||||
it('throws authorization error', async () => {
|
||||
client = new GraphQLClient(host)
|
||||
let { id, key, type, status, icon } = params
|
||||
await expect(
|
||||
client.request(`mutation {
|
||||
CreateBadge(
|
||||
id: "${id}",
|
||||
key: "${key}",
|
||||
type: ${type},
|
||||
status: ${status},
|
||||
icon: "${icon}"
|
||||
) { id }
|
||||
}`)
|
||||
client.request(mutation, params)
|
||||
).rejects.toThrow('Not Authorised')
|
||||
})
|
||||
})
|
||||
|
||||
describe('authenticated admin', () => {
|
||||
let client
|
||||
let headers
|
||||
let response
|
||||
let { id, key, type, status, icon } = params
|
||||
beforeEach(async () => {
|
||||
headers = await login({ email: 'admin@example.org', password: '1234' })
|
||||
const headers = await login({ email: 'admin@example.org', password: '1234' })
|
||||
client = new GraphQLClient(host, { headers })
|
||||
response = await client.request(`mutation {
|
||||
CreateBadge(
|
||||
id: "${id}",
|
||||
key: "${key}",
|
||||
type: ${type},
|
||||
status: ${status},
|
||||
icon: "${icon}"
|
||||
) { id }
|
||||
}`,
|
||||
{ headers }
|
||||
)
|
||||
})
|
||||
it('creates a badge', () => {
|
||||
let { id } = response.CreateBadge
|
||||
expect(response).toEqual({
|
||||
CreateBadge: { id }
|
||||
})
|
||||
it('creates a badge', async () => {
|
||||
const expected = {
|
||||
CreateBadge: {
|
||||
id: 'b1'
|
||||
}
|
||||
}
|
||||
await expect(client.request(mutation, params)).resolves.toEqual(expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe('authenticated moderator', () => {
|
||||
let client
|
||||
let headers
|
||||
let { id, key, type, status, icon } = params
|
||||
beforeEach(async () => {
|
||||
headers = await login({ email: 'moderator@example.org', password: '1234' })
|
||||
const headers = await login({ email: 'moderator@example.org', password: '1234' })
|
||||
client = new GraphQLClient(host, { headers })
|
||||
})
|
||||
|
||||
it('throws authorization error', async () => {
|
||||
await expect(client.request(`mutation {
|
||||
CreateBadge(
|
||||
id: "${id}",
|
||||
key: "${key}",
|
||||
type: ${type},
|
||||
status: ${status},
|
||||
icon: "${icon}"
|
||||
) { id }
|
||||
}`,
|
||||
{ headers }
|
||||
)).rejects.toThrow('Not Authorised')
|
||||
await expect(
|
||||
client.request(mutation, params)
|
||||
).rejects.toThrow('Not Authorised')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user