mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Merge pull request #596 from Human-Connection/refactoring_validation_middleware
Refactoring validation middleware
This commit is contained in:
commit
eb995d6ad4
@ -10,14 +10,14 @@ import permissionsMiddleware from './permissionsMiddleware'
|
||||
import userMiddleware from './userMiddleware'
|
||||
import includedFieldsMiddleware from './includedFieldsMiddleware'
|
||||
import orderByMiddleware from './orderByMiddleware'
|
||||
import validUrlMiddleware from './validUrlMiddleware'
|
||||
import validationMiddleware from './validation'
|
||||
import notificationsMiddleware from './notifications'
|
||||
|
||||
export default schema => {
|
||||
let middleware = [
|
||||
passwordMiddleware,
|
||||
dateTimeMiddleware,
|
||||
validUrlMiddleware,
|
||||
validationMiddleware,
|
||||
sluggifyMiddleware,
|
||||
excerptMiddleware,
|
||||
notificationsMiddleware,
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import dotenv from 'dotenv'
|
||||
import { UserInputError } from 'apollo-server'
|
||||
|
||||
import createOrUpdateLocations from './nodes/locations'
|
||||
|
||||
@ -13,10 +12,6 @@ export default {
|
||||
return result
|
||||
},
|
||||
UpdateUser: async (resolve, root, args, context, info) => {
|
||||
const USERNAME_MIN_LENGTH = 3 // TODO move to the correct place
|
||||
if (!args.name || args.name.length < USERNAME_MIN_LENGTH) {
|
||||
throw new UserInputError(`Username must be at least ${USERNAME_MIN_LENGTH} characters long!`)
|
||||
}
|
||||
const result = await resolve(root, args, context, info)
|
||||
await createOrUpdateLocations(args.id, args.locationName, context.driver)
|
||||
return result
|
||||
|
||||
@ -1,18 +0,0 @@
|
||||
const validURL = str => {
|
||||
const isValid = str.match(/^(?:https?:\/\/)(?:[^@\n])?(?:www\.)?([^:/\n?]+)/g)
|
||||
return !!isValid
|
||||
}
|
||||
|
||||
export default {
|
||||
Mutation: {
|
||||
CreateSocialMedia: async (resolve, root, args, context, info) => {
|
||||
let socialMedia
|
||||
if (validURL(args.url)) {
|
||||
socialMedia = await resolve(root, args, context, info)
|
||||
} else {
|
||||
throw Error('Input is not a URL')
|
||||
}
|
||||
return socialMedia
|
||||
}
|
||||
}
|
||||
}
|
||||
31
backend/src/middleware/validation/index.js
Normal file
31
backend/src/middleware/validation/index.js
Normal file
@ -0,0 +1,31 @@
|
||||
import { UserInputError } from 'apollo-server'
|
||||
|
||||
const USERNAME_MIN_LENGTH = 3
|
||||
|
||||
const validateUsername = async (resolve, root, args, context, info) => {
|
||||
if (args.name && args.name.length >= USERNAME_MIN_LENGTH) {
|
||||
/* eslint-disable-next-line no-return-await */
|
||||
return await resolve(root, args, context, info)
|
||||
} else {
|
||||
throw new UserInputError(`Username must be at least ${USERNAME_MIN_LENGTH} characters long!`)
|
||||
}
|
||||
}
|
||||
|
||||
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')
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
Mutation: {
|
||||
CreateUser: validateUsername,
|
||||
UpdateUser: validateUsername,
|
||||
CreateSocialMedia: validateUrl
|
||||
}
|
||||
}
|
||||
@ -5,15 +5,15 @@ import Factory from '../seed/factories'
|
||||
const factory = Factory()
|
||||
let client
|
||||
|
||||
afterAll(async () => {
|
||||
afterEach(async () => {
|
||||
await factory.cleanDatabase()
|
||||
})
|
||||
|
||||
describe('userMiddleware', () => {
|
||||
describe('create User', () => {
|
||||
describe('users', () => {
|
||||
describe('CreateUser', () => {
|
||||
const mutation = `
|
||||
mutation($id: ID, $password: String!, $email: String!) {
|
||||
CreateUser(id: $id, password: $password, email: $email) {
|
||||
mutation($name: String, $password: String!, $email: String!) {
|
||||
CreateUser(name: $name, password: $password, email: $email) {
|
||||
id
|
||||
}
|
||||
}
|
||||
@ -22,6 +22,7 @@ describe('userMiddleware', () => {
|
||||
|
||||
it('with password and email', async () => {
|
||||
const variables = {
|
||||
name: 'John Doe',
|
||||
password: '123',
|
||||
email: '123@123.de'
|
||||
}
|
||||
@ -35,34 +36,39 @@ describe('userMiddleware', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('update User', () => {
|
||||
describe('UpdateUser', () => {
|
||||
beforeEach(async () => {
|
||||
await factory.create('User', { id: 'u47', name: 'John Doe' })
|
||||
})
|
||||
|
||||
const mutation = `
|
||||
mutation($id: ID!, $name: String) {
|
||||
UpdateUser(id: $id, name: $name) {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
`
|
||||
client = new GraphQLClient(host)
|
||||
|
||||
// TODO why is this failing - it returns { UpdateUser: null } - that should not be
|
||||
/* it('name within specifications', async () => {
|
||||
it('name within specifications', async () => {
|
||||
const variables = {
|
||||
id: 'u1',
|
||||
name: 'Peter Lustig'
|
||||
id: 'u47',
|
||||
name: 'James Doe'
|
||||
}
|
||||
const expected = {
|
||||
UpdateUser: {
|
||||
name: 'Peter Lustig'
|
||||
id: 'u47',
|
||||
name: 'James Doe'
|
||||
}
|
||||
}
|
||||
await expect(client.request(mutation, variables))
|
||||
.resolves.toEqual(expected)
|
||||
}) */
|
||||
})
|
||||
|
||||
it('with no name', async () => {
|
||||
const variables = {
|
||||
id: 'u1'
|
||||
id: 'u47'
|
||||
}
|
||||
const expected = 'Username must be at least 3 characters long!'
|
||||
await expect(client.request(mutation, variables))
|
||||
@ -71,7 +77,8 @@ describe('userMiddleware', () => {
|
||||
|
||||
it('with too short name', async () => {
|
||||
const variables = {
|
||||
id: 'u1'
|
||||
id: 'u47',
|
||||
name: ' '
|
||||
}
|
||||
const expected = 'Username must be at least 3 characters long!'
|
||||
await expect(client.request(mutation, variables))
|
||||
Loading…
x
Reference in New Issue
Block a user