diff --git a/handlers/user/get.js b/handlers/user/get.js new file mode 100644 index 0000000..b29a9ec --- /dev/null +++ b/handlers/user/get.js @@ -0,0 +1,25 @@ +'use strict'; + +const Boom = require('boom'); +const db = require('../../connection'); +const uuidv4 = require('uuid/v4'); + +module.exports = async (r, h) => { + try { + const id = r.params.id, + ids = (await db('Users').select('id')).map(user => user.id); + + if (ids.indexOf(id) === -1) { + return Boom.badRequest('User not in database') + } + + + const user = (await db('Users').select('name').where({id: id}))[0]; + + return h.response({ upload: 'successful', name: user.name }).code(200) + + } catch (error) { + console.error(error); + throw error; + } +} \ No newline at end of file diff --git a/handlers/user/index.js b/handlers/user/index.js index 6e868e6..d3ae3d1 100644 --- a/handlers/user/index.js +++ b/handlers/user/index.js @@ -1,5 +1,6 @@ 'use strict'; module.exports = { + get: require('./get'), post: require('./post') } \ No newline at end of file diff --git a/handlers/user/post.js b/handlers/user/post.js index 752d8f1..f7df10e 100644 --- a/handlers/user/post.js +++ b/handlers/user/post.js @@ -6,17 +6,18 @@ const uuidv4 = require('uuid/v4'); module.exports = async (r, h) => { try { - const user = r.payload, - users = await db('Users').select('name'); + const userName = r.payload.name, + users = (await db('Users').select('name')).map(user => user.name); - if (users.indexOf(user) === -1) { - Boom.badRequest('user already exists'); + if (users.indexOf(userName) > -1) { + return Boom.badRequest('user already exists'); } - await db('Users').insert({id: 3, name: user.name}); + await db('Users').insert({id: uuidv4(), name: userName}); return h.response({ upload: 'successful' }).code(200) } catch (error) { + console.error(error); throw error; } } \ No newline at end of file diff --git a/routes/user/get.js b/routes/user/get.js new file mode 100644 index 0000000..205eb75 --- /dev/null +++ b/routes/user/get.js @@ -0,0 +1,14 @@ +'use strict'; + +const Joi = require('joi'); + +module.exports = { + method: 'GET', + path: '/user/{id}', + config: { + handler: require('../../handlers/user').get, + validate: { + params: { id: Joi.string().guid({ version: ['uuidv4'] }) } + } + } +} diff --git a/routes/user/index.js b/routes/user/index.js index 6e868e6..d3ae3d1 100644 --- a/routes/user/index.js +++ b/routes/user/index.js @@ -1,5 +1,6 @@ 'use strict'; module.exports = { + get: require('./get'), post: require('./post') } \ No newline at end of file diff --git a/test/server.js b/test/server.js index 12a6089..96174cd 100644 --- a/test/server.js +++ b/test/server.js @@ -2,7 +2,6 @@ const Hapi = require('hapi'); const config = require('../config')['development']; -const routes = require('../routes'); const host = config.host; const server = Hapi.server({ port: 3001, host: host }) diff --git a/test/user/handler.js b/test/user/handler.js index 0645043..42d3e36 100644 --- a/test/user/handler.js +++ b/test/user/handler.js @@ -3,11 +3,14 @@ const Joi = require('joi'); const chai = require('chai'); const expect = chai.expect; + +const uuidv4 = require('uuid/v4'); + const server = require('../server'); const mergeDefaults = require('../helpers').mergeDefaults; -const newUserPayload = require('../../testData/payloads').postUser[0]; -const oldUserPayload = require('../../testData/payloads').postUser[1]; +const oldUserPayload = require('../../testData/payloads').postUser; const routes = [ + require('../../routes/user').get, require('../../routes/user').post ]; @@ -15,9 +18,17 @@ before(async () => await server.liftOff(routes)) describe('post', () => { it('replies 200 when successful adding user to db', async () => { try { + // thanks stack exchange + // https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript + let fakeName = '', + letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + + for (let i = 0; i < 5; i++) { + fakeName += letters.charAt(Math.floor(Math.random() * letters.length)); + } const request = mergeDefaults({ method: 'POST', - payload: newUserPayload, + payload: { name: fakeName }, url:'/user' }), r = await server.inject(request), @@ -34,7 +45,7 @@ describe('post', () => { try { const request = mergeDefaults({ method: 'POST', - payload: oldUserPayload, + payload: { name: oldUserPayload.name }, url: '/user' }), r = await server.inject(request), @@ -47,3 +58,37 @@ describe('post', () => { } }) }) +describe('get', () => { + it ('replies 200 and username when given valid uuid', async () => { + try { + const request = mergeDefaults({ + method: 'GET', + url: `/user/${oldUserPayload.id}` + }), + r = await server.inject(request), + statusCode = r.statusCode, + result = r.result; + + expect(result.upload).to.equal('successful'); + expect(result.name).to.equal(oldUserPayload.name); + expect(statusCode).to.equal(200); + + } catch (error) { + console.error(error); + } + }) + it ('replies 400 when given user id invalid', async () => { + try { + const request = mergeDefaults({ + method: 'GET', + url: `/user/${uuidv4()}` + }), + r = await server.inject(request), + statusCode = r.statusCode; + + expect(statusCode).to.equal(400); + } catch (error) { + console.error(error); + } + }) +}) diff --git a/testData/users.js b/testData/users.js index 779b6f2..9e245ca 100644 --- a/testData/users.js +++ b/testData/users.js @@ -1,6 +1,6 @@ 'use strict'; module.exports = [ - {name: 'danbjoseph'}, - {name: 'giblet'} + { id: '6cc99b2f-a00a-45c3-a74b-d532547dd852', name: 'danbjoseph' }, + { id: '8aa16355-edff-4c14-bb2d-fef05780b4be', name: 'giblet' } ] \ No newline at end of file