mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Merge pull request #1884 from Human-Connection/1773_refactor-rewards-spec
1773 refactor rewards spec
This commit is contained in:
commit
1d7dc8f9ff
@ -1,31 +1,49 @@
|
||||
import { GraphQLClient } from 'graphql-request'
|
||||
import { createTestClient } from 'apollo-server-testing'
|
||||
import Factory from '../../seed/factories'
|
||||
import { host, login, gql } from '../../jest/helpers'
|
||||
import { gql } from '../../jest/helpers'
|
||||
import { neode as getNeode, getDriver } from '../../bootstrap/neo4j'
|
||||
import createServer from '../../server'
|
||||
|
||||
const factory = Factory()
|
||||
let user
|
||||
let badge
|
||||
const driver = getDriver()
|
||||
const instance = getNeode()
|
||||
|
||||
let authenticatedUser, regularUser, administrator, moderator, badge, query, mutate
|
||||
|
||||
describe('rewards', () => {
|
||||
const variables = {
|
||||
from: 'indiegogo_en_rhino',
|
||||
to: 'u1',
|
||||
to: 'regular-user-id',
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
const { server } = createServer({
|
||||
context: () => {
|
||||
return {
|
||||
driver,
|
||||
neode: instance,
|
||||
user: authenticatedUser,
|
||||
}
|
||||
},
|
||||
})
|
||||
query = createTestClient(server).query
|
||||
mutate = createTestClient(server).mutate
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
user = await factory.create('User', {
|
||||
id: 'u1',
|
||||
regularUser = await factory.create('User', {
|
||||
id: 'regular-user-id',
|
||||
role: 'user',
|
||||
email: 'user@example.org',
|
||||
password: '1234',
|
||||
})
|
||||
await factory.create('User', {
|
||||
id: 'u2',
|
||||
moderator = await factory.create('User', {
|
||||
id: 'moderator-id',
|
||||
role: 'moderator',
|
||||
email: 'moderator@example.org',
|
||||
})
|
||||
await factory.create('User', {
|
||||
id: 'u3',
|
||||
administrator = await factory.create('User', {
|
||||
id: 'admin-id',
|
||||
role: 'admin',
|
||||
email: 'admin@example.org',
|
||||
})
|
||||
@ -42,7 +60,7 @@ describe('rewards', () => {
|
||||
})
|
||||
|
||||
describe('reward', () => {
|
||||
const mutation = gql`
|
||||
const rewardMutation = gql`
|
||||
mutation($from: ID!, $to: ID!) {
|
||||
reward(badgeKey: $from, userId: $to) {
|
||||
id
|
||||
@ -54,51 +72,61 @@ describe('rewards', () => {
|
||||
`
|
||||
|
||||
describe('unauthenticated', () => {
|
||||
let client
|
||||
|
||||
it('throws authorization error', async () => {
|
||||
client = new GraphQLClient(host)
|
||||
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||
authenticatedUser = null
|
||||
await expect(mutate({ mutation: rewardMutation, variables })).resolves.toMatchObject({
|
||||
data: { reward: null },
|
||||
errors: [{ message: 'Not Authorised!' }],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('authenticated admin', () => {
|
||||
let client
|
||||
beforeEach(async () => {
|
||||
const headers = await login({ email: 'admin@example.org', password: '1234' })
|
||||
client = new GraphQLClient(host, { headers })
|
||||
authenticatedUser = await administrator.toJson()
|
||||
})
|
||||
|
||||
describe('badge for id does not exist', () => {
|
||||
it('rejects with a telling error message', async () => {
|
||||
it('rejects with an informative error message', async () => {
|
||||
await expect(
|
||||
client.request(mutation, {
|
||||
...variables,
|
||||
from: 'bullshit',
|
||||
mutate({
|
||||
mutation: rewardMutation,
|
||||
variables: { to: 'regular-user-id', from: 'non-existent-badge-id' },
|
||||
}),
|
||||
).rejects.toThrow("Couldn't find a badge with that id")
|
||||
).resolves.toMatchObject({
|
||||
data: { reward: null },
|
||||
errors: [{ message: "Couldn't find a badge with that id" }],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('user for id does not exist', () => {
|
||||
describe('non-existent user', () => {
|
||||
it('rejects with a telling error message', async () => {
|
||||
await expect(
|
||||
client.request(mutation, {
|
||||
...variables,
|
||||
to: 'bullshit',
|
||||
mutate({
|
||||
mutation: rewardMutation,
|
||||
variables: { to: 'non-existent-user-id', from: 'indiegogo_en_rhino' },
|
||||
}),
|
||||
).rejects.toThrow("Couldn't find a user with that id")
|
||||
).resolves.toMatchObject({
|
||||
data: { reward: null },
|
||||
errors: [{ message: "Couldn't find a user with that id" }],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('rewards a badge to user', async () => {
|
||||
const expected = {
|
||||
reward: {
|
||||
id: 'u1',
|
||||
badges: [{ id: 'indiegogo_en_rhino' }],
|
||||
data: {
|
||||
reward: {
|
||||
id: 'regular-user-id',
|
||||
badges: [{ id: 'indiegogo_en_rhino' }],
|
||||
},
|
||||
},
|
||||
errors: undefined,
|
||||
}
|
||||
await expect(client.request(mutation, variables)).resolves.toEqual(expected)
|
||||
await expect(mutate({ mutation: rewardMutation, variables })).resolves.toMatchObject(
|
||||
expected,
|
||||
)
|
||||
})
|
||||
|
||||
it('rewards a second different badge to same user', async () => {
|
||||
@ -108,42 +136,74 @@ describe('rewards', () => {
|
||||
})
|
||||
const badges = [{ id: 'indiegogo_en_racoon' }, { id: 'indiegogo_en_rhino' }]
|
||||
const expected = {
|
||||
reward: {
|
||||
id: 'u1',
|
||||
badges: expect.arrayContaining(badges),
|
||||
data: {
|
||||
reward: {
|
||||
id: 'regular-user-id',
|
||||
badges: expect.arrayContaining(badges),
|
||||
},
|
||||
},
|
||||
errors: undefined,
|
||||
}
|
||||
await client.request(mutation, variables)
|
||||
await mutate({
|
||||
mutation: rewardMutation,
|
||||
variables: {
|
||||
to: 'regular-user-id',
|
||||
from: 'indiegogo_en_rhino',
|
||||
},
|
||||
})
|
||||
await expect(
|
||||
client.request(mutation, {
|
||||
...variables,
|
||||
from: 'indiegogo_en_racoon',
|
||||
mutate({
|
||||
mutation: rewardMutation,
|
||||
variables: {
|
||||
to: 'regular-user-id',
|
||||
from: 'indiegogo_en_racoon',
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(expected)
|
||||
).resolves.toMatchObject(expected)
|
||||
})
|
||||
|
||||
it('rewards the same badge as well to another user', async () => {
|
||||
const expected = {
|
||||
reward: {
|
||||
id: 'u2',
|
||||
badges: [{ id: 'indiegogo_en_rhino' }],
|
||||
data: {
|
||||
reward: {
|
||||
id: 'regular-user-2-id',
|
||||
badges: [{ id: 'indiegogo_en_rhino' }],
|
||||
},
|
||||
},
|
||||
errors: undefined,
|
||||
}
|
||||
await factory.create('User', {
|
||||
id: 'regular-user-2-id',
|
||||
email: 'regular2@email.com',
|
||||
})
|
||||
await mutate({
|
||||
mutation: rewardMutation,
|
||||
variables,
|
||||
})
|
||||
await expect(
|
||||
client.request(mutation, {
|
||||
...variables,
|
||||
to: 'u2',
|
||||
mutate({
|
||||
mutation: rewardMutation,
|
||||
variables: {
|
||||
to: 'regular-user-2-id',
|
||||
from: 'indiegogo_en_rhino',
|
||||
},
|
||||
}),
|
||||
).resolves.toEqual(expected)
|
||||
).resolves.toMatchObject(expected)
|
||||
})
|
||||
|
||||
it('creates no duplicate reward relationships', async () => {
|
||||
await client.request(mutation, variables)
|
||||
await client.request(mutation, variables)
|
||||
await mutate({
|
||||
mutation: rewardMutation,
|
||||
variables,
|
||||
})
|
||||
await mutate({
|
||||
mutation: rewardMutation,
|
||||
variables,
|
||||
})
|
||||
|
||||
const query = gql`
|
||||
const userQuery = gql`
|
||||
{
|
||||
User(id: "u1") {
|
||||
User(id: "regular-user-id") {
|
||||
badgesCount
|
||||
badges {
|
||||
id
|
||||
@ -151,22 +211,26 @@ describe('rewards', () => {
|
||||
}
|
||||
}
|
||||
`
|
||||
const expected = { User: [{ badgesCount: 1, badges: [{ id: 'indiegogo_en_rhino' }] }] }
|
||||
const expected = {
|
||||
data: { User: [{ badgesCount: 1, badges: [{ id: 'indiegogo_en_rhino' }] }] },
|
||||
errors: undefined,
|
||||
}
|
||||
|
||||
await expect(client.request(query)).resolves.toEqual(expected)
|
||||
await expect(query({ query: userQuery })).resolves.toMatchObject(expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe('authenticated moderator', () => {
|
||||
let client
|
||||
beforeEach(async () => {
|
||||
const headers = await login({ email: 'moderator@example.org', password: '1234' })
|
||||
client = new GraphQLClient(host, { headers })
|
||||
authenticatedUser = moderator.toJson()
|
||||
})
|
||||
|
||||
describe('rewards bage to user', () => {
|
||||
describe('rewards badge to user', () => {
|
||||
it('throws authorization error', async () => {
|
||||
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||
await expect(mutate({ mutation: rewardMutation, variables })).resolves.toMatchObject({
|
||||
data: { reward: null },
|
||||
errors: [{ message: 'Not Authorised!' }],
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -174,11 +238,14 @@ describe('rewards', () => {
|
||||
|
||||
describe('unreward', () => {
|
||||
beforeEach(async () => {
|
||||
await user.relateTo(badge, 'rewarded')
|
||||
await regularUser.relateTo(badge, 'rewarded')
|
||||
})
|
||||
const expected = { unreward: { id: 'u1', badges: [] } }
|
||||
const expected = {
|
||||
data: { unreward: { id: 'regular-user-id', badges: [] } },
|
||||
errors: undefined,
|
||||
}
|
||||
|
||||
const mutation = gql`
|
||||
const unrewardMutation = gql`
|
||||
mutation($from: ID!, $to: ID!) {
|
||||
unreward(badgeKey: $from, userId: $to) {
|
||||
id
|
||||
@ -191,9 +258,10 @@ describe('rewards', () => {
|
||||
|
||||
describe('check test setup', () => {
|
||||
it('user has one badge', async () => {
|
||||
const query = gql`
|
||||
authenticatedUser = regularUser.toJson()
|
||||
const userQuery = gql`
|
||||
{
|
||||
User(id: "u1") {
|
||||
User(id: "regular-user-id") {
|
||||
badgesCount
|
||||
badges {
|
||||
id
|
||||
@ -201,48 +269,54 @@ describe('rewards', () => {
|
||||
}
|
||||
}
|
||||
`
|
||||
const expected = { User: [{ badgesCount: 1, badges: [{ id: 'indiegogo_en_rhino' }] }] }
|
||||
const client = new GraphQLClient(host)
|
||||
await expect(client.request(query)).resolves.toEqual(expected)
|
||||
const expected = {
|
||||
data: { User: [{ badgesCount: 1, badges: [{ id: 'indiegogo_en_rhino' }] }] },
|
||||
errors: undefined,
|
||||
}
|
||||
await expect(query({ query: userQuery })).resolves.toMatchObject(expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe('unauthenticated', () => {
|
||||
let client
|
||||
|
||||
it('throws authorization error', async () => {
|
||||
client = new GraphQLClient(host)
|
||||
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||
authenticatedUser = null
|
||||
await expect(mutate({ mutation: unrewardMutation, variables })).resolves.toMatchObject({
|
||||
data: { unreward: null },
|
||||
errors: [{ message: 'Not Authorised!' }],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('authenticated admin', () => {
|
||||
let client
|
||||
beforeEach(async () => {
|
||||
const headers = await login({ email: 'admin@example.org', password: '1234' })
|
||||
client = new GraphQLClient(host, { headers })
|
||||
authenticatedUser = await administrator.toJson()
|
||||
})
|
||||
|
||||
it('removes a badge from user', async () => {
|
||||
await expect(client.request(mutation, variables)).resolves.toEqual(expected)
|
||||
await expect(mutate({ mutation: unrewardMutation, variables })).resolves.toMatchObject(
|
||||
expected,
|
||||
)
|
||||
})
|
||||
|
||||
it('does not crash when unrewarding multiple times', async () => {
|
||||
await client.request(mutation, variables)
|
||||
await expect(client.request(mutation, variables)).resolves.toEqual(expected)
|
||||
await mutate({ mutation: unrewardMutation, variables })
|
||||
await expect(mutate({ mutation: unrewardMutation, variables })).resolves.toMatchObject(
|
||||
expected,
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('authenticated moderator', () => {
|
||||
let client
|
||||
beforeEach(async () => {
|
||||
const headers = await login({ email: 'moderator@example.org', password: '1234' })
|
||||
client = new GraphQLClient(host, { headers })
|
||||
authenticatedUser = await moderator.toJson()
|
||||
})
|
||||
|
||||
describe('removes bage from user', () => {
|
||||
it('throws authorization error', async () => {
|
||||
await expect(client.request(mutation, variables)).rejects.toThrow('Not Authorised')
|
||||
await expect(mutate({ mutation: unrewardMutation, variables })).resolves.toMatchObject({
|
||||
data: { unreward: null },
|
||||
errors: [{ message: 'Not Authorised!' }],
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user