Permalink
Browse files

support conditional GET payload; only return payload when client is o…

…utdated
  • Loading branch information...
1 parent d3d7810 commit 7213aea535dd127a4b34921e329e3a2217f19c1e @zaach zaach committed Jan 23, 2013
Showing with 70 additions and 33 deletions.
  1. +11 −5 client/client.js
  2. +1 −1 client/crypto.js
  3. +15 −6 lib/api/v1/payload.js
  4. +43 −21 test/api.v1.payload.js
View
@@ -192,15 +192,21 @@ GombotClient.prototype = {
getPayload: function(args, cb) {
args = mergeArgs(args, this);
args.method = 'get';
- args.path = this.path + '/v1/payload';
+ args.path = this.path + '/v1/payload' + '?updated=' + (args.updated || 0);
var keys = this.keys;
authRequest(args, function (err, data) {
if (err) return cb(err);
- GombotCrypto.decrypt(keys, data.payload, function (err, plaintext) {
- if (err) return cb(err);
- cb(null, {success: data.success, payload: JSON.parse(plaintext), updated: data.updated, ciphertext: data.payload });
- });
+ if (data.sync) {
+ GombotCrypto.decrypt(keys, data.payload, function (err, plaintext) {
+ if (err) return cb(err);
+ cb(null, {success: data.success, payload: JSON.parse(plaintext), updated: data.updated, ciphertext: data.payload });
+ });
+ } else {
+ // both will be null
+ data.ciphertext = data.payload;
+ cb(null, data);
+ }
});
},
getTimestamp: function(args, cb) {
View
@@ -231,7 +231,7 @@ var GombotCrypto = (function() {
// normalized method
args.method + '\n' +
// path
- url.path + '\n' +
+ url.path + (url.query ? '?' + url.query : '') + '\n' +
// hostname
url.host + '\n' +
// port
View
@@ -28,12 +28,18 @@ module.exports = [
mode: 'hawk'
},
config: {
- description: 'Retreive user credentials',
+ description: 'Returns user credentials if the client\'s are outdated.',
+ validate: {
+ query: {
+ updated: N().required()
+ }
+ },
response: {
schema: {
- success: B(),
- payload: S().required(),
- updated: N().required()
+ success: B().required(),
+ payload: S().required().allow(null),
+ updated: N().required(),
+ sync: B().required()
}
}
}
@@ -55,12 +61,15 @@ function put(request) {
function get(request) {
var id = request.session.id;
+ var ts = request.query.updated;
db.getPayload(id, function(err, doc) {
if (err) return request.reply(Hapi.Error.internal("Could not retreive payload: " + err));
+ var sync = doc.timestamp > ts;
request.reply({
success: true,
- payload: doc.payload,
- updated: doc.timestamp
+ payload: (sync ? doc.payload : null),
+ updated: doc.timestamp,
+ sync: sync
});
});
}
View
@@ -9,6 +9,8 @@ var client;
var test_user = 'foo@payload.com';
var test_pass = 'bar';
+var updated;
+
describe('the servers', function() {
it('should start up', function(done) {
runner(function(err, r) {
@@ -35,36 +37,56 @@ function createAccount(email, pass, cb) {
}
describe("/api/v1/payload", function() {
- it ('should store payload', function(done) {
- createAccount(test_user, test_pass, function(err) {
- try {
- client.storePayload({
- payload: 'foo'
- }, function(err, r) {
- should.not.exist(err);
- should.exist(r);
- should.exist(r.updated);
- return done();
- });
- } catch (e) {
- return done(e);
- }
- //done(err);
- });
+ it ('should create account', function(done) {
+ createAccount(test_user, test_pass, done);
});
- it ('should get payload', function(done) {
+ it ('should fail if missing payload', function(done) {
try {
- client.getPayload({}, function(err, r) {
+ client.storeEncryptedPayload({}, function(err, r) {
+ should.exist(err);
+ should.not.exist(r);
+ should.exist(err.error);
+ (err.error.errorCode).should.equal(400);
+ return done();
+ });
+ } catch (e) {
+ return done(e);
+ }
+ });
+ it ('should store payload', function(done) {
+ try {
+ client.storePayload({
+ payload: 'foo'
+ }, function(err, r) {
should.not.exist(err);
should.exist(r);
should.exist(r.updated);
- (r.payload).should.equal('foo');
- done();
+ return done();
});
} catch (e) {
- done(e);
+ return done(e);
}
});
+ it ('should get payload', function(done) {
+ client.getPayload({}, function(err, r) {
+ should.not.exist(err);
+ should.exist(r);
+ should.exist(r.updated);
+ (r.payload).should.equal('foo');
+ updated = r.updated;
+ done();
+ });
+ });
+ it ('should not get payload if in sync', function(done) {
+ client.getPayload({ updated: updated }, function(err, r) {
+ should.not.exist(err);
+ should.exist(r);
+ should.exist(r.sync);
+ (r.sync).should.equal(false);
+ should.not.exist(r.payload);
+ done();
+ });
+ });
});
describe('the servers', function() {

2 comments on commit 7213aea

Contributor

ckarlof replied Jan 28, 2013

Why doesn't 'PUT' specify in a response schema?

Contributor

zaach replied Jan 28, 2013

@ckarlof nice catch, add in 7b75ece

Please sign in to comment.