Disable/enable fullfills tests

This commit is contained in:
Robert Schäfer 2019-03-06 00:55:26 +01:00
parent 0a73ddd46d
commit 5cff508bd6
7 changed files with 73 additions and 44 deletions

View File

@ -1,4 +1,4 @@
import { rule, shield, allow, and, or } from 'graphql-shield'
import { rule, shield, allow, or } from 'graphql-shield'
/*
* TODO: implement
@ -41,12 +41,6 @@ const isAuthor = rule({ cache: 'no_cache' })(async (parent, args, { user, driver
return authorId === user.id
})
const fromUserMatchesCurrentUser = rule({ cache: 'no_cache' })(async (parent, args, { user, driver }) => {
if (!user) return false
const { from: { id: fromId } } = args
return user.id === fromId
})
// Permissions
const permissions = shield({
Query: {
@ -64,7 +58,7 @@ const permissions = shield({
DeleteBadge: isAdmin,
enable: isModerator,
disable: isModerator,
disable: isModerator
// addFruitToBasket: isAuthenticated
// CreateUser: allow,
},

View File

@ -20,7 +20,15 @@ beforeEach(async () => {
])
const moderatorFactory = Factory()
await moderatorFactory.authenticateAs({ email: 'moderator@example.org', password: '1234' })
await moderatorFactory.relate('Post', 'DisabledBy', { from: 'm1', to: 'p2' })
const disableMutation = `
mutation {
disable(resource: {
id: "p2"
type: contribution
})
}
`
await moderatorFactory.mutate(disableMutation)
})
afterEach(async () => {

View File

@ -1,10 +1,30 @@
export default {
Mutation: {
disable: async (object, params, {user, driver}) => {
return true
disable: async (object, params, { user, driver }) => {
const { resource: { id: postId } } = params
const { id: userId } = user
const cypher = `
MATCH (u:User {id: $userId})
MATCH (p:Post {id: $postId})
SET p.disabled = true
MERGE (p)<-[:DISABLED]-(u)
`
const session = driver.session()
const res = await session.run(cypher, { postId, userId })
session.close()
return Boolean(res)
},
enable: async (object, params, {user, driver}) => {
return true
enable: async (object, params, { user, driver }) => {
const { resource: { id: postId } } = params
const cypher = `
MATCH (p:Post {id: $postId})<-[d:DISABLED]-()
SET p.disabled = false
DELETE d
`
const session = driver.session()
const res = await session.run(cypher, { postId })
session.close()
return Boolean(res)
}
}
}

View File

@ -65,8 +65,8 @@ describe('disable', () => {
await expect(client.request(mutation)).resolves.toEqual(expected)
})
it('sets current user', async () => {
const before = { Post: [{ id: 'p9', disabledBy: null }] }
it('changes .disabledBy', async () => {
const before = { Post: [{ id: 'p9', disabledBy: null }] }
const expected = { Post: [{ id: 'p9', disabledBy: { id: 'u7' } }] }
await expect(client.request(
@ -101,10 +101,15 @@ describe('enable', () => {
await factory.create('Post', {
id: 'p9' // that's the ID we will look for
})
await factory.relate('Post', 'DisabledBy', {
from: 'u123',
to: 'p9'
}) // that's we want to delete
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
@ -115,7 +120,6 @@ describe('enable', () => {
client = new GraphQLClient(host, { headers })
}
const mutation = `
mutation {
enable(resource: {
@ -153,6 +157,19 @@ describe('enable', () => {
await expect(client.request(mutation)).resolves.toEqual(expected)
})
it('changes .disabledBy', async () => {
const before = { Post: [{ id: 'p9', disabledBy: { id: 'u123' } }] }
const expected = { Post: [{ id: 'p9', disabledBy: null }] }
await expect(client.request(
'{ Post(disabled: true) { id, disabledBy { id } } }'
)).resolves.toEqual(before)
await client.request(mutation)
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 } ] }

View File

@ -2,26 +2,6 @@ import { neo4jgraphql } from 'neo4j-graphql-js'
export default {
Mutation: {
AddPostDisabledBy: async (object, params, context, resolveInfo) => {
const { to: { id: postId } } = params
const session = context.driver.session()
await session.run(`
MATCH (p:Post {id: $postId})
SET p.disabled = true`, { postId })
session.close()
return neo4jgraphql(object, params, context, resolveInfo, false)
},
RemovePostDisabledBy: async (object, params, context, resolveInfo) => {
const { to: { id: postId } } = params
const session = context.driver.session()
await session.run(`
MATCH (p:Post {id: $postId})
SET p.disabled = false`, { postId })
session.close()
return neo4jgraphql(object, params, context, resolveInfo, false)
},
CreatePost: async (object, params, context, resolveInfo) => {
const result = await neo4jgraphql(object, params, context, resolveInfo, false)

View File

@ -86,6 +86,10 @@ export default function Factory (options = {}) {
this.lastResponse = await this.graphQLClient.request(mutation)
return this
},
async mutate (mutation, variables) {
this.lastResponse = await this.graphQLClient.request(mutation, variables)
return this
},
async cleanDatabase () {
this.lastResponse = await cleanDatabase({ driver: this.neo4jDriver })
return this
@ -94,6 +98,7 @@ export default function Factory (options = {}) {
result.authenticateAs.bind(result)
result.create.bind(result)
result.relate.bind(result)
result.mutate.bind(result)
result.cleanDatabase.bind(result)
return result
}

View File

@ -98,10 +98,15 @@ import Factory from './factories'
asTick.create('Post', { id: 'p15' })
])
await asModerator.relate('Post', 'DisabledBy', {
from: 'u2',
to: 'p15'
})
const disableMutation = `
mutation {
disable(resource: {
id: "p11"
type: contribution
})
}
`
await asModerator.mutate(disableMutation)
await Promise.all([
f.relate('Post', 'Categories', { from: 'p0', to: 'cat16' }),