loopback/test/model.application.test.js

343 lines
10 KiB
JavaScript
Raw Normal View History

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
'use strict';
const loopback = require(('../'));
const assert = require('assert');
const Application = loopback.Application;
2014-11-21 02:35:36 +00:00
describe('Application', function() {
let registeredApp = null;
before(function attachToMemory() {
Application.attachTo(loopback.memory());
});
it('honors `application.register` - callback variant', function(done) {
2015-07-20 05:47:38 +00:00
Application.register('rfeng', 'MyTestApp',
{description: 'My test application'}, function(err, result) {
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');
2015-07-20 05:47:38 +00:00
done(err, result);
});
});
it('honors `application.register` - promise variant', function(done) {
Application.register('rfeng', 'MyTestApp',
{description: 'My test application'})
2015-07-20 05:47:38 +00:00
.then(function(result) {
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');
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) {
Application.create({owner: 'rfeng',
2013-12-18 20:28:58 +00:00
name: 'MyApp1',
description: 'My first mobile application'}, function(err, result) {
const 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);
assert.equal(typeof app.id, 'string');
done(err, result);
});
});
2014-11-21 02:35:36 +00:00
it('Create a new application with push settings', function(done) {
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,
},
feedbackOptions: {
gateway: 'feedback.sandbox.push.apple.com',
port: 2196,
interval: 300,
batchFeedback: true,
},
},
gcm: {
serverApiKey: 'serverKey',
},
}},
function(err, result) {
const app = result;
assert.deepEqual(app.pushSettings.toObject(), {
apns: {
production: false,
certData: 'cert',
keyData: 'key',
pushOptions: {
gateway: 'gateway.sandbox.push.apple.com',
port: 2195,
},
feedbackOptions: {
gateway: 'feedback.sandbox.push.apple.com',
port: 2196,
interval: 300,
batchFeedback: true,
},
},
gcm: {
serverApiKey: 'serverKey',
},
});
done(err, result);
});
});
2014-11-21 02:35:36 +00:00
beforeEach(function(done) {
Application.register('rfeng', 'MyApp2',
{description: 'My second mobile application'}, function(err, result) {
const 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);
});
});
2014-11-21 02:35:36 +00:00
it('Reset keys', function(done) {
Application.resetKeys(registeredApp.id, function(err, result) {
const 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(err, result);
});
});
2015-07-20 05:47:38 +00:00
it('Reset keys - promise variant', function(done) {
Application.resetKeys(registeredApp.id)
.then(function(result) {
const 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);
2015-07-20 05:47:38 +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
assert(app.created);
assert(app.modified);
registeredApp = app;
done();
})
.catch(function(err) {
done(err);
});
2015-07-20 05:47:38 +00:00
});
it('Reset keys without create a new instance', function(done) {
Application.resetKeys(registeredApp.id, function(err, result) {
const 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) {
const 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) {
Application.authenticate(registeredApp.id, registeredApp.clientKey,
2014-11-21 02:35:36 +00:00
function(err, result) {
assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'clientKey');
done(err, result);
});
});
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) {
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) {
Application.authenticate(registeredApp.id, registeredApp.javaScriptKey,
2014-11-21 02:35:36 +00:00
function(err, result) {
assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'javaScriptKey');
done(err, result);
});
});
2014-11-21 02:35:36 +00:00
it('Authenticate with application id & restApiKey', function(done) {
Application.authenticate(registeredApp.id, registeredApp.restApiKey,
2014-11-21 02:35:36 +00:00
function(err, result) {
assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'restApiKey');
done(err, result);
});
});
2014-11-21 02:35:36 +00:00
it('Authenticate with application id & masterKey', function(done) {
Application.authenticate(registeredApp.id, registeredApp.masterKey,
2014-11-21 02:35:36 +00:00
function(err, result) {
assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'masterKey');
done(err, result);
});
});
2014-11-21 02:35:36 +00:00
it('Authenticate with application id & windowsKey', function(done) {
Application.authenticate(registeredApp.id, registeredApp.windowsKey,
2014-11-21 02:35:36 +00:00
function(err, result) {
assert.equal(result.application.id, registeredApp.id);
assert.equal(result.keyType, 'windowsKey');
done(err, result);
});
});
2014-11-21 02:35:36 +00:00
it('Fail to authenticate with application id & invalid key', function(done) {
Application.authenticate(registeredApp.id, 'invalid-key',
2014-11-21 02:35:36 +00:00
function(err, result) {
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');
});
2015-07-20 05:47:38 +00:00
});
});
2014-11-21 02:35:36 +00:00
describe('Application subclass', function() {
it('should use subclass model name', function(done) {
const MyApp = Application.extend('MyApp');
const ds = loopback.createDataSource({connector: loopback.Memory});
MyApp.attachTo(ds);
MyApp.register('rfeng', 'MyApp123',
{description: 'My 123 mobile application'}, function(err, result) {
const app = result;
assert.equal(app.owner, 'rfeng');
assert.equal(app.name, 'MyApp123');
assert.equal(app.description, 'My 123 mobile application');
assert(app.clientKey);
assert(app.javaScriptKey);
assert(app.restApiKey);
assert(app.windowsKey);
assert(app.masterKey);
assert(app.created);
assert(app.modified);
// 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) {
assert(!err);
assert(myApp);
2014-11-21 02:35:36 +00:00
Application.findById(app.id, function(err, myApp) {
assert(!err);
assert(myApp === null);
done(err, myApp);
});
});
});
});
});
});