diff --git a/backend/src/middleware/index.js b/backend/src/middleware/index.js index 4f725ef72..17b9d63fb 100644 --- a/backend/src/middleware/index.js +++ b/backend/src/middleware/index.js @@ -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, diff --git a/backend/src/middleware/userMiddleware.js b/backend/src/middleware/userMiddleware.js index 4789b4cbd..b3fc1bf2c 100644 --- a/backend/src/middleware/userMiddleware.js +++ b/backend/src/middleware/userMiddleware.js @@ -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 diff --git a/backend/src/middleware/validUrlMiddleware.js b/backend/src/middleware/validUrlMiddleware.js deleted file mode 100644 index 37f06199c..000000000 --- a/backend/src/middleware/validUrlMiddleware.js +++ /dev/null @@ -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 - } - } -} diff --git a/backend/src/middleware/validation/index.js b/backend/src/middleware/validation/index.js new file mode 100644 index 000000000..de9be72e9 --- /dev/null +++ b/backend/src/middleware/validation/index.js @@ -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 + } +} diff --git a/backend/src/middleware/userMiddleware.spec.js b/backend/src/resolvers/users.spec.js similarity index 71% rename from backend/src/middleware/userMiddleware.spec.js rename to backend/src/resolvers/users.spec.js index 9aa8f96c1..48e4741d7 100644 --- a/backend/src/middleware/userMiddleware.spec.js +++ b/backend/src/resolvers/users.spec.js @@ -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))