diff --git a/backend/src/constants/categories.js b/backend/src/constants/categories.js index 37cac8151..64ceb9021 100644 --- a/backend/src/constants/categories.js +++ b/backend/src/constants/categories.js @@ -1,5 +1,3 @@ -// this file is duplicated in `backend/src/config/metadata.js` and `webapp/constants/metadata.js` -export default { - CATEGORIES_MIN: 1, - CATEGORIES_MAX: 3, -} +// this file is duplicated in `backend/src/constants/metadata.js` and `webapp/constants/metadata.js` +export const CATEGORIES_MIN = 1 +export const CATEGORIES_MAX = 3 diff --git a/backend/src/constants/groups.js b/backend/src/constants/groups.js new file mode 100644 index 000000000..b4a6063f1 --- /dev/null +++ b/backend/src/constants/groups.js @@ -0,0 +1,2 @@ +// this file is duplicated in `backend/src/constants/group.js` and `webapp/constants/group.js` +export const DESCRIPTION_WITHOUT_HTML_LENGTH_MIN = 100 // with removed HTML tags diff --git a/backend/src/middleware/helpers/cleanHtml.js b/backend/src/middleware/helpers/cleanHtml.js index 72976b43c..ac71f6bdc 100644 --- a/backend/src/middleware/helpers/cleanHtml.js +++ b/backend/src/middleware/helpers/cleanHtml.js @@ -1,6 +1,13 @@ import sanitizeHtml from 'sanitize-html' import linkifyHtml from 'linkifyjs/html' +export const removeHtmlTags = (input) => { + return sanitizeHtml(input, { + allowedTags: [], + allowedAttributes: {}, + }) +} + const standardSanitizeHtmlOptions = { allowedTags: [ 'img', diff --git a/backend/src/middleware/languages/languages.js b/backend/src/middleware/languages/languages.js index 3cf760f31..087252975 100644 --- a/backend/src/middleware/languages/languages.js +++ b/backend/src/middleware/languages/languages.js @@ -1,12 +1,5 @@ import LanguageDetect from 'languagedetect' -import sanitizeHtml from 'sanitize-html' - -const removeHtmlTags = (input) => { - return sanitizeHtml(input, { - allowedTags: [], - allowedAttributes: {}, - }) -} +import { removeHtmlTags } from '../helpers/cleanHtml.js' const setPostLanguage = (text) => { const lngDetector = new LanguageDetect() diff --git a/backend/src/schema/resolvers/groups.js b/backend/src/schema/resolvers/groups.js index a958e990e..0e07b7542 100644 --- a/backend/src/schema/resolvers/groups.js +++ b/backend/src/schema/resolvers/groups.js @@ -3,7 +3,9 @@ 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' +import { CATEGORIES_MIN, CATEGORIES_MAX } from '../../constants/categories' +import { DESCRIPTION_WITHOUT_HTML_LENGTH_MIN } from '../../constants/groups' +import { removeHtmlTags } from '../../middleware/helpers/cleanHtml.js' // Wolle: import { mergeImage, deleteImage } from './images/images' import Resolver from './helpers/Resolver' // Wolle: import { filterForMutedUsers } from './helpers/filterForMutedUsers' @@ -70,12 +72,19 @@ export default { CreateGroup: async (_parent, params, context, _resolveInfo) => { const { categoryIds } = params delete params.categoryIds - if (!categoryIds || categoryIds.length < categories.CATEGORIES_MIN) { + if (!categoryIds || categoryIds.length < CATEGORIES_MIN) { throw new UserInputError('To Less Categories!') } - if (categoryIds && categoryIds.length > categories.CATEGORIES_MAX) { + if (categoryIds && categoryIds.length > CATEGORIES_MAX) { throw new UserInputError('To Many Categories!') } + if ( + params.description === undefined || + params.description === null || + removeHtmlTags(params.description).length < DESCRIPTION_WITHOUT_HTML_LENGTH_MIN + ) { + throw new UserInputError('To Short Description!') + } 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 8f20c4fa7..ad9b6d68e 100644 --- a/backend/src/schema/resolvers/groups.spec.js +++ b/backend/src/schema/resolvers/groups.spec.js @@ -13,6 +13,8 @@ let authenticatedUser let user const categoryIds = ['cat9', 'cat4', 'cat15'] +const descriptionAddition100 = + ' 123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789' let variables = {} beforeAll(async () => { @@ -116,7 +118,7 @@ describe('Group', () => { id: 'others-group', name: 'Uninteresting Group', about: 'We will change nothing!', - description: 'We love it like it is!?', + description: 'We love it like it is!?' + descriptionAddition100, groupType: 'closed', actionRadius: 'international', categoryIds, @@ -129,7 +131,7 @@ describe('Group', () => { id: 'my-group', name: 'The Best Group', about: 'We will change the world!', - description: 'Some description', + description: 'Some description' + descriptionAddition100, groupType: 'public', actionRadius: 'regional', categoryIds, @@ -363,7 +365,7 @@ describe('CreateGroup', () => { name: 'The Best Group', slug: 'the-group', about: 'We will change the world!', - description: 'Some description', + description: 'Some description' + descriptionAddition100, groupType: 'public', actionRadius: 'regional', categoryIds, @@ -423,6 +425,25 @@ describe('CreateGroup', () => { ) }) + describe('description', () => { + describe('length without HTML', () => { + describe('less then 100 chars', () => { + it('throws error: "To Less Categories!"', async () => { + const { errors } = await mutate({ + mutation: createGroupMutation, + variables: { + ...variables, + description: + '0123456789' + + '0123456789', + }, + }) + expect(errors[0]).toHaveProperty('message', 'To Short Description!') + }) + }) + }) + }) + describe('categories', () => { describe('not even one', () => { it('throws error: "To Less Categories!"', async () => { diff --git a/webapp/constants/categories.js b/webapp/constants/categories.js new file mode 100644 index 000000000..64ceb9021 --- /dev/null +++ b/webapp/constants/categories.js @@ -0,0 +1,3 @@ +// this file is duplicated in `backend/src/constants/metadata.js` and `webapp/constants/metadata.js` +export const CATEGORIES_MIN = 1 +export const CATEGORIES_MAX = 3 diff --git a/webapp/constants/groups.js b/webapp/constants/groups.js new file mode 100644 index 000000000..b4a6063f1 --- /dev/null +++ b/webapp/constants/groups.js @@ -0,0 +1,2 @@ +// this file is duplicated in `backend/src/constants/group.js` and `webapp/constants/group.js` +export const DESCRIPTION_WITHOUT_HTML_LENGTH_MIN = 100 // with removed HTML tags