2020-01-21 19:19:18 +00:00
|
|
|
// Copyright IBM Corp. 2013,2019. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2019-10-07 09:45:34 +00:00
|
|
|
const loopback = require(('../'));
|
|
|
|
const assert = require('assert');
|
|
|
|
const Application = loopback.Application;
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('Application', function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
let registeredApp = null;
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2016-01-25 09:36:51 +00:00
|
|
|
before(function attachToMemory() {
|
|
|
|
Application.attachTo(loopback.memory());
|
|
|
|
});
|
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
it('honors `application.register` - callback variant', function(done) {
|
2015-07-20 05:47:38 +00:00
|
|
|
Application.register('rfeng', 'MyTestApp',
|
2016-11-15 21:46:23 +00:00
|
|
|
{description: 'My test application'}, function(err, result) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = result;
|
2015-07-20 05:47:38 +00:00
|
|
|
assert.equal(app.owner, 'rfeng');
|
|
|
|
assert.equal(app.name, 'MyTestApp');
|
|
|
|
assert.equal(app.description, 'My test application');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-20 05:47:38 +00:00
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('honors `application.register` - promise variant', function(done) {
|
|
|
|
Application.register('rfeng', 'MyTestApp',
|
2016-11-15 21:46:23 +00:00
|
|
|
{description: 'My test application'})
|
2015-07-20 05:47:38 +00:00
|
|
|
.then(function(result) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = result;
|
2015-07-20 05:47:38 +00:00
|
|
|
assert.equal(app.owner, 'rfeng');
|
|
|
|
assert.equal(app.name, 'MyTestApp');
|
|
|
|
assert.equal(app.description, 'My test application');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-07-20 05:47:38 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('Create a new application', function(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
Application.create({owner: 'rfeng',
|
2013-12-18 20:28:58 +00:00
|
|
|
name: 'MyApp1',
|
2016-11-15 21:46:23 +00:00
|
|
|
description: 'My first mobile application'}, function(err, result) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = result;
|
2013-12-18 19:49:09 +00:00
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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-12-06 14:40:42 +00:00
|
|
|
Application.create({
|
|
|
|
owner: 'rfeng',
|
|
|
|
name: 'MyAppWithPush',
|
|
|
|
description: 'My push mobile application',
|
|
|
|
pushSettings: {
|
|
|
|
apns: {
|
|
|
|
production: false,
|
|
|
|
certData: 'cert',
|
|
|
|
keyData: 'key',
|
|
|
|
pushOptions: {
|
|
|
|
gateway: 'gateway.sandbox.push.apple.com',
|
|
|
|
port: 2195,
|
2013-12-18 19:49:09 +00:00
|
|
|
},
|
2016-12-06 14:40:42 +00:00
|
|
|
feedbackOptions: {
|
|
|
|
gateway: 'feedback.sandbox.push.apple.com',
|
|
|
|
port: 2196,
|
|
|
|
interval: 300,
|
|
|
|
batchFeedback: true,
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2016-12-06 14:40:42 +00:00
|
|
|
},
|
|
|
|
gcm: {
|
|
|
|
serverApiKey: 'serverKey',
|
|
|
|
},
|
|
|
|
}},
|
2017-12-12 08:33:15 +00:00
|
|
|
function(err, result) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = result;
|
2017-12-12 08:33:15 +00:00
|
|
|
assert.deepEqual(app.pushSettings.toObject(), {
|
|
|
|
apns: {
|
|
|
|
production: false,
|
|
|
|
certData: 'cert',
|
|
|
|
keyData: 'key',
|
|
|
|
pushOptions: {
|
|
|
|
gateway: 'gateway.sandbox.push.apple.com',
|
|
|
|
port: 2195,
|
2013-12-18 19:49:09 +00:00
|
|
|
},
|
2017-12-12 08:33:15 +00:00
|
|
|
feedbackOptions: {
|
|
|
|
gateway: 'feedback.sandbox.push.apple.com',
|
|
|
|
port: 2196,
|
|
|
|
interval: 300,
|
|
|
|
batchFeedback: true,
|
2016-04-01 09:14:26 +00:00
|
|
|
},
|
2017-12-12 08:33:15 +00:00
|
|
|
},
|
|
|
|
gcm: {
|
|
|
|
serverApiKey: 'serverKey',
|
|
|
|
},
|
2013-12-18 19:49:09 +00:00
|
|
|
});
|
2017-12-12 08:33:15 +00:00
|
|
|
|
|
|
|
done(err, result);
|
|
|
|
});
|
2013-12-18 19:49:09 +00:00
|
|
|
});
|
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-11-15 21:46:23 +00:00
|
|
|
{description: 'My second mobile application'}, function(err, result) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = result;
|
2013-12-18 19:49:09 +00:00
|
|
|
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;
|
2016-05-05 04:09:06 +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
|
|
|
it('Reset keys', function(done) {
|
|
|
|
Application.resetKeys(registeredApp.id, function(err, result) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = result;
|
2013-12-18 19:49:09 +00:00
|
|
|
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;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-12-18 19:49:09 +00:00
|
|
|
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)
|
2017-12-12 08:33:15 +00:00
|
|
|
.then(function(result) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = result;
|
2017-12-12 08:33:15 +00:00
|
|
|
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);
|
2015-07-20 05:47:38 +00:00
|
|
|
|
2017-12-12 08:33:15 +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);
|
2015-07-20 05:47:38 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
assert(app.created);
|
|
|
|
assert(app.modified);
|
|
|
|
registeredApp = app;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2015-07-20 05:47:38 +00:00
|
|
|
});
|
|
|
|
|
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) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = result;
|
2015-10-08 05:10:14 +00:00
|
|
|
assert(app.id);
|
|
|
|
assert(app.id === registeredApp.id);
|
|
|
|
registeredApp = app;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-10-08 05:10:14 +00:00
|
|
|
done(err, result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Reset keys without create a new instance - promise variant', function(done) {
|
|
|
|
Application.resetKeys(registeredApp.id)
|
2017-12-12 08:33:15 +00:00
|
|
|
.then(function(result) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = result;
|
2017-12-12 08:33:15 +00:00
|
|
|
assert(app.id);
|
|
|
|
assert(app.id === registeredApp.id);
|
|
|
|
registeredApp = app;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2017-12-12 08:33:15 +00:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2015-10-08 05:10:14 +00:00
|
|
|
});
|
|
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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)
|
2017-12-12 08:33:15 +00:00
|
|
|
.then(function(result) {
|
|
|
|
assert.equal(result.application.id, registeredApp.id);
|
|
|
|
assert.equal(result.keyType, 'clientKey');
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2015-07-20 05:47:38 +00:00
|
|
|
});
|
|
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
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);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-12-18 19:49:09 +00:00
|
|
|
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')
|
2017-12-12 08:33:15 +00:00
|
|
|
.then(function(result) {
|
|
|
|
assert(!result);
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
done(err);
|
|
|
|
throw new Error('Error should NOT be thrown');
|
|
|
|
});
|
2015-07-20 05:47:38 +00:00
|
|
|
});
|
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) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const MyApp = Application.extend('MyApp');
|
|
|
|
const ds = loopback.createDataSource({connector: loopback.Memory});
|
2014-02-07 19:14:01 +00:00
|
|
|
MyApp.attachTo(ds);
|
|
|
|
MyApp.register('rfeng', 'MyApp123',
|
2016-11-15 21:46:23 +00:00
|
|
|
{description: 'My 123 mobile application'}, function(err, result) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const app = result;
|
2014-01-14 23:34:44 +00:00
|
|
|
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);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-02-07 19:14:01 +00:00
|
|
|
done(err, myApp);
|
|
|
|
});
|
2014-01-14 23:34:44 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|