From a6f4362a3f439bc3962c736aec517b7c8172a2d8 Mon Sep 17 00:00:00 2001 From: aonomike Date: Tue, 17 Sep 2019 15:51:49 +0300 Subject: [PATCH 01/10] Refactor Moderator spec tp use apollo-server-testing - Refactor unauthentiacted path for logged in and un logged in user --- .../src/schema/resolvers/moderation.spec.js | 745 +++++++++--------- 1 file changed, 374 insertions(+), 371 deletions(-) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 3107a5799..c65243271 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -1,43 +1,51 @@ +import { createTestClient } from 'apollo-server-testing' import { GraphQLClient } from 'graphql-request' import Factory from '../../seed/factories' -import { host, login, gql } from '../../jest/helpers' -import { neode } from '../../bootstrap/neo4j' +import { gql } from '../../jest/helpers' +import { neode as getNeode, getDriver } from '../../bootstrap/neo4j' +import createServer from '../../server' -let client const factory = Factory() -const instance = neode() +const neode = getNeode() +const driver = getDriver() const categoryIds = ['cat9'] -const setupAuthenticateClient = params => { - const authenticateClient = async () => { - await factory.create('User', params) - const headers = await login(params) - client = new GraphQLClient(host, { headers }) - } - return authenticateClient -} - -let createResource -let authenticateClient +let query, mutate, authenticatedUser, variables, currentUser let createPostVariables let createCommentVariables -beforeEach(async () => { - createResource = () => {} - authenticateClient = () => { - client = new GraphQLClient(host) - } - await instance.create('Category', { - id: 'cat9', - name: 'Democracy & Politics', - icon: 'university', +beforeAll(() => { + authenticatedUser = undefined + const { server } = createServer({ + context: () => { + return { + driver, + neode, + user: authenticatedUser, + } + }, }) + mutate = createTestClient(server).mutate }) -const setup = async () => { - await createResource() - await authenticateClient() -} +beforeEach(async () => { + variables = {} + currentUser = await factory.create('User', { + id: 'current-user', + name: 'TestUser', + email: 'test@example.org', + password: '1234', + }) + + await Promise.all([ + neode.create('Category', { + id: 'cat9', + name: 'Democracy & Politics', + icon: 'university', + }), + ]) + authenticatedUser = null +}) afterEach(async () => { await factory.cleanDatabase() @@ -54,369 +62,364 @@ describe('disable', () => { beforeEach(() => { // our defaul set of variables variables = { - id: 'blabla', + id: 'some-resource', } }) - - const action = async () => { - return client.request(mutation, variables) - } - - it('throws authorization error', async () => { - await setup() - await expect(action()).rejects.toThrow('Not Authorised') + describe('unauthenticated', () => { + beforeEach(async () => { + authenticatedUser = await currentUser.toJson() + }) + it('throws authorization error', async () => { + const { errors } = await mutate({ mutation, variables }) + expect(errors[0]).toHaveProperty('message', 'Not Authorised!') + }) }) - describe('authenticated', () => { - beforeEach(() => { - authenticateClient = setupAuthenticateClient({ - email: 'user@example.org', - password: '1234', + describe('i am not a moderator', () => { + let nonModerator + + 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 () => { + const { errors } = await mutate({ mutation, variables }) + expect(errors[0]).toHaveProperty('message', 'Not Authorised!') }) }) - it('throws authorization error', async () => { - await setup() - await expect(action()).rejects.toThrow('Not Authorised') - }) - - describe('as moderator', () => { - beforeEach(() => { - authenticateClient = setupAuthenticateClient({ - id: 'u7', + describe('I am a moderator', () => { + let moderator + beforeEach(async () => { + moderator = await factory.create('User', { + id: 'moderator', + name: 'Moderator', email: 'moderator@example.org', password: '1234', - role: 'moderator', }) + authenticatedUser = await moderator.toJson() }) - describe('on something that is not a (Comment|Post|User) ', () => { + describe('moderate a resource that is not a (Comment|Post|User) ', () => { beforeEach(async () => { variables = { - id: 't23', - } - createResource = () => { - return Promise.all([factory.create('Tag', { id: 't23' })]) + id: 'sample-tag-id', } + await Promise.all([factory.create('Tag', { id: 'sample-tag-id' })]) }) it('returns null', async () => { - const expected = { disable: null } - await setup() - await expect(action()).resolves.toEqual(expected) - }) - }) - - describe('on a comment', () => { - beforeEach(async () => { - variables = { - 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', - }) - await asAuthenticatedUser.create('Post', createPostVariables) - await asAuthenticatedUser.create('Comment', createCommentVariables) - } - }) - - it('returns disabled resource id', async () => { - const expected = { disable: 'c47' } - await setup() - 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', { - id: 'p9', // that's the ID we will look for - categoryIds, - }) - } - }) - - it('returns disabled resource id', async () => { - const expected = { disable: 'p9' } - await setup() - await expect(action()).resolves.toEqual(expected) - }) - - it('changes .disabledBy', async () => { - const before = { Post: [{ id: 'p9', disabledBy: null }] } - const expected = { Post: [{ id: 'p9', disabledBy: { id: 'u7' } }] } - - await setup() - await expect(client.request('{ Post { id, disabledBy { id } } }')).resolves.toEqual( - before, - ) - await action() - await expect( - client.request('{ Post(disabled: true) { id, disabledBy { id } } }'), - ).resolves.toEqual(expected) - }) - - it('updates .disabled on post', async () => { - const before = { Post: [{ id: 'p9', disabled: false }] } - const expected = { Post: [{ id: 'p9', disabled: true }] } - - await setup() - await expect(client.request('{ Post { id disabled } }')).resolves.toEqual(before) - await action() - await expect(client.request('{ Post(disabled: true) { id disabled } }')).resolves.toEqual( - 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) + const expected = { data: { disable: null } } + await expect(mutate({ mutation, variables })).resolves.toMatchObject(expected) }) }) }) }) + + // describe('authenticated', () => { + // beforeEach(() => { + // authenticateClient = setupAuthenticateClient({ + // email: 'user@example.org', + // password: '1234', + // }) + // }) + + // describe('as moderator', () => { + + // describe('on something that is not a (Comment|Post|User) ', () => { + // describe('on a comment', () => { + // beforeEach(async () => { + // variables = { + // 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', + // }) + // await asAuthenticatedUser.create('Post', createPostVariables) + // await asAuthenticatedUser.create('Comment', createCommentVariables) + // } + // }) + + // it('returns disabled resource id', async () => { + // const expected = { disable: 'c47' } + // await setup() + // 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', { + // id: 'p9', // that's the ID we will look for + // categoryIds, + // }) + // } + // }) + + // it('returns disabled resource id', async () => { + // const expected = { disable: 'p9' } + // await setup() + // await expect(action()).resolves.toEqual(expected) + // }) + + // it('changes .disabledBy', async () => { + // const before = { Post: [{ id: 'p9', disabledBy: null }] } + // const expected = { Post: [{ id: 'p9', disabledBy: { id: 'u7' } }] } + + // await setup() + // await expect(client.request('{ Post { id, disabledBy { id } } }')).resolves.toEqual( + // before, + // ) + // await action() + // await expect( + // client.request('{ Post(disabled: true) { id, disabledBy { id } } }'), + // ).resolves.toEqual(expected) + // }) + + // it('updates .disabled on post', async () => { + // const before = { Post: [{ id: 'p9', disabled: false }] } + // const expected = { Post: [{ id: 'p9', disabled: true }] } + + // await setup() + // await expect(client.request('{ Post { id disabled } }')).resolves.toEqual(before) + // await action() + // await expect( + // client.request('{ Post(disabled: true) { id disabled } }'), + // ).resolves.toEqual(expected) + // }) + // }) + // }) + // }) + + // describe('enable', () => { + // const mutation = gql` + // mutation($id: ID!) { + // enable(id: $id) + // } + // ` + // const action = async () => { + // return client.request(mutation, variables) + // } + + // beforeEach(() => { + // our default 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) + // }) + // }) + // }) + // }) }) From 5c06ba39e254896ff472be66225dd4f58da85725 Mon Sep 17 00:00:00 2001 From: aonomike Date: Tue, 17 Sep 2019 18:46:45 +0300 Subject: [PATCH 02/10] Refactor moderate Comment tests --- .../src/schema/resolvers/moderation.spec.js | 87 ++++++++++++------- 1 file changed, 54 insertions(+), 33 deletions(-) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index c65243271..235afdda2 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -26,6 +26,7 @@ beforeAll(() => { }, }) mutate = createTestClient(server).mutate + query = createTestClient(server).query }) beforeEach(async () => { @@ -97,10 +98,11 @@ describe('disable', () => { let moderator beforeEach(async () => { moderator = await factory.create('User', { - id: 'moderator', + id: 'moderator-id', name: 'Moderator', email: 'moderator@example.org', password: '1234', + role: 'moderator', }) authenticatedUser = await moderator.toJson() }) @@ -118,6 +120,55 @@ describe('disable', () => { await expect(mutate({ mutation, variables })).resolves.toMatchObject(expected) }) }) + + describe('moderate a comment', () => { + let postVariables, commentVariables + beforeEach(async () => { + variables = { id: 'comment-id' } + postVariables = { + id: 'post-id', + title: 'post to comment on', + content: 'please comment on me', + categoryIds, + } + commentVariables = { + id: 'comment-id', + postId: 'post-id', + content: 'this comment was created for this post', + } + + await factory.create('Post', postVariables) + await factory.create('Comment', commentVariables) + }) + + it('returns disabled resource id', async () => { + const expected = { data: { disable: 'comment-id' } } + await expect(mutate({ mutation, variables })).resolves.toMatchObject(expected) + }) + + it('changes .disabledBy', async () => { + const commentQuery = gql` + query($id: ID!) { + Comment(id: $id) { + id + disabledBy { + id + } + } + } + ` + const before = { data: { Comment: [{ id: 'comment-id', disabledBy: null }] } } + const expected = { + data: { Comment: [{ id: 'comment-id', disabledBy: { id: 'moderator-id' } }] }, + } + variables = { id: 'comment-id' } + await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before) + await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + data: { disable: 'comment-id' }, + }) + await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) + }) + }) }) }) @@ -131,23 +182,13 @@ describe('disable', () => { // describe('as moderator', () => { - // describe('on something that is not a (Comment|Post|User) ', () => { // describe('on a comment', () => { // beforeEach(async () => { // variables = { // 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', @@ -163,26 +204,6 @@ describe('disable', () => { // } // }) - // it('returns disabled resource id', async () => { - // const expected = { disable: 'c47' } - // await setup() - // 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 }] } From 3e7e2403928f88fe2da42cef78d21c6eb8d4ef93 Mon Sep 17 00:00:00 2001 From: aonomike Date: Tue, 17 Sep 2019 18:59:21 +0300 Subject: [PATCH 03/10] Refactor it updates disabled test for Comment --- .../src/schema/resolvers/moderation.spec.js | 55 +++++++------------ 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 235afdda2..379f4aebf 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -151,6 +151,7 @@ describe('disable', () => { query($id: ID!) { Comment(id: $id) { id + disabled disabledBy { id } @@ -168,6 +169,25 @@ describe('disable', () => { }) await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) }) + + it('updates .disabled on comment', async () => { + const commentQuery = gql` + query($id: ID!) { + Comment(id: $id) { + id + disabled + } + } + ` + 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, variables })).resolves.toMatchObject({ + data: { disable: 'comment-id' }, + }) + await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) + }) }) }) }) @@ -182,41 +202,6 @@ describe('disable', () => { // describe('as moderator', () => { - // describe('on a comment', () => { - // beforeEach(async () => { - // variables = { - // id: 'c47', - // } - - // - // 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', - // }) - // await asAuthenticatedUser.create('Post', createPostVariables) - // await asAuthenticatedUser.create('Comment', createCommentVariables) - // } - // }) - - // 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 = { From 84c1d8ef252b3308374022b82a478078e638af21 Mon Sep 17 00:00:00 2001 From: aonomike Date: Wed, 18 Sep 2019 17:31:24 +0300 Subject: [PATCH 04/10] Refactor to eliminate duplicate code and start implementing refactor for Post --- .../src/schema/resolvers/moderation.spec.js | 71 +++++++++---------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 379f4aebf..64d39ecd1 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -122,47 +122,36 @@ describe('disable', () => { }) describe('moderate a comment', () => { - let postVariables, commentVariables + const commentQuery = gql` + query($id: ID!) { + Comment(id: $id) { + id + disabled + disabledBy { + id + } + } + } + ` beforeEach(async () => { - variables = { id: 'comment-id' } - postVariables = { - id: 'post-id', - title: 'post to comment on', - content: 'please comment on me', - categoryIds, - } - commentVariables = { + variables = {} + await factory.create('Comment', { id: 'comment-id', - postId: 'post-id', - content: 'this comment was created for this post', - } - - await factory.create('Post', postVariables) - await factory.create('Comment', commentVariables) + }) }) it('returns disabled resource id', async () => { + variables = { id: 'comment-id' } const expected = { data: { disable: 'comment-id' } } await expect(mutate({ mutation, variables })).resolves.toMatchObject(expected) }) it('changes .disabledBy', async () => { - const commentQuery = gql` - query($id: ID!) { - Comment(id: $id) { - id - disabled - disabledBy { - id - } - } - } - ` + variables = { id: 'comment-id' } const before = { data: { Comment: [{ id: 'comment-id', disabledBy: null }] } } const expected = { data: { Comment: [{ id: 'comment-id', disabledBy: { id: 'moderator-id' } }] }, } - variables = { id: 'comment-id' } await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before) await expect(mutate({ mutation, variables })).resolves.toMatchObject({ data: { disable: 'comment-id' }, @@ -171,14 +160,7 @@ describe('disable', () => { }) it('updates .disabled on comment', async () => { - const commentQuery = gql` - query($id: ID!) { - Comment(id: $id) { - id - disabled - } - } - ` + variables = { id: 'comment-id' } const before = { data: { Comment: [{ id: 'comment-id', disabled: false }] } } const expected = { data: { Comment: [{ id: 'comment-id', disabled: true }] } } @@ -189,6 +171,21 @@ describe('disable', () => { await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) }) }) + + describe('moderate a post', () => { + beforeEach(async () => { + variables = {} + await factory.create('Post', { + id: 'sample-post-id', + }) + }) + + it('returns disabled resource id', async () => { + variables = { id: 'sample-post-id' } + const expected = { data: { disable: 'sample-post-id' } } + await expect(mutate({ mutation, variables })).resolves.toMatchObject(expected) + }) + }) }) }) @@ -204,9 +201,7 @@ describe('disable', () => { // describe('on a post', () => { // beforeEach(async () => { - // variables = { - // id: 'p9', - // } + // // createResource = async () => { // await factory.create('User', { email: 'author@example.org', password: '1234' }) From e109b5f6b140ace085087666f3be4e974feb0741 Mon Sep 17 00:00:00 2001 From: aonomike Date: Wed, 18 Sep 2019 17:51:35 +0300 Subject: [PATCH 05/10] Refactor disable post updates disabledBy --- .../src/schema/resolvers/moderation.spec.js | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 64d39ecd1..208332b79 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -179,11 +179,35 @@ describe('disable', () => { id: 'sample-post-id', }) }) - + const postQuery = gql` + query($id: ID) { + Post(id: $id) { + id + disabledBy { + id + } + } + } + ` it('returns disabled resource id', async () => { variables = { id: 'sample-post-id' } - const expected = { data: { disable: 'sample-post-id' } } - await expect(mutate({ mutation, variables })).resolves.toMatchObject(expected) + await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + data: { disable: 'sample-post-id' }, + }) + }) + + it.only('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, variables })).resolves.toMatchObject({ + data: { disable: 'sample-post-id' }, + }) + await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected) }) }) }) @@ -213,12 +237,6 @@ describe('disable', () => { // } // }) - // it('returns disabled resource id', async () => { - // const expected = { disable: 'p9' } - // await setup() - // await expect(action()).resolves.toEqual(expected) - // }) - // it('changes .disabledBy', async () => { // const before = { Post: [{ id: 'p9', disabledBy: null }] } // const expected = { Post: [{ id: 'p9', disabledBy: { id: 'u7' } }] } From ca970eb30ac1e5e08e6d0ef4b9dff58edab2af37 Mon Sep 17 00:00:00 2001 From: aonomike Date: Wed, 18 Sep 2019 17:59:29 +0300 Subject: [PATCH 06/10] Refactor disable post updates disabled --- .../src/schema/resolvers/moderation.spec.js | 60 +++++-------------- 1 file changed, 14 insertions(+), 46 deletions(-) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 208332b79..d02527632 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -183,6 +183,7 @@ describe('disable', () => { query($id: ID) { Post(id: $id) { id + disabled disabledBy { id } @@ -196,7 +197,7 @@ describe('disable', () => { }) }) - it.only('changes .disabledBy', async () => { + it('changes .disabledBy', async () => { variables = { id: 'sample-post-id' } const before = { data: { Post: [{ id: 'sample-post-id', disabledBy: null }] } } const expected = { @@ -209,6 +210,18 @@ describe('disable', () => { }) 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, variables })).resolves.toMatchObject({ + data: { disable: 'sample-post-id' }, + }) + await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected) + }) }) }) }) @@ -221,51 +234,6 @@ describe('disable', () => { // }) // }) - // describe('as moderator', () => { - - // describe('on a post', () => { - // beforeEach(async () => { - // - - // 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', { - // id: 'p9', // that's the ID we will look for - // categoryIds, - // }) - // } - // }) - - // it('changes .disabledBy', async () => { - // const before = { Post: [{ id: 'p9', disabledBy: null }] } - // const expected = { Post: [{ id: 'p9', disabledBy: { id: 'u7' } }] } - - // await setup() - // await expect(client.request('{ Post { id, disabledBy { id } } }')).resolves.toEqual( - // before, - // ) - // await action() - // await expect( - // client.request('{ Post(disabled: true) { id, disabledBy { id } } }'), - // ).resolves.toEqual(expected) - // }) - - // it('updates .disabled on post', async () => { - // const before = { Post: [{ id: 'p9', disabled: false }] } - // const expected = { Post: [{ id: 'p9', disabled: true }] } - - // await setup() - // await expect(client.request('{ Post { id disabled } }')).resolves.toEqual(before) - // await action() - // await expect( - // client.request('{ Post(disabled: true) { id disabled } }'), - // ).resolves.toEqual(expected) - // }) - // }) - // }) - // }) - // describe('enable', () => { // const mutation = gql` // mutation($id: ID!) { From bdfc2127d5bf1925d0db642eb012e1981adbf487 Mon Sep 17 00:00:00 2001 From: aonomike Date: Wed, 18 Sep 2019 22:29:14 +0300 Subject: [PATCH 07/10] fix code review issues --- .../src/schema/resolvers/moderation.spec.js | 648 +++++++++--------- 1 file changed, 325 insertions(+), 323 deletions(-) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index d02527632..52ad3d1a8 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -51,362 +51,364 @@ beforeEach(async () => { afterEach(async () => { await factory.cleanDatabase() }) +describe('moderate resources', () => { + describe('disable', () => { + const mutation = gql` + mutation($id: ID!) { + disable(id: $id) + } + ` + let variables -describe('disable', () => { - const mutation = gql` - mutation($id: ID!) { - disable(id: $id) - } - ` - let variables - - beforeEach(() => { - // our defaul set of variables - variables = { - id: 'some-resource', - } - }) - describe('unauthenticated', () => { - beforeEach(async () => { - authenticatedUser = await currentUser.toJson() + beforeEach(() => { + variables = { + id: 'some-resource', + } }) - it('throws authorization error', async () => { - const { errors } = await mutate({ mutation, variables }) - expect(errors[0]).toHaveProperty('message', 'Not Authorised!') - }) - }) - describe('authenticated', () => { - describe('i am not a moderator', () => { - let nonModerator - + describe('unauthenticated', () => { beforeEach(async () => { - nonModerator = await factory.create('User', { - id: 'non-moderator', - name: 'Non Moderator', - email: 'non.moderator@example.org', - password: '1234', - }) - authenticatedUser = await nonModerator.toJson() + authenticatedUser = await currentUser.toJson() }) it('throws authorization error', async () => { - const { errors } = await mutate({ mutation, variables }) - expect(errors[0]).toHaveProperty('message', 'Not Authorised!') + await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + errors: [{ message: 'Not Authorised!' }], + }) }) }) + describe('authenticated', () => { + describe('i am not a moderator', () => { + let nonModerator - describe('I am a moderator', () => { - let moderator - beforeEach(async () => { - moderator = await factory.create('User', { - id: 'moderator-id', - name: 'Moderator', - email: 'moderator@example.org', - password: '1234', - role: 'moderator', - }) - authenticatedUser = await moderator.toJson() - }) - - describe('moderate a resource that is not a (Comment|Post|User) ', () => { beforeEach(async () => { - variables = { - id: 'sample-tag-id', - } - await Promise.all([factory.create('Tag', { id: 'sample-tag-id' })]) + nonModerator = await factory.create('User', { + id: 'non-moderator', + name: 'Non Moderator', + email: 'non.moderator@example.org', + password: '1234', + }) + authenticatedUser = await nonModerator.toJson() }) - - it('returns null', async () => { - const expected = { data: { disable: null } } - await expect(mutate({ mutation, variables })).resolves.toMatchObject(expected) + it('throws authorization error', async () => { + await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + errors: [{ message: 'Not Authorised!' }], + }) }) }) - describe('moderate a comment', () => { - const commentQuery = gql` - query($id: ID!) { - Comment(id: $id) { - id - disabled - disabledBy { + describe('I am a moderator', () => { + let moderator + beforeEach(async () => { + moderator = await factory.create('User', { + id: 'moderator-id', + name: 'Moderator', + email: 'moderator@example.org', + password: '1234', + role: 'moderator', + }) + authenticatedUser = await moderator.toJson() + }) + + describe('moderate a resource that is not a (Comment|Post|User) ', () => { + beforeEach(async () => { + variables = { + id: 'sample-tag-id', + } + await Promise.all([factory.create('Tag', { id: 'sample-tag-id' })]) + }) + + it('returns null', async () => { + await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + data: { disable: null }, + }) + }) + }) + + describe('moderate a comment', () => { + const commentQuery = gql` + query($id: ID!) { + Comment(id: $id) { id + disabled + disabledBy { + id + } } } - } - ` - beforeEach(async () => { - variables = {} - await factory.create('Comment', { - id: 'comment-id', + ` + beforeEach(async () => { + variables = {} + await factory.create('Comment', { + id: 'comment-id', + }) + }) + + it('returns disabled resource id', async () => { + variables = { id: 'comment-id' } + await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + data: { disable: 'comment-id' }, + }) + }) + + 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, 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, variables })).resolves.toMatchObject({ + data: { disable: 'comment-id' }, + }) + await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) }) }) - it('returns disabled resource id', async () => { - variables = { id: 'comment-id' } - const expected = { data: { disable: 'comment-id' } } - await expect(mutate({ mutation, variables })).resolves.toMatchObject(expected) - }) - - 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, variables })).resolves.toMatchObject({ - data: { disable: 'comment-id' }, + describe('moderate a post', () => { + beforeEach(async () => { + variables = {} + await factory.create('Post', { + id: 'sample-post-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, variables })).resolves.toMatchObject({ - data: { disable: 'comment-id' }, - }) - await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) - }) - }) - - describe('moderate a post', () => { - beforeEach(async () => { - variables = {} - await factory.create('Post', { - id: 'sample-post-id', - }) - }) - const postQuery = gql` - query($id: ID) { - Post(id: $id) { - id - disabled - disabledBy { + const postQuery = gql` + query($id: ID) { + Post(id: $id) { id + disabled + disabledBy { + id + } } } - } - ` - it('returns disabled resource id', async () => { - variables = { id: 'sample-post-id' } - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ - data: { disable: 'sample-post-id' }, + ` + it('returns disabled resource id', async () => { + variables = { id: 'sample-post-id' } + await expect(mutate({ mutation, 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' } }] }, - } + 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, variables })).resolves.toMatchObject({ - data: { disable: 'sample-post-id' }, + await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before) + await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + data: { disable: 'sample-post-id' }, + }) + await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected) }) - 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' } + 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, variables })).resolves.toMatchObject({ - data: { disable: 'sample-post-id' }, + await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before) + await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + data: { disable: 'sample-post-id' }, + }) + await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected) }) - await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected) }) }) }) }) - // describe('authenticated', () => { - // beforeEach(() => { - // authenticateClient = setupAuthenticateClient({ - // email: 'user@example.org', - // password: '1234', - // }) - // }) - - // describe('enable', () => { - // const mutation = gql` - // mutation($id: ID!) { - // enable(id: $id) - // } - // ` - // const action = async () => { - // return client.request(mutation, variables) - // } - - // beforeEach(() => { - // our default 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) - // }) - // }) - // }) - // }) + describe('enable', () => { + // describe('authenticated', () => { + // beforeEach(() => { + // authenticateClient = setupAuthenticateClient({ + // email: 'user@example.org', + // password: '1234', + // }) + // }) + // describe('enable', () => { + // const mutation = gql` + // mutation($id: ID!) { + // enable(id: $id) + // } + // ` + // const action = async () => { + // return client.request(mutation, variables) + // } + // beforeEach(() => { + // our default 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) + // }) + // }) + // }) + // }) + }) }) From 8f2456aec48d2c86deea92a860a9dd37fb2135ce Mon Sep 17 00:00:00 2001 From: aonomike Date: Thu, 19 Sep 2019 17:01:42 +0300 Subject: [PATCH 08/10] Test unauthanticated enable --- .../src/schema/resolvers/moderation.spec.js | 81 +++++++++---------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 52ad3d1a8..650e92d62 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -8,50 +8,34 @@ import createServer from '../../server' const factory = Factory() const neode = getNeode() const driver = getDriver() -const categoryIds = ['cat9'] let query, mutate, authenticatedUser, variables, currentUser -let createPostVariables -let createCommentVariables -beforeAll(() => { - authenticatedUser = undefined - const { server } = createServer({ - context: () => { - return { - driver, - neode, - user: authenticatedUser, - } - }, - }) - mutate = createTestClient(server).mutate - query = createTestClient(server).query -}) - -beforeEach(async () => { - variables = {} - currentUser = await factory.create('User', { - id: 'current-user', - name: 'TestUser', - email: 'test@example.org', - password: '1234', - }) - - await Promise.all([ - neode.create('Category', { - id: 'cat9', - name: 'Democracy & Politics', - icon: 'university', - }), - ]) - authenticatedUser = null -}) - -afterEach(async () => { - await factory.cleanDatabase() -}) describe('moderate resources', () => { + beforeAll(() => { + authenticatedUser = undefined + const { server } = createServer({ + context: () => { + return { + driver, + neode, + user: authenticatedUser, + } + }, + }) + mutate = createTestClient(server).mutate + query = createTestClient(server).query + }) + + beforeEach(async () => { + variables = {} + authenticatedUser = null + }) + + afterEach(async () => { + await factory.cleanDatabase() + }) + describe('disable', () => { const mutation = gql` mutation($id: ID!) { @@ -66,9 +50,6 @@ describe('moderate resources', () => { } }) describe('unauthenticated', () => { - beforeEach(async () => { - authenticatedUser = await currentUser.toJson() - }) it('throws authorization error', async () => { await expect(mutate({ mutation, variables })).resolves.toMatchObject({ errors: [{ message: 'Not Authorised!' }], @@ -231,6 +212,20 @@ describe('moderate resources', () => { }) describe('enable', () => { + const mutation = gql` + mutation($id: ID!) { + enable(id: $id) + } + ` + describe('unautenticated', () => { + it('throws authorization error', async () => { + variables = { id: 'sample-post-id' } + await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + errors: [{ message: 'Not Authorised!' }], + }) + }) + }) + // describe('authenticated', () => { // beforeEach(() => { // authenticateClient = setupAuthenticateClient({ From 347f37efe4837f4798e156a6d5e1fbd9e264411f Mon Sep 17 00:00:00 2001 From: aonomike Date: Fri, 20 Sep 2019 14:23:39 +0300 Subject: [PATCH 09/10] Add enable resources test path --- .../src/schema/resolvers/moderation.spec.js | 418 ++++++++---------- 1 file changed, 180 insertions(+), 238 deletions(-) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 650e92d62..784ea4545 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -1,5 +1,4 @@ import { createTestClient } from 'apollo-server-testing' -import { GraphQLClient } from 'graphql-request' import Factory from '../../seed/factories' import { gql } from '../../jest/helpers' import { neode as getNeode, getDriver } from '../../bootstrap/neo4j' @@ -9,7 +8,42 @@ const factory = Factory() const neode = getNeode() const driver = getDriver() -let query, mutate, authenticatedUser, variables, currentUser +let query, mutate, authenticatedUser, variables, moderator, nonModerator + +const disableMutation = gql` + mutation($id: ID!) { + disable(id: $id) + } +` +const enableMutation = gql` + mutation($id: ID!) { + enable(id: $id) + } +` + +const commentQuery = gql` + query($id: ID!) { + Comment(id: $id) { + id + disabled + disabledBy { + id + } + } + } +` + +const postQuery = gql` + query($id: ID) { + Post(id: $id) { + id + disabled + disabledBy { + id + } + } + } +` describe('moderate resources', () => { beforeAll(() => { @@ -30,6 +64,13 @@ describe('moderate resources', () => { 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 () => { @@ -37,13 +78,6 @@ describe('moderate resources', () => { }) describe('disable', () => { - const mutation = gql` - mutation($id: ID!) { - disable(id: $id) - } - ` - let variables - beforeEach(() => { variables = { id: 'some-resource', @@ -51,15 +85,13 @@ describe('moderate resources', () => { }) describe('unauthenticated', () => { it('throws authorization error', async () => { - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ errors: [{ message: 'Not Authorised!' }], }) }) }) describe('authenticated', () => { - describe('i am not a moderator', () => { - let nonModerator - + describe('non moderator', () => { beforeEach(async () => { nonModerator = await factory.create('User', { id: 'non-moderator', @@ -70,22 +102,14 @@ describe('moderate resources', () => { authenticatedUser = await nonModerator.toJson() }) it('throws authorization error', async () => { - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ errors: [{ message: 'Not Authorised!' }], }) }) }) - describe('I am a moderator', () => { - let moderator + describe('moderator', () => { beforeEach(async () => { - moderator = await factory.create('User', { - id: 'moderator-id', - name: 'Moderator', - email: 'moderator@example.org', - password: '1234', - role: 'moderator', - }) authenticatedUser = await moderator.toJson() }) @@ -98,24 +122,13 @@ describe('moderate resources', () => { }) it('returns null', async () => { - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ data: { disable: null }, }) }) }) describe('moderate a comment', () => { - const commentQuery = gql` - query($id: ID!) { - Comment(id: $id) { - id - disabled - disabledBy { - id - } - } - } - ` beforeEach(async () => { variables = {} await factory.create('Comment', { @@ -125,8 +138,9 @@ describe('moderate resources', () => { it('returns disabled resource id', async () => { variables = { id: 'comment-id' } - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ data: { disable: 'comment-id' }, + errors: undefined, }) }) @@ -137,7 +151,7 @@ describe('moderate resources', () => { data: { Comment: [{ id: 'comment-id', disabledBy: { id: 'moderator-id' } }] }, } await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before) - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ data: { disable: 'comment-id' }, }) await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) @@ -149,7 +163,7 @@ describe('moderate resources', () => { const expected = { data: { Comment: [{ id: 'comment-id', disabled: true }] } } await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(before) - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ data: { disable: 'comment-id' }, }) await expect(query({ query: commentQuery, variables })).resolves.toMatchObject(expected) @@ -163,20 +177,10 @@ describe('moderate resources', () => { id: 'sample-post-id', }) }) - const postQuery = gql` - query($id: ID) { - Post(id: $id) { - id - disabled - disabledBy { - id - } - } - } - ` + it('returns disabled resource id', async () => { variables = { id: 'sample-post-id' } - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ data: { disable: 'sample-post-id' }, }) }) @@ -189,7 +193,7 @@ describe('moderate resources', () => { } await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before) - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ data: { disable: 'sample-post-id' }, }) await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected) @@ -201,7 +205,7 @@ describe('moderate resources', () => { variables = { id: 'sample-post-id' } await expect(query({ query: postQuery, variables })).resolves.toMatchObject(before) - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect(mutate({ mutation: disableMutation, variables })).resolves.toMatchObject({ data: { disable: 'sample-post-id' }, }) await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected) @@ -212,198 +216,136 @@ describe('moderate resources', () => { }) describe('enable', () => { - const mutation = gql` - mutation($id: ID!) { - enable(id: $id) - } - ` - describe('unautenticated', () => { + describe('unautenticated user', () => { it('throws authorization error', async () => { variables = { id: 'sample-post-id' } - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect(mutate({ mutation: enableMutation, variables })).resolves.toMatchObject({ errors: [{ message: 'Not Authorised!' }], }) }) }) - // describe('authenticated', () => { - // beforeEach(() => { - // authenticateClient = setupAuthenticateClient({ - // email: 'user@example.org', - // password: '1234', - // }) - // }) - // describe('enable', () => { - // const mutation = gql` - // mutation($id: ID!) { - // enable(id: $id) - // } - // ` - // const action = async () => { - // return client.request(mutation, variables) - // } - // beforeEach(() => { - // our default 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) - // }) - // }) - // }) - // }) + 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 }, + }) + }) + }) + + describe('moderate a comment', () => { + beforeEach(async () => { + variables = { id: 'comment-id' } + 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) + }) + }) + + describe('moderate a post', () => { + beforeEach(async () => { + variables = { id: 'post-id' } + await factory.create('Post', { + id: 'post-id', + }) + await mutate({ mutation: disableMutation, variables }) + }) + + it('returns enabled resource id', async () => { + await expect(mutate({ mutation: enableMutation, variables })).resolves.toMatchObject({ + data: { enable: 'post-id' }, + errors: undefined, + }) + }) + + it('changes .disabledBy', async () => { + const expected = { + 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) + }) + + it.only('updates .disabled on post', async () => { + const expected = { + data: { Post: [{ id: 'post-id', disabled: false }] }, + 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) + }) + }) + }) + }) }) }) From 1952d63b3cc448955c467feca1fd54cfa56de3af Mon Sep 17 00:00:00 2001 From: roschaefer Date: Fri, 20 Sep 2019 18:27:37 +0200 Subject: [PATCH 10/10] Remove .only from moderation.spec.js @aonomike @vbelolapotkov you made it!! You are the best. Sure, the disable/enable can be improved by returning a union type. But I leave that for another PR. :+1: --- backend/src/schema/resolvers/moderation.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/schema/resolvers/moderation.spec.js b/backend/src/schema/resolvers/moderation.spec.js index 784ea4545..2ac0dd934 100644 --- a/backend/src/schema/resolvers/moderation.spec.js +++ b/backend/src/schema/resolvers/moderation.spec.js @@ -118,7 +118,7 @@ describe('moderate resources', () => { variables = { id: 'sample-tag-id', } - await Promise.all([factory.create('Tag', { id: 'sample-tag-id' })]) + await factory.create('Tag', { id: 'sample-tag-id' }) }) it('returns null', async () => { @@ -332,7 +332,7 @@ describe('moderate resources', () => { await expect(query({ query: postQuery, variables })).resolves.toMatchObject(expected) }) - it.only('updates .disabled on post', async () => { + it('updates .disabled on post', async () => { const expected = { data: { Post: [{ id: 'post-id', disabled: false }] }, errors: undefined,