mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Merge pull request #1620 from Human-Connection/1612_refactor-moderator-spec
1612 refactor moderator spec
This commit is contained in:
commit
ede9dc0d76
@ -1,420 +1,349 @@
|
|||||||
import { GraphQLClient } from 'graphql-request'
|
import { createTestClient } from 'apollo-server-testing'
|
||||||
import Factory from '../../seed/factories'
|
import Factory from '../../seed/factories'
|
||||||
import { host, login, gql } from '../../jest/helpers'
|
import { gql } from '../../jest/helpers'
|
||||||
import { neode } from '../../bootstrap/neo4j'
|
import { neode as getNeode, getDriver } from '../../bootstrap/neo4j'
|
||||||
|
import createServer from '../../server'
|
||||||
|
|
||||||
let client
|
|
||||||
const factory = Factory()
|
const factory = Factory()
|
||||||
const instance = neode()
|
const neode = getNeode()
|
||||||
const categoryIds = ['cat9']
|
const driver = getDriver()
|
||||||
|
|
||||||
const setupAuthenticateClient = params => {
|
let query, mutate, authenticatedUser, variables, moderator, nonModerator
|
||||||
const authenticateClient = async () => {
|
|
||||||
await factory.create('User', params)
|
const disableMutation = gql`
|
||||||
const headers = await login(params)
|
mutation($id: ID!) {
|
||||||
client = new GraphQLClient(host, { headers })
|
disable(id: $id)
|
||||||
}
|
}
|
||||||
return authenticateClient
|
`
|
||||||
}
|
const enableMutation = gql`
|
||||||
|
mutation($id: ID!) {
|
||||||
let createResource
|
enable(id: $id)
|
||||||
let authenticateClient
|
|
||||||
let createPostVariables
|
|
||||||
let createCommentVariables
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
createResource = () => {}
|
|
||||||
authenticateClient = () => {
|
|
||||||
client = new GraphQLClient(host)
|
|
||||||
}
|
}
|
||||||
await instance.create('Category', {
|
`
|
||||||
id: 'cat9',
|
|
||||||
name: 'Democracy & Politics',
|
|
||||||
icon: 'university',
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
const setup = async () => {
|
const commentQuery = gql`
|
||||||
await createResource()
|
query($id: ID!) {
|
||||||
await authenticateClient()
|
Comment(id: $id) {
|
||||||
}
|
id
|
||||||
|
disabled
|
||||||
afterEach(async () => {
|
disabledBy {
|
||||||
await factory.cleanDatabase()
|
id
|
||||||
})
|
}
|
||||||
|
|
||||||
describe('disable', () => {
|
|
||||||
const mutation = gql`
|
|
||||||
mutation($id: ID!) {
|
|
||||||
disable(id: $id)
|
|
||||||
}
|
}
|
||||||
`
|
|
||||||
let variables
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
// our defaul set of variables
|
|
||||||
variables = {
|
|
||||||
id: 'blabla',
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
const action = async () => {
|
|
||||||
return client.request(mutation, variables)
|
|
||||||
}
|
}
|
||||||
|
`
|
||||||
|
|
||||||
it('throws authorization error', async () => {
|
const postQuery = gql`
|
||||||
await setup()
|
query($id: ID) {
|
||||||
await expect(action()).rejects.toThrow('Not Authorised')
|
Post(id: $id) {
|
||||||
|
id
|
||||||
|
disabled
|
||||||
|
disabledBy {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
|
describe('moderate resources', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
authenticatedUser = undefined
|
||||||
|
const { server } = createServer({
|
||||||
|
context: () => {
|
||||||
|
return {
|
||||||
|
driver,
|
||||||
|
neode,
|
||||||
|
user: authenticatedUser,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
mutate = createTestClient(server).mutate
|
||||||
|
query = createTestClient(server).query
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('authenticated', () => {
|
beforeEach(async () => {
|
||||||
|
variables = {}
|
||||||
|
authenticatedUser = null
|
||||||
|
moderator = await factory.create('User', {
|
||||||
|
id: 'moderator-id',
|
||||||
|
name: 'Moderator',
|
||||||
|
email: 'moderator@example.org',
|
||||||
|
password: '1234',
|
||||||
|
role: 'moderator',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await factory.cleanDatabase()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('disable', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
authenticateClient = setupAuthenticateClient({
|
variables = {
|
||||||
email: 'user@example.org',
|
id: 'some-resource',
|
||||||
password: '1234',
|
}
|
||||||
|
})
|
||||||
|
describe('unauthenticated', () => {
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
|
||||||
|
errors: [{ message: 'Not Authorised!' }],
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('authenticated', () => {
|
||||||
it('throws authorization error', async () => {
|
describe('non moderator', () => {
|
||||||
await setup()
|
beforeEach(async () => {
|
||||||
await expect(action()).rejects.toThrow('Not Authorised')
|
nonModerator = await factory.create('User', {
|
||||||
})
|
id: 'non-moderator',
|
||||||
|
name: 'Non Moderator',
|
||||||
describe('as moderator', () => {
|
email: 'non.moderator@example.org',
|
||||||
beforeEach(() => {
|
password: '1234',
|
||||||
authenticateClient = setupAuthenticateClient({
|
})
|
||||||
id: 'u7',
|
authenticatedUser = await nonModerator.toJson()
|
||||||
email: 'moderator@example.org',
|
})
|
||||||
password: '1234',
|
it('throws authorization error', async () => {
|
||||||
role: 'moderator',
|
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
|
||||||
|
errors: [{ message: 'Not Authorised!' }],
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('on something that is not a (Comment|Post|User) ', () => {
|
describe('moderator', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
variables = {
|
authenticatedUser = await moderator.toJson()
|
||||||
id: 't23',
|
|
||||||
}
|
|
||||||
createResource = () => {
|
|
||||||
return Promise.all([factory.create('Tag', { id: 't23' })])
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns null', async () => {
|
describe('moderate a resource that is not a (Comment|Post|User) ', () => {
|
||||||
const expected = { disable: null }
|
beforeEach(async () => {
|
||||||
await setup()
|
variables = {
|
||||||
await expect(action()).resolves.toEqual(expected)
|
id: 'sample-tag-id',
|
||||||
})
|
}
|
||||||
})
|
await factory.create('Tag', { id: 'sample-tag-id' })
|
||||||
|
})
|
||||||
|
|
||||||
describe('on a comment', () => {
|
it('returns null', async () => {
|
||||||
beforeEach(async () => {
|
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
|
||||||
variables = {
|
data: { disable: null },
|
||||||
id: 'c47',
|
|
||||||
}
|
|
||||||
createPostVariables = {
|
|
||||||
id: 'p3',
|
|
||||||
title: 'post to comment on',
|
|
||||||
content: 'please comment on me',
|
|
||||||
categoryIds,
|
|
||||||
}
|
|
||||||
createCommentVariables = {
|
|
||||||
id: 'c47',
|
|
||||||
postId: 'p3',
|
|
||||||
content: 'this comment was created for this post',
|
|
||||||
}
|
|
||||||
createResource = async () => {
|
|
||||||
await factory.create('User', {
|
|
||||||
id: 'u45',
|
|
||||||
email: 'commenter@example.org',
|
|
||||||
password: '1234',
|
|
||||||
})
|
})
|
||||||
const asAuthenticatedUser = await factory.authenticateAs({
|
})
|
||||||
email: 'commenter@example.org',
|
})
|
||||||
password: '1234',
|
|
||||||
|
describe('moderate a comment', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
variables = {}
|
||||||
|
await factory.create('Comment', {
|
||||||
|
id: 'comment-id',
|
||||||
})
|
})
|
||||||
await asAuthenticatedUser.create('Post', createPostVariables)
|
})
|
||||||
await asAuthenticatedUser.create('Comment', createCommentVariables)
|
|
||||||
}
|
it('returns disabled resource id', async () => {
|
||||||
|
variables = { id: 'comment-id' }
|
||||||
|
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { disable: 'comment-id' },
|
||||||
|
errors: undefined,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
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: disableMutation, 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: disableMutation, 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', () => {
|
||||||
const expected = { disable: 'c47' }
|
beforeEach(async () => {
|
||||||
await setup()
|
variables = {}
|
||||||
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 setup()
|
|
||||||
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 setup()
|
|
||||||
await expect(client.request('{ Comment { id disabled } }')).resolves.toEqual(before)
|
|
||||||
await action()
|
|
||||||
await expect(
|
|
||||||
client.request('{ Comment(disabled: true) { id disabled } }'),
|
|
||||||
).resolves.toEqual(expected)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('on a post', () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
variables = {
|
|
||||||
id: 'p9',
|
|
||||||
}
|
|
||||||
|
|
||||||
createResource = async () => {
|
|
||||||
await factory.create('User', { email: 'author@example.org', password: '1234' })
|
|
||||||
await factory.authenticateAs({ email: 'author@example.org', password: '1234' })
|
|
||||||
await factory.create('Post', {
|
await factory.create('Post', {
|
||||||
id: 'p9', // that's the ID we will look for
|
id: 'sample-post-id',
|
||||||
categoryIds,
|
|
||||||
})
|
})
|
||||||
}
|
})
|
||||||
|
|
||||||
|
it('returns disabled resource id', async () => {
|
||||||
|
variables = { id: 'sample-post-id' }
|
||||||
|
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { disable: 'sample-post-id' },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('changes .disabledBy', async () => {
|
||||||
|
variables = { id: 'sample-post-id' }
|
||||||
|
const before = { data: { Post: [{ id: 'sample-post-id', disabledBy: null }] } }
|
||||||
|
const expected = {
|
||||||
|
data: { Post: [{ id: 'sample-post-id', disabledBy: { id: 'moderator-id' } }] },
|
||||||
|
}
|
||||||
|
|
||||||
|
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before)
|
||||||
|
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { disable: 'sample-post-id' },
|
||||||
|
})
|
||||||
|
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('updates .disabled on post', async () => {
|
||||||
|
const before = { data: { Post: [{ id: 'sample-post-id', disabled: false }] } }
|
||||||
|
const expected = { data: { Post: [{ id: 'sample-post-id', disabled: true }] } }
|
||||||
|
variables = { id: 'sample-post-id' }
|
||||||
|
|
||||||
|
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before)
|
||||||
|
await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { disable: 'sample-post-id' },
|
||||||
|
})
|
||||||
|
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('enable', () => {
|
||||||
|
describe('unautenticated user', () => {
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
variables = { id: 'sample-post-id' }
|
||||||
|
await expect(mutate({ mutation: enableMutation, variables })).resolves.toMatchObject({
|
||||||
|
errors: [{ message: 'Not Authorised!' }],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('authenticated user', () => {
|
||||||
|
describe('non moderator', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
nonModerator = await factory.create('User', {
|
||||||
|
id: 'non-moderator',
|
||||||
|
name: 'Non Moderator',
|
||||||
|
email: 'non.moderator@example.org',
|
||||||
|
password: '1234',
|
||||||
|
})
|
||||||
|
authenticatedUser = await nonModerator.toJson()
|
||||||
|
})
|
||||||
|
it('throws authorization error', async () => {
|
||||||
|
variables = { id: 'sample-post-id' }
|
||||||
|
await expect(mutate({ mutation: enableMutation, variables })).resolves.toMatchObject({
|
||||||
|
errors: [{ message: 'Not Authorised!' }],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('moderator', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
authenticatedUser = await moderator.toJson()
|
||||||
|
})
|
||||||
|
describe('moderate a resource that is not a (Comment|Post|User) ', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
await Promise.all([factory.create('Tag', { id: 'sample-tag-id' })])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns null', async () => {
|
||||||
|
await expect(
|
||||||
|
mutate({ mutation: enableMutation, variables: { id: 'sample-tag-id' } }),
|
||||||
|
).resolves.toMatchObject({
|
||||||
|
data: { enable: null },
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns disabled resource id', async () => {
|
describe('moderate a comment', () => {
|
||||||
const expected = { disable: 'p9' }
|
beforeEach(async () => {
|
||||||
await setup()
|
variables = { id: 'comment-id' }
|
||||||
await expect(action()).resolves.toEqual(expected)
|
await factory.create('Comment', {
|
||||||
|
id: 'comment-id',
|
||||||
|
})
|
||||||
|
await mutate({ mutation: disableMutation, variables })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns enabled resource id', async () => {
|
||||||
|
await expect(mutate({ mutation: enableMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { enable: 'comment-id' },
|
||||||
|
errors: undefined,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('changes .disabledBy', async () => {
|
||||||
|
const expected = {
|
||||||
|
data: { Comment: [{ id: 'comment-id', disabledBy: null }] },
|
||||||
|
errors: undefined,
|
||||||
|
}
|
||||||
|
await expect(mutate({ mutation: enableMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { enable: 'comment-id' },
|
||||||
|
errors: undefined,
|
||||||
|
})
|
||||||
|
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('updates .disabled on comment', async () => {
|
||||||
|
const expected = {
|
||||||
|
data: { Comment: [{ id: 'comment-id', disabled: false }] },
|
||||||
|
errors: undefined,
|
||||||
|
}
|
||||||
|
|
||||||
|
await expect(mutate({ mutation: enableMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { enable: 'comment-id' },
|
||||||
|
errors: undefined,
|
||||||
|
})
|
||||||
|
await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('changes .disabledBy', async () => {
|
describe('moderate a post', () => {
|
||||||
const before = { Post: [{ id: 'p9', disabledBy: null }] }
|
beforeEach(async () => {
|
||||||
const expected = { Post: [{ id: 'p9', disabledBy: { id: 'u7' } }] }
|
variables = { id: 'post-id' }
|
||||||
|
await factory.create('Post', {
|
||||||
|
id: 'post-id',
|
||||||
|
})
|
||||||
|
await mutate({ mutation: disableMutation, variables })
|
||||||
|
})
|
||||||
|
|
||||||
await setup()
|
it('returns enabled resource id', async () => {
|
||||||
await expect(client.request('{ Post { id, disabledBy { id } } }')).resolves.toEqual(
|
await expect(mutate({ mutation: enableMutation, variables })).resolves.toMatchObject({
|
||||||
before,
|
data: { enable: 'post-id' },
|
||||||
)
|
errors: undefined,
|
||||||
await action()
|
})
|
||||||
await expect(
|
})
|
||||||
client.request('{ Post(disabled: true) { id, disabledBy { id } } }'),
|
|
||||||
).resolves.toEqual(expected)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('updates .disabled on post', async () => {
|
it('changes .disabledBy', async () => {
|
||||||
const before = { Post: [{ id: 'p9', disabled: false }] }
|
const expected = {
|
||||||
const expected = { Post: [{ id: 'p9', disabled: true }] }
|
data: { Post: [{ id: 'post-id', disabledBy: null }] },
|
||||||
|
errors: undefined,
|
||||||
|
}
|
||||||
|
await expect(mutate({ mutation: enableMutation, variables })).resolves.toMatchObject({
|
||||||
|
data: { enable: 'post-id' },
|
||||||
|
errors: undefined,
|
||||||
|
})
|
||||||
|
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
|
||||||
|
})
|
||||||
|
|
||||||
await setup()
|
it('updates .disabled on post', async () => {
|
||||||
await expect(client.request('{ Post { id disabled } }')).resolves.toEqual(before)
|
const expected = {
|
||||||
await action()
|
data: { Post: [{ id: 'post-id', disabled: false }] },
|
||||||
await expect(client.request('{ Post(disabled: true) { id disabled } }')).resolves.toEqual(
|
errors: undefined,
|
||||||
expected,
|
}
|
||||||
)
|
|
||||||
})
|
await expect(mutate({ mutation: enableMutation, variables })).resolves.toMatchObject({
|
||||||
})
|
data: { enable: 'post-id' },
|
||||||
})
|
errors: undefined,
|
||||||
})
|
})
|
||||||
})
|
await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected)
|
||||||
|
})
|
||||||
describe('enable', () => {
|
|
||||||
const mutation = gql`
|
|
||||||
mutation($id: ID!) {
|
|
||||||
enable(id: $id)
|
|
||||||
}
|
|
||||||
`
|
|
||||||
let variables
|
|
||||||
|
|
||||||
const action = async () => {
|
|
||||||
return client.request(mutation, variables)
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
// our defaul set of variables
|
|
||||||
variables = {
|
|
||||||
id: 'blabla',
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws authorization error', async () => {
|
|
||||||
await setup()
|
|
||||||
await expect(action()).rejects.toThrow('Not Authorised')
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('authenticated', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
authenticateClient = setupAuthenticateClient({
|
|
||||||
email: 'user@example.org',
|
|
||||||
password: '1234',
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws authorization error', async () => {
|
|
||||||
await setup()
|
|
||||||
await expect(action()).rejects.toThrow('Not Authorised')
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('as moderator', () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
authenticateClient = setupAuthenticateClient({
|
|
||||||
role: 'moderator',
|
|
||||||
email: 'someuser@example.org',
|
|
||||||
password: '1234',
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('on something that is not a (Comment|Post|User) ', () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
variables = {
|
|
||||||
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()
|
|
||||||
await expect(action()).resolves.toEqual(expected)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('on a comment', () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
variables = {
|
|
||||||
id: 'c456',
|
|
||||||
}
|
|
||||||
createPostVariables = {
|
|
||||||
id: 'p9',
|
|
||||||
title: 'post to comment on',
|
|
||||||
content: 'please comment on me',
|
|
||||||
categoryIds,
|
|
||||||
}
|
|
||||||
createCommentVariables = {
|
|
||||||
id: 'c456',
|
|
||||||
postId: 'p9',
|
|
||||||
content: 'this comment was created for this post',
|
|
||||||
}
|
|
||||||
createResource = async () => {
|
|
||||||
await factory.create('User', {
|
|
||||||
id: 'u123',
|
|
||||||
email: 'author@example.org',
|
|
||||||
password: '1234',
|
|
||||||
})
|
|
||||||
const asAuthenticatedUser = await factory.authenticateAs({
|
|
||||||
email: 'author@example.org',
|
|
||||||
password: '1234',
|
|
||||||
})
|
|
||||||
await asAuthenticatedUser.create('Post', createPostVariables)
|
|
||||||
await asAuthenticatedUser.create('Comment', createCommentVariables)
|
|
||||||
|
|
||||||
const disableMutation = gql`
|
|
||||||
mutation {
|
|
||||||
disable(id: "c456")
|
|
||||||
}
|
|
||||||
`
|
|
||||||
await factory.mutate(disableMutation) // that's we want to delete
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns disabled resource id', async () => {
|
|
||||||
const expected = { enable: 'c456' }
|
|
||||||
await setup()
|
|
||||||
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 setup()
|
|
||||||
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 setup()
|
|
||||||
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)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('on a post', () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
variables = {
|
|
||||||
id: 'p9',
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
categoryIds,
|
|
||||||
})
|
|
||||||
|
|
||||||
const disableMutation = gql`
|
|
||||||
mutation {
|
|
||||||
disable(id: "p9")
|
|
||||||
}
|
|
||||||
`
|
|
||||||
await factory.mutate(disableMutation) // that's we want to delete
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it('returns disabled resource id', async () => {
|
|
||||||
const expected = { enable: 'p9' }
|
|
||||||
await setup()
|
|
||||||
await expect(action()).resolves.toEqual(expected)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('changes .disabledBy', async () => {
|
|
||||||
const before = { Post: [{ id: 'p9', disabledBy: { id: 'u123' } }] }
|
|
||||||
const expected = { Post: [{ id: 'p9', disabledBy: null }] }
|
|
||||||
|
|
||||||
await setup()
|
|
||||||
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 setup()
|
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user