Promisify 'Application' model
This commit is contained in:
parent
3c18d38548
commit
2ee7c94a4e
|
@ -1,4 +1,5 @@
|
|||
var assert = require('assert');
|
||||
var utils = require('../../lib/utils');
|
||||
|
||||
/*!
|
||||
* Application management functions
|
||||
|
@ -115,6 +116,8 @@ module.exports = function(Application) {
|
|||
cb = options;
|
||||
options = {};
|
||||
}
|
||||
cb = cb || utils.createPromiseCallback();
|
||||
|
||||
var props = {owner: owner, name: name};
|
||||
for (var p in options) {
|
||||
if (!(p in props)) {
|
||||
|
@ -122,6 +125,7 @@ module.exports = function(Application) {
|
|||
}
|
||||
}
|
||||
this.create(props, cb);
|
||||
return cb.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -146,6 +150,7 @@ module.exports = function(Application) {
|
|||
* @param {Error} err
|
||||
*/
|
||||
Application.resetKeys = function(appId, cb) {
|
||||
cb = cb || utils.createPromiseCallback();
|
||||
this.findById(appId, function(err, app) {
|
||||
if (err) {
|
||||
if (cb) cb(err, app);
|
||||
|
@ -153,6 +158,7 @@ module.exports = function(Application) {
|
|||
}
|
||||
app.resetKeys(cb);
|
||||
});
|
||||
return cb.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -171,10 +177,12 @@ module.exports = function(Application) {
|
|||
*
|
||||
*/
|
||||
Application.authenticate = function(appId, key, cb) {
|
||||
cb = cb || utils.createPromiseCallback();
|
||||
|
||||
this.findById(appId, function(err, app) {
|
||||
if (err || !app) {
|
||||
if (cb) cb(err, null);
|
||||
return;
|
||||
cb(err, null);
|
||||
return cb.promise;
|
||||
}
|
||||
var result = null;
|
||||
var keyNames = ['clientKey', 'javaScriptKey', 'restApiKey', 'windowsKey', 'masterKey'];
|
||||
|
@ -187,7 +195,8 @@ module.exports = function(Application) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
if (cb) cb(null, result);
|
||||
cb(null, result);
|
||||
});
|
||||
return cb.promise;
|
||||
};
|
||||
};
|
||||
|
|
|
@ -5,6 +5,32 @@ var Application = loopback.Application;
|
|||
describe('Application', function() {
|
||||
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) {
|
||||
Application.create({owner: 'rfeng',
|
||||
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) {
|
||||
Application.authenticate(registeredApp.id, registeredApp.clientKey,
|
||||
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) {
|
||||
Application.authenticate(registeredApp.id, registeredApp.javaScriptKey,
|
||||
function(err, result) {
|
||||
|
@ -171,6 +239,18 @@ describe('Application', function() {
|
|||
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() {
|
||||
|
|
Loading…
Reference in New Issue