Files
cloudron-box/src/test/updatechecker-test.js

495 lines
18 KiB
JavaScript
Raw Normal View History

2016-01-23 15:57:56 -08:00
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
var appdb = require('../appdb.js'),
async = require('async'),
config = require('../config.js'),
2017-01-27 09:40:46 -08:00
constants = require('../constants.js'),
2016-01-23 15:57:56 -08:00
database = require('../database.js'),
expect = require('expect.js'),
2016-04-12 13:12:17 -07:00
mailer = require('../mailer.js'),
2016-01-23 15:57:56 -08:00
nock = require('nock'),
2017-02-06 16:33:55 -08:00
paths = require('../paths.js'),
safe = require('safetydance'),
2016-01-23 15:57:56 -08:00
settings = require('../settings.js'),
2017-04-13 23:19:37 -07:00
settingsdb = require('../settingsdb.js'),
2016-01-23 15:57:56 -08:00
updatechecker = require('../updatechecker.js'),
2017-04-14 01:13:42 -07:00
user = require('../user.js');
2016-01-23 15:57:56 -08:00
2016-04-12 13:12:17 -07:00
// owner
var USER_0 = {
username: 'username0',
password: 'Username0pass?1234',
email: 'user0@email.com',
displayName: 'User 0'
};
2017-11-11 03:06:57 +01:00
const DOMAIN_0 = {
domain: 'example.com',
zoneName: 'example.com',
config: { provider: 'manual' }
};
2016-05-01 20:01:34 -07:00
var AUDIT_SOURCE = {
ip: '1.2.3.4'
};
2016-04-12 13:15:40 -07:00
function checkMails(number, done) {
// mails are enqueued async
setTimeout(function () {
expect(mailer._getMailQueue().length).to.equal(number);
mailer._clearMailQueue();
done();
}, 500);
}
function cleanup(done) {
mailer._clearMailQueue();
safe.fs.unlinkSync(paths.UPDATE_CHECKER_FILE);
async.series([
settings.uninitialize,
database._clear
], done);
}
describe('updatechecker - box - manual (email)', function () {
2016-01-23 15:57:56 -08:00
before(function (done) {
2017-04-14 01:13:42 -07:00
config._reset();
2017-11-11 03:06:57 +01:00
config.setFqdn(DOMAIN_0.domain);
2016-01-24 00:06:32 -08:00
config.set('version', '1.0.0');
2017-04-14 01:13:42 -07:00
config.set('apiServerOrigin', 'http://localhost:4444');
2017-04-13 23:19:37 -07:00
config.set('provider', 'notcaas');
2017-02-06 16:33:55 -08:00
safe.fs.unlinkSync(paths.UPDATE_CHECKER_FILE);
2016-01-23 15:57:56 -08:00
async.series([
2016-04-12 13:12:17 -07:00
database.initialize,
2017-04-13 23:19:37 -07:00
database._clear,
settings.initialize,
2017-01-27 09:40:46 -08:00
user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
2017-02-06 16:33:55 -08:00
settings.setAutoupdatePattern.bind(null, constants.AUTOUPDATE_PATTERN_NEVER),
2017-04-13 23:19:37 -07:00
settingsdb.set.bind(null, settings.APPSTORE_CONFIG_KEY, JSON.stringify({ userId: 'uid', cloudronId: 'cid', token: 'token' })),
2017-10-25 20:52:05 -07:00
mailer._clearMailQueue,
mailer.start
2016-01-23 15:57:56 -08:00
], done);
});
after(cleanup);
2016-01-23 15:57:56 -08:00
2016-01-24 00:06:32 -08:00
it('no updates', function (done) {
2016-01-23 15:57:56 -08:00
nock.cleanAll();
var scope = nock('http://localhost:4444')
2017-04-13 23:19:37 -07:00
.get('/api/v1/users/uid/cloudrons/cid/boxupdate')
.query({ boxVersion: config.version(), accessToken: 'token' })
.reply(204, { } );
2016-01-23 15:57:56 -08:00
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
2016-01-24 00:06:32 -08:00
expect(updatechecker.getUpdateInfo().box).to.be(null);
2017-01-27 09:40:46 -08:00
expect(scope.isDone()).to.be.ok();
2016-04-12 13:15:40 -07:00
checkMails(0, done);
2016-01-24 00:06:32 -08:00
});
});
it('new version', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
2017-04-13 23:19:37 -07:00
.get('/api/v1/users/uid/cloudrons/cid/boxupdate')
.query({ boxVersion: config.version(), accessToken: 'token' })
.reply(200, { version: '2.0.0', changelog: [''], sourceTarballUrl: '2.0.0.tar.gz' } );
2016-01-23 15:57:56 -08:00
var scope2 = nock('http://localhost:4444')
.get('/api/v1/users/uid/cloudrons/cid/subscription')
.query({ accessToken: 'token' })
.reply(200, { subscription: { plan: { id: 'pro' } } } );
2016-01-24 00:06:32 -08:00
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().box.version).to.be('2.0.0');
2017-04-13 23:19:37 -07:00
expect(updatechecker.getUpdateInfo().box.sourceTarballUrl).to.be('2.0.0.tar.gz');
2017-01-27 09:40:46 -08:00
expect(scope.isDone()).to.be.ok();
expect(scope2.isDone()).to.be.ok();
2016-04-12 13:15:40 -07:00
checkMails(1, done);
2016-01-24 00:06:32 -08:00
});
});
2017-10-25 20:52:05 -07:00
it('offers prerelease', function (done) {
2016-01-24 00:06:32 -08:00
nock.cleanAll();
var scope = nock('http://localhost:4444')
2017-04-13 23:19:37 -07:00
.get('/api/v1/users/uid/cloudrons/cid/boxupdate')
.query({ boxVersion: config.version(), accessToken: 'token' })
.reply(200, { version: '2.0.0-pre.0', changelog: [''], sourceTarballUrl: '2.0.0-pre.0.tar.gz' } );
2016-01-24 00:06:32 -08:00
2017-10-25 20:52:05 -07:00
var scope2 = nock('http://localhost:4444')
.get('/api/v1/users/uid/cloudrons/cid/subscription')
.query({ accessToken: 'token' })
.reply(200, { subscription: { plan: { id: 'pro' } } } );
2016-01-24 00:06:32 -08:00
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
2017-10-25 20:52:05 -07:00
expect(updatechecker.getUpdateInfo().box.version).to.be('2.0.0-pre.0');
2017-01-27 09:40:46 -08:00
expect(scope.isDone()).to.be.ok();
2017-10-25 20:52:05 -07:00
expect(scope2.isDone()).to.be.ok();
2017-01-27 09:40:46 -08:00
2017-10-25 20:52:05 -07:00
checkMails(1, done);
2016-01-24 00:06:32 -08:00
});
});
it('bad response offers nothing', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
2017-04-13 23:19:37 -07:00
.get('/api/v1/users/uid/cloudrons/cid/boxupdate')
.query({ boxVersion: config.version(), accessToken: 'token' })
.reply(404, { version: '2.0.0-pre.0', changelog: [''], sourceTarballUrl: '2.0.0-pre.0.tar.gz' } );
2016-01-24 00:06:32 -08:00
updatechecker.checkBoxUpdates(function (error) {
expect(error).to.be.ok();
expect(updatechecker.getUpdateInfo().box).to.be(null);
2017-01-27 09:40:46 -08:00
expect(scope.isDone()).to.be.ok();
checkMails(0, done);
2016-01-24 00:06:32 -08:00
});
});
});
2016-01-24 00:44:46 -08:00
describe('updatechecker - box - automatic (no email)', function () {
2017-01-27 09:50:39 -08:00
before(function (done) {
2017-11-11 03:06:57 +01:00
config.setFqdn(DOMAIN_0.domain);
2017-01-27 09:50:39 -08:00
config.set('version', '1.0.0');
2017-04-14 01:13:42 -07:00
config.set('apiServerOrigin', 'http://localhost:4444');
2017-04-13 23:19:37 -07:00
config.set('provider', 'notcaas');
2017-11-11 03:06:57 +01:00
2017-01-27 09:50:39 -08:00
async.series([
database.initialize,
settings.initialize,
2017-01-27 09:50:39 -08:00
mailer._clearMailQueue,
2017-10-25 20:52:05 -07:00
mailer.start,
2017-04-13 23:19:37 -07:00
user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
settingsdb.set.bind(null, settings.APPSTORE_CONFIG_KEY, JSON.stringify({ userId: 'uid', cloudronId: 'cid', token: 'token' }))
2017-01-27 09:50:39 -08:00
], done);
});
after(cleanup);
2017-01-27 09:50:39 -08:00
it('new version', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
2017-04-13 23:19:37 -07:00
.get('/api/v1/users/uid/cloudrons/cid/boxupdate')
.query({ boxVersion: config.version(), accessToken: 'token' })
.reply(200, { version: '2.0.0', sourceTarballUrl: '2.0.0.tar.gz' } );
2017-01-27 09:50:39 -08:00
var scope2 = nock('http://localhost:4444')
.get('/api/v1/users/uid/cloudrons/cid/subscription')
.query({ accessToken: 'token' })
.reply(200, { subscription: { plan: { id: 'pro' } } } );
2017-01-27 09:50:39 -08:00
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().box.version).to.be('2.0.0');
expect(scope.isDone()).to.be.ok();
expect(scope2.isDone()).to.be.ok();
2017-01-27 09:50:39 -08:00
checkMails(0, done);
});
});
});
describe('updatechecker - box - automatic free (email)', function () {
before(function (done) {
2017-11-11 03:06:57 +01:00
config.setFqdn(DOMAIN_0.domain);
config.set('version', '1.0.0');
config.set('apiServerOrigin', 'http://localhost:4444');
config.set('provider', 'notcaas');
2017-11-11 03:06:57 +01:00
async.series([
database.initialize,
settings.initialize,
mailer._clearMailQueue,
user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
settingsdb.set.bind(null, settings.APPSTORE_CONFIG_KEY, JSON.stringify({ userId: 'uid', cloudronId: 'cid', token: 'token' }))
], done);
});
after(cleanup);
it('new version', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.get('/api/v1/users/uid/cloudrons/cid/boxupdate')
.query({ boxVersion: config.version(), accessToken: 'token' })
.reply(200, { version: '2.0.0', changelog: [''], sourceTarballUrl: '2.0.0.tar.gz' } );
var scope2 = nock('http://localhost:4444')
.get('/api/v1/users/uid/cloudrons/cid/subscription')
.query({ accessToken: 'token' })
.reply(200, { subscription: { plan: { id: 'free' } } } );
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().box.version).to.be('2.0.0');
expect(scope.isDone()).to.be.ok();
expect(scope2.isDone()).to.be.ok();
checkMails(1, done);
});
});
});
describe('updatechecker - app - manual (email)', function () {
2016-01-24 00:44:46 -08:00
var APP_0 = {
id: 'appid-0',
appStoreId: 'io.cloudron.app',
installationState: appdb.ISTATE_PENDING_INSTALL,
installationProgress: null,
runState: null,
location: 'some-location-0',
2017-11-11 03:06:57 +01:00
domain: DOMAIN_0.domain,
2016-01-24 00:44:46 -08:00
manifest: {
version: '1.0.0', dockerImage: 'docker/app0', healthCheckPath: '/', httpPort: 80, title: 'app0',
tcpPorts: {
PORT: {
description: 'this is a port that i expose',
containerPort: '1234'
}
}
},
httpPort: null,
containerId: null,
portBindings: { PORT: 5678 },
healthy: null,
accessRestriction: null,
2016-02-10 12:28:57 +01:00
memoryLimit: 0
2016-01-24 00:44:46 -08:00
};
before(function (done) {
2017-11-11 03:06:57 +01:00
config.setFqdn(DOMAIN_0.domain);
2016-01-24 00:44:46 -08:00
config.set('version', '1.0.0');
config.set('apiServerOrigin', 'http://localhost:4444');
2017-04-13 23:19:37 -07:00
config.set('provider', 'notcaas');
2016-01-24 00:44:46 -08:00
async.series([
database.initialize,
database._clear,
settings.initialize,
mailer._clearMailQueue,
2017-11-11 03:06:57 +01:00
appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, APP_0.portBindings, APP_0),
2017-01-27 09:40:46 -08:00
user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
2017-04-13 23:19:37 -07:00
settings.setAutoupdatePattern.bind(null, constants.AUTOUPDATE_PATTERN_NEVER),
settingsdb.set.bind(null, settings.APPSTORE_CONFIG_KEY, JSON.stringify({ userId: 'uid', cloudronId: 'cid', token: 'token' }))
2016-01-24 00:44:46 -08:00
], done);
});
after(cleanup);
2016-01-24 00:44:46 -08:00
it('no updates', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
2017-04-13 23:19:37 -07:00
.get('/api/v1/users/uid/cloudrons/cid/appupdate')
.query({ boxVersion: config.version(), accessToken: 'token', appId: APP_0.appStoreId, appVersion: APP_0.manifest.version })
.reply(204, { } );
2016-01-24 00:44:46 -08:00
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({});
2017-01-27 09:40:46 -08:00
expect(scope.isDone()).to.be.ok();
checkMails(0, done);
2016-01-24 00:44:46 -08:00
});
});
it('bad response', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
2017-04-13 23:19:37 -07:00
.get('/api/v1/users/uid/cloudrons/cid/appupdate')
.query({ boxVersion: config.version(), accessToken: 'token', appId: APP_0.appStoreId, appVersion: APP_0.manifest.version })
.reply(500, { update: { manifest: { version: '1.0.0' } } } );
2016-01-24 00:44:46 -08:00
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({});
2017-01-27 09:40:46 -08:00
expect(scope.isDone()).to.be.ok();
checkMails(0, done);
2016-01-24 00:44:46 -08:00
});
});
it('offers new version', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
2017-04-13 23:19:37 -07:00
.get('/api/v1/users/uid/cloudrons/cid/appupdate')
.query({ boxVersion: config.version(), accessToken: 'token', appId: APP_0.appStoreId, appVersion: APP_0.manifest.version })
.reply(200, { manifest: { version: '2.0.0' } } );
2016-01-24 00:44:46 -08:00
var scope2 = nock('http://localhost:4444')
.get('/api/v1/users/uid/cloudrons/cid/subscription')
.query({ accessToken: 'token' })
.reply(200, { subscription: { plan: { id: 'pro' } } } );
2016-01-24 00:44:46 -08:00
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({ 'appid-0': { manifest: { version: '2.0.0' } } });
2017-01-27 09:40:46 -08:00
expect(scope.isDone()).to.be.ok();
expect(scope2.isDone()).to.be.ok();
2017-01-27 09:40:46 -08:00
checkMails(1, done);
2016-01-24 00:44:46 -08:00
});
});
it('does not offer old version', function (done) {
nock.cleanAll();
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({ });
checkMails(0, done);
2016-01-24 00:44:46 -08:00
});
});
});
2017-01-27 09:50:39 -08:00
describe('updatechecker - app - automatic (no email)', function () {
2017-01-27 09:50:39 -08:00
var APP_0 = {
id: 'appid-0',
appStoreId: 'io.cloudron.app',
installationState: appdb.ISTATE_PENDING_INSTALL,
installationProgress: null,
runState: null,
location: 'some-location-0',
2017-11-11 03:06:57 +01:00
domain: DOMAIN_0.domain,
2017-01-27 09:50:39 -08:00
manifest: {
version: '1.0.0', dockerImage: 'docker/app0', healthCheckPath: '/', httpPort: 80, title: 'app0',
tcpPorts: {
PORT: {
description: 'this is a port that i expose',
containerPort: '1234'
}
}
},
httpPort: null,
containerId: null,
portBindings: { PORT: 5678 },
healthy: null,
accessRestriction: null,
memoryLimit: 0
};
before(function (done) {
2017-11-11 03:06:57 +01:00
config.setFqdn(DOMAIN_0.domain);
2017-01-27 09:50:39 -08:00
config.set('version', '1.0.0');
config.set('apiServerOrigin', 'http://localhost:4444');
2017-04-13 23:19:37 -07:00
config.set('provider', 'notcaas');
2017-01-27 09:50:39 -08:00
async.series([
database.initialize,
database._clear,
settings.initialize,
2017-01-27 09:50:39 -08:00
mailer._clearMailQueue,
2017-11-11 03:06:57 +01:00
appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, APP_0.portBindings, APP_0),
2017-04-13 23:19:37 -07:00
user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
settingsdb.set.bind(null, settings.APPSTORE_CONFIG_KEY, JSON.stringify({ userId: 'uid', cloudronId: 'cid', token: 'token' }))
2017-01-27 09:50:39 -08:00
], done);
});
after(cleanup);
2017-01-27 09:50:39 -08:00
it('offers new version', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
2017-04-13 23:19:37 -07:00
.get('/api/v1/users/uid/cloudrons/cid/appupdate')
.query({ boxVersion: config.version(), accessToken: 'token', appId: APP_0.appStoreId, appVersion: APP_0.manifest.version })
.reply(200, { manifest: { version: '2.0.0' } } );
2017-01-27 09:50:39 -08:00
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({ 'appid-0': { manifest: { version: '2.0.0' } } });
expect(scope.isDone()).to.be.ok();
checkMails(0, done);
});
});
});
describe('updatechecker - app - automatic free (email)', function () {
var APP_0 = {
id: 'appid-0',
appStoreId: 'io.cloudron.app',
installationState: appdb.ISTATE_PENDING_INSTALL,
installationProgress: null,
runState: null,
location: 'some-location-0',
2017-11-11 03:06:57 +01:00
domain: DOMAIN_0.domain,
manifest: {
version: '1.0.0', dockerImage: 'docker/app0', healthCheckPath: '/', httpPort: 80, title: 'app0',
tcpPorts: {
PORT: {
description: 'this is a port that i expose',
containerPort: '1234'
}
}
},
httpPort: null,
containerId: null,
portBindings: { PORT: 5678 },
healthy: null,
accessRestriction: null,
memoryLimit: 0
};
before(function (done) {
2017-11-11 03:06:57 +01:00
config.setFqdn(DOMAIN_0.domain);
config.set('version', '1.0.0');
config.set('apiServerOrigin', 'http://localhost:4444');
config.set('provider', 'notcaas');
async.series([
database.initialize,
database._clear,
settings.initialize,
mailer._clearMailQueue,
2017-11-11 03:06:57 +01:00
appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, APP_0.portBindings, APP_0),
user.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
settingsdb.set.bind(null, settings.APPSTORE_CONFIG_KEY, JSON.stringify({ userId: 'uid', cloudronId: 'cid', token: 'token' }))
], done);
});
after(cleanup);
it('offers new version', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.get('/api/v1/users/uid/cloudrons/cid/appupdate')
.query({ boxVersion: config.version(), accessToken: 'token', appId: APP_0.appStoreId, appVersion: APP_0.manifest.version })
.reply(200, { manifest: { version: '2.0.0' } } );
var scope2 = nock('http://localhost:4444')
.get('/api/v1/users/uid/cloudrons/cid/subscription')
.query({ accessToken: 'token' })
.reply(200, { subscription: { plan: { id: 'free' } } } );
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({ 'appid-0': { manifest: { version: '2.0.0' } } });
expect(scope.isDone()).to.be.ok();
expect(scope2.isDone()).to.be.ok();
checkMails(1, done);
});
});
});