Skip to content

Commit

Permalink
successful post/get of users
Browse files Browse the repository at this point in the history
ref #10-splitter-adapter
  • Loading branch information
maxgrossman committed Jun 12, 2018
1 parent 1cbaa17 commit 61e2059
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 12 deletions.
25 changes: 25 additions & 0 deletions handlers/user/get.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions handlers/user/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

module.exports = {
get: require('./get'),
post: require('./post')
}
11 changes: 6 additions & 5 deletions handlers/user/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions routes/user/get.js
Original file line number Diff line number Diff line change
@@ -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'] }) }
}
}
}
1 change: 1 addition & 0 deletions routes/user/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

module.exports = {
get: require('./get'),
post: require('./post')
}
1 change: 0 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down
53 changes: 49 additions & 4 deletions test/user/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@
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
];

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),
Expand All @@ -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),
Expand All @@ -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);
}
})
})
4 changes: 2 additions & 2 deletions testData/users.js
Original file line number Diff line number Diff line change
@@ -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' }
]

0 comments on commit 61e2059

Please sign in to comment.