mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2026-04-27 00:08:45 +00:00
Add backend validation tests to expose non-validation
- remove validation/index.js as it's not being used - move validations to validationMiddleware.js where it validates
This commit is contained in:
parent
3b88c47206
commit
1c53c58f42
@ -1,29 +0,0 @@
|
|||||||
import { UserInputError } from 'apollo-server'
|
|
||||||
|
|
||||||
const validateUrl = async (resolve, root, args, context, info) => {
|
|
||||||
const { url } = args
|
|
||||||
const isValid = url.match(/^(?:https?:\/\/)(?:[^@\n])?(?:www\.)?([^:/\n?]+)/g)
|
|
||||||
if (isValid) {
|
|
||||||
/* eslint-disable-next-line no-return-await */
|
|
||||||
return await resolve(root, args, context, info)
|
|
||||||
} else {
|
|
||||||
throw new UserInputError('Input is not a URL')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const validateUpdateComment = async (resolve, root, args, context, info) => {
|
|
||||||
const COMMENT_MIN_LENGTH = 1
|
|
||||||
const content = args.content.replace(/<(?:.|\n)*?>/gm, '').trim()
|
|
||||||
if (!args.content || content.length < COMMENT_MIN_LENGTH) {
|
|
||||||
throw new UserInputError(`Comment must be at least ${COMMENT_MIN_LENGTH} character long!`)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(root, args, context, info)
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
|
||||||
Mutation: {
|
|
||||||
CreateSocialMedia: validateUrl,
|
|
||||||
UpdateComment: validateUpdateComment,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
@ -45,9 +45,20 @@ const validateCommentCreation = async (resolve, root, args, context, info) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const validateUpdateComment = async (resolve, root, args, context, info) => {
|
||||||
|
const COMMENT_MIN_LENGTH = 1
|
||||||
|
const content = args.content.replace(/<(?:.|\n)*?>/gm, '').trim()
|
||||||
|
if (!args.content || content.length < COMMENT_MIN_LENGTH) {
|
||||||
|
throw new UserInputError(`Comment must be at least ${COMMENT_MIN_LENGTH} character long!`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolve(root, args, context, info)
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
Mutation: {
|
Mutation: {
|
||||||
CreateSocialMedia: validate(socialMediaSchema),
|
CreateSocialMedia: validate(socialMediaSchema),
|
||||||
CreateComment: validateCommentCreation,
|
CreateComment: validateCommentCreation,
|
||||||
|
UpdateComment: validateUpdateComment,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@ -267,6 +267,50 @@ describe('ManageComments', () => {
|
|||||||
client.request(updateCommentMutation, updateCommentVariables),
|
client.request(updateCommentMutation, updateCommentVariables),
|
||||||
).resolves.toEqual(expected)
|
).resolves.toEqual(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('throw an error if an empty string is sent from the editor as content', async () => {
|
||||||
|
updateCommentVariables = {
|
||||||
|
id: 'c456',
|
||||||
|
content: '<p></p>',
|
||||||
|
}
|
||||||
|
|
||||||
|
await expect(client.request(updateCommentMutation, updateCommentVariables)).rejects.toThrow(
|
||||||
|
'Comment must be at least 1 character long!',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws an error if a comment sent from the editor does not contain a single character', async () => {
|
||||||
|
updateCommentVariables = {
|
||||||
|
id: 'c456',
|
||||||
|
content: '<p></p>',
|
||||||
|
}
|
||||||
|
|
||||||
|
await expect(client.request(updateCommentMutation, updateCommentVariables)).rejects.toThrow(
|
||||||
|
'Comment must be at least 1 character long!',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws an error if postId is sent as an empty string', async () => {
|
||||||
|
updateCommentVariables = {
|
||||||
|
id: 'c456',
|
||||||
|
content: '<p></p>',
|
||||||
|
}
|
||||||
|
|
||||||
|
await expect(client.request(updateCommentMutation, updateCommentVariables)).rejects.toThrow(
|
||||||
|
'Comment must be at least 1 character long!',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws an error if content is sent as an string of empty characters', async () => {
|
||||||
|
updateCommentVariables = {
|
||||||
|
id: 'c456',
|
||||||
|
content: '<p></p>',
|
||||||
|
}
|
||||||
|
|
||||||
|
await expect(client.request(updateCommentMutation, updateCommentVariables)).rejects.toThrow(
|
||||||
|
'Comment must be at least 1 character long!',
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user