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

446 lines
16 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'),
apps = require('../apps.js'),
2016-01-23 15:57:56 -08:00
async = require('async'),
2017-01-27 09:40:46 -08:00
constants = require('../constants.js'),
cron = require('../cron.js'),
2016-01-23 15:57:56 -08:00
database = require('../database.js'),
domains = require('../domains.js'),
2016-01-23 15:57:56 -08:00
expect = require('expect.js'),
mail = require('../mail.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'),
semver = require('semver'),
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'),
2018-04-29 10:58:45 -07:00
users = require('../users.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',
fallbackCertificate: null,
tlsConfig: { provider: 'fallback' }
2017-11-11 03:06:57 +01:00
};
2016-05-01 20:01:34 -07:00
var AUDIT_SOURCE = {
ip: '1.2.3.4'
};
const UPDATE_VERSION = semver.inc(constants.VERSION, 'major');
2016-04-12 13:15:40 -07:00
function checkMails(number, done) {
// mails are enqueued async
setTimeout(function () {
expect(mailer._mailQueue.length).to.equal(number);
mailer._mailQueue = [];
2016-04-12 13:15:40 -07:00
done();
}, 500);
}
function cleanup(done) {
mailer._mailQueue = [];
safe.fs.unlinkSync(paths.UPDATE_CHECKER_FILE);
async.series([
cron.stopJobs,
database._clear,
database.uninitialize
], done);
}
describe('updatechecker - box - manual (email)', function () {
2016-01-23 15:57:56 -08:00
before(function (done) {
2017-02-06 16:33:55 -08:00
safe.fs.unlinkSync(paths.UPDATE_CHECKER_FILE);
mailer._mailQueue = [];
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._setApiServerOrigin.bind(null, 'http://localhost:4444'),
2019-05-09 15:09:55 +02:00
cron.startJobs,
2018-11-10 01:28:59 -08:00
domains.add.bind(null, DOMAIN_0.domain, DOMAIN_0, AUDIT_SOURCE),
2018-04-03 14:37:52 -07:00
mail.addDomain.bind(null, DOMAIN_0.domain),
2018-04-29 10:58:45 -07:00
users.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
2018-02-06 19:21:44 +01:00
settings.setBoxAutoupdatePattern.bind(null, constants.AUTOUPDATE_PATTERN_NEVER),
settingsdb.set.bind(null, settings.CLOUDRON_TOKEN_KEY, 'atoken'),
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')
.get('/api/v1/boxupdate')
.query({ boxVersion: constants.VERSION, accessToken: 'atoken' })
2017-04-13 23:19:37 -07:00
.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')
.get('/api/v1/boxupdate')
.query({ boxVersion: constants.VERSION, accessToken: 'atoken' })
.reply(200, { version: UPDATE_VERSION, changelog: [''], sourceTarballUrl: 'box.tar.gz', sourceTarballSigUrl: 'box.tar.gz.sig', boxVersionsUrl: 'box.versions', boxVersionsSigUrl: 'box.versions.sig' } );
2016-01-24 00:06:32 -08:00
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().box.version).to.be(UPDATE_VERSION);
expect(updatechecker.getUpdateInfo().box.sourceTarballUrl).to.be('box.tar.gz');
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('bad response offers nothing', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.get('/api/v1/boxupdate')
.query({ boxVersion: constants.VERSION, accessToken: 'atoken' })
.reply(404, { version: '2.0.0-pre.0', changelog: [''], sourceTarballUrl: 'box-pre.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) {
mailer._mailQueue = [];
2017-01-27 09:50:39 -08:00
async.series([
database.initialize,
settings._setApiServerOrigin.bind(null, 'http://localhost:4444'),
2019-05-09 15:09:55 +02:00
cron.startJobs,
2018-11-10 01:28:59 -08:00
domains.add.bind(null, DOMAIN_0.domain, DOMAIN_0, AUDIT_SOURCE),
2018-04-03 14:37:52 -07:00
mail.addDomain.bind(null, DOMAIN_0.domain),
2018-04-29 10:58:45 -07:00
users.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
settingsdb.set.bind(null, settings.CLOUDRON_TOKEN_KEY, 'atoken'),
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')
.get('/api/v1/boxupdate')
.query({ boxVersion: constants.VERSION, accessToken: 'atoken' })
.reply(200, { version: UPDATE_VERSION, changelog: [''], sourceTarballUrl: 'box.tar.gz', sourceTarballSigUrl: 'box.tar.gz.sig', boxVersionsUrl: 'box.versions', boxVersionsSigUrl: 'box.versions.sig' } );
2017-01-27 09:50:39 -08:00
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().box.version).to.be(UPDATE_VERSION);
2017-01-27 09:50:39 -08:00
expect(scope.isDone()).to.be.ok();
checkMails(0, done);
});
});
});
describe('updatechecker - box - automatic free (email)', function () {
before(function (done) {
mailer._mailQueue = [];
async.series([
database.initialize,
settings._setApiServerOrigin.bind(null, 'http://localhost:4444'),
2019-05-09 15:09:55 +02:00
cron.startJobs,
2018-11-10 01:28:59 -08:00
domains.add.bind(null, DOMAIN_0.domain, DOMAIN_0, AUDIT_SOURCE),
2018-04-03 14:37:52 -07:00
mail.addDomain.bind(null, DOMAIN_0.domain),
2018-04-29 10:58:45 -07:00
users.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
settingsdb.set.bind(null, settings.CLOUDRON_TOKEN_KEY, 'atoken'),
], done);
});
after(cleanup);
it('new version', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.get('/api/v1/boxupdate')
.query({ boxVersion: constants.VERSION, accessToken: 'atoken' })
.reply(200, { version: UPDATE_VERSION, changelog: [''], sourceTarballUrl: 'box.tar.gz', sourceTarballSigUrl: 'box.tar.gz.sig', boxVersionsUrl: 'box.versions', boxVersionsSigUrl: 'box.versions.sig' } );
updatechecker.checkBoxUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().box.version).to.be(UPDATE_VERSION);
expect(scope.isDone()).to.be.ok();
checkMails(0, 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,
2019-08-30 09:45:43 -07:00
error: null,
2016-01-24 00:44:46 -08:00
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,
2019-07-02 20:22:17 -07:00
memoryLimit: 0
2016-01-24 00:44:46 -08:00
};
before(function (done) {
mailer._mailQueue = [];
2016-01-24 00:44:46 -08:00
async.series([
database.initialize,
database._clear,
settings._setApiServerOrigin.bind(null, 'http://localhost:4444'),
2019-05-09 15:09:55 +02:00
cron.startJobs,
2018-11-10 01:28:59 -08:00
domains.add.bind(null, DOMAIN_0.domain, DOMAIN_0, AUDIT_SOURCE),
2018-04-03 14:37:52 -07:00
mail.addDomain.bind(null, DOMAIN_0.domain),
2019-07-02 20:22:17 -07:00
users.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, apps._translatePortBindings(APP_0.portBindings, APP_0.manifest), APP_0),
2018-02-06 19:21:44 +01:00
settings.setAppAutoupdatePattern.bind(null, constants.AUTOUPDATE_PATTERN_NEVER),
settingsdb.set.bind(null, settings.CLOUDRON_TOKEN_KEY, 'atoken'),
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')
.get('/api/v1/appupdate')
.query({ boxVersion: constants.VERSION, accessToken: 'atoken', appId: APP_0.appStoreId, appVersion: APP_0.manifest.version })
2017-04-13 23:19:37 -07:00
.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')
.get('/api/v1/appupdate')
.query({ boxVersion: constants.VERSION, accessToken: 'atoken', appId: APP_0.appStoreId, appVersion: APP_0.manifest.version })
.reply(500, { update: { manifest: { version: '1.0.0', changelog: '* some changes' } } } );
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')
.get('/api/v1/appupdate')
.query({ boxVersion: constants.VERSION, accessToken: 'atoken', appId: APP_0.appStoreId, appVersion: APP_0.manifest.version })
.reply(200, { manifest: { version: '2.0.0', changelog: '* some changes' } } );
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', changelog: '* some changes' } } });
2017-01-27 09:40:46 -08:00
expect(scope.isDone()).to.be.ok();
2019-05-03 20:05:03 -07: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,
2019-08-30 09:45:43 -07:00
error: null,
2017-01-27 09:50:39 -08:00
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,
2019-07-02 20:22:17 -07:00
memoryLimit: 0
2017-01-27 09:50:39 -08:00
};
before(function (done) {
mailer._mailQueue = [];
2017-01-27 09:50:39 -08:00
async.series([
database.initialize,
database._clear,
settings._setApiServerOrigin.bind(null, 'http://localhost:4444'),
2019-05-09 15:09:55 +02:00
cron.startJobs,
2018-11-10 01:28:59 -08:00
domains.add.bind(null, DOMAIN_0.domain, DOMAIN_0, AUDIT_SOURCE),
2018-04-03 14:37:52 -07:00
mail.addDomain.bind(null, DOMAIN_0.domain),
2019-07-02 20:22:17 -07:00
users.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, apps._translatePortBindings(APP_0.portBindings, APP_0.manifest), APP_0),
2018-02-06 19:21:44 +01:00
settings.setAppAutoupdatePattern.bind(null, '00 00 1,3,5,23 * * *'),
settingsdb.set.bind(null, settings.CLOUDRON_TOKEN_KEY, 'atoken'),
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')
.get('/api/v1/appupdate')
.query({ boxVersion: constants.VERSION, accessToken: 'atoken', appId: APP_0.appStoreId, appVersion: APP_0.manifest.version })
.reply(200, { manifest: { version: '2.0.0', changelog: 'c' } } );
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', changelog: 'c' } } });
2017-01-27 09:50:39 -08:00
expect(scope.isDone()).to.be.ok();
2019-05-06 19:53:54 -07:00
checkMails(1, done);
2017-01-27 09:50:39 -08:00
});
});
});
describe('updatechecker - app - automatic free (email)', function () {
var APP_0 = {
id: 'appid-0',
appStoreId: 'io.cloudron.app',
installationState: appdb.ISTATE_PENDING_INSTALL,
2019-08-30 09:45:43 -07:00
error: 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,
2019-07-02 20:22:17 -07:00
memoryLimit: 0
};
before(function (done) {
mailer._mailQueue = [];
async.series([
database.initialize,
database._clear,
settings._setApiServerOrigin.bind(null, 'http://localhost:4444'),
2019-05-09 15:09:55 +02:00
cron.startJobs,
2018-11-10 01:28:59 -08:00
domains.add.bind(null, DOMAIN_0.domain, DOMAIN_0, AUDIT_SOURCE),
2018-04-03 14:37:52 -07:00
mail.addDomain.bind(null, DOMAIN_0.domain),
2019-07-02 20:22:17 -07:00
users.createOwner.bind(null, USER_0.username, USER_0.password, USER_0.email, USER_0.displayName, AUDIT_SOURCE),
appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, apps._translatePortBindings(APP_0.portBindings, APP_0.manifest), APP_0),
2018-02-06 19:21:44 +01:00
settings.setAppAutoupdatePattern.bind(null, '00 00 1,3,5,23 * * *'),
settingsdb.set.bind(null, settings.CLOUDRON_TOKEN_KEY, 'atoken'),
], done);
});
after(cleanup);
it('offers new version', function (done) {
nock.cleanAll();
var scope = nock('http://localhost:4444')
.get('/api/v1/appupdate')
.query({ boxVersion: constants.VERSION, accessToken: 'atoken', appId: APP_0.appStoreId, appVersion: APP_0.manifest.version })
.reply(200, { manifest: { version: '2.0.0', changelog: 'c' } } );
updatechecker.checkAppUpdates(function (error) {
expect(!error).to.be.ok();
expect(updatechecker.getUpdateInfo().apps).to.eql({ 'appid-0': { manifest: { version: '2.0.0', changelog: 'c' } } });
expect(scope.isDone()).to.be.ok();
2019-05-06 19:53:54 -07:00
checkMails(1, done);
});
});
});