Refactor report mutation, return a proper Report

This commit is contained in:
Robert Schäfer 2019-03-07 22:42:32 +01:00
parent 61a45d39ea
commit 0529e2b2b0
3 changed files with 128 additions and 67 deletions

View File

@ -12,36 +12,51 @@ export default {
} }
const res = await session.run(` const res = await session.run(`
MATCH (author:User {id: $userId}) MATCH (submitter:User {id: $userId})
MATCH (resource {id: $resourceId}) MATCH (resource {id: $resourceId})
CREATE (report:Report $reportData) CREATE (report:Report $reportData)
MERGE (resource)<-[:REPORTED]-(report) MERGE (resource)<-[:REPORTED]-(report)
MERGE (report)<-[:REPORTED]-(author) MERGE (report)<-[:REPORTED]-(submitter)
RETURN report, author, resource RETURN report, submitter, resource, labels(resource)[0] as type
`, { `, {
resourceId: id, resourceId: id,
userId: user.id, userId: user.id,
reportData reportData
} }
) )
const [{ report, author, resource }] = res.records.map(r => { session.close()
const [dbResponse] = res.records.map(r => {
return { return {
report: r.get('report'), report: r.get('report'),
author: r.get('author'), submitter: r.get('submitter'),
resource: r.get('resource') resource: r.get('resource'),
type: r.get('type')
} }
}) })
session.close() if(!dbResponse) return null
console.log(report) const { report, submitter, resource, type } = dbResponse
console.log(author)
// TODO: output Report compatible object let response = {
return {
...report.properties, ...report.properties,
reporter: author.properties, post: null,
user: resource.properties, comment: null,
type: 'blablabla' user: null,
submitter: submitter.properties,
type
} }
switch(type){
case "Post":
response.post = resource.properties
break;
case "Comment":
response.comment = resource.properties
break;
case "User":
response.user = resource.properties
break;
}
return response
} }
} }
} }

View File

@ -7,6 +7,9 @@ const factory = Factory()
describe('report', () => { describe('report', () => {
let mutation let mutation
let headers let headers
let returnedObject
let variables
beforeEach(async () => { beforeEach(async () => {
headers = {} headers = {}
await factory.create('User', { await factory.create('User', {
@ -20,14 +23,6 @@ describe('report', () => {
role: 'user', role: 'user',
email: 'abusive-user@example.org' email: 'abusive-user@example.org'
}) })
mutation = `
mutation {
report(
id: "u2",
description: "I don't like this user"
) { description }
}
`
}) })
afterEach(async () => { afterEach(async () => {
@ -36,8 +31,17 @@ describe('report', () => {
let client let client
const action = () => { const action = () => {
mutation = `
mutation($id: ID!) {
report(
id: $id,
description: "Violates code of conduct"
) ${returnedObject || '{ description }'}
}
`
variables = variables || { id: 'whatever' }
client = new GraphQLClient(host, { headers }) client = new GraphQLClient(host, { headers })
return client.request(mutation) return client.request(mutation, variables)
} }
describe('unauthenticated', () => { describe('unauthenticated', () => {
@ -50,55 +54,97 @@ describe('report', () => {
headers = await login({ email: 'test@example.org', password: '1234' }) headers = await login({ email: 'test@example.org', password: '1234' })
}) })
it('creates a report', async () => { describe('invalid resource id', () => {
await expect(action()).resolves.toEqual({ it('returns null', async () => {
report: { description: 'I don\'t like this user' } await expect(action()).resolves.toEqual({
report: null
})
}) })
}) })
it('returns the reporter', async () => { describe('valid resource id', () => {
mutation = ` beforeEach(async () => {
mutation { variables = {id: 'u2'}
report(
id: "u2",
description: "I don't like this user"
) { reporter {
email
} }
}
`
await expect(action()).resolves.toEqual({
report: { reporter: { email: 'test@example.org' } }
}) })
})
it('returns type', async () => { it('creates a report', async () => {
mutation = ` await expect(action()).resolves.toEqual({
mutation { report: { description: 'Violates code of conduct' }
report( })
id: "u2",
description: "I don't like this user"
) { type }
}
`
await expect(action()).resolves.toEqual({
report: { type: 'User' }
}) })
})
it('returns user', async () => { it('returns the submitter', async () => {
mutation = ` returnedObject = '{ submitter { email } }'
mutation { await expect(action()).resolves.toEqual({
report( report: { submitter: { email: 'test@example.org' } }
id: "u2", })
description: "I don't like this user" })
) { user {
name describe('reported resource is a user', () => {
} } it('returns type "User"', async () => {
} returnedObject = '{ type }'
` await expect(action()).resolves.toEqual({
await expect(action()).resolves.toEqual({ report: { type: 'User' }
report: { user: { name: 'abusive-user' } } })
})
it('returns resource in user attribute', async () => {
returnedObject = '{ user { name } }'
await expect(action()).resolves.toEqual({
report: { user: { name: 'abusive-user' } }
})
})
})
describe('reported resource is a post', () => {
beforeEach(async () => {
await factory.authenticateAs({email: 'test@example.org', password: '1234'})
await factory.create('Post', {id: 'p23', title: 'Matt and Robert having a pair-programming' })
variables = { id: 'p23' }
})
it('returns type "Post"', async () => {
returnedObject = '{ type }'
await expect(action()).resolves.toEqual({
report: { type: 'Post' }
})
})
it('returns resource in post attribute', async () => {
returnedObject = '{ post { title } }'
await expect(action()).resolves.toEqual({
report: { post: { title: 'Matt and Robert having a pair-programming' } }
})
})
it('returns null in user attribute', async () => {
returnedObject = '{ user { name } }'
await expect(action()).resolves.toEqual({
report: { user: null }
})
})
})
describe('reported resource is a comment', () => {
beforeEach(async () => {
await factory.authenticateAs({email: 'test@example.org', password: '1234'})
await factory.create('Comment', {id: 'c34', content: 'Robert getting tired.' })
variables = { id: 'c34' }
})
it('returns type "Comment"', async () => {
returnedObject = '{ type }'
await expect(action()).resolves.toEqual({
report: { type: 'Comment' }
})
})
it('returns resource in comment attribute', async () => {
returnedObject = '{ comment { content } }'
await expect(action()).resolves.toEqual({
report: { comment: { content: 'Robert getting tired.' } }
})
})
}) })
}) })
}) })

View File

@ -160,12 +160,12 @@ type Comment {
type Report { type Report {
id: ID! id: ID!
reporter: User @relation(name: "REPORTED", direction: "IN") submitter: User @relation(name: "REPORTED", direction: "IN")
description: String description: String
type: String! @cypher(statement: "MATCH (resource)<-[:REPORTED]-(this) RETURN labels(resource)[0]") type: String! @cypher(statement: "MATCH (resource)<-[:REPORTED]-(this) RETURN labels(resource)[0]")
createdAt: String createdAt: String
comment: Comment @relation(name: "REPORTED", direction: "OUT") comment: Comment @relation(name: "REPORTED", direction: "OUT")
contribution: Post @relation(name: "REPORTED", direction: "OUT") post: Post @relation(name: "REPORTED", direction: "OUT")
user: User @relation(name: "REPORTED", direction: "OUT") user: User @relation(name: "REPORTED", direction: "OUT")
} }