Merge pull request #596 from Human-Connection/refactoring_validation_middleware

Refactoring validation middleware
This commit is contained in:
Ulf Gebhardt 2019-05-14 12:02:59 +02:00 committed by GitHub
commit eb995d6ad4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 39 deletions

View File

@ -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,

View File

@ -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

View File

@ -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
}
}
}

View 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
}
}

View File

@ -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))