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

View File

@ -8,7 +8,6 @@ export default {
return resolve(root, args, context, info) return resolve(root, args, context, info)
}, },
UpdateGroup: async (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 args.descriptionExcerpt = trunc(args.description, DESCRIPTION_EXCERPT_HTML_LENGTH).html
return resolve(root, args, context, info) return resolve(root, args, context, info)
}, },

View File

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

View File

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

View File

@ -2,7 +2,6 @@ import { getNeode, getDriver } from '../db/neo4j'
import createServer from '../server' import createServer from '../server'
import { createTestClient } from 'apollo-server-testing' import { createTestClient } from 'apollo-server-testing'
import Factory, { cleanDatabase } from '../db/factories' import Factory, { cleanDatabase } from '../db/factories'
import { sleep } from '../helpers/jest.js'
import { createGroupMutation, updateGroupMutation } from '../db/graphql/groups' import { createGroupMutation, updateGroupMutation } from '../db/graphql/groups'
import { createPostMutation } from '../db/graphql/posts' import { createPostMutation } from '../db/graphql/posts'
import { signupVerificationMutation } from '../db/graphql/authentications' import { signupVerificationMutation } from '../db/graphql/authentications'
@ -191,35 +190,32 @@ describe('slugifyMiddleware', () => {
}) })
describe('UpdateGroup', () => { describe('UpdateGroup', () => {
let createGroupResult
beforeEach(async () => { beforeEach(async () => {
variables = { createGroupResult = await mutate({
...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({
mutation: createGroupMutation, 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 group exists', () => {
describe('if new slug not(!) exists', () => { describe('if new slug not(!) exists', () => {
it.only('has the new slug', async () => { it('has the new slug', async () => {
console.log('updateGroupMutation: ', updateGroupMutation) // Wolle: console.log('createGroupResult: ', createGroupResult)
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation,
variables: { variables: {
...variables, id: createGroupResult.data.CreateGroup.id,
slug: 'my-best-group', 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) => { 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 { try {
await expect( await expect(
mutate({ mutate({
mutation: updateGroupMutation, mutation: updateGroupMutation,
variables: { variables: {
...variables, id: createGroupResult.data.CreateGroup.id,
name: 'Pre-Existing Group',
about: 'As an about',
slug: 'pre-existing-group', slug: 'pre-existing-group',
}, },
}), }),

View File

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

View File

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