mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Add tests for 'JoinGroup' and 'GroupMember' resolver
This commit is contained in:
parent
d36f0eab57
commit
14620b00eb
@ -39,9 +39,9 @@ export const createGroupMutation = gql`
|
||||
}
|
||||
`
|
||||
|
||||
export const enterGroupMutation = gql`
|
||||
export const joinGroupMutation = gql`
|
||||
mutation ($id: ID!, $userId: ID!) {
|
||||
EnterGroup(id: $id, userId: $userId) {
|
||||
JoinGroup(id: $id, userId: $userId) {
|
||||
id
|
||||
name
|
||||
slug
|
||||
|
||||
@ -5,7 +5,7 @@ import createServer from '../server'
|
||||
import faker from '@faker-js/faker'
|
||||
import Factory from '../db/factories'
|
||||
import { getNeode, getDriver } from '../db/neo4j'
|
||||
import { createGroupMutation, enterGroupMutation } from './graphql/groups'
|
||||
import { createGroupMutation, joinGroupMutation } from './graphql/groups'
|
||||
import { createPostMutation } from './graphql/posts'
|
||||
import { createCommentMutation } from './graphql/comments'
|
||||
|
||||
@ -402,14 +402,14 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl']
|
||||
])
|
||||
await Promise.all([
|
||||
mutate({
|
||||
mutation: enterGroupMutation,
|
||||
mutation: joinGroupMutation,
|
||||
variables: {
|
||||
id: 'g0',
|
||||
userId: 'u2',
|
||||
},
|
||||
}),
|
||||
mutate({
|
||||
mutation: enterGroupMutation,
|
||||
mutation: joinGroupMutation,
|
||||
variables: {
|
||||
id: 'g0',
|
||||
userId: 'u3',
|
||||
@ -434,28 +434,28 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl']
|
||||
])
|
||||
await Promise.all([
|
||||
mutate({
|
||||
mutation: enterGroupMutation,
|
||||
mutation: joinGroupMutation,
|
||||
variables: {
|
||||
id: 'g1',
|
||||
userId: 'u1',
|
||||
},
|
||||
}),
|
||||
mutate({
|
||||
mutation: enterGroupMutation,
|
||||
mutation: joinGroupMutation,
|
||||
variables: {
|
||||
id: 'g1',
|
||||
userId: 'u5',
|
||||
},
|
||||
}),
|
||||
mutate({
|
||||
mutation: enterGroupMutation,
|
||||
mutation: joinGroupMutation,
|
||||
variables: {
|
||||
id: 'g1',
|
||||
userId: 'u6',
|
||||
},
|
||||
}),
|
||||
mutate({
|
||||
mutation: enterGroupMutation,
|
||||
mutation: joinGroupMutation,
|
||||
variables: {
|
||||
id: 'g1',
|
||||
userId: 'u7',
|
||||
@ -480,28 +480,28 @@ const languages = ['de', 'en', 'es', 'fr', 'it', 'pt', 'pl']
|
||||
])
|
||||
await Promise.all([
|
||||
mutate({
|
||||
mutation: enterGroupMutation,
|
||||
mutation: joinGroupMutation,
|
||||
variables: {
|
||||
id: 'g2',
|
||||
userId: 'u4',
|
||||
},
|
||||
}),
|
||||
mutate({
|
||||
mutation: enterGroupMutation,
|
||||
mutation: joinGroupMutation,
|
||||
variables: {
|
||||
id: 'g2',
|
||||
userId: 'u5',
|
||||
},
|
||||
}),
|
||||
mutate({
|
||||
mutation: enterGroupMutation,
|
||||
mutation: joinGroupMutation,
|
||||
variables: {
|
||||
id: 'g2',
|
||||
userId: 'u6',
|
||||
},
|
||||
}),
|
||||
mutate({
|
||||
mutation: enterGroupMutation,
|
||||
mutation: joinGroupMutation,
|
||||
variables: {
|
||||
id: 'g2',
|
||||
userId: 'u7',
|
||||
|
||||
@ -64,22 +64,26 @@ const isAllowedSeeingMembersOfGroup = rule({
|
||||
const transactionResponse = await transaction.run(
|
||||
`
|
||||
MATCH (group:Group {id: $groupId})
|
||||
OPTIONAL MATCH (admin:User {id: $userId})-[membership:MEMBER_OF]->(group)
|
||||
WHERE membership.role IN ['admin', 'owner']
|
||||
RETURN group {.*}, admin {.*, myRoleInGroup: membership.role}
|
||||
OPTIONAL MATCH (member:User {id: $userId})-[membership:MEMBER_OF]->(group)
|
||||
RETURN group {.*}, member {.*, myRoleInGroup: membership.role}
|
||||
`,
|
||||
{ groupId, userId: user.id },
|
||||
)
|
||||
return {
|
||||
admin: transactionResponse.records.map((record) => record.get('admin'))[0],
|
||||
member: transactionResponse.records.map((record) => record.get('member'))[0],
|
||||
group: transactionResponse.records.map((record) => record.get('group'))[0],
|
||||
}
|
||||
})
|
||||
try {
|
||||
const { admin, group } = await readTxPromise
|
||||
// Wolle: console.log('admin: ', admin)
|
||||
const { member, group } = await readTxPromise
|
||||
// Wolle: console.log('member: ', member)
|
||||
// console.log('group: ', group)
|
||||
return group.groupType === 'public' || !!admin
|
||||
return (
|
||||
group.groupType === 'public' ||
|
||||
(['closed', 'hidden'].includes(group.groupType) &&
|
||||
!!member &&
|
||||
['usual', 'admin', 'owner'].includes(member.myRoleInGroup))
|
||||
)
|
||||
} catch (error) {
|
||||
// Wolle: console.log('error: ', error)
|
||||
throw new Error(error)
|
||||
@ -179,7 +183,7 @@ export default shield(
|
||||
SignupVerification: allow,
|
||||
UpdateUser: onlyYourself,
|
||||
CreateGroup: isAuthenticated,
|
||||
EnterGroup: isAuthenticated,
|
||||
JoinGroup: isAuthenticated,
|
||||
CreatePost: isAuthenticated,
|
||||
UpdatePost: isAuthor,
|
||||
DeletePost: isAuthor,
|
||||
|
||||
@ -131,11 +131,11 @@ export default {
|
||||
session.close()
|
||||
}
|
||||
},
|
||||
EnterGroup: async (_parent, params, context, _resolveInfo) => {
|
||||
JoinGroup: async (_parent, params, context, _resolveInfo) => {
|
||||
const { id: groupId, userId } = params
|
||||
const session = context.driver.session()
|
||||
const writeTxResultPromise = session.writeTransaction(async (transaction) => {
|
||||
const enterGroupCypher = `
|
||||
const joinGroupCypher = `
|
||||
MATCH (member:User {id: $userId}), (group:Group {id: $groupId})
|
||||
MERGE (member)-[membership:MEMBER_OF]->(group)
|
||||
ON CREATE SET
|
||||
@ -148,7 +148,7 @@ export default {
|
||||
END
|
||||
RETURN member {.*, myRoleInGroup: membership.role}
|
||||
`
|
||||
const result = await transaction.run(enterGroupCypher, { groupId, userId })
|
||||
const result = await transaction.run(joinGroupCypher, { groupId, userId })
|
||||
const [member] = await result.records.map((record) => record.get('member'))
|
||||
return member
|
||||
})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -114,7 +114,7 @@ type Mutation {
|
||||
|
||||
DeleteGroup(id: ID!): Group
|
||||
|
||||
EnterGroup(
|
||||
JoinGroup(
|
||||
id: ID!
|
||||
userId: ID!
|
||||
): User
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user