Promisify 'Application' model

This commit is contained in:
Pradnya Baviskar 2015-07-20 11:17:38 +05:30 committed by Miroslav Bajtoš
parent 3c18d38548
commit 2ee7c94a4e
2 changed files with 92 additions and 3 deletions

View File

@ -1,4 +1,5 @@
var assert = require('assert'); var assert = require('assert');
var utils = require('../../lib/utils');
/*! /*!
* Application management functions * Application management functions
@ -115,6 +116,8 @@ module.exports = function(Application) {
cb = options; cb = options;
options = {}; options = {};
} }
cb = cb || utils.createPromiseCallback();
var props = {owner: owner, name: name}; var props = {owner: owner, name: name};
for (var p in options) { for (var p in options) {
if (!(p in props)) { if (!(p in props)) {
@ -122,6 +125,7 @@ module.exports = function(Application) {
} }
} }
this.create(props, cb); this.create(props, cb);
return cb.promise;
}; };
/** /**
@ -146,6 +150,7 @@ module.exports = function(Application) {
* @param {Error} err * @param {Error} err
*/ */
Application.resetKeys = function(appId, cb) { Application.resetKeys = function(appId, cb) {
cb = cb || utils.createPromiseCallback();
this.findById(appId, function(err, app) { this.findById(appId, function(err, app) {
if (err) { if (err) {
if (cb) cb(err, app); if (cb) cb(err, app);
@ -153,6 +158,7 @@ module.exports = function(Application) {
} }
app.resetKeys(cb); app.resetKeys(cb);
}); });
return cb.promise;
}; };
/** /**
@ -171,10 +177,12 @@ module.exports = function(Application) {
* *
*/ */
Application.authenticate = function(appId, key, cb) { Application.authenticate = function(appId, key, cb) {
cb = cb || utils.createPromiseCallback();
this.findById(appId, function(err, app) { this.findById(appId, function(err, app) {
if (err || !app) { if (err || !app) {
if (cb) cb(err, null); cb(err, null);
return; return cb.promise;
} }
var result = null; var result = null;
var keyNames = ['clientKey', 'javaScriptKey', 'restApiKey', 'windowsKey', 'masterKey']; var keyNames = ['clientKey', 'javaScriptKey', 'restApiKey', 'windowsKey', 'masterKey'];
@ -187,7 +195,8 @@ module.exports = function(Application) {
break; break;
} }
} }
if (cb) cb(null, result); cb(null, result);
}); });
return cb.promise;
}; };
}; };

View File

@ -5,6 +5,32 @@ var Application = loopback.Application;
describe('Application', function() { describe('Application', function() {
var registeredApp = null; var registeredApp = null;
it('honors `application.register` - promise variant', function(done) {
Application.register('rfeng', 'MyTestApp',
{description: 'My test application'}, function(err, result) {
var app = result;
assert.equal(app.owner, 'rfeng');
assert.equal(app.name, 'MyTestApp');
assert.equal(app.description, 'My test application');
done(err, result);
});
});
it('honors `application.register` - promise variant', function(done) {
Application.register('rfeng', 'MyTestApp',
{description: 'My test application'})
.then(function(result) {
var app = result;
assert.equal(app.owner, 'rfeng');
assert.equal(app.name, 'MyTestApp');
assert.equal(app.description, 'My test application');
done();
})
.catch(function(err) {
done(err);
});
});
it('Create a new application', function(done) { it('Create a new application', function(done) {
Application.create({owner: 'rfeng', Application.create({owner: 'rfeng',
name: 'MyApp1', name: 'MyApp1',
@ -119,6 +145,35 @@ describe('Application', function() {
}); });
}); });
it('Reset keys - promise variant', function(done) {
Application.resetKeys(registeredApp.id)
.then(function(result) {
var app = result;
assert.equal(app.owner, 'rfeng');
assert.equal(app.name, 'MyApp2');
assert.equal(app.description, 'My second mobile application');
assert(app.clientKey);
assert(app.javaScriptKey);
assert(app.restApiKey);
assert(app.windowsKey);
assert(app.masterKey);
assert(app.clientKey !== registeredApp.clientKey);
assert(app.javaScriptKey !== registeredApp.javaScriptKey);
assert(app.restApiKey !== registeredApp.restApiKey);
assert(app.windowsKey !== registeredApp.windowsKey);
assert(app.masterKey !== registeredApp.masterKey);
assert(app.created);
assert(app.modified);
registeredApp = app;
done();
})
.catch(function(err) {
done(err);
});
});
it('Authenticate with application id & clientKey', function(done) { it('Authenticate with application id & clientKey', function(done) {
Application.authenticate(registeredApp.id, registeredApp.clientKey, Application.authenticate(registeredApp.id, registeredApp.clientKey,
function(err, result) { function(err, result) {
@ -128,6 +183,19 @@ describe('Application', function() {
}); });
}); });
it('Authenticate with application id & clientKey - promise variant',
function(done) {
Application.authenticate(registeredApp.id, registeredApp.clientKey)
.then(function(result) {
assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'clientKey');
done();
})
.catch(function(err) {
done(err);
});
});
it('Authenticate with application id & javaScriptKey', function(done) { it('Authenticate with application id & javaScriptKey', function(done) {
Application.authenticate(registeredApp.id, registeredApp.javaScriptKey, Application.authenticate(registeredApp.id, registeredApp.javaScriptKey,
function(err, result) { function(err, result) {
@ -171,6 +239,18 @@ describe('Application', function() {
done(err, result); done(err, result);
}); });
}); });
it('Fail to authenticate with application id - promise variant', function(done) {
Application.authenticate(registeredApp.id, 'invalid-key')
.then(function(result) {
assert(!result);
done();
})
.catch(function(err) {
done(err);
throw new Error('Error should NOT be thrown');
});
});
}); });
describe('Application subclass', function() { describe('Application subclass', function() {