Implement errors for to less or to many categories and test it

This commit is contained in:
Wolfgang Huß 2022-08-09 08:50:25 +02:00
parent 7847d6912c
commit 61344fc96b
3 changed files with 35 additions and 1 deletions

View File

@ -0,0 +1,5 @@
// this file is duplicated in `backend/src/config/metadata.js` and `webapp/constants/metadata.js`
export default {
CATEGORIES_MIN: 1,
CATEGORIES_MAX: 3,
}

View File

@ -3,6 +3,7 @@ import { v4 as uuid } from 'uuid'
// Wolle: import { isEmpty } from 'lodash' // Wolle: import { isEmpty } from 'lodash'
import { UserInputError } from 'apollo-server' import { UserInputError } from 'apollo-server'
import CONFIG from '../../config' import CONFIG from '../../config'
import categories from '../../constants/categories'
// Wolle: import { mergeImage, deleteImage } from './images/images' // Wolle: import { mergeImage, deleteImage } from './images/images'
import Resolver from './helpers/Resolver' import Resolver from './helpers/Resolver'
// Wolle: import { filterForMutedUsers } from './helpers/filterForMutedUsers' // Wolle: import { filterForMutedUsers } from './helpers/filterForMutedUsers'
@ -69,6 +70,12 @@ export default {
CreateGroup: async (_parent, params, context, _resolveInfo) => { CreateGroup: async (_parent, params, context, _resolveInfo) => {
const { categoryIds } = params const { categoryIds } = params
delete params.categoryIds delete params.categoryIds
if (!categoryIds || categoryIds.length < categories.CATEGORIES_MIN) {
throw new UserInputError('To Less Categories!')
}
if (categoryIds && categoryIds.length > categories.CATEGORIES_MAX) {
throw new UserInputError('To Many Categories!')
}
params.id = params.id || uuid() params.id = params.id || uuid()
const session = context.driver.session() const session = context.driver.session()
const writeTxResultPromise = session.writeTransaction(async (transaction) => { const writeTxResultPromise = session.writeTransaction(async (transaction) => {

View File

@ -416,12 +416,34 @@ describe('CreateGroup', () => {
) )
}) })
it('`disabled` and `deleted` default to `false`', async () => { it('"disabled" and "deleted" default to "false"', async () => {
const expected = { data: { CreateGroup: { disabled: false, deleted: false } } } const expected = { data: { CreateGroup: { disabled: false, deleted: false } } }
await expect(mutate({ mutation: createGroupMutation, variables })).resolves.toMatchObject( await expect(mutate({ mutation: createGroupMutation, variables })).resolves.toMatchObject(
expected, expected,
) )
}) })
describe('categories', () => {
describe('not even one', () => {
it('throws error: "To Less Categories!"', async () => {
const { errors } = await mutate({
mutation: createGroupMutation,
variables: { ...variables, categoryIds: null },
})
expect(errors[0]).toHaveProperty('message', 'To Less Categories!')
})
})
describe('four', () => {
it('throws error: "To Many Categories!"', async () => {
const { errors } = await mutate({
mutation: createGroupMutation,
variables: { ...variables, categoryIds: ['cat9', 'cat4', 'cat15', 'cat27'] },
})
expect(errors[0]).toHaveProperty('message', 'To Many Categories!')
})
})
})
}) })
}) })