From ce48521efbc17255905cb8d1b6d1464782070711 Mon Sep 17 00:00:00 2001 From: wusuopu Date: Thu, 8 Oct 2015 13:10:14 +0800 Subject: [PATCH] Set application's id property only if it's empty. Fix `Application.resetKeys()` to reset instance id only if it is not already set. This fixes a bug where each call of resetKeys created a new instance. --- common/models/application.js | 4 +++- test/model.application.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/common/models/application.js b/common/models/application.js index 3ff2a614..617798c1 100644 --- a/common/models/application.js +++ b/common/models/application.js @@ -92,7 +92,9 @@ module.exports = function(Application) { var app = ctx.instance; app.created = app.modified = new Date(); - app.id = generateKey('id', 'md5'); + if (!app.id) { + app.id = generateKey('id', 'md5'); + } app.clientKey = generateKey('client'); app.javaScriptKey = generateKey('javaScript'); app.restApiKey = generateKey('restApi'); diff --git a/test/model.application.test.js b/test/model.application.test.js index d3c8575d..96d8ab75 100644 --- a/test/model.application.test.js +++ b/test/model.application.test.js @@ -174,6 +174,30 @@ describe('Application', function() { }); }); + it('Reset keys without create a new instance', function(done) { + Application.resetKeys(registeredApp.id, function(err, result) { + var app = result; + assert(app.id); + assert(app.id === registeredApp.id); + registeredApp = app; + done(err, result); + }); + }); + + it('Reset keys without create a new instance - promise variant', function(done) { + Application.resetKeys(registeredApp.id) + .then(function(result) { + var app = result; + assert(app.id); + assert(app.id === registeredApp.id); + registeredApp = app; + done(); + }) + .catch(function(err) { + done(err); + }); + }); + it('Authenticate with application id & clientKey', function(done) { Application.authenticate(registeredApp.id, registeredApp.clientKey, function(err, result) {