From cf5042d9a426a80cea2d726068be44de11cf7f50 Mon Sep 17 00:00:00 2001 From: aonomike Date: Sat, 19 Oct 2019 11:35:36 +0300 Subject: [PATCH 1/8] Refactor report resolver spec - Set up the apollo server testing - Refactor unauthenticated path for the mutation --- backend/src/schema/resolvers/reports.spec.js | 1063 ++++++++++-------- 1 file changed, 564 insertions(+), 499 deletions(-) diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index 4022be1b1..2c853d1fa 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -1,521 +1,586 @@ -import { GraphQLClient } from 'graphql-request' -import Factory from '../../seed/factories' -import { host, login, gql } from '../../jest/helpers' -import { getDriver, neode } from '../../bootstrap/neo4j' import { createTestClient } from 'apollo-server-testing' import createServer from '../.././server' +import Factory from '../../seed/factories' +import { gql } from '../../jest/helpers' +import { getDriver, neode as getNeode } from '../../bootstrap/neo4j' const factory = Factory() -const instance = neode() +const instance = getNeode() const driver = getDriver() -describe('report mutation', () => { - let reportMutation - let headers - let client - let variables - let createPostVariables - let user - const categoryIds = ['cat9'] - - const action = () => { - reportMutation = gql` - mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { - report( - resourceId: $resourceId - reasonCategory: $reasonCategory - reasonDescription: $reasonDescription - ) { - createdAt - reasonCategory - reasonDescription - type - submitter { - email - } - user { - name - } - post { - title - } - comment { - content - } - } - } - ` - client = new GraphQLClient(host, { - headers, - }) - return client.request(reportMutation, variables) - } - - beforeEach(async () => { - variables = { - resourceId: 'whatever', - reasonCategory: 'other', - reasonDescription: 'Violates code of conduct !!!', - } - headers = {} - user = await factory.create('User', { - id: 'u1', - role: 'user', - email: 'test@example.org', - password: '1234', - }) - await factory.create('User', { - id: 'u2', - role: 'user', - name: 'abusive-user', - email: 'abusive-user@example.org', - }) - await instance.create('Category', { - id: 'cat9', - name: 'Democracy & Politics', - icon: 'university', - }) - }) - - afterEach(async () => { - await factory.cleanDatabase() - }) - - describe('unauthenticated', () => { - it('throws authorization error', async () => { - await expect(action()).rejects.toThrow('Not Authorised') - }) - }) - - describe('authenticated', () => { - beforeEach(async () => { - headers = await login({ - email: 'test@example.org', - password: '1234', - }) - }) - - describe('invalid resource id', () => { - it('returns null', async () => { - await expect(action()).resolves.toEqual({ - report: null, - }) - }) - }) - - describe('valid resource id', () => { - describe('reported resource is a user', () => { - beforeEach(async () => { - variables = { - ...variables, - resourceId: 'u2', - } - }) - - it('returns type "User"', async () => { - await expect(action()).resolves.toMatchObject({ - report: { - type: 'User', - }, - }) - }) - - it('returns resource in user attribute', async () => { - await expect(action()).resolves.toMatchObject({ - report: { - user: { - name: 'abusive-user', - }, - }, - }) - }) - - it('returns the submitter', async () => { - await expect(action()).resolves.toMatchObject({ - report: { - submitter: { - email: 'test@example.org', - }, - }, - }) - }) - - it('returns a date', async () => { - await expect(action()).resolves.toMatchObject({ - report: { - createdAt: expect.any(String), - }, - }) - }) - - it('returns the reason category', async () => { - variables = { - ...variables, - reasonCategory: 'criminal_behavior_violation_german_law', - } - await expect(action()).resolves.toMatchObject({ - report: { - reasonCategory: 'criminal_behavior_violation_german_law', - }, - }) - }) - - it('gives an error if the reason category is not in enum "ReasonCategory"', async () => { - variables = { - ...variables, - reasonCategory: 'my_category', - } - await expect(action()).rejects.toThrow( - 'got invalid value "my_category"; Expected type ReasonCategory', - ) - }) - - it('returns the reason description', async () => { - variables = { - ...variables, - reasonDescription: 'My reason!', - } - await expect(action()).resolves.toMatchObject({ - report: { - reasonDescription: 'My reason!', - }, - }) - }) - - it('sanitize the reason description', async () => { - variables = { - ...variables, - reasonDescription: 'My reason !', - } - await expect(action()).resolves.toMatchObject({ - report: { - reasonDescription: 'My reason !', - }, - }) - }) - }) - - describe('reported resource is a post', () => { - beforeEach(async () => { - await factory.create('Post', { - author: user, - id: 'p23', - title: 'Matt and Robert having a pair-programming', - categoryIds, - }) - variables = { - ...variables, - resourceId: 'p23', - } - }) - - it('returns type "Post"', async () => { - await expect(action()).resolves.toMatchObject({ - report: { - type: 'Post', - }, - }) - }) - - it('returns resource in post attribute', async () => { - await expect(action()).resolves.toMatchObject({ - report: { - post: { - title: 'Matt and Robert having a pair-programming', - }, - }, - }) - }) - - it('returns null in user attribute', async () => { - await expect(action()).resolves.toMatchObject({ - report: { - user: null, - }, - }) - }) - }) - - /* An der Stelle würde ich den p23 noch mal prüfen, diesmal muss aber eine error meldung kommen. - At this point I would check the p23 again, but this time there must be an error message. */ - - describe('reported resource is a comment', () => { - beforeEach(async () => { - createPostVariables = { - id: 'p1', - title: 'post to comment on', - content: 'please comment on me', - categoryIds, - } - await factory.create('Post', { ...createPostVariables, author: user }) - await factory.create('Comment', { - author: user, - postId: 'p1', - id: 'c34', - content: 'Robert getting tired.', - }) - variables = { - ...variables, - resourceId: 'c34', - } - }) - - it('returns type "Comment"', async () => { - await expect(action()).resolves.toMatchObject({ - report: { - type: 'Comment', - }, - }) - }) - - it('returns resource in comment attribute', async () => { - await expect(action()).resolves.toMatchObject({ - report: { - comment: { - content: 'Robert getting tired.', - }, - }, - }) - }) - }) - - /* An der Stelle würde ich den c34 noch mal prüfen, diesmal muss aber eine error meldung kommen. - At this point I would check the c34 again, but this time there must be an error message. */ - - describe('reported resource is a tag', () => { - beforeEach(async () => { - await factory.create('Tag', { - id: 't23', - }) - variables = { - ...variables, - resourceId: 't23', - } - }) - - it('returns null', async () => { - await expect(action()).resolves.toMatchObject({ - report: null, - }) - }) - }) - - /* An der Stelle würde ich den t23 noch mal prüfen, diesmal muss aber eine error meldung kommen. - At this point I would check the t23 again, but this time there must be an error message. */ - }) - }) -}) - -describe('reports query', () => { - let query, mutate, authenticatedUser, moderator, user, author - const categoryIds = ['cat9'] - +describe('report',()=>{ + let authenticatedUser, mutate, query const reportMutation = gql` - mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { - report( - resourceId: $resourceId - reasonCategory: $reasonCategory - reasonDescription: $reasonDescription - ) { - type + mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { + report( + resourceId: $resourceId + reasonCategory: $reasonCategory + reasonDescription: $reasonDescription + ) { + createdAt + reasonCategory + reasonDescription + type + submitter { + email + } + user { + name + } + post { + title + } + comment { + content } } - ` - const reportsQuery = gql` - query { - reports(orderBy: createdAt_desc) { - createdAt - reasonCategory - reasonDescription - submitter { - id - } - type - user { - id - } - post { - id - } - comment { - id - } - } - } - ` + } +` +const variables = { + resourceId: 'whatever', + reasonCategory: 'other', + reasonDescription: 'Violates code of conduct !!!', +} - beforeAll(async () => { - await factory.cleanDatabase() - const { server } = createServer({ - context: () => { + beforeAll(()=>{ + const {server} = createServer({ + context: ()=>{ return { driver, - user: authenticatedUser, + neode: instance, + user: authenticatedUser } - }, - }) - query = createTestClient(server).query - mutate = createTestClient(server).mutate + + } + } + ) + mutate = createTestClient(server).mutate + query = createTestClient(server).query }) - beforeEach(async () => { - authenticatedUser = null - - moderator = await factory.create('User', { - id: 'mod1', - role: 'moderator', - email: 'moderator@example.org', - password: '1234', - }) - user = await factory.create('User', { - id: 'user1', - role: 'user', - email: 'test@example.org', - password: '1234', - }) - author = await factory.create('User', { - id: 'auth1', - role: 'user', - name: 'abusive-user', - email: 'abusive-user@example.org', - }) - await instance.create('Category', { - id: 'cat9', - name: 'Democracy & Politics', - icon: 'university', - }) - - await Promise.all([ - factory.create('Post', { - author, - id: 'p1', - categoryIds, - content: 'Interesting Knowledge', - }), - factory.create('Post', { - author: moderator, - id: 'p2', - categoryIds, - content: 'More things to do …', - }), - factory.create('Post', { - author: user, - id: 'p3', - categoryIds, - content: 'I am at school …', - }), - ]) - await Promise.all([ - factory.create('Comment', { - author: user, - id: 'c1', - postId: 'p1', - }), - ]) - - authenticatedUser = await user.toJson() - await Promise.all([ - mutate({ - mutation: reportMutation, - variables: { - resourceId: 'p1', - reasonCategory: 'other', - reasonDescription: 'This comment is bigoted', - }, - }), - mutate({ - mutation: reportMutation, - variables: { - resourceId: 'c1', - reasonCategory: 'discrimination_etc', - reasonDescription: 'This post is bigoted', - }, - }), - mutate({ - mutation: reportMutation, - variables: { - resourceId: 'auth1', - reasonCategory: 'doxing', - reasonDescription: 'This user is harassing me with bigoted remarks', - }, - }), - ]) - authenticatedUser = null - }) - - afterEach(async () => { + afterEach(async()=>{ await factory.cleanDatabase() }) - describe('unauthenticated', () => { - it('throws authorization error', async () => { - authenticatedUser = null - expect(query({ query: reportsQuery })).resolves.toMatchObject({ - data: { reports: null }, - errors: [{ message: 'Not Authorised!' }], + describe('create report', ()=>{ + describe('unauthenticated',()=>{ + it.only('throws authorization error', async() =>{ + await expect(mutate({mutation: reportMutation, variables})).resolves.toMatchObject({ + data: {report: null}, + errors: [{message: 'Not Authorised!'}] + }) }) }) - - it('role "user" gets no reports', async () => { - authenticatedUser = await user.toJson() - expect(query({ query: reportsQuery })).resolves.toMatchObject({ - data: { reports: null }, - errors: [{ message: 'Not Authorised!' }], - }) - }) - - it('role "moderator" gets reports', async () => { - const expected = { - // to check 'orderBy: createdAt_desc' is not possible here, because 'createdAt' does not differ - reports: expect.arrayContaining([ - expect.objectContaining({ - createdAt: expect.any(String), - reasonCategory: 'doxing', - reasonDescription: 'This user is harassing me with bigoted remarks', - submitter: expect.objectContaining({ - id: 'user1', - }), - type: 'User', - user: expect.objectContaining({ - id: 'auth1', - }), - post: null, - comment: null, - }), - expect.objectContaining({ - createdAt: expect.any(String), - reasonCategory: 'other', - reasonDescription: 'This comment is bigoted', - submitter: expect.objectContaining({ - id: 'user1', - }), - type: 'Post', - user: null, - post: expect.objectContaining({ - id: 'p1', - }), - comment: null, - }), - expect.objectContaining({ - createdAt: expect.any(String), - reasonCategory: 'discrimination_etc', - reasonDescription: 'This post is bigoted', - submitter: expect.objectContaining({ - id: 'user1', - }), - type: 'Comment', - user: null, - post: null, - comment: expect.objectContaining({ - id: 'c1', - }), - }), - ]), - } - - authenticatedUser = await moderator.toJson() - const { data } = await query({ query: reportsQuery }) - expect(data).toEqual(expected) - }) }) }) + +// describe('report mutation', () => { +// let reportMutation +// let headers +// let client +// let variables +// let createPostVariables +// let user +// const categoryIds = ['cat9'] + +// const action = () => { +// reportMutation = gql` +// mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { +// report( +// resourceId: $resourceId +// reasonCategory: $reasonCategory +// reasonDescription: $reasonDescription +// ) { +// createdAt +// reasonCategory +// reasonDescription +// type +// submitter { +// email +// } +// user { +// name +// } +// post { +// title +// } +// comment { +// content +// } +// } +// } +// ` +// client = new GraphQLClient(host, { +// headers, +// }) +// return client.request(reportMutation, variables) +// } + +// beforeEach(async () => { +// variables = { +// resourceId: 'whatever', +// reasonCategory: 'other', +// reasonDescription: 'Violates code of conduct !!!', +// } +// headers = {} +// user = await factory.create('User', { +// id: 'u1', +// role: 'user', +// email: 'test@example.org', +// password: '1234', +// }) +// await factory.create('User', { +// id: 'u2', +// role: 'user', +// name: 'abusive-user', +// email: 'abusive-user@example.org', +// }) +// await instance.create('Category', { +// id: 'cat9', +// name: 'Democracy & Politics', +// icon: 'university', +// }) +// }) + +// afterEach(async () => { +// await factory.cleanDatabase() +// }) + +// describe('unauthenticated', () => { +// it('throws authorization error', async () => { +// await expect(action()).rejects.toThrow('Not Authorised') +// }) +// }) + +// describe('authenticated', () => { +// beforeEach(async () => { +// headers = await login({ +// email: 'test@example.org', +// password: '1234', +// }) +// }) + +// describe('invalid resource id', () => { +// it('returns null', async () => { +// await expect(action()).resolves.toEqual({ +// report: null, +// }) +// }) +// }) + +// describe('valid resource id', () => { +// describe('reported resource is a user', () => { +// beforeEach(async () => { +// variables = { +// ...variables, +// resourceId: 'u2', +// } +// }) + +// it('returns type "User"', async () => { +// await expect(action()).resolves.toMatchObject({ +// report: { +// type: 'User', +// }, +// }) +// }) + +// it('returns resource in user attribute', async () => { +// await expect(action()).resolves.toMatchObject({ +// report: { +// user: { +// name: 'abusive-user', +// }, +// }, +// }) +// }) + +// it('returns the submitter', async () => { +// await expect(action()).resolves.toMatchObject({ +// report: { +// submitter: { +// email: 'test@example.org', +// }, +// }, +// }) +// }) + +// it('returns a date', async () => { +// await expect(action()).resolves.toMatchObject({ +// report: { +// createdAt: expect.any(String), +// }, +// }) +// }) + +// it('returns the reason category', async () => { +// variables = { +// ...variables, +// reasonCategory: 'criminal_behavior_violation_german_law', +// } +// await expect(action()).resolves.toMatchObject({ +// report: { +// reasonCategory: 'criminal_behavior_violation_german_law', +// }, +// }) +// }) + +// it('gives an error if the reason category is not in enum "ReasonCategory"', async () => { +// variables = { +// ...variables, +// reasonCategory: 'my_category', +// } +// await expect(action()).rejects.toThrow( +// 'got invalid value "my_category"; Expected type ReasonCategory', +// ) +// }) + +// it('returns the reason description', async () => { +// variables = { +// ...variables, +// reasonDescription: 'My reason!', +// } +// await expect(action()).resolves.toMatchObject({ +// report: { +// reasonDescription: 'My reason!', +// }, +// }) +// }) + +// it('sanitize the reason description', async () => { +// variables = { +// ...variables, +// reasonDescription: 'My reason !', +// } +// await expect(action()).resolves.toMatchObject({ +// report: { +// reasonDescription: 'My reason !', +// }, +// }) +// }) +// }) + +// describe('reported resource is a post', () => { +// beforeEach(async () => { +// await factory.create('Post', { +// author: user, +// id: 'p23', +// title: 'Matt and Robert having a pair-programming', +// categoryIds, +// }) +// variables = { +// ...variables, +// resourceId: 'p23', +// } +// }) + +// it('returns type "Post"', async () => { +// await expect(action()).resolves.toMatchObject({ +// report: { +// type: 'Post', +// }, +// }) +// }) + +// it('returns resource in post attribute', async () => { +// await expect(action()).resolves.toMatchObject({ +// report: { +// post: { +// title: 'Matt and Robert having a pair-programming', +// }, +// }, +// }) +// }) + +// it('returns null in user attribute', async () => { +// await expect(action()).resolves.toMatchObject({ +// report: { +// user: null, +// }, +// }) +// }) +// }) + +// /* An der Stelle würde ich den p23 noch mal prüfen, diesmal muss aber eine error meldung kommen. +// At this point I would check the p23 again, but this time there must be an error message. */ + +// describe('reported resource is a comment', () => { +// beforeEach(async () => { +// createPostVariables = { +// id: 'p1', +// title: 'post to comment on', +// content: 'please comment on me', +// categoryIds, +// } +// await factory.create('Post', { ...createPostVariables, author: user }) +// await factory.create('Comment', { +// author: user, +// postId: 'p1', +// id: 'c34', +// content: 'Robert getting tired.', +// }) +// variables = { +// ...variables, +// resourceId: 'c34', +// } +// }) + +// it('returns type "Comment"', async () => { +// await expect(action()).resolves.toMatchObject({ +// report: { +// type: 'Comment', +// }, +// }) +// }) + +// it('returns resource in comment attribute', async () => { +// await expect(action()).resolves.toMatchObject({ +// report: { +// comment: { +// content: 'Robert getting tired.', +// }, +// }, +// }) +// }) +// }) + +// /* An der Stelle würde ich den c34 noch mal prüfen, diesmal muss aber eine error meldung kommen. +// At this point I would check the c34 again, but this time there must be an error message. */ + +// describe('reported resource is a tag', () => { +// beforeEach(async () => { +// await factory.create('Tag', { +// id: 't23', +// }) +// variables = { +// ...variables, +// resourceId: 't23', +// } +// }) + +// it('returns null', async () => { +// await expect(action()).resolves.toMatchObject({ +// report: null, +// }) +// }) +// }) + +// /* An der Stelle würde ich den t23 noch mal prüfen, diesmal muss aber eine error meldung kommen. +// At this point I would check the t23 again, but this time there must be an error message. */ +// }) +// }) +// }) + +// describe('reports query', () => { +// let query, mutate, authenticatedUser, moderator, user, author +// const categoryIds = ['cat9'] + +// const reportMutation = gql` +// mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { +// report( +// resourceId: $resourceId +// reasonCategory: $reasonCategory +// reasonDescription: $reasonDescription +// ) { +// type +// } +// } +// ` +// const reportsQuery = gql` +// query { +// reports(orderBy: createdAt_desc) { +// createdAt +// reasonCategory +// reasonDescription +// submitter { +// id +// } +// type +// user { +// id +// } +// post { +// id +// } +// comment { +// id +// } +// } +// } +// ` + +// beforeAll(async () => { +// await factory.cleanDatabase() +// const { server } = createServer({ +// context: () => { +// return { +// driver, +// user: authenticatedUser, +// } +// }, +// }) +// query = createTestClient(server).query +// mutate = createTestClient(server).mutate +// }) + +// beforeEach(async () => { +// authenticatedUser = null + +// moderator = await factory.create('User', { +// id: 'mod1', +// role: 'moderator', +// email: 'moderator@example.org', +// password: '1234', +// }) +// user = await factory.create('User', { +// id: 'user1', +// role: 'user', +// email: 'test@example.org', +// password: '1234', +// }) +// author = await factory.create('User', { +// id: 'auth1', +// role: 'user', +// name: 'abusive-user', +// email: 'abusive-user@example.org', +// }) +// await instance.create('Category', { +// id: 'cat9', +// name: 'Democracy & Politics', +// icon: 'university', +// }) + +// await Promise.all([ +// factory.create('Post', { +// author, +// id: 'p1', +// categoryIds, +// content: 'Interesting Knowledge', +// }), +// factory.create('Post', { +// author: moderator, +// id: 'p2', +// categoryIds, +// content: 'More things to do …', +// }), +// factory.create('Post', { +// author: user, +// id: 'p3', +// categoryIds, +// content: 'I am at school …', +// }), +// ]) +// await Promise.all([ +// factory.create('Comment', { +// author: user, +// id: 'c1', +// postId: 'p1', +// }), +// ]) + +// authenticatedUser = await user.toJson() +// await Promise.all([ +// mutate({ +// mutation: reportMutation, +// variables: { +// resourceId: 'p1', +// reasonCategory: 'other', +// reasonDescription: 'This comment is bigoted', +// }, +// }), +// mutate({ +// mutation: reportMutation, +// variables: { +// resourceId: 'c1', +// reasonCategory: 'discrimination_etc', +// reasonDescription: 'This post is bigoted', +// }, +// }), +// mutate({ +// mutation: reportMutation, +// variables: { +// resourceId: 'auth1', +// reasonCategory: 'doxing', +// reasonDescription: 'This user is harassing me with bigoted remarks', +// }, +// }), +// ]) +// authenticatedUser = null +// }) + +// afterEach(async () => { +// await factory.cleanDatabase() +// }) + +// describe('unauthenticated', () => { +// it('throws authorization error', async () => { +// authenticatedUser = null +// expect(query({ query: reportsQuery })).resolves.toMatchObject({ +// data: { reports: null }, +// errors: [{ message: 'Not Authorised!' }], +// }) +// }) + +// it('role "user" gets no reports', async () => { +// authenticatedUser = await user.toJson() +// expect(query({ query: reportsQuery })).resolves.toMatchObject({ +// data: { reports: null }, +// errors: [{ message: 'Not Authorised!' }], +// }) +// }) + +// it('role "moderator" gets reports', async () => { +// const expected = { +// // to check 'orderBy: createdAt_desc' is not possible here, because 'createdAt' does not differ +// reports: expect.arrayContaining([ +// expect.objectContaining({ +// createdAt: expect.any(String), +// reasonCategory: 'doxing', +// reasonDescription: 'This user is harassing me with bigoted remarks', +// submitter: expect.objectContaining({ +// id: 'user1', +// }), +// type: 'User', +// user: expect.objectContaining({ +// id: 'auth1', +// }), +// post: null, +// comment: null, +// }), +// expect.objectContaining({ +// createdAt: expect.any(String), +// reasonCategory: 'other', +// reasonDescription: 'This comment is bigoted', +// submitter: expect.objectContaining({ +// id: 'user1', +// }), +// type: 'Post', +// user: null, +// post: expect.objectContaining({ +// id: 'p1', +// }), +// comment: null, +// }), +// expect.objectContaining({ +// createdAt: expect.any(String), +// reasonCategory: 'discrimination_etc', +// reasonDescription: 'This post is bigoted', +// submitter: expect.objectContaining({ +// id: 'user1', +// }), +// type: 'Comment', +// user: null, +// post: null, +// comment: expect.objectContaining({ +// id: 'c1', +// }), +// }), +// ]), +// } + +// authenticatedUser = await moderator.toJson() +// const { data } = await query({ query: reportsQuery }) +// expect(data).toEqual(expected) +// }) +// }) +// }) From bcb880db5e4ec2bb1b23b395f7ef7052fc158ace Mon Sep 17 00:00:00 2001 From: aonomike Date: Sat, 19 Oct 2019 12:51:16 +0300 Subject: [PATCH 2/8] Start refactor of authenticated user path - Refactor report invlid resource - Start refactor of valid resource path --- backend/src/schema/resolvers/reports.spec.js | 239 ++++++++----------- 1 file changed, 96 insertions(+), 143 deletions(-) diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index 2c853d1fa..f247e2ec5 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -8,66 +8,116 @@ const factory = Factory() const instance = getNeode() const driver = getDriver() -describe('report',()=>{ - let authenticatedUser, mutate, query +describe('report', () => { + let authenticatedUser, user, mutate, query const reportMutation = gql` - mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { - report( - resourceId: $resourceId - reasonCategory: $reasonCategory - reasonDescription: $reasonDescription - ) { - createdAt - reasonCategory - reasonDescription - type - submitter { - email - } - user { - name - } - post { - title - } - comment { - content + mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { + report( + resourceId: $resourceId + reasonCategory: $reasonCategory + reasonDescription: $reasonDescription + ) { + createdAt + reasonCategory + reasonDescription + type + submitter { + email + } + user { + name + } + post { + title + } + comment { + content + } } } + ` + const variables = { + resourceId: 'whatever', + reasonCategory: 'other', + reasonDescription: 'Violates code of conduct !!!', } -` -const variables = { - resourceId: 'whatever', - reasonCategory: 'other', - reasonDescription: 'Violates code of conduct !!!', -} - beforeAll(()=>{ - const {server} = createServer({ - context: ()=>{ + beforeAll(() => { + const { server } = createServer({ + context: () => { return { driver, neode: instance, - user: authenticatedUser + user: authenticatedUser, } - - } - } - ) - mutate = createTestClient(server).mutate - query = createTestClient(server).query + }, + }) + mutate = createTestClient(server).mutate + query = createTestClient(server).query }) - afterEach(async()=>{ + afterEach(async () => { await factory.cleanDatabase() }) - describe('create report', ()=>{ - describe('unauthenticated',()=>{ - it.only('throws authorization error', async() =>{ - await expect(mutate({mutation: reportMutation, variables})).resolves.toMatchObject({ - data: {report: null}, - errors: [{message: 'Not Authorised!'}] + describe('create report', () => { + describe('unauthenticated', () => { + it('throws authorization error', async () => { + authenticatedUser = null + await expect(mutate({ mutation: reportMutation, variables })).resolves.toMatchObject({ + data: { report: null }, + errors: [{ message: 'Not Authorised!' }], + }) + }) + }) + + describe('authenticated', () => { + const categoryIds = ['cat9'] + beforeEach(async () => { + user = await factory.create('User', { + id: 'u1', + role: 'user', + email: 'test@example.org', + password: '1234', + }) + await factory.create('User', { + id: 'abusive-user-id', + role: 'user', + name: 'abusive-user', + email: 'abusive-user@example.org', + }) + await instance.create('Category', { + id: 'cat9', + name: 'Democracy & Politics', + icon: 'university', + }) + + authenticatedUser = await user.toJson() + }) + + describe('invalid resource id', () => { + it('returns null', async () => { + await expect(mutate({ mutation: reportMutation, variables })).resolves.toMatchObject({ + data: { report: null }, + errors: undefined, + }) + }) + }) + + describe('valid resource', () => { + describe('reported resource is a user', () => { + it('returns type "User"', async () => { + await expect( + mutate({ mutation: reportMutation, variables: { ...variables, resourceId: 'abusive-user-id' } }), + ).resolves.toMatchObject({ + data: { + report: { + type: 'User', + }, + }, + errors: undefined, + }) + }) }) }) }) @@ -83,107 +133,10 @@ const variables = { // let user // const categoryIds = ['cat9'] -// const action = () => { -// reportMutation = gql` -// mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { -// report( -// resourceId: $resourceId -// reasonCategory: $reasonCategory -// reasonDescription: $reasonDescription -// ) { -// createdAt -// reasonCategory -// reasonDescription -// type -// submitter { -// email -// } -// user { -// name -// } -// post { -// title -// } -// comment { -// content -// } -// } -// } -// ` -// client = new GraphQLClient(host, { -// headers, -// }) -// return client.request(reportMutation, variables) -// } - -// beforeEach(async () => { -// variables = { -// resourceId: 'whatever', -// reasonCategory: 'other', -// reasonDescription: 'Violates code of conduct !!!', -// } -// headers = {} -// user = await factory.create('User', { -// id: 'u1', -// role: 'user', -// email: 'test@example.org', -// password: '1234', -// }) -// await factory.create('User', { -// id: 'u2', -// role: 'user', -// name: 'abusive-user', -// email: 'abusive-user@example.org', -// }) -// await instance.create('Category', { -// id: 'cat9', -// name: 'Democracy & Politics', -// icon: 'university', -// }) -// }) - -// afterEach(async () => { -// await factory.cleanDatabase() -// }) - -// describe('unauthenticated', () => { -// it('throws authorization error', async () => { -// await expect(action()).rejects.toThrow('Not Authorised') -// }) -// }) - // describe('authenticated', () => { -// beforeEach(async () => { -// headers = await login({ -// email: 'test@example.org', -// password: '1234', -// }) -// }) - -// describe('invalid resource id', () => { -// it('returns null', async () => { -// await expect(action()).resolves.toEqual({ -// report: null, -// }) -// }) -// }) // describe('valid resource id', () => { // describe('reported resource is a user', () => { -// beforeEach(async () => { -// variables = { -// ...variables, -// resourceId: 'u2', -// } -// }) - -// it('returns type "User"', async () => { -// await expect(action()).resolves.toMatchObject({ -// report: { -// type: 'User', -// }, -// }) -// }) // it('returns resource in user attribute', async () => { // await expect(action()).resolves.toMatchObject({ From 28f0431ffae99939903b2eced1b78c5de264f7b0 Mon Sep 17 00:00:00 2001 From: aonomike Date: Sat, 19 Oct 2019 13:34:54 +0300 Subject: [PATCH 3/8] Refactor tests for reporting a user --- backend/src/schema/resolvers/reports.spec.js | 211 ++++++++++++------- 1 file changed, 137 insertions(+), 74 deletions(-) diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index f247e2ec5..e035938c5 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -108,7 +108,10 @@ describe('report', () => { describe('reported resource is a user', () => { it('returns type "User"', async () => { await expect( - mutate({ mutation: reportMutation, variables: { ...variables, resourceId: 'abusive-user-id' } }), + mutate({ + mutation: reportMutation, + variables: { ...variables, resourceId: 'abusive-user-id' }, + }), ).resolves.toMatchObject({ data: { report: { @@ -118,6 +121,139 @@ describe('report', () => { errors: undefined, }) }) + + it('returns resource in user attribute', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { ...variables, resourceId: 'abusive-user-id' }, + }), + ).resolves.toMatchObject({ + data: { + report: { + user: { + name: 'abusive-user', + }, + }, + }, + errors: undefined, + }) + }) + + it('returns the submitter', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { ...variables, resourceId: 'abusive-user-id' }, + }), + ).resolves.toMatchObject({ + data: { + report: { + submitter: { + email: 'test@example.org', + }, + }, + }, + errors: undefined, + }) + }) + + it('returns a date', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { ...variables, resourceId: 'abusive-user-id' }, + }), + ).resolves.toMatchObject({ + data: { + report: { + createdAt: expect.any(String), + }, + }, + errors: undefined, + }) + }) + + it('returns the reason category', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { + ...variables, + resourceId: 'abusive-user-id', + reasonCategory: 'criminal_behavior_violation_german_law', + }, + }), + ).resolves.toMatchObject({ + data: { + report: { + reasonCategory: 'criminal_behavior_violation_german_law', + }, + }, + errors: undefined, + }) + }) + + it('gives an error if the reason category is not in enum "ReasonCategory"', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { + ...variables, + resourceId: 'abusive-user-id', + reasonCategory: 'category_missing_from_enum_reason_category', + }, + }), + ).resolves.toMatchObject({ + data: undefined, + errors: [ + { + message: + 'Variable "$reasonCategory" got invalid value "category_missing_from_enum_reason_category"; Expected type ReasonCategory.', + }, + ], + }) + }) + + it('returns the reason description', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { + ...variables, + resourceId: 'abusive-user-id', + reasonDescription: 'My reason!', + }, + }), + ).resolves.toMatchObject({ + data: { + report: { + reasonDescription: 'My reason!', + }, + }, + errors: undefined, + }) + }) + + it('sanitize the reason description', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { + ...variables, + resourceId: 'abusive-user-id', + reasonDescription: 'My reason !', + }, + }), + ).resolves.toMatchObject({ + data: { + report: { + reasonDescription: 'My reason !', + }, + }, + errors: undefined, + }) + }) }) }) }) @@ -138,79 +274,6 @@ describe('report', () => { // describe('valid resource id', () => { // describe('reported resource is a user', () => { -// it('returns resource in user attribute', async () => { -// await expect(action()).resolves.toMatchObject({ -// report: { -// user: { -// name: 'abusive-user', -// }, -// }, -// }) -// }) - -// it('returns the submitter', async () => { -// await expect(action()).resolves.toMatchObject({ -// report: { -// submitter: { -// email: 'test@example.org', -// }, -// }, -// }) -// }) - -// it('returns a date', async () => { -// await expect(action()).resolves.toMatchObject({ -// report: { -// createdAt: expect.any(String), -// }, -// }) -// }) - -// it('returns the reason category', async () => { -// variables = { -// ...variables, -// reasonCategory: 'criminal_behavior_violation_german_law', -// } -// await expect(action()).resolves.toMatchObject({ -// report: { -// reasonCategory: 'criminal_behavior_violation_german_law', -// }, -// }) -// }) - -// it('gives an error if the reason category is not in enum "ReasonCategory"', async () => { -// variables = { -// ...variables, -// reasonCategory: 'my_category', -// } -// await expect(action()).rejects.toThrow( -// 'got invalid value "my_category"; Expected type ReasonCategory', -// ) -// }) - -// it('returns the reason description', async () => { -// variables = { -// ...variables, -// reasonDescription: 'My reason!', -// } -// await expect(action()).resolves.toMatchObject({ -// report: { -// reasonDescription: 'My reason!', -// }, -// }) -// }) - -// it('sanitize the reason description', async () => { -// variables = { -// ...variables, -// reasonDescription: 'My reason !', -// } -// await expect(action()).resolves.toMatchObject({ -// report: { -// reasonDescription: 'My reason !', -// }, -// }) -// }) // }) // describe('reported resource is a post', () => { From 58d1ce65155e4258aea07699f58b950d04e3f444 Mon Sep 17 00:00:00 2001 From: aonomike Date: Sat, 19 Oct 2019 14:27:51 +0300 Subject: [PATCH 4/8] Refactor tests for reporting a post --- backend/src/schema/resolvers/reports.spec.js | 119 ++++++++++++------- 1 file changed, 79 insertions(+), 40 deletions(-) diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index e035938c5..890908cb4 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -9,7 +9,7 @@ const instance = getNeode() const driver = getDriver() describe('report', () => { - let authenticatedUser, user, mutate, query + let authenticatedUser, currentUser, mutate, query const reportMutation = gql` mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { report( @@ -74,8 +74,8 @@ describe('report', () => { describe('authenticated', () => { const categoryIds = ['cat9'] beforeEach(async () => { - user = await factory.create('User', { - id: 'u1', + currentUser = await factory.create('User', { + id: 'current-user-id', role: 'user', email: 'test@example.org', password: '1234', @@ -92,7 +92,7 @@ describe('report', () => { icon: 'university', }) - authenticatedUser = await user.toJson() + authenticatedUser = await currentUser.toJson() }) describe('invalid resource id', () => { @@ -255,16 +255,87 @@ describe('report', () => { }) }) }) + + describe('reported resource is a post', () => { + beforeEach(async () => { + await factory.create('Post', { + author: currentUser, + id: 'post-to-report-id', + title: 'This is a post that is going to be reported', + categoryIds, + }) + }) + + it('returns type "Post"', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { + ...variables, + resourceId: 'post-to-report-id', + }, + }), + ).resolves.toMatchObject({ + data: { + report: { + type: 'Post', + }, + }, + errors: undefined, + }) + }) + + it('returns resource in post attribute', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { + ...variables, + resourceId: 'post-to-report-id', + }, + }), + ).resolves.toMatchObject({ + data: { + report: { + post: { + title: 'This is a post that is going to be reported', + }, + }, + }, + errors: undefined, + }) + }) + + it('returns null in user attribute', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { + ...variables, + resourceId: 'post-to-report-id', + }, + }), + ).resolves.toMatchObject({ + data: { + report: { + user: null, + }, + }, + errors: undefined, + }) + }) + }) + + /* An der Stelle würde ich den p23 noch mal prüfen, diesmal muss aber eine error meldung kommen. + At this point I would check the p23 again, but this time there must be an error message. */ }) }) }) }) // describe('report mutation', () => { -// let reportMutation -// let headers + // let client -// let variables // let createPostVariables // let user // const categoryIds = ['cat9'] @@ -272,41 +343,9 @@ describe('report', () => { // describe('authenticated', () => { // describe('valid resource id', () => { -// describe('reported resource is a user', () => { - -// }) // describe('reported resource is a post', () => { -// beforeEach(async () => { -// await factory.create('Post', { -// author: user, -// id: 'p23', -// title: 'Matt and Robert having a pair-programming', -// categoryIds, -// }) -// variables = { -// ...variables, -// resourceId: 'p23', -// } -// }) - -// it('returns type "Post"', async () => { -// await expect(action()).resolves.toMatchObject({ -// report: { -// type: 'Post', -// }, -// }) -// }) - -// it('returns resource in post attribute', async () => { -// await expect(action()).resolves.toMatchObject({ -// report: { -// post: { -// title: 'Matt and Robert having a pair-programming', -// }, -// }, -// }) -// }) +// // it('returns null in user attribute', async () => { // await expect(action()).resolves.toMatchObject({ From 55abe975d9936820bfb9d8dcd0123d4e29e2685f Mon Sep 17 00:00:00 2001 From: aonomike Date: Sat, 19 Oct 2019 14:56:50 +0300 Subject: [PATCH 5/8] Refactor tests for reporting a comment --- backend/src/schema/resolvers/reports.spec.js | 124 +++++++++---------- 1 file changed, 61 insertions(+), 63 deletions(-) diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index 890908cb4..a2b4fc081 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -328,79 +328,77 @@ describe('report', () => { /* An der Stelle würde ich den p23 noch mal prüfen, diesmal muss aber eine error meldung kommen. At this point I would check the p23 again, but this time there must be an error message. */ + + describe('reported resource is a comment', () => { + let createPostVariables + beforeEach(async () => { + createPostVariables = { + id: 'p1', + title: 'post to comment on', + content: 'please comment on me', + categoryIds, + } + await factory.create('Post', { ...createPostVariables, author: currentUser }) + await factory.create('Comment', { + author: currentUser, + postId: 'p1', + id: 'comment-to-report-id', + content: 'Post comment to be reported.', + }) + }) + + it('returns type "Comment"', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { + ...variables, + resourceId: 'comment-to-report-id', + }, + }), + ).resolves.toMatchObject({ + data: { + report: { + type: 'Comment', + }, + }, + errors: undefined, + }) + }) + + it('returns resource in comment attribute', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { + ...variables, + resourceId: 'comment-to-report-id', + }, + }), + ).resolves.toMatchObject({ + data: { + report: { + comment: { + content: 'Post comment to be reported.', + }, + }, + }, + errors: undefined, + }) + }) + }) + /* An der Stelle würde ich den c34 noch mal prüfen, diesmal muss aber eine error meldung kommen. + At this point I would check the c34 again, but this time there must be an error message. */ }) }) }) }) - // describe('report mutation', () => { - -// let client -// let createPostVariables -// let user -// const categoryIds = ['cat9'] - // describe('authenticated', () => { // describe('valid resource id', () => { // describe('reported resource is a post', () => { -// - -// it('returns null in user attribute', async () => { -// await expect(action()).resolves.toMatchObject({ -// report: { -// user: null, -// }, -// }) -// }) -// }) - -// /* An der Stelle würde ich den p23 noch mal prüfen, diesmal muss aber eine error meldung kommen. -// At this point I would check the p23 again, but this time there must be an error message. */ - -// describe('reported resource is a comment', () => { -// beforeEach(async () => { -// createPostVariables = { -// id: 'p1', -// title: 'post to comment on', -// content: 'please comment on me', -// categoryIds, -// } -// await factory.create('Post', { ...createPostVariables, author: user }) -// await factory.create('Comment', { -// author: user, -// postId: 'p1', -// id: 'c34', -// content: 'Robert getting tired.', -// }) -// variables = { -// ...variables, -// resourceId: 'c34', -// } -// }) - -// it('returns type "Comment"', async () => { -// await expect(action()).resolves.toMatchObject({ -// report: { -// type: 'Comment', -// }, -// }) -// }) - -// it('returns resource in comment attribute', async () => { -// await expect(action()).resolves.toMatchObject({ -// report: { -// comment: { -// content: 'Robert getting tired.', -// }, -// }, -// }) -// }) -// }) - -// /* An der Stelle würde ich den c34 noch mal prüfen, diesmal muss aber eine error meldung kommen. -// At this point I would check the c34 again, but this time there must be an error message. */ // describe('reported resource is a tag', () => { // beforeEach(async () => { From 08adff74bf8d63c4792bdf704d85d0baeaf36025 Mon Sep 17 00:00:00 2001 From: aonomike Date: Sat, 19 Oct 2019 15:08:26 +0300 Subject: [PATCH 6/8] Refactor tests for reporting a tag --- backend/src/schema/resolvers/reports.spec.js | 61 +++++++++----------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index a2b4fc081..1e3c4e371 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -8,7 +8,7 @@ const factory = Factory() const instance = getNeode() const driver = getDriver() -describe('report', () => { +describe('report resources', () => { let authenticatedUser, currentUser, mutate, query const reportMutation = gql` mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { @@ -387,42 +387,37 @@ describe('report', () => { }) }) }) - /* An der Stelle würde ich den c34 noch mal prüfen, diesmal muss aber eine error meldung kommen. - At this point I would check the c34 again, but this time there must be an error message. */ + /* An der Stelle würde ich den comment-to-report-id noch mal prüfen, diesmal muss aber eine error meldung kommen. + At this point I would check the comment-to-report-id again, but this time there must be an error message. */ + describe('reported resource is a tag', () => { + beforeEach(async () => { + await factory.create('Tag', { + id: 'tag-to-report-id', + }) + }) + + it('returns null', async () => { + await expect( + mutate({ + mutation: reportMutation, + variables: { + ...variables, + resourceId: 'tag-to-report-id', + }, + }), + ).resolves.toMatchObject({ + data: { report: null }, + errors: undefined, + }) + }) + }) + + /* An der Stelle würde ich den tag-to-report-id noch mal prüfen, diesmal muss aber eine error meldung kommen. +// At this point I would check the tag-to-report-id again, but this time there must be an error message. */ }) }) }) }) -// describe('report mutation', () => { -// describe('authenticated', () => { - -// describe('valid resource id', () => { - -// describe('reported resource is a post', () => { - -// describe('reported resource is a tag', () => { -// beforeEach(async () => { -// await factory.create('Tag', { -// id: 't23', -// }) -// variables = { -// ...variables, -// resourceId: 't23', -// } -// }) - -// it('returns null', async () => { -// await expect(action()).resolves.toMatchObject({ -// report: null, -// }) -// }) -// }) - -// /* An der Stelle würde ich den t23 noch mal prüfen, diesmal muss aber eine error meldung kommen. -// At this point I would check the t23 again, but this time there must be an error message. */ -// }) -// }) -// }) // describe('reports query', () => { // let query, mutate, authenticatedUser, moderator, user, author From 4e42017afaa97fa87ec726a5bbd1605cca911375 Mon Sep 17 00:00:00 2001 From: aonomike Date: Sat, 19 Oct 2019 15:50:48 +0300 Subject: [PATCH 7/8] Refactor tests for querying reported resources --- backend/src/schema/resolvers/reports.spec.js | 401 +++++++++---------- 1 file changed, 183 insertions(+), 218 deletions(-) diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index 1e3c4e371..460853af8 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -9,7 +9,8 @@ const instance = getNeode() const driver = getDriver() describe('report resources', () => { - let authenticatedUser, currentUser, mutate, query + let authenticatedUser, currentUser, mutate, query, moderator, abusiveUser + const categoryIds = ['cat9'] const reportMutation = gql` mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { report( @@ -60,7 +61,7 @@ describe('report resources', () => { await factory.cleanDatabase() }) - describe('create report', () => { + describe('report a resource', () => { describe('unauthenticated', () => { it('throws authorization error', async () => { authenticatedUser = null @@ -72,7 +73,6 @@ describe('report resources', () => { }) describe('authenticated', () => { - const categoryIds = ['cat9'] beforeEach(async () => { currentUser = await factory.create('User', { id: 'current-user-id', @@ -413,222 +413,187 @@ describe('report resources', () => { }) /* An der Stelle würde ich den tag-to-report-id noch mal prüfen, diesmal muss aber eine error meldung kommen. -// At this point I would check the tag-to-report-id again, but this time there must be an error message. */ + At this point I would check the tag-to-report-id again, but this time there must be an error message. */ + }) + }) + }) + describe('query for reported resource', () => { + const reportsQuery = gql` + query { + reports(orderBy: createdAt_desc) { + createdAt + reasonCategory + reasonDescription + submitter { + id + } + type + user { + id + } + post { + id + } + comment { + id + } + } + } + ` + + beforeEach(async () => { + authenticatedUser = null + + moderator = await factory.create('User', { + id: 'moderator-1', + role: 'moderator', + email: 'moderator@example.org', + password: '1234', + }) + currentUser = await factory.create('User', { + id: 'current-user-id', + role: 'user', + email: 'current.user@example.org', + password: '1234', + }) + abusiveUser = await factory.create('User', { + id: 'abusive-user-1', + role: 'user', + name: 'abusive-user', + email: 'abusive-user@example.org', + }) + await instance.create('Category', { + id: 'cat9', + name: 'Democracy & Politics', + icon: 'university', + }) + + await Promise.all([ + factory.create('Post', { + author: abusiveUser, + id: 'abusive-post-1', + categoryIds, + content: 'Interesting Knowledge', + }), + factory.create('Post', { + author: moderator, + id: 'post-2', + categoryIds, + content: 'More things to do …', + }), + factory.create('Post', { + author: currentUser, + id: 'post-3', + categoryIds, + content: 'I am at school …', + }), + ]) + await Promise.all([ + factory.create('Comment', { + author: currentUser, + id: 'abusive-comment-1', + postId: 'post-1', + }), + ]) + authenticatedUser = await currentUser.toJson() + await Promise.all([ + mutate({ + mutation: reportMutation, + variables: { + resourceId: 'abusive-post-1', + reasonCategory: 'other', + reasonDescription: 'This comment is bigoted', + }, + }), + mutate({ + mutation: reportMutation, + variables: { + resourceId: 'abusive-comment-1', + reasonCategory: 'discrimination_etc', + reasonDescription: 'This post is bigoted', + }, + }), + mutate({ + mutation: reportMutation, + variables: { + resourceId: 'abusive-user-1', + reasonCategory: 'doxing', + reasonDescription: 'This user is harassing me with bigoted remarks', + }, + }), + ]) + authenticatedUser = null + }) + describe('unauthenticated', () => { + it('throws authorization error', async () => { + authenticatedUser = null + expect(query({ query: reportsQuery })).resolves.toMatchObject({ + data: { reports: null }, + errors: [{ message: 'Not Authorised!' }], + }) + }) + }) + describe('authenticated', () => { + it('role "user" gets no reports', async () => { + authenticatedUser = await currentUser.toJson() + expect(query({ query: reportsQuery })).resolves.toMatchObject({ + data: { reports: null }, + errors: [{ message: 'Not Authorised!' }], + }) + }) + + it('role "moderator" gets reports', async () => { + const expected = { + // to check 'orderBy: createdAt_desc' is not possible here, because 'createdAt' does not differ + reports: expect.arrayContaining([ + expect.objectContaining({ + createdAt: expect.any(String), + reasonCategory: 'doxing', + reasonDescription: 'This user is harassing me with bigoted remarks', + submitter: expect.objectContaining({ + id: 'current-user-id', + }), + type: 'User', + user: expect.objectContaining({ + id: 'abusive-user-1', + }), + post: null, + comment: null, + }), + expect.objectContaining({ + createdAt: expect.any(String), + reasonCategory: 'other', + reasonDescription: 'This comment is bigoted', + submitter: expect.objectContaining({ + id: 'current-user-id', + }), + type: 'Post', + user: null, + post: expect.objectContaining({ + id: 'abusive-post-1', + }), + comment: null, + }), + expect.objectContaining({ + createdAt: expect.any(String), + reasonCategory: 'discrimination_etc', + reasonDescription: 'This post is bigoted', + submitter: expect.objectContaining({ + id: 'current-user-id', + }), + type: 'Comment', + user: null, + post: null, + comment: expect.objectContaining({ + id: 'abusive-comment-1', + }), + }), + ]), + } + authenticatedUser = await moderator.toJson() + const { data } = await query({ query: reportsQuery }) + expect(data).toEqual(expected) }) }) }) }) - -// describe('reports query', () => { -// let query, mutate, authenticatedUser, moderator, user, author -// const categoryIds = ['cat9'] - -// const reportMutation = gql` -// mutation($resourceId: ID!, $reasonCategory: ReasonCategory!, $reasonDescription: String!) { -// report( -// resourceId: $resourceId -// reasonCategory: $reasonCategory -// reasonDescription: $reasonDescription -// ) { -// type -// } -// } -// ` -// const reportsQuery = gql` -// query { -// reports(orderBy: createdAt_desc) { -// createdAt -// reasonCategory -// reasonDescription -// submitter { -// id -// } -// type -// user { -// id -// } -// post { -// id -// } -// comment { -// id -// } -// } -// } -// ` - -// beforeAll(async () => { -// await factory.cleanDatabase() -// const { server } = createServer({ -// context: () => { -// return { -// driver, -// user: authenticatedUser, -// } -// }, -// }) -// query = createTestClient(server).query -// mutate = createTestClient(server).mutate -// }) - -// beforeEach(async () => { -// authenticatedUser = null - -// moderator = await factory.create('User', { -// id: 'mod1', -// role: 'moderator', -// email: 'moderator@example.org', -// password: '1234', -// }) -// user = await factory.create('User', { -// id: 'user1', -// role: 'user', -// email: 'test@example.org', -// password: '1234', -// }) -// author = await factory.create('User', { -// id: 'auth1', -// role: 'user', -// name: 'abusive-user', -// email: 'abusive-user@example.org', -// }) -// await instance.create('Category', { -// id: 'cat9', -// name: 'Democracy & Politics', -// icon: 'university', -// }) - -// await Promise.all([ -// factory.create('Post', { -// author, -// id: 'p1', -// categoryIds, -// content: 'Interesting Knowledge', -// }), -// factory.create('Post', { -// author: moderator, -// id: 'p2', -// categoryIds, -// content: 'More things to do …', -// }), -// factory.create('Post', { -// author: user, -// id: 'p3', -// categoryIds, -// content: 'I am at school …', -// }), -// ]) -// await Promise.all([ -// factory.create('Comment', { -// author: user, -// id: 'c1', -// postId: 'p1', -// }), -// ]) - -// authenticatedUser = await user.toJson() -// await Promise.all([ -// mutate({ -// mutation: reportMutation, -// variables: { -// resourceId: 'p1', -// reasonCategory: 'other', -// reasonDescription: 'This comment is bigoted', -// }, -// }), -// mutate({ -// mutation: reportMutation, -// variables: { -// resourceId: 'c1', -// reasonCategory: 'discrimination_etc', -// reasonDescription: 'This post is bigoted', -// }, -// }), -// mutate({ -// mutation: reportMutation, -// variables: { -// resourceId: 'auth1', -// reasonCategory: 'doxing', -// reasonDescription: 'This user is harassing me with bigoted remarks', -// }, -// }), -// ]) -// authenticatedUser = null -// }) - -// afterEach(async () => { -// await factory.cleanDatabase() -// }) - -// describe('unauthenticated', () => { -// it('throws authorization error', async () => { -// authenticatedUser = null -// expect(query({ query: reportsQuery })).resolves.toMatchObject({ -// data: { reports: null }, -// errors: [{ message: 'Not Authorised!' }], -// }) -// }) - -// it('role "user" gets no reports', async () => { -// authenticatedUser = await user.toJson() -// expect(query({ query: reportsQuery })).resolves.toMatchObject({ -// data: { reports: null }, -// errors: [{ message: 'Not Authorised!' }], -// }) -// }) - -// it('role "moderator" gets reports', async () => { -// const expected = { -// // to check 'orderBy: createdAt_desc' is not possible here, because 'createdAt' does not differ -// reports: expect.arrayContaining([ -// expect.objectContaining({ -// createdAt: expect.any(String), -// reasonCategory: 'doxing', -// reasonDescription: 'This user is harassing me with bigoted remarks', -// submitter: expect.objectContaining({ -// id: 'user1', -// }), -// type: 'User', -// user: expect.objectContaining({ -// id: 'auth1', -// }), -// post: null, -// comment: null, -// }), -// expect.objectContaining({ -// createdAt: expect.any(String), -// reasonCategory: 'other', -// reasonDescription: 'This comment is bigoted', -// submitter: expect.objectContaining({ -// id: 'user1', -// }), -// type: 'Post', -// user: null, -// post: expect.objectContaining({ -// id: 'p1', -// }), -// comment: null, -// }), -// expect.objectContaining({ -// createdAt: expect.any(String), -// reasonCategory: 'discrimination_etc', -// reasonDescription: 'This post is bigoted', -// submitter: expect.objectContaining({ -// id: 'user1', -// }), -// type: 'Comment', -// user: null, -// post: null, -// comment: expect.objectContaining({ -// id: 'c1', -// }), -// }), -// ]), -// } - -// authenticatedUser = await moderator.toJson() -// const { data } = await query({ query: reportsQuery }) -// expect(data).toEqual(expected) -// }) -// }) -// }) From 86907f2800c2893b30ab1bd0d1da628c21772e34 Mon Sep 17 00:00:00 2001 From: aonomike Date: Mon, 21 Oct 2019 13:25:56 +0300 Subject: [PATCH 8/8] Fix code review issues --- backend/src/schema/resolvers/reports.spec.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/backend/src/schema/resolvers/reports.spec.js b/backend/src/schema/resolvers/reports.spec.js index 460853af8..21b1b4a7b 100644 --- a/backend/src/schema/resolvers/reports.spec.js +++ b/backend/src/schema/resolvers/reports.spec.js @@ -43,7 +43,8 @@ describe('report resources', () => { reasonDescription: 'Violates code of conduct !!!', } - beforeAll(() => { + beforeAll(async () => { + await factory.cleanDatabase() const { server } = createServer({ context: () => { return { @@ -326,9 +327,6 @@ describe('report resources', () => { }) }) - /* An der Stelle würde ich den p23 noch mal prüfen, diesmal muss aber eine error meldung kommen. - At this point I would check the p23 again, but this time there must be an error message. */ - describe('reported resource is a comment', () => { let createPostVariables beforeEach(async () => { @@ -387,8 +385,7 @@ describe('report resources', () => { }) }) }) - /* An der Stelle würde ich den comment-to-report-id noch mal prüfen, diesmal muss aber eine error meldung kommen. - At this point I would check the comment-to-report-id again, but this time there must be an error message. */ + describe('reported resource is a tag', () => { beforeEach(async () => { await factory.create('Tag', { @@ -411,9 +408,6 @@ describe('report resources', () => { }) }) }) - - /* An der Stelle würde ich den tag-to-report-id noch mal prüfen, diesmal muss aber eine error meldung kommen. - At this point I would check the tag-to-report-id again, but this time there must be an error message. */ }) }) })