Skip to content

Commit

Permalink
implements GET /timer/all to retrieve all timers for a person. #78
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Mar 15, 2015
1 parent 9dca409 commit 1622d3c
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
> *The **price** of anything is the **amount** of **life** you **exchange for it**.*
~ Henry David Thoreau

Time (a *finite* amount) is one of the few things we *all* have in common.
Time (a *finite* amount it) is one of the few things we *all* have in common.
Wether we like it or not, we each only have a ***fixed amount***.
It is our most precious ***non-renewable*** 'resource'.
We need a *much* better way of keeping track of how we spend it.
Expand Down
18 changes: 18 additions & 0 deletions api/handlers/timer_find_all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var ES = require('esta');

module.exports = function(req, reply) {
var query = {
"index": "time",
"type": "timer",
"field": "person",
"text": req.auth.credentials.person.toString() // using issuer as the person
};
ES.SEARCH(query, function(res) {
if(res.hits.total > 0) {
return reply(res);
}
else {
return reply(res).code(404);
}
});
}
10 changes: 5 additions & 5 deletions api/handlers/timer_start.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
var JWT = require('jsonwebtoken');
// var JWT = require('jsonwebtoken');
var ES = require('esta');
var perma = require('perma');
var aguid = require('aguid');
var Hoek = require('hoek');

module.exports = function(req, reply) {
// extract the person id from JWT
var decoded = JWT.verify(req.headers.authorization, process.env.JWT_SECRET);
var decoded = req.auth.credentials; //JWT.verify(req.headers.authorization, process.env.JWT_SECRET);
var person = decoded.person;
var created = new Date().toISOString();
var id = perma(person+created);
var id = aguid();
var timer = {
index: "time",
type: "timer",
person: person,
person: decoded.person,
session: decoded.jti, // session id from JWT
ct: created,
id: id
Expand Down
8 changes: 4 additions & 4 deletions api/lib/auth_jwt_sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module.exports = function sign(request, callback) {
var payload = { jti:aguid() }; // v4 random UUID used as Session ID below

if (request.payload && request.payload.email) {
payload.iss = aguid(request.payload.email);
payload.person = aguid(request.payload.email);
} // see: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html#issDef
else { // no email is set (means an anonymous person)
payload.iss = "anonymous";
payload.person = "anonymous";
} // this will need to be extended for other auth: http://git.io/pc1c

var token = JWT.sign(payload, process.env.JWT_SECRET); // http://git.io/xPBn
Expand All @@ -21,9 +21,9 @@ module.exports = function sign(request, callback) {
index: "time",
type: "session",
id : payload.jti,
person: payload.iss,
person: payload.person,
ua: request.headers['user-agent'],
ct: new Date().toISOString()
created: new Date().toISOString()
}

ES.CREATE(session, function(esres) {
Expand Down
2 changes: 2 additions & 0 deletions api/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module.exports = [
config: { auth: 'jwt', handler: require('./handlers/logout.js') } },
{ path: '/timer/{id}', method: 'GET', // Validate {id} to prevent injection?
config: { auth: 'jwt', handler: require('./handlers/timer_find.js') } },
{ path: '/timer/all', method: 'GET', // Validate {id} to prevent injection?
config: { auth: 'jwt', handler: require('./handlers/timer_find_all.js') } },
{ path: '/timer/new', method: 'POST',
config: { validate: require('../models/timer'),
auth: 'jwt', handler: require('./handlers/timer_start.js')
Expand Down
3 changes: 2 additions & 1 deletion api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ server.connection({ port: port });

var routes = require('./routes.js');

server.register([ {register: Basic}, {register: AuthJWT}, { register: lout } ], function (err) {
server.register([ {register: Basic},
{register: AuthJWT}, { register: lout } ], function (err) {

server.auth.strategy('basic', 'basic', {
validateFunc: require('./lib/auth_basic_validate.js')
Expand Down
2 changes: 1 addition & 1 deletion api/test/anonymous.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var server = require("../../app.js");
var server = require("../../web.js");
var dir = __dirname.split('/')[__dirname.split('/').length-1];
var file = dir + __filename.replace(__dirname, '') + " -> ";

Expand Down
2 changes: 1 addition & 1 deletion api/test/auth_basic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var server = require("../../app.js");
var server = require("../../web.js");
var dir = __dirname.split('/')[__dirname.split('/').length-1];
var file = dir + __filename.replace(__dirname, '') + " -> ";

Expand Down
2 changes: 1 addition & 1 deletion api/test/logout.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Logout https://github.com/ideaq/time/issues/65
var ES = require('esta');
var test = require('tape');
var server = require("../../app.js");
var server = require("../../web.js");
var dir = __dirname.split('/')[__dirname.split('/').length-1];
var file = dir + __filename.replace(__dirname, '') + " -> ";
var token; // used below
Expand Down
2 changes: 1 addition & 1 deletion api/test/register.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var server = require("../../app.js");
var server = require("../../web.js");
var dir = __dirname.split('/')[__dirname.split('/').length-1];
var file = dir + __filename.replace(__dirname, '');

Expand Down
2 changes: 1 addition & 1 deletion api/test/timer_find.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var server = require("../../app.js");
var server = require("../../web.js");
var dir = __dirname.split('/')[__dirname.split('/').length-1];
var file = dir + __filename.replace(__dirname, '') + " -> ";

Expand Down
128 changes: 128 additions & 0 deletions api/test/timer_find_all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
var test = require('tape');
var server = require("../../web.js");
var dir = __dirname.split('/')[__dirname.split('/').length-1];
var file = dir + __filename.replace(__dirname, '') + " -> ";
var token;
var records = 100;
var countdown = records;

var drop = require('./z_drop');
test(file+ "Teardown", function(t) {
drop(function(res){
t.equal(res.acknowledged, true, file+"ALL Records DELETED!");
t.end();
}).end();
});

function create(t, callback) {
var timer = {
"desc" : "My Amazing Timer #"+countdown,
"st" : new Date().toISOString()
}
var options = {
method: "POST",
url: "/timer/new",
payload: timer,
headers : { authorization : token }
};
server.inject(options, function(res) {
countdown--;
// console.log(" >>> "+countdown + " res.created "+ T.created);
if(countdown === 0) {
var T = JSON.parse(res.payload);
t.equal(res.statusCode, 200, records+ " New timers started! " + T.st);
callback(res, t);
}
});
}

function finish(res, t){
// console.log(res);
var T = JSON.parse(res.payload);
var tid = T._id;
var options = {
method: "GET",
url: "/timer/"+tid,
headers : { authorization : token }
};

server.inject(options, function(res) {
t.equal(res.statusCode, 200, "New timer retrieved!"+'\n');
server.stop();
t.end();
});
}



// new anonymous person
test(file + "Register new person to create a few timers", function(t) {
var person = {
"email" : "[email protected]",
"password" : "EveryThingisAwesome",
"firstname": "Jenny"
}
var options = {
method : "POST",
url : "/register",
payload : person
};
server.inject(options, function(res) {
t.equal(res.statusCode, 200, "Session Created = "+res.result.created);
token = res.headers.authorization;
// can't create create functions inside a for loop so no anon callbacks!
for(var i = 0; i < records; i++) {
create(t, finish);
} // end for
});
});

test(file + "GET /timer/all to list all timers", function(t) {
var options = {
method: "GET",
url: "/timer/all",
headers : { authorization : token }
};
setTimeout(function(){
server.inject(options, function(res) {
// console.log(res.result);
var T = JSON.parse(res.payload);
t.equal(res.statusCode, 200, "Find all records for this person");
t.equal(T.hits.total, 100, "100 records found");
server.stop();
t.end();
});
},1000)
});

test(file + "GET /timer/all should fail for Timmy no timers", function(t) {
var person = {
"email" : "[email protected]",
"password" : "EveryThingisAwesome",
"firstname": "Timmay!"
}
var options = {
method : "POST",
url : "/register",
payload : person
};
server.inject(options, function(res) {
var newtoken = res.headers.authorization;
var options = {
method: "GET",
url: "/timer/all",
headers : { authorization : newtoken }
};
setTimeout(function(){
server.inject(options, function(res) {
console.log(res.result);
var T = JSON.parse(res.payload);
console.log(T);
t.equal(res.statusCode, 404, "Timmay! has no timers...");
// t.equal(T.hits.total, 100, "100 records found");
server.stop();
t.end();
});
},1000)
});
});
2 changes: 1 addition & 1 deletion api/test/timer_start.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var server = require("../../app.js");
var server = require("../../web.js");
var dir = __dirname.split('/')[__dirname.split('/').length-1];
var file = dir + __filename.replace(__dirname, '') + " -> ";
var token;
Expand Down
4 changes: 2 additions & 2 deletions api/test/z_drop.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var http = require('http');

var options = {
host:"127.0.0.1",
port: 9200, // use ENV var?
host: process.env.ES_HOST,
port: process.env.ES_PORT,
path: "/_all", // DELETEs EVERYTHING!!
method: 'DELETE',
headers: { 'Content-Type': 'application/json' }
Expand Down
12 changes: 6 additions & 6 deletions models/person.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var Joi = require('joi');
module.exports = {
payload: {
person : Joi.string(), // unique id
email : Joi.string().email().required(),
password : Joi.string().required().min(4),
fn : Joi.string(),
ln : Joi.string(),
ct : Joi.forbidden() // don't allow people to set this!
person : Joi.string(), // unique id
email : Joi.string().email().required(),
password : Joi.string().required().min(4),
firstname : Joi.string(),
lastname : Joi.string(),
created : Joi.forbidden() // don't allow people to set this!
}
}
6 changes: 4 additions & 2 deletions app.js → web.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ server.register([ {register: Basic}, {register: AuthJWT} ], function (err) {

});

server.start();
console.log('Now Visit: http://localhost:'+port);
server.start(function(){
console.log('Now Visit: http://localhost:'+port);
});


module.exports = server;

0 comments on commit 1622d3c

Please sign in to comment.