2013-07-18 18:44:25 +00:00
|
|
|
var loopback = require(('../'));
|
|
|
|
var assert = require('assert');
|
2013-10-11 20:44:10 +00:00
|
|
|
var Application = loopback.Application;
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Application', function() {
|
2013-12-18 19:49:09 +00:00
|
|
|
var registeredApp = null;
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2015-07-20 05:47:38 +00:00
|
|
|
it('honors `application.register` - promise variant', function(done) {
|
|
|
|
Application.register('rfeng', 'MyTestApp',
|
2016-04-01 09:14:26 +00:00
|
|
|
{ description: 'My test application' }, function(err, result) {
|
2015-07-20 05:47:38 +00:00
|
|
|
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',
|
2016-04-01 09:14:26 +00:00
|
|
|
{ description: 'My test application' })
|
2015-07-20 05:47:38 +00:00
|
|
|
.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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Create a new application', function(done) {
|
2016-04-01 09:14:26 +00:00
|
|
|
Application.create({ owner: 'rfeng',
|
2013-12-18 20:28:58 +00:00
|
|
|
name: 'MyApp1',
|
2016-04-01 09:14:26 +00:00
|
|
|
description: 'My first mobile application' }, function(err, result) {
|
2013-12-18 19:49:09 +00:00
|
|
|
var app = result;
|
|
|
|
assert.equal(app.owner, 'rfeng');
|
|
|
|
assert.equal(app.name, 'MyApp1');
|
|
|
|
assert.equal(app.description, 'My first mobile application');
|
|
|
|
assert(app.clientKey);
|
|
|
|
assert(app.javaScriptKey);
|
|
|
|
assert(app.restApiKey);
|
|
|
|
assert(app.windowsKey);
|
|
|
|
assert(app.masterKey);
|
|
|
|
assert(app.created);
|
|
|
|
assert(app.modified);
|
2014-03-17 17:01:29 +00:00
|
|
|
assert.equal(typeof app.id, 'string');
|
2013-12-18 19:49:09 +00:00
|
|
|
done(err, result);
|
2013-07-18 18:44:25 +00:00
|
|
|
});
|
2013-12-18 19:49:09 +00:00
|
|
|
});
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Create a new application with push settings', function(done) {
|
2016-04-01 09:14:26 +00:00
|
|
|
Application.create({ owner: 'rfeng',
|
2013-12-18 20:28:58 +00:00
|
|
|
name: 'MyAppWithPush',
|
2013-12-18 19:49:09 +00:00
|
|
|
description: 'My push mobile application',
|
|
|
|
pushSettings: {
|
|
|
|
apns: {
|
|
|
|
production: false,
|
|
|
|
certData: 'cert',
|
|
|
|
keyData: 'key',
|
|
|
|
pushOptions: {
|
2013-12-18 20:28:58 +00:00
|
|
|
gateway: 'gateway.sandbox.push.apple.com',
|
2016-04-01 09:14:26 +00:00
|
|
|
port: 2195,
|
2013-12-18 18:58:34 +00:00
|
|
|
},
|
2013-12-18 19:49:09 +00:00
|
|
|
feedbackOptions: {
|
2013-12-18 20:28:58 +00:00
|
|
|
gateway: 'feedback.sandbox.push.apple.com',
|
|
|
|
port: 2196,
|
2013-12-18 19:49:09 +00:00
|
|
|
interval: 300,
|
2016-04-01 09:14:26 +00:00
|
|
|
batchFeedback: true,
|
|
|
|
},
|
2013-12-18 19:49:09 +00:00
|
|
|
},
|
|
|
|
gcm: {
|
2016-04-01 09:14:26 +00:00
|
|
|
serverApiKey: 'serverKey',
|
|
|
|
},
|
2013-12-18 19:49:09 +00:00
|
|
|
}},
|
2014-11-21 02:35:36 +00:00
|
|
|
function(err, result) {
|
2013-12-18 19:49:09 +00:00
|
|
|
var app = result;
|
|
|
|
assert.deepEqual(app.pushSettings.toObject(), {
|
|
|
|
apns: {
|
|
|
|
production: false,
|
|
|
|
certData: 'cert',
|
|
|
|
keyData: 'key',
|
|
|
|
pushOptions: {
|
|
|
|
gateway: 'gateway.sandbox.push.apple.com',
|
2016-04-01 09:14:26 +00:00
|
|
|
port: 2195,
|
2013-12-18 19:49:09 +00:00
|
|
|
},
|
|
|
|
feedbackOptions: {
|
|
|
|
gateway: 'feedback.sandbox.push.apple.com',
|
|
|
|
port: 2196,
|
|
|
|
interval: 300,
|
2016-04-01 09:14:26 +00:00
|
|
|
batchFeedback: true,
|
|
|
|
},
|
2013-12-18 19:49:09 +00:00
|
|
|
},
|
|
|
|
gcm: {
|
2016-04-01 09:14:26 +00:00
|
|
|
serverApiKey: 'serverKey',
|
|
|
|
},
|
2013-07-18 18:44:25 +00:00
|
|
|
});
|
2013-12-18 19:49:09 +00:00
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
beforeEach(function(done) {
|
2013-12-18 19:49:09 +00:00
|
|
|
Application.register('rfeng', 'MyApp2',
|
2016-04-01 09:14:26 +00:00
|
|
|
{ description: 'My second mobile application' }, function(err, result) {
|
2013-12-18 19:49:09 +00:00
|
|
|
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.created);
|
|
|
|
assert(app.modified);
|
|
|
|
registeredApp = app;
|
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Reset keys', function(done) {
|
|
|
|
Application.resetKeys(registeredApp.id, function(err, result) {
|
2013-12-18 19:49:09 +00:00
|
|
|
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);
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2013-12-18 19:49:09 +00:00
|
|
|
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);
|
2013-07-22 18:15:02 +00:00
|
|
|
|
2013-12-18 19:49:09 +00:00
|
|
|
assert(app.created);
|
|
|
|
assert(app.modified);
|
|
|
|
registeredApp = app;
|
|
|
|
done(err, result);
|
2013-07-22 18:15:02 +00:00
|
|
|
});
|
2013-12-18 19:49:09 +00:00
|
|
|
});
|
2013-07-22 18:15:02 +00:00
|
|
|
|
2015-07-20 05:47:38 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-10-08 05:10:14 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Authenticate with application id & clientKey', function(done) {
|
2013-12-18 19:49:09 +00:00
|
|
|
Application.authenticate(registeredApp.id, registeredApp.clientKey,
|
2014-11-21 02:35:36 +00:00
|
|
|
function(err, result) {
|
2014-02-07 19:14:01 +00:00
|
|
|
assert.equal(result.application.id, registeredApp.id);
|
|
|
|
assert.equal(result.keyType, 'clientKey');
|
2013-12-18 19:49:09 +00:00
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
2013-07-22 18:15:02 +00:00
|
|
|
|
2015-07-20 05:47:38 +00:00
|
|
|
it('Authenticate with application id & clientKey - promise variant',
|
|
|
|
function(done) {
|
|
|
|
Application.authenticate(registeredApp.id, registeredApp.clientKey)
|
|
|
|
.then(function(result) {
|
2016-04-01 09:14:26 +00:00
|
|
|
assert.equal(result.application.id, registeredApp.id);
|
|
|
|
assert.equal(result.keyType, 'clientKey');
|
|
|
|
done();
|
|
|
|
})
|
2015-07-20 05:47:38 +00:00
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Authenticate with application id & javaScriptKey', function(done) {
|
2013-12-18 19:49:09 +00:00
|
|
|
Application.authenticate(registeredApp.id, registeredApp.javaScriptKey,
|
2014-11-21 02:35:36 +00:00
|
|
|
function(err, result) {
|
2014-02-07 19:14:01 +00:00
|
|
|
assert.equal(result.application.id, registeredApp.id);
|
|
|
|
assert.equal(result.keyType, 'javaScriptKey');
|
2013-12-18 19:49:09 +00:00
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
2013-07-22 18:15:02 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Authenticate with application id & restApiKey', function(done) {
|
2013-12-18 19:49:09 +00:00
|
|
|
Application.authenticate(registeredApp.id, registeredApp.restApiKey,
|
2014-11-21 02:35:36 +00:00
|
|
|
function(err, result) {
|
2014-02-07 19:14:01 +00:00
|
|
|
assert.equal(result.application.id, registeredApp.id);
|
|
|
|
assert.equal(result.keyType, 'restApiKey');
|
2013-12-18 19:49:09 +00:00
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
2013-07-22 18:15:02 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Authenticate with application id & masterKey', function(done) {
|
2013-12-18 19:49:09 +00:00
|
|
|
Application.authenticate(registeredApp.id, registeredApp.masterKey,
|
2014-11-21 02:35:36 +00:00
|
|
|
function(err, result) {
|
2014-02-07 19:14:01 +00:00
|
|
|
assert.equal(result.application.id, registeredApp.id);
|
|
|
|
assert.equal(result.keyType, 'masterKey');
|
2013-12-18 19:49:09 +00:00
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
2013-07-22 18:15:02 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Authenticate with application id & windowsKey', function(done) {
|
2013-12-18 19:49:09 +00:00
|
|
|
Application.authenticate(registeredApp.id, registeredApp.windowsKey,
|
2014-11-21 02:35:36 +00:00
|
|
|
function(err, result) {
|
2014-02-07 19:14:01 +00:00
|
|
|
assert.equal(result.application.id, registeredApp.id);
|
|
|
|
assert.equal(result.keyType, 'windowsKey');
|
2013-12-18 19:49:09 +00:00
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
2013-07-22 18:15:02 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Fail to authenticate with application id & invalid key', function(done) {
|
2013-12-18 19:49:09 +00:00
|
|
|
Application.authenticate(registeredApp.id, 'invalid-key',
|
2014-11-21 02:35:36 +00:00
|
|
|
function(err, result) {
|
2013-12-18 19:49:09 +00:00
|
|
|
assert(!result);
|
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
2015-07-20 05:47:38 +00:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
2013-07-18 18:44:25 +00:00
|
|
|
});
|
2013-07-22 18:15:02 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Application subclass', function() {
|
|
|
|
it('should use subclass model name', function(done) {
|
2014-01-14 23:34:44 +00:00
|
|
|
var MyApp = Application.extend('MyApp');
|
2016-04-01 09:14:26 +00:00
|
|
|
var ds = loopback.createDataSource({ connector: loopback.Memory });
|
2014-02-07 19:14:01 +00:00
|
|
|
MyApp.attachTo(ds);
|
|
|
|
MyApp.register('rfeng', 'MyApp123',
|
2016-04-01 09:14:26 +00:00
|
|
|
{ description: 'My 123 mobile application' }, function(err, result) {
|
2014-01-14 23:34:44 +00:00
|
|
|
var app = result;
|
|
|
|
assert.equal(app.owner, 'rfeng');
|
2014-02-07 19:14:01 +00:00
|
|
|
assert.equal(app.name, 'MyApp123');
|
|
|
|
assert.equal(app.description, 'My 123 mobile application');
|
2014-01-14 23:34:44 +00:00
|
|
|
assert(app.clientKey);
|
|
|
|
assert(app.javaScriptKey);
|
|
|
|
assert(app.restApiKey);
|
|
|
|
assert(app.windowsKey);
|
|
|
|
assert(app.masterKey);
|
|
|
|
assert(app.created);
|
|
|
|
assert(app.modified);
|
2014-02-07 19:14:01 +00:00
|
|
|
// Remove all instances from Application model to avoid left-over data
|
2014-11-21 02:35:36 +00:00
|
|
|
Application.destroyAll(function() {
|
|
|
|
MyApp.findById(app.id, function(err, myApp) {
|
2014-01-14 23:34:44 +00:00
|
|
|
assert(!err);
|
2014-02-07 19:14:01 +00:00
|
|
|
assert(myApp);
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
Application.findById(app.id, function(err, myApp) {
|
2014-02-07 19:14:01 +00:00
|
|
|
assert(!err);
|
|
|
|
assert(myApp === null);
|
|
|
|
done(err, myApp);
|
|
|
|
});
|
2014-01-14 23:34:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|