Skip to content

Commit

Permalink
add user routes/tests and beginning of folder -> images/sequences
Browse files Browse the repository at this point in the history
couple weeks of metro ride work here... both beginning of adaptation from sequence array build by sequence adapter and some tests to add users to the db (needed for relating sequences/images to a user as well as managing account upload to external services)
  • Loading branch information
maxgrossman committed Jun 11, 2018
1 parent 811b6cf commit 1cbaa17
Show file tree
Hide file tree
Showing 21 changed files with 760 additions and 1,007 deletions.
3 changes: 2 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const base = {

const development = {
port: '8080',
mapillary: 'https://a.mapillary.com/v3/'
mapillary: 'https://a.mapillary.com/v3/',
injectDefaults: { simulate: { error: false }}
};

// for now...
Expand Down
9 changes: 6 additions & 3 deletions db/posm-paths.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
-- bring in spatialite --
SELECT load_extension("libspatialite");
SELECT load_extension("mod_spatialite");
SELECT InitSpatialMetaData();

-- make the tables --
CREATE TABLE Users (
id INTEGER PRIMARY KEY,
id UUID PRIMARY KEY,
name TEXT NOT NULL
);

Expand All @@ -28,4 +28,7 @@ CREATE TABLE Images(
--- SpatialLite for image locations ---
SELECT AddGeometryColumn(
'Images', 'loc', 4326, 'POINT', 'XY'
);
);

--- get out of here!!! ---
.exit
5 changes: 5 additions & 0 deletions handlers/sequence/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
post: require('./post')
}
16 changes: 9 additions & 7 deletions handlers/sequence/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ module.exports = async (r, h) => {
sequences.forEach(async (sequence) => {
try {
const sequenceId = uuidv4();
await Promise.map([insertImages, insertSequence], async (builder) => {
try {
await builder(sequenceId, sequence, userId);
} catch (e) {
throw e;
}
})
console.log(sequenceId);
return {}
// await Promise.map([insertImages, insertSequence], async (builder) => {
// try {
// await builder(sequenceId, sequence, userId);
// } catch (e) {
// throw e;
// }
// })
} catch (e) {
throw e;
}
Expand Down
12 changes: 6 additions & 6 deletions handlers/user/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ const uuidv4 = require('uuid/v4');

module.exports = async (r, h) => {
try {
const user = r.params.user,
users = await db('Users').select('name')[0];
const user = r.payload,
users = await db('Users').select('name');

if (users.contains(user)) {
if (users.indexOf(user) === -1) {
Boom.badRequest('user already exists');

}

await db('Users').insert({id: uuidv4(), name: user});

await db('Users').insert({id: 3, name: user.name});
return h.response({ upload: 'successful' }).code(200)

} catch (error) {
throw error;
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"scripts": {
"migrate": "sqlite3 db/posm-paths.sqlite3 < db/posm-paths.sql",
"test": "./node_modules/.bin/mocha test/**/* --exit",
"seed": "knex seed:run"
"seed": "knex seed:run",
"fixture": "yarn run migrate && yarn run seed"
},
"dependencies": {
"bluebird": "^3.5.1",
Expand Down
1 change: 1 addition & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

module.exports = [
require('./user').post,
require('./sequence').post
]
12 changes: 9 additions & 3 deletions routes/user/post.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
'use strict';

const pathsSchema = require('../../schema/paths');
const Joi = require('joi');

module.exports = {
method: 'POST',
path: '/sequence',
config: { handler: require('../../handlers/user').post }
path: '/user',
config: {
handler: require('../../handlers/user').post,
validate: {
payload: Joi.object().keys({ name: Joi.string() }),
failAction: async (request, h, err) => err
}
}
}
3 changes: 2 additions & 1 deletion seeds/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const users = require('../testData/seeds').users;
const uuidv4 = require('uuid/v4');

Promise = require('bluebird');

Expand All @@ -9,7 +10,7 @@ exports.seed = async (knex, Promise) => {
await knex('Users').del();
return Promise.map(users, async (user) => {
try {
await knex('Users').insert({name: user.name})
await knex('Users').insert({id: uuidv4(), name: user.name})
} catch (error) {
console.error(error);
}
Expand Down
7 changes: 7 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const injectDefaults = require('../config')['development'].injectDefaults;

module.exports = {
mergeDefaults: (request) => Object.assign(injectDefaults, request)
}
File renamed without changes.
30 changes: 30 additions & 0 deletions test/sequence/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 'use strict';

// const Joi = require('joi');
// const chai = require('chai');
// const expect = chai.expect;
// const server = require('../server');
// const mergeDefaults = require('../helpers').mergeDefaults;
// const routes = [
// require('../../routes/sequence').post
// ];

// before(async () => await server.liftOff(routes))
// describe('post', () => {
// it('replies 200 when successful creating/inserting image sequences', async () => {
// try {
// const request = mergeDefaults({
// method: 'POST',
// payload: require('../../testData/payloads.json').postSequence,
// url:'/sequence'
// }),
// r = await server.inject(request),
// statusCode = r.statusCode;

// expect(statusCode).to.equal(202);
// } catch (error) {
// console.error(error);

// }
// })
// })
6 changes: 6 additions & 0 deletions test/sequence/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
handlerSpec: require('./handler'),
adapterSpec: require('./adapter')
}
51 changes: 51 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

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


server.liftOff = async (route) => {
try {
server.route(route);

if (!module.parent) {
await server.start();
console.log(`test server started at ${server.info.uri}`)

} else {
await server.initialize()

}

} catch (error) {
throw error;

}
}

server.crashLanding = async () => {
try {
return await server.stop();

} catch (error) {
console.error(error)

}
}

void async function () {
try {
if (!module.parent) {
await server.start();

}
} catch (error) {
console.error(error);

}
}()

module.exports = server;
49 changes: 49 additions & 0 deletions test/user/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const Joi = require('joi');
const chai = require('chai');
const expect = chai.expect;
const server = require('../server');
const mergeDefaults = require('../helpers').mergeDefaults;
const newUserPayload = require('../../testData/payloads').postUser[0];
const oldUserPayload = require('../../testData/payloads').postUser[1];
const routes = [
require('../../routes/user').post
];

before(async () => await server.liftOff(routes))
describe('post', () => {
it('replies 200 when successful adding user to db', async () => {
try {
const request = mergeDefaults({
method: 'POST',
payload: newUserPayload,
url:'/user'
}),
r = await server.inject(request),
statusCode = r.statusCode;

expect(statusCode).to.equal(200);

} catch (error) {
console.error(error);

}
})
it('replies 400 when unsuccessful because user already in db', async () => {
try {
const request = mergeDefaults({
method: 'POST',
payload: oldUserPayload,
url: '/user'
}),
r = await server.inject(request),
statusCode = r.statusCode;

expect(statusCode).to.equal(400);

} catch (error) {
console.error(error);
}
})
})
5 changes: 5 additions & 0 deletions test/user/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
handlerSpec: require('./handler')
}
5 changes: 5 additions & 0 deletions testData/payloads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

module.exports = {
postUser: require('./users')[0]
}
5 changes: 5 additions & 0 deletions testData/seeds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
users: require('./users')
// sequences: require('./sequences'),
// images: require('./images')
}
6 changes: 6 additions & 0 deletions testData/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = [
{name: 'danbjoseph'},
{name: 'giblet'}
]
Loading

0 comments on commit 1cbaa17

Please sign in to comment.