loopback/test/model.application.test.js

210 lines
6.7 KiB
JavaScript
Raw Normal View History

var loopback = require(('../'));
var assert = require('assert');
var Application = loopback.Application;
2014-11-21 02:35:36 +00:00
describe('Application', function() {
var registeredApp = null;
2014-11-21 02:35:36 +00:00
it('Create a new application', function(done) {
2013-12-18 20:28:58 +00:00
Application.create({owner: 'rfeng',
name: 'MyApp1',
2014-11-21 02:35:36 +00:00
description: 'My first mobile application'}, function(err, result) {
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);
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) {
2013-12-18 20:28:58 +00:00
Application.create({owner: 'rfeng',
name: 'MyAppWithPush',
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',
port: 2195
},
feedbackOptions: {
2013-12-18 20:28:58 +00:00
gateway: 'feedback.sandbox.push.apple.com',
port: 2196,
interval: 300,
batchFeedback: true
}
},
gcm: {
serverApiKey: 'serverKey'
}
}},
2014-11-21 02:35:36 +00:00
function(err, result) {
var 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',
2014-11-21 02:35:36 +00:00
{description: 'My second mobile application'}, function(err, 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.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) {
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(err, result);
});
});
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);
});
});
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);
});
});
});
2014-11-21 02:35:36 +00:00
describe('Application subclass', function() {
it('should use subclass model name', function(done) {
var MyApp = Application.extend('MyApp');
var ds = loopback.createDataSource({connector: loopback.Memory});
MyApp.attachTo(ds);
MyApp.register('rfeng', 'MyApp123',
2014-11-21 02:35:36 +00:00
{description: 'My 123 mobile application'}, function(err, result) {
var 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);
});
});
});
});
});
});