From 61344fc96bb920e3fe04da97a3ee67dbcc643b84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Tue, 9 Aug 2022 08:50:25 +0200 Subject: [PATCH] Implement errors for to less or to many categories and test it --- backend/src/constants/categories.js | 5 +++++ backend/src/schema/resolvers/groups.js | 7 ++++++ backend/src/schema/resolvers/groups.spec.js | 24 ++++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 backend/src/constants/categories.js diff --git a/backend/src/constants/categories.js b/backend/src/constants/categories.js new file mode 100644 index 000000000..37cac8151 --- /dev/null +++ b/backend/src/constants/categories.js @@ -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, +} diff --git a/backend/src/schema/resolvers/groups.js b/backend/src/schema/resolvers/groups.js index be07fecc6..a958e990e 100644 --- a/backend/src/schema/resolvers/groups.js +++ b/backend/src/schema/resolvers/groups.js @@ -3,6 +3,7 @@ import { v4 as uuid } from 'uuid' // Wolle: import { isEmpty } from 'lodash' import { UserInputError } from 'apollo-server' import CONFIG from '../../config' +import categories from '../../constants/categories' // Wolle: import { mergeImage, deleteImage } from './images/images' import Resolver from './helpers/Resolver' // Wolle: import { filterForMutedUsers } from './helpers/filterForMutedUsers' @@ -69,6 +70,12 @@ export default { CreateGroup: async (_parent, params, context, _resolveInfo) => { const { categoryIds } = params 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() const session = context.driver.session() const writeTxResultPromise = session.writeTransaction(async (transaction) => { diff --git a/backend/src/schema/resolvers/groups.spec.js b/backend/src/schema/resolvers/groups.spec.js index dd5a48568..8f20c4fa7 100644 --- a/backend/src/schema/resolvers/groups.spec.js +++ b/backend/src/schema/resolvers/groups.spec.js @@ -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 } } } await expect(mutate({ mutation: createGroupMutation, variables })).resolves.toMatchObject( 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!') + }) + }) + }) }) })