Implement+test unblock mutation

This commit is contained in:
Robert Schäfer 2019-08-07 17:21:52 +02:00 committed by roschaefer
parent f5a59568ab
commit 700bdcb8f1
3 changed files with 150 additions and 31 deletions

View File

@ -197,6 +197,7 @@ const permissions = shield(
AddPostEmotions: isAuthenticated,
RemovePostEmotions: isAuthenticated,
block: isAuthenticated,
unblock: isAuthenticated,
},
User: {
email: isMyOwn,

View File

@ -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 {

View File

@ -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 } },
}),
)
})
})
})
})
})