From 700bdcb8f1608bb10af0ca24e20230284ab4f956 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Wed, 7 Aug 2019 17:21:52 +0200 Subject: [PATCH] Implement+test unblock mutation --- .../src/middleware/permissionsMiddleware.js | 1 + backend/src/schema/resolvers/users.js | 17 +- .../resolvers/users/blockedUsers.spec.js | 163 ++++++++++++++---- 3 files changed, 150 insertions(+), 31 deletions(-) diff --git a/backend/src/middleware/permissionsMiddleware.js b/backend/src/middleware/permissionsMiddleware.js index bd5046331..0f6f9e1c5 100644 --- a/backend/src/middleware/permissionsMiddleware.js +++ b/backend/src/middleware/permissionsMiddleware.js @@ -197,6 +197,7 @@ const permissions = shield( AddPostEmotions: isAuthenticated, RemovePostEmotions: isAuthenticated, block: isAuthenticated, + unblock: isAuthenticated, }, User: { email: isMyOwn, diff --git a/backend/src/schema/resolvers/users.js b/backend/src/schema/resolvers/users.js index 394f74e8e..66adc8734 100644 --- a/backend/src/schema/resolvers/users.js +++ b/backend/src/schema/resolvers/users.js @@ -38,14 +38,27 @@ export default { }, Mutation: { block: async (object, args, context, resolveInfo) => { - if(context.user.id === args.id) return null + if (context.user.id === args.id) return null const [user, blockedUser] = await Promise.all([ instance.find('User', context.user.id), - instance.find('User', args.id) + instance.find('User', args.id), ]) await user.relateTo(blockedUser, 'blocked') return blockedUser.toJson() }, + unblock: async (object, args, context, resolveInfo) => { + const { user: currentUser } = context + if (currentUser.id === args.id) return null + await instance.cypher( + ` + MATCH(u:User {id: $currentUser.id})-[r:BLOCKED]->(b:User {id: $args.id}) + DELETE r + `, + { currentUser, args }, + ) + const blockedUser = await instance.find('User', args.id) + return blockedUser.toJson() + }, UpdateUser: async (object, args, context, resolveInfo) => { args = await fileUpload(args, { file: 'avatarUpload', url: 'avatar' }) try { diff --git a/backend/src/schema/resolvers/users/blockedUsers.spec.js b/backend/src/schema/resolvers/users/blockedUsers.spec.js index 826ea649d..75fa791c0 100644 --- a/backend/src/schema/resolvers/users/blockedUsers.spec.js +++ b/backend/src/schema/resolvers/users/blockedUsers.spec.js @@ -89,15 +89,17 @@ describe('block', () => { beforeEach(() => { currentUser = undefined - blockAction = (variables) => { + blockAction = variables => { const { mutate } = createTestClient(server) - const blockMutation = gql`mutation($id: ID!) { - block(id: $id) { - id - name - isBlocked + const blockMutation = gql` + mutation($id: ID!) { + block(id: $id) { + id + name + isBlocked + } } - }` + ` return mutate({ mutation: blockMutation, variables }) } }) @@ -118,15 +120,17 @@ describe('block', () => { describe('block yourself', () => { it('returns null', async () => { - await expect(blockAction({ id: 'u1' })) - .resolves.toEqual(expect.objectContaining({ data: { block: null } })) + await expect(blockAction({ id: 'u1' })).resolves.toEqual( + expect.objectContaining({ data: { block: null } }), + ) }) }) describe('block not existing user', () => { it('returns null', async () => { - await expect(blockAction({ id: 'u2' })) - .resolves.toEqual(expect.objectContaining({ data: { block: null } })) + await expect(blockAction({ id: 'u2' })).resolves.toEqual( + expect.objectContaining({ data: { block: null } }), + ) }) }) @@ -139,24 +143,37 @@ describe('block', () => { }) it('blocks a user', async () => { - await expect(blockAction({ id: 'u2' })) - .resolves.toEqual(expect.objectContaining({ - data: { block: { id: 'u2', name: 'Blocked User', isBlocked: true} } - })) + await expect(blockAction({ id: 'u2' })).resolves.toEqual( + expect.objectContaining({ + data: { block: { id: 'u2', name: 'Blocked User', isBlocked: true } }, + }), + ) }) it('unfollows the user', async () => { const user = await instance.find('User', currentUser.id) await user.relateTo(blockedUser, 'following') - const queryUser = gql`query { User(id: "u2") { id isBlocked followedByCurrentUser } }` + const queryUser = gql` + query { + User(id: "u2") { + id + isBlocked + followedByCurrentUser + } + } + ` const { query } = createTestClient(server) - await expect(query({ query: queryUser })).resolves.toEqual(expect.objectContaining({ - data: { User: [{id: "u2", isBlocked: false, followedByCurrentUser: true }] } - })) - await blockAction({id: 'u2'}) - await expect(query({ query: queryUser })).resolves.toEqual(expect.objectContaining({ - data: { User: [{id: "u2", isBlocked: true, followedByCurrentUser: false }] } - })) + await expect(query({ query: queryUser })).resolves.toEqual( + expect.objectContaining({ + data: { User: [{ id: 'u2', isBlocked: false, followedByCurrentUser: true }] }, + }), + ) + await blockAction({ id: 'u2' }) + await expect(query({ query: queryUser })).resolves.toEqual( + expect.objectContaining({ + data: { User: [{ id: 'u2', isBlocked: true, followedByCurrentUser: false }] }, + }), + ) }) describe('blocked user writes a post', () => { @@ -171,13 +188,101 @@ describe('block', () => { }) describe('unblock', () => { - it.todo('throws permission error') + let unblockAction + + beforeEach(() => { + currentUser = undefined + unblockAction = variables => { + const { mutate } = createTestClient(server) + const unblockMutation = gql` + mutation($id: ID!) { + unblock(id: $id) { + id + name + isBlocked + } + } + ` + return mutate({ mutation: unblockMutation, variables }) + } + }) + + it('throws permission error', async () => { + const result = await unblockAction({ id: 'u2' }) + expect(result.errors[0]).toHaveProperty('message', 'Not Authorised!') + }) + describe('authenticated', () => { - it.todo('throws argument error') - describe('given a blocked user', () => { - it.todo('unblocks a user') - describe('unblocking twice', () => { - it.todo('has no effect') + beforeEach(async () => { + currentUser = await instance.create('User', { + name: 'Current User', + id: 'u1', + }) + currentUser = await currentUser.toJson() + }) + + describe('unblock yourself', () => { + it('returns null', async () => { + await expect(unblockAction({ id: 'u1' })).resolves.toEqual( + expect.objectContaining({ data: { unblock: null } }), + ) + }) + }) + + describe('unblock not-existing user', () => { + it('returns null', async () => { + await expect(unblockAction({ id: 'lksjdflksfdj' })).resolves.toEqual( + expect.objectContaining({ data: { unblock: null } }), + ) + }) + }) + + describe('given another user', () => { + let user, blockedUser + + beforeEach(async () => { + ;[user, blockedUser] = await Promise.all([ + instance.find('User', 'u1'), + instance.create('User', { + name: 'Blocked User', + id: 'u2', + }), + ]) + }) + + describe('unblocking a not yet blocked user', () => { + it('does not hurt', async () => { + await expect(unblockAction({ id: 'u2' })).resolves.toEqual( + expect.objectContaining({ + data: { unblock: { id: 'u2', name: 'Blocked User', isBlocked: false } }, + }), + ) + }) + }) + + describe('given a blocked user', () => { + beforeEach(async () => { + await user.relateTo(blockedUser, 'blocked') + }) + + it('unblocks a user', async () => { + await expect(unblockAction({ id: 'u2' })).resolves.toEqual( + expect.objectContaining({ + data: { unblock: { id: 'u2', name: 'Blocked User', isBlocked: false } }, + }), + ) + }) + + describe('unblocking twice', () => { + it('has no effect', async () => { + await unblockAction({ id: 'u2' }) + await expect(unblockAction({ id: 'u2' })).resolves.toEqual( + expect.objectContaining({ + data: { unblock: { id: 'u2', name: 'Blocked User', isBlocked: false } }, + }), + ) + }) + }) }) }) })