From 0078b743fe5ae3bcd401c628e53e14ad7a191eb3 Mon Sep 17 00:00:00 2001 From: Matt Rider Date: Wed, 24 Apr 2019 09:11:33 -0300 Subject: [PATCH 1/2] Validate comments length, presence/test - Co-authored-by: Wolfgang Huss - Co-authored-by: Mike Aono --- backend/src/resolvers/comments.js | 7 +++++++ backend/src/resolvers/comments.spec.js | 22 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/backend/src/resolvers/comments.js b/backend/src/resolvers/comments.js index b3350ec8e..60ecbcc8e 100644 --- a/backend/src/resolvers/comments.js +++ b/backend/src/resolvers/comments.js @@ -1,5 +1,7 @@ import { neo4jgraphql } from 'neo4j-graphql-js' +import { UserInputError } from 'apollo-server' +const COMMENT_MIN_LENGTH = 3 export default { Query: { CommentByPost: async (object, params, context, resolveInfo) => { @@ -23,6 +25,11 @@ export default { }, Mutation: { CreateComment: async (object, params, context, resolveInfo) => { + const content = params.content.replace(/<(?:.|\n)*?>/gm, '').trim() + + if (!params.content || content.length < COMMENT_MIN_LENGTH) { + throw new UserInputError(`Comment must be at least ${COMMENT_MIN_LENGTH} characters long!`) + } const { postId } = params delete params.postId const comment = await neo4jgraphql(object, params, context, resolveInfo, false) diff --git a/backend/src/resolvers/comments.spec.js b/backend/src/resolvers/comments.spec.js index 9918038a7..eb4e39633 100644 --- a/backend/src/resolvers/comments.spec.js +++ b/backend/src/resolvers/comments.spec.js @@ -44,7 +44,7 @@ describe('CreateComment', () => { client = new GraphQLClient(host, { headers }) }) - it('creates a post', async () => { + it('creates a comment', async () => { variables = { postId: 'p1', content: 'I\'m authorised to comment' @@ -57,5 +57,25 @@ describe('CreateComment', () => { await expect(client.request(mutation, variables)).resolves.toMatchObject(expected) }) + + it('throw an error if an empty string is sent as content', async () => { + variables = { + postId: 'p1', + content: '

' + } + + await expect(client.request(mutation, variables)) + .rejects.toThrow('Comment must be at least 3 characters long!') + }) + + it('throws an error if a comment is less than 3 characters', async () => { + variables = { + postId: 'p1', + content: '

ab

' + } + + await expect(client.request(mutation, variables)) + .rejects.toThrow('Comment must be at least 3 characters long!') + }) }) }) From 546e2c99adb1c96ca4128aa0e929ff385ebdf84c Mon Sep 17 00:00:00 2001 From: Matt Rider Date: Wed, 24 Apr 2019 09:15:41 -0300 Subject: [PATCH 2/2] Localise text, remove frontend validations - validations are not working for the editor, they only work for our ds-input --- webapp/components/Editor/index.vue | 2 +- webapp/locales/de.json | 3 +++ webapp/locales/en.json | 3 +++ webapp/pages/post/_id/_slug/index.vue | 5 ----- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/webapp/components/Editor/index.vue b/webapp/components/Editor/index.vue index 5636c3714..57998fcbc 100644 --- a/webapp/components/Editor/index.vue +++ b/webapp/components/Editor/index.vue @@ -224,7 +224,7 @@ export default { new ListItem(), new Placeholder({ emptyNodeClass: 'is-empty', - emptyNodeText: 'Schreib etwas inspirerendes…' + emptyNodeText: this.$t('editor.placeholder') }), new History(), new Mention({ diff --git a/webapp/locales/de.json b/webapp/locales/de.json index 04a14f2a7..3fac0310d 100644 --- a/webapp/locales/de.json +++ b/webapp/locales/de.json @@ -8,6 +8,9 @@ "moreInfo": "Was ist Human Connection?", "hello": "Hallo" }, + "editor": { + "placeholder": "Schreib etwas inspirerendes…" + }, "profile": { "name": "Mein Profil", "memberSince": "Mitglied seit", diff --git a/webapp/locales/en.json b/webapp/locales/en.json index c74cbed52..83e8c4eda 100644 --- a/webapp/locales/en.json +++ b/webapp/locales/en.json @@ -8,6 +8,9 @@ "moreInfo": "What is Human Connection?", "hello": "Hello" }, + "editor": { + "placeholder": "Leave your inspirational thoughts…" + }, "profile": { "name": "My Profile", "memberSince": "Member since", diff --git a/webapp/pages/post/_id/_slug/index.vue b/webapp/pages/post/_id/_slug/index.vue index 0ecd98c4e..29ad646f1 100644 --- a/webapp/pages/post/_id/_slug/index.vue +++ b/webapp/pages/post/_id/_slug/index.vue @@ -115,7 +115,6 @@