Fix slugification tests of 'UpdateGroup'

This commit is contained in:
Wolfgang Huß 2022-09-01 15:04:49 +02:00
parent 44eb9d0bde
commit 4f7ce5a6c1
7 changed files with 80 additions and 48 deletions

View File

@ -12,6 +12,8 @@ export const createGroupMutation = gql`
$groupType: GroupType!
$actionRadius: GroupActionRadius!
$categoryIds: [ID]
$avatar: ImageInput
$locationName: String
) {
CreateGroup(
id: $id
@ -22,6 +24,8 @@ export const createGroupMutation = gql`
groupType: $groupType
actionRadius: $actionRadius
categoryIds: $categoryIds
avatar: $avatar
locationName: $locationName
) {
id
name
@ -34,6 +38,14 @@ export const createGroupMutation = gql`
description
groupType
actionRadius
# categories {
# id
# slug
# name
# icon
# }
# avatar
# locationName
myRole
}
}
@ -44,11 +56,12 @@ export const updateGroupMutation = gql`
$id: ID!
$name: String
$slug: String
$avatar: ImageInput
$about: String
$description: String
$actionRadius: GroupActionRadius
$categoryIds: [ID]
$avatar: ImageInput
$locationName: String
) {
UpdateGroup(
id: $id
@ -59,11 +72,11 @@ export const updateGroupMutation = gql`
description: $description
actionRadius: $actionRadius
categoryIds: $categoryIds
locationName: $locationName
) {
id
name
slug
avatar
createdAt
updatedAt
disabled
@ -72,6 +85,14 @@ export const updateGroupMutation = gql`
description
groupType
actionRadius
# categories {
# id
# slug
# name
# icon
# }
# avatar
# locationName
myRole
}
}

View File

@ -8,7 +8,6 @@ export default {
return resolve(root, args, context, info)
},
UpdateGroup: async (resolve, root, args, context, info) => {
console.log('excerptMiddleware - UpdateGroup !!!')
args.descriptionExcerpt = trunc(args.description, DESCRIPTION_EXCERPT_HTML_LENGTH).html
return resolve(root, args, context, info)
},

View File

@ -55,18 +55,18 @@ const isMySocialMedia = rule({
const isAllowedToChangeGroupSettings = rule({
cache: 'no_cache',
})(async (_parent, args, { user, driver }) => {
console.log('isAllowedToChangeGroupSettings !!!')
// Wolle: console.log('isAllowedToChangeGroupSettings !!!')
if (!(user && user.id)) return false
const ownerId = user.id
const { id: groupId } = args
console.log('ownerId: ', ownerId)
console.log('groupId: ', groupId)
// Wolle: console.log('ownerId: ', ownerId)
// Wolle: console.log('groupId: ', groupId)
const session = driver.session()
const readTxPromise = session.readTransaction(async (transaction) => {
const transactionResponse = await transaction.run(
`
MATCH (owner:User {id: $ownerId})-[adminMembership:MEMBER_OF]->(group:Group {id: $groupId})
RETURN group {.*}, owner {.*, myRoleInGroup: adminMembership.role}
MATCH (owner:User {id: $ownerId})-[membership:MEMBER_OF]->(group:Group {id: $groupId})
RETURN group {.*}, owner {.*, myRoleInGroup: membership.role}
`,
{ groupId, ownerId },
)
@ -77,6 +77,8 @@ const isAllowedToChangeGroupSettings = rule({
})
try {
const { owner, group } = await readTxPromise
// Wolle: console.log('owner: ', owner)
// Wolle: console.log('group: ', group)
return !!group && !!owner && ['owner'].includes(owner.myRoleInGroup)
} catch (error) {
throw new Error(error)

View File

@ -31,7 +31,6 @@ export default {
return resolve(root, args, context, info)
},
UpdateGroup: async (resolve, root, args, context, info) => {
console.log('sluggifyMiddleware - UpdateGroup !!!')
args.slug = args.slug || (await uniqueSlug(args.name, isUniqueFor(context, 'Group')))
return resolve(root, args, context, info)
},

View File

@ -2,7 +2,6 @@ import { getNeode, getDriver } from '../db/neo4j'
import createServer from '../server'
import { createTestClient } from 'apollo-server-testing'
import Factory, { cleanDatabase } from '../db/factories'
import { sleep } from '../helpers/jest.js'
import { createGroupMutation, updateGroupMutation } from '../db/graphql/groups'
import { createPostMutation } from '../db/graphql/posts'
import { signupVerificationMutation } from '../db/graphql/authentications'
@ -191,35 +190,32 @@ describe('slugifyMiddleware', () => {
})
describe('UpdateGroup', () => {
let createGroupResult
beforeEach(async () => {
variables = {
...variables,
name: 'Pre-Existing Group',
slug: 'pre-existing-group',
about: 'Some about',
description: 'Some description' + descriptionAdditional100,
groupType: 'closed',
actionRadius: 'national',
categoryIds,
}
console.log('createGroupMutation: ', createGroupMutation)
await mutate({
createGroupResult = await mutate({
mutation: createGroupMutation,
variables,
variables: {
name: 'The Best Group',
slug: 'the-best-group',
about: 'Some about',
description: 'Some description' + descriptionAdditional100,
groupType: 'closed',
actionRadius: 'national',
categoryIds,
},
})
// Wolle: console.log('sleep !!!')
// await sleep(4 * 1000)
})
describe('if group exists', () => {
describe('if new slug not(!) exists', () => {
it.only('has the new slug', async () => {
console.log('updateGroupMutation: ', updateGroupMutation)
it('has the new slug', async () => {
// Wolle: console.log('createGroupResult: ', createGroupResult)
await expect(
mutate({
mutation: updateGroupMutation,
variables: {
...variables,
id: createGroupResult.data.CreateGroup.id,
slug: 'my-best-group',
},
}),
@ -239,16 +235,26 @@ describe('slugifyMiddleware', () => {
})
})
describe('if new slug exists', () => {
describe('if new slug exists in another group', () => {
it('rejects UpdateGroup', async (done) => {
await mutate({
mutation: createGroupMutation,
variables: {
name: 'Pre-Existing Group',
slug: 'pre-existing-group',
about: 'Some about',
description: 'Some description' + descriptionAdditional100,
groupType: 'closed',
actionRadius: 'national',
categoryIds,
},
})
try {
await expect(
mutate({
mutation: updateGroupMutation,
variables: {
...variables,
name: 'Pre-Existing Group',
about: 'As an about',
id: createGroupResult.data.CreateGroup.id,
slug: 'pre-existing-group',
},
}),

View File

@ -132,21 +132,22 @@ export default {
}
},
UpdateGroup: async (_parent, params, context, _resolveInfo) => {
console.log('UpdateGroup !!!')
// Wolle: console.log('UpdateGroup !!!')
const { categoryIds } = params
const { id: groupId } = params
console.log('categoryIds: ', categoryIds)
console.log('groupId: ', groupId)
// Wolle: console.log('categoryIds: ', categoryIds)
// Wolle: console.log('groupId: ', groupId)
delete params.categoryIds
if (CONFIG.CATEGORIES_ACTIVE && (!categoryIds || categoryIds.length < CATEGORIES_MIN)) {
throw new UserInputError('Too view categories!')
}
if (CONFIG.CATEGORIES_ACTIVE && categoryIds && categoryIds.length > CATEGORIES_MAX) {
throw new UserInputError('Too many categories!')
if (CONFIG.CATEGORIES_ACTIVE && categoryIds) {
if (categoryIds.length < CATEGORIES_MIN) {
throw new UserInputError('Too view categories!')
}
if (categoryIds.length > CATEGORIES_MAX) {
throw new UserInputError('Too many categories!')
}
}
if (
params.description === undefined ||
params.description === null ||
params.description &&
removeHtmlTags(params.description).length < DESCRIPTION_WITHOUT_HTML_LENGTH_MIN
) {
throw new UserInputError('Description too short!')
@ -164,7 +165,7 @@ export default {
MATCH (group:Group {id: $groupId})-[previousRelations:CATEGORIZED]->(category:Category)
DELETE previousRelations
RETURN group, category
`
`
await session.writeTransaction((transaction) => {
return transaction.run(cypherDeletePreviousRelations, { groupId })
})
@ -173,11 +174,13 @@ export default {
MATCH (category:Category {id: categoryId})
MERGE (group)-[:CATEGORIZED]->(category)
WITH group
OPTIONAL MATCH (:User {id: $userId})-[membership:MEMBER_OF]->(group)
WITH group, membership # Wolle: is not needed in my eyes
`
}
updateGroupCypher += `RETURN group {.*, myRole: membership.role}`
updateGroupCypher += `
OPTIONAL MATCH (:User {id: $userId})-[membership:MEMBER_OF]->(group)
RETURN group {.*, myRole: membership.role}
`
// Wolle: console.log('updateGroupCypher: ', updateGroupCypher)
const transactionResponse = await transaction.run(updateGroupCypher, {
groupId,
userId: context.user.id,

View File

@ -106,10 +106,12 @@ type Mutation {
id: ID!
name: String
slug: String
avatar: ImageInput
locationName: String
about: String
description: String
actionRadius: GroupActionRadius
categoryIds: [ID]
avatar: ImageInput
locationName: String
): Group
# DeleteGroup(id: ID!): Group