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

791 lines
28 KiB
JavaScript
Raw Normal View History

/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
2021-05-04 21:40:11 -07:00
const appdb = require('../appdb.js'),
2019-08-30 13:12:49 -07:00
apps = require('../apps.js'),
2016-04-30 10:16:27 -07:00
async = require('async'),
BoxError = require('../boxerror.js'),
database = require('../database'),
2017-10-28 21:42:32 +02:00
domaindb = require('../domaindb'),
expect = require('expect.js'),
2016-05-26 22:34:04 -07:00
mailboxdb = require('../mailboxdb.js'),
2021-05-04 21:40:11 -07:00
reverseProxy = require('../reverseproxy.js'),
settingsdb = require('../settingsdb.js'),
_ = require('underscore');
2018-01-26 18:32:13 +01:00
const DOMAIN_0 = {
domain: 'foobar.com',
zoneName: 'foobar.com',
provider: 'digitalocean',
config: { token: 'abcd' },
tlsConfig: { provider: 'fallback' },
wellKnown: null
2018-01-26 18:32:13 +01:00
};
2021-05-04 21:40:11 -07:00
DOMAIN_0.fallbackCertificate = reverseProxy.generateFallbackCertificateSync(DOMAIN_0.domain);
2018-01-26 18:32:13 +01:00
const DOMAIN_1 = {
domain: 'foo.cloudron.io',
zoneName: 'cloudron.io',
provider: 'manual',
config: null,
tlsConfig: { provider: 'fallback' },
wellKnown: null
2018-01-26 18:32:13 +01:00
};
2021-05-04 21:40:11 -07:00
DOMAIN_1.fallbackCertificate = reverseProxy.generateFallbackCertificateSync(DOMAIN_1.domain);
2018-01-26 18:32:13 +01:00
describe('database', function () {
before(function (done) {
async.series([
database.initialize,
database._clear
], done);
});
after(function (done) {
async.series([
database._clear,
database.uninitialize
], done);
});
2017-10-28 21:42:32 +02:00
describe('domains', function () {
after(function (done) {
database._clear(done);
});
2017-10-28 21:42:32 +02:00
it('can add domain', function (done) {
2021-05-04 21:40:11 -07:00
domaindb.add(DOMAIN_0.domain, DOMAIN_0, done);
2017-10-28 21:42:32 +02:00
});
it('can add another domain', function (done) {
2021-05-04 21:40:11 -07:00
domaindb.add(DOMAIN_1.domain, DOMAIN_1, done);
2017-10-28 21:42:32 +02:00
});
it('cannot add same domain twice', function (done) {
2021-05-04 21:40:11 -07:00
domaindb.add(DOMAIN_0.domain, DOMAIN_0, function (error) {
2017-10-28 21:42:32 +02:00
expect(error).to.be.ok();
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
2017-10-28 21:42:32 +02:00
done();
});
});
it('can get domain', function (done) {
domaindb.get(DOMAIN_0.domain, function (error, result) {
expect(error).to.equal(null);
expect(result).to.be.an('object');
expect(result.domain).to.equal(DOMAIN_0.domain);
expect(result.zoneName).to.equal(DOMAIN_0.zoneName);
expect(result.config).to.eql(DOMAIN_0.config);
done();
});
});
it('can update domain', function (done) {
const newConfig = { provider: 'manual' };
const newTlsConfig = { provider: 'foobar' };
2017-10-28 21:42:32 +02:00
domaindb.update(DOMAIN_1.domain, { provider: DOMAIN_1.provider, config: newConfig, tlsConfig: newTlsConfig }, function (error) {
2017-10-28 21:42:32 +02:00
expect(error).to.equal(null);
domaindb.get(DOMAIN_1.domain, function (error, result) {
expect(error).to.equal(null);
expect(result).to.be.an('object');
expect(result.domain).to.equal(DOMAIN_1.domain);
expect(result.zoneName).to.equal(DOMAIN_1.zoneName);
2018-01-09 14:46:38 -08:00
expect(result.provider).to.equal(DOMAIN_1.provider);
2017-10-28 21:42:32 +02:00
expect(result.config).to.eql(newConfig);
expect(result.tlsConfig).to.eql(newTlsConfig);
2017-10-28 21:42:32 +02:00
DOMAIN_1.config = newConfig;
DOMAIN_1.tlsConfig = newTlsConfig;
2017-10-28 21:42:32 +02:00
done();
});
});
});
it('can get all domains', function (done) {
domaindb.getAll(function (error, result) {
expect(error).to.equal(null);
expect(result).to.be.an('array');
2018-01-26 18:32:13 +01:00
expect(result.length).to.equal(2);
2017-10-28 21:42:32 +02:00
// sorted by domain
2018-01-26 18:32:13 +01:00
expect(result[0].domain).to.equal(DOMAIN_1.domain);
expect(result[0].zoneName).to.equal(DOMAIN_1.zoneName);
expect(result[0].provider).to.equal(DOMAIN_1.provider);
expect(result[0].config).to.eql(DOMAIN_1.config);
expect(result[0].tlsConfig).to.eql(DOMAIN_1.tlsConfig);
2017-10-28 21:42:32 +02:00
2018-01-26 18:32:13 +01:00
expect(result[1].domain).to.equal(DOMAIN_0.domain);
expect(result[1].zoneName).to.equal(DOMAIN_0.zoneName);
expect(result[1].provider).to.equal(DOMAIN_0.provider);
expect(result[1].config).to.eql(DOMAIN_0.config);
expect(result[1].tlsConfig).to.eql(DOMAIN_0.tlsConfig);
2017-10-28 21:42:32 +02:00
done();
});
});
it('cannot delete non-existing domain', function (done) {
domaindb.del('not.exists', function (error) {
expect(error).to.be.a(BoxError);
expect(error.reason).to.equal(BoxError.NOT_FOUND);
2017-10-28 21:42:32 +02:00
done();
});
});
var APP_0 = {
id: 'appid-0',
appStoreId: 'appStoreId-0',
2019-08-30 13:12:49 -07:00
installationState: apps.ISTATE_PENDING_INSTALL,
2019-08-30 09:45:43 -07:00
error: null,
2019-09-22 22:07:14 -07:00
runState: 'running',
location: 'some-location-0',
domain: DOMAIN_0.domain,
manifest: { version: '0.1', dockerImage: 'docker/app0', healthCheckPath: '/', httpPort: 80, title: 'app0' },
containerId: null,
containerIp: null,
portBindings: { port: { hostPort: 5678, type: 'tcp' } },
health: null,
accessRestriction: null,
lastBackupId: null,
memoryLimit: 4294967296,
2020-01-28 21:30:35 -08:00
cpuShares: 1024,
sso: true,
debugMode: null,
reverseProxyConfig: {},
enableBackup: true,
rework how app mailboxes are allocated Our current setup had a mailbox allocated for an app during app install (into the mailboxes table). This has many issues: * When set to a custom mailbox location, there was no way to access this mailbox even via IMAP. Even when using app credentials, we cannot use IMAP since the ldap logic was testing on the addon type (most of our apps only use sendmail addon and thus cannot recvmail). * The mailboxes table was being used to add hidden 'app' type entries. This made it very hard for the user to understand why a mailbox conflicts. For example, if you set an app to use custom mailbox 'blog', this is hidden from all views. The solution is to let an app send email as whatever mailbox name is allocated to it (which we now track in the apps table. the default is in the db already so that REST response contains it). When not using Cloudron email, it will just send mail as that mailbox and the auth checks the "app password" in the addons table. Any replies to that mailbox will end up in the domain's mail server (not our problem). When using cloudron email, the app can send mail like above. Any responses will not end anywhere and bounce since there is no 'mailbox'. This is the expected behavior. If user wants to access this mailbox name, he can create a concrete mailbox and set himself as owner OR set this as an alias. For apps using the recvmail addon, the workflow is to actually create a mailbox at some point. Currently, we have no UI for this 'flow'. It's fine because we have only meemo using it. Intuitive much!
2018-12-06 21:08:19 -08:00
env: {},
mailboxName: 'talktome',
2019-11-14 21:43:14 -08:00
mailboxDomain: DOMAIN_0.domain,
enableAutomaticUpdate: true,
2019-09-15 21:51:38 -07:00
dataDir: null,
2019-03-22 14:26:25 -07:00
tags: [],
2019-08-26 15:28:29 -07:00
label: null,
taskId: null,
2020-10-28 19:42:48 -07:00
mounts: [],
2020-11-10 09:59:28 -08:00
proxyAuth: false,
servicesConfig: {},
hasIcon: false,
hasAppStoreIcon: false
};
it('cannot delete referenced domain', function (done) {
2019-07-02 20:22:17 -07:00
appdb.add(APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, APP_0.portBindings, APP_0, function (error) {
expect(error).to.be(null);
domaindb.del(DOMAIN_0.domain, function (error) {
expect(error).to.be.a(BoxError);
expect(error.reason).to.equal(BoxError.CONFLICT);
appdb.del(APP_0.id, done);
});
});
});
2017-10-28 21:42:32 +02:00
it('can delete existing domain', function (done) {
domaindb.del(DOMAIN_0.domain, function (error) {
expect(error).to.be(null);
domaindb.get(DOMAIN_0.domain, function (error) {
expect(error).to.be.a(BoxError);
expect(error.reason).to.equal(BoxError.NOT_FOUND);
2017-10-28 21:42:32 +02:00
done();
});
});
});
});
describe('apps', function () {
var APP_0 = {
id: 'appid-0',
appStoreId: 'appStoreId-0',
2019-08-30 13:12:49 -07:00
installationState: apps.ISTATE_PENDING_INSTALL,
2019-08-30 09:45:43 -07:00
error: null,
2019-09-22 22:07:14 -07:00
runState: 'running',
location: 'some-location-0',
2018-01-26 18:32:13 +01:00
domain: DOMAIN_0.domain,
manifest: { version: '0.1', dockerImage: 'docker/app0', healthCheckPath: '/', httpPort: 80, title: 'app0' },
containerId: null,
containerIp: null,
portBindings: { port: { hostPort: 5678, type: 'tcp' } },
health: null,
accessRestriction: null,
2016-04-19 00:10:11 -07:00
memoryLimit: 4294967296,
2020-01-28 21:30:35 -08:00
cpuShares: 256,
2017-01-19 12:51:16 -08:00
sso: true,
debugMode: null,
reverseProxyConfig: {},
enableBackup: true,
2021-03-17 12:14:36 -07:00
enableMailbox: true,
alternateDomains: [],
2021-01-19 19:20:26 -08:00
aliasDomains: [],
env: {
'CUSTOM_KEY': 'CUSTOM_VALUE'
rework how app mailboxes are allocated Our current setup had a mailbox allocated for an app during app install (into the mailboxes table). This has many issues: * When set to a custom mailbox location, there was no way to access this mailbox even via IMAP. Even when using app credentials, we cannot use IMAP since the ldap logic was testing on the addon type (most of our apps only use sendmail addon and thus cannot recvmail). * The mailboxes table was being used to add hidden 'app' type entries. This made it very hard for the user to understand why a mailbox conflicts. For example, if you set an app to use custom mailbox 'blog', this is hidden from all views. The solution is to let an app send email as whatever mailbox name is allocated to it (which we now track in the apps table. the default is in the db already so that REST response contains it). When not using Cloudron email, it will just send mail as that mailbox and the auth checks the "app password" in the addons table. Any replies to that mailbox will end up in the domain's mail server (not our problem). When using cloudron email, the app can send mail like above. Any responses will not end anywhere and bounce since there is no 'mailbox'. This is the expected behavior. If user wants to access this mailbox name, he can create a concrete mailbox and set himself as owner OR set this as an alias. For apps using the recvmail addon, the workflow is to actually create a mailbox at some point. Currently, we have no UI for this 'flow'. It's fine because we have only meemo using it. Intuitive much!
2018-12-06 21:08:19 -08:00
},
mailboxName: 'talktome',
2019-11-14 21:43:14 -08:00
mailboxDomain: DOMAIN_0.domain,
enableAutomaticUpdate: true,
2019-09-15 21:51:38 -07:00
dataDir: null,
2019-03-22 14:26:25 -07:00
tags: [],
2019-08-26 15:28:29 -07:00
label: null,
taskId: null,
2020-10-28 19:42:48 -07:00
mounts: [],
2020-11-10 09:59:28 -08:00
proxyAuth: false,
servicesConfig: {},
hasIcon: false,
hasAppStoreIcon: false
};
2018-01-26 18:32:13 +01:00
var APP_1 = {
id: 'appid-1',
appStoreId: 'appStoreId-1',
2019-08-30 13:12:49 -07:00
installationState: apps.ISTATE_PENDING_INSTALL, // app health tests rely on this initial state
2019-08-30 09:45:43 -07:00
error: null,
2019-09-22 22:07:14 -07:00
runState: 'running',
location: 'some-location-1',
2018-01-26 18:32:13 +01:00
domain: DOMAIN_0.domain,
manifest: { version: '0.2', dockerImage: 'docker/app1', healthCheckPath: '/', httpPort: 80, title: 'app1' },
containerId: null,
containerIp: null,
portBindings: { },
health: null,
accessRestriction: { users: [ 'foobar' ] },
2016-04-19 00:10:11 -07:00
memoryLimit: 0,
2020-01-28 21:30:35 -08:00
cpuShares: 512,
2017-01-19 12:51:16 -08:00
sso: true,
debugMode: null,
reverseProxyConfig: {},
enableBackup: true,
alternateDomains: [],
2021-01-19 19:20:26 -08:00
aliasDomains: [],
rework how app mailboxes are allocated Our current setup had a mailbox allocated for an app during app install (into the mailboxes table). This has many issues: * When set to a custom mailbox location, there was no way to access this mailbox even via IMAP. Even when using app credentials, we cannot use IMAP since the ldap logic was testing on the addon type (most of our apps only use sendmail addon and thus cannot recvmail). * The mailboxes table was being used to add hidden 'app' type entries. This made it very hard for the user to understand why a mailbox conflicts. For example, if you set an app to use custom mailbox 'blog', this is hidden from all views. The solution is to let an app send email as whatever mailbox name is allocated to it (which we now track in the apps table. the default is in the db already so that REST response contains it). When not using Cloudron email, it will just send mail as that mailbox and the auth checks the "app password" in the addons table. Any replies to that mailbox will end up in the domain's mail server (not our problem). When using cloudron email, the app can send mail like above. Any responses will not end anywhere and bounce since there is no 'mailbox'. This is the expected behavior. If user wants to access this mailbox name, he can create a concrete mailbox and set himself as owner OR set this as an alias. For apps using the recvmail addon, the workflow is to actually create a mailbox at some point. Currently, we have no UI for this 'flow'. It's fine because we have only meemo using it. Intuitive much!
2018-12-06 21:08:19 -08:00
env: {},
2021-03-17 12:14:36 -07:00
enableMailbox: true,
mailboxName: 'callme',
2019-11-14 21:43:14 -08:00
mailboxDomain: DOMAIN_0.domain,
enableAutomaticUpdate: true,
2019-09-15 21:51:38 -07:00
dataDir: null,
2019-03-22 14:26:25 -07:00
tags: [],
2019-08-26 15:28:29 -07:00
label: null,
taskId: null,
2020-10-28 19:42:48 -07:00
mounts: [],
2020-11-10 09:59:28 -08:00
proxyAuth: false,
servicesConfig: {},
hasIcon: false,
hasAppStoreIcon: false
};
2018-01-26 18:32:13 +01:00
before(function (done) {
async.series([
2021-06-28 15:15:28 -07:00
database._clear,
2021-05-04 21:40:11 -07:00
domaindb.add.bind(null, DOMAIN_0.domain, DOMAIN_0)
], done);
2018-01-26 18:32:13 +01:00
});
after(function (done) {
database._clear(done);
});
it('add fails due to missing arguments', function () {
expect(function () { appdb.add(APP_0.id, APP_0.manifest, APP_0.installationState, function () {}); }).to.throwError();
expect(function () { appdb.add(APP_0.id, function () {}); }).to.throwError();
});
it('exists returns false', function (done) {
appdb.exists(APP_0.id, function (error, exists) {
expect(error).to.be(null);
expect(exists).to.be(false);
done();
});
});
it('add succeeds', function (done) {
2019-07-02 20:22:17 -07:00
appdb.add(APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, APP_0.portBindings, APP_0, function (error) {
expect(error).to.be(null);
done();
});
});
it('exists succeeds', function (done) {
appdb.exists(APP_0.id, function (error, exists) {
expect(error).to.be(null);
expect(exists).to.be(true);
done();
});
});
it('getPortBindings succeeds', function (done) {
appdb.getPortBindings(APP_0.id, function (error, bindings) {
expect(error).to.be(null);
expect(bindings).to.be.an(Object);
expect(bindings).to.be.eql({ port: { hostPort: '5678', type: 'tcp' } });
done();
});
});
it('add of same app fails', function (done) {
2019-07-02 20:22:17 -07:00
appdb.add(APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, [], APP_0, function (error) {
expect(error).to.be.a(BoxError);
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
done();
});
});
it('get succeeds', function (done) {
appdb.get(APP_0.id, function (error, result) {
expect(error).to.be(null);
expect(result).to.be.an('object');
expect(_.omit(result, ['creationTime', 'updateTime', 'ts', 'healthTime', 'resetTokenCreationTime'])).to.be.eql(APP_0);
done();
});
});
it('get of nonexisting code fails', function (done) {
appdb.get(APP_1.id, function (error, result) {
expect(error).to.be.a(BoxError);
expect(error.reason).to.be(BoxError.NOT_FOUND);
expect(result).to.not.be.ok();
done();
});
});
it('update succeeds', function (done) {
APP_0.installationState = 'some-other-status';
APP_0.location = 'some-other-location';
APP_0.manifest.version = '0.2';
2015-10-13 09:52:21 +02:00
APP_0.accessRestriction = '';
APP_0.memoryLimit = 1337;
2020-01-28 21:30:35 -08:00
APP_0.cpuShares = 1024;
2015-10-13 09:52:21 +02:00
var data = {
installationState: APP_0.installationState,
location: APP_0.location,
2018-12-11 16:26:19 -08:00
domain: APP_0.domain,
2015-10-13 09:52:21 +02:00
manifest: APP_0.manifest,
accessRestriction: APP_0.accessRestriction,
2020-01-28 21:30:35 -08:00
memoryLimit: APP_0.memoryLimit,
cpuShares: APP_0.cpuShares
2015-10-13 09:52:21 +02:00
};
appdb.update(APP_0.id, data, function (error) {
expect(error).to.be(null);
appdb.get(APP_0.id, function (error, result) {
expect(error).to.be(null);
expect(result).to.be.an('object');
expect(_.omit(result, ['creationTime', 'updateTime', 'ts', 'healthTime','resetTokenCreationTime'])).to.be.eql(APP_0);
done();
});
});
});
it('update of nonexisting app fails', function (done) {
appdb.update(APP_1.id, { installationState: APP_1.installationState, location: APP_1.location }, function (error) {
expect(error).to.be.a(BoxError);
expect(error.reason).to.be(BoxError.NOT_FOUND);
done();
});
});
it('add second app succeeds', function (done) {
2019-07-02 20:22:17 -07:00
appdb.add(APP_1.id, APP_1.appStoreId, APP_1.manifest, APP_1.location, APP_1.domain, [], APP_1, function (error) {
expect(error).to.be(null);
done();
});
});
it('getAll succeeds', function (done) {
rework how app mailboxes are allocated Our current setup had a mailbox allocated for an app during app install (into the mailboxes table). This has many issues: * When set to a custom mailbox location, there was no way to access this mailbox even via IMAP. Even when using app credentials, we cannot use IMAP since the ldap logic was testing on the addon type (most of our apps only use sendmail addon and thus cannot recvmail). * The mailboxes table was being used to add hidden 'app' type entries. This made it very hard for the user to understand why a mailbox conflicts. For example, if you set an app to use custom mailbox 'blog', this is hidden from all views. The solution is to let an app send email as whatever mailbox name is allocated to it (which we now track in the apps table. the default is in the db already so that REST response contains it). When not using Cloudron email, it will just send mail as that mailbox and the auth checks the "app password" in the addons table. Any replies to that mailbox will end up in the domain's mail server (not our problem). When using cloudron email, the app can send mail like above. Any responses will not end anywhere and bounce since there is no 'mailbox'. This is the expected behavior. If user wants to access this mailbox name, he can create a concrete mailbox and set himself as owner OR set this as an alias. For apps using the recvmail addon, the workflow is to actually create a mailbox at some point. Currently, we have no UI for this 'flow'. It's fine because we have only meemo using it. Intuitive much!
2018-12-06 21:08:19 -08:00
appdb.getAll(function (error, result) {
expect(error).to.be(null);
expect(result).to.be.an(Array);
expect(result.length).to.be(2);
expect(_.omit(result[0], ['creationTime', 'updateTime','ts', 'healthTime', 'resetTokenCreationTime'])).to.be.eql(APP_0);
expect(_.omit(result[1], ['creationTime', 'updateTime','ts', 'healthTime', 'resetTokenCreationTime'])).to.be.eql(APP_1);
done();
});
});
it('getAppStoreIds succeeds', function (done) {
appdb.getAppStoreIds(function (error, results) {
expect(error).to.be(null);
expect(results).to.be.an(Array);
expect(results.length).to.be(2);
expect(results[0].appStoreId).to.equal(APP_0.appStoreId);
expect(results[1].appStoreId).to.equal(APP_1.appStoreId);
done();
});
});
it('delete succeeds', function (done) {
appdb.del(APP_0.id, function (error) {
expect(error).to.be(null);
done();
});
});
it('getPortBindings should be empty', function (done) {
appdb.getPortBindings(APP_0.id, function (error, bindings) {
expect(error).to.be(null);
expect(bindings).to.be.an(Object);
expect(bindings).to.be.eql({ });
done();
});
});
it('cannot delete previously delete record', function (done) {
appdb.del(APP_0.id, function (error) {
expect(error).to.be.a(BoxError);
expect(error.reason).to.be(BoxError.NOT_FOUND);
done();
});
});
2019-08-30 13:12:49 -07:00
it('can set app as healthy', function (done) {
appdb.setHealth(APP_1.id, apps.HEALTH_HEALTHY, new Date(), function (error) {
expect(error).to.be(null);
2019-08-30 13:12:49 -07:00
done();
});
});
it('cannot set health of unknown app', function (done) {
2019-08-30 13:12:49 -07:00
appdb.setHealth('randomId', apps.HEALTH_HEALTHY, new Date(), function (error) {
expect(error).to.be.ok();
done();
});
});
it('return empty addon config array for invalid app', function (done) {
appdb.getAddonConfigByAppId('randomid', function (error, results) {
expect(error).to.be(null);
expect(results).to.eql([ ]);
done();
});
});
it('setAddonConfig succeeds', function (done) {
2017-03-26 19:06:36 -07:00
appdb.setAddonConfig(APP_1.id, 'addonid1', [ { name: 'ENV1', value: 'env' }, { name: 'ENV2', value: 'env2' } ], function (error) {
expect(error).to.be(null);
done();
});
});
it('setAddonConfig succeeds', function (done) {
appdb.setAddonConfig(APP_1.id, 'addonid2', [ { name: 'ENV3', value: 'env' } ], function (error) {
expect(error).to.be(null);
done();
});
});
it('getAddonConfig succeeds', function (done) {
appdb.getAddonConfig(APP_1.id, 'addonid1', function (error, results) {
expect(error).to.be(null);
2017-03-26 19:06:36 -07:00
expect(results).to.eql([ { name: 'ENV1', value: 'env' }, { name: 'ENV2', value: 'env2' } ]);
done();
});
});
it('getAddonConfigByAppId succeeds', function (done) {
appdb.getAddonConfigByAppId(APP_1.id, function (error, results) {
expect(error).to.be(null);
2017-03-26 19:06:36 -07:00
expect(results).to.eql([ { name: 'ENV1', value: 'env' }, { name: 'ENV2', value: 'env2' }, { name: 'ENV3', value: 'env' } ]);
done();
});
});
it('getAddonConfigByName succeeds', function (done) {
appdb.getAddonConfigByName(APP_1.id, 'addonid1', 'ENV2', function (error, value) {
expect(error).to.be(null);
expect(value).to.be('env2');
done();
});
});
it('getAddonConfigByName of unknown value succeeds', function (done) {
appdb.getAddonConfigByName(APP_1.id, 'addonid1', 'NOPE', function (error) {
expect(error.reason).to.be(BoxError.NOT_FOUND);
done();
});
});
it('unsetAddonConfig succeeds', function (done) {
appdb.unsetAddonConfig(APP_1.id, 'addonid1', function (error) {
expect(error).to.be(null);
done();
});
});
it('unsetAddonConfig did remove configs', function (done) {
appdb.getAddonConfigByAppId(APP_1.id, function (error, results) {
expect(error).to.be(null);
expect(results).to.eql([ { name: 'ENV3', value: 'env' }]);
done();
});
});
it('unsetAddonConfigByAppId succeeds', function (done) {
appdb.unsetAddonConfigByAppId(APP_1.id, function (error) {
expect(error).to.be(null);
done();
});
});
it('unsetAddonConfigByAppId did remove configs', function (done) {
appdb.getAddonConfigByAppId(APP_1.id, function (error, results) {
expect(error).to.be(null);
expect(results).to.eql([ ]);
done();
});
});
});
describe('settings', function () {
it('can set value', function (done) {
settingsdb.set('somekey', 'somevalue', function (error) {
expect(error).to.be(null);
done();
});
});
it('can get the set value', function (done) {
settingsdb.get('somekey', function (error, value) {
expect(error).to.be(null);
expect(value).to.be('somevalue');
done();
});
});
it('can get all values', function (done) {
settingsdb.getAll(function (error, result) {
expect(error).to.be(null);
expect(result).to.be.an(Array);
expect(result[0].name).to.be('somekey');
expect(result[0].value).to.be('somevalue');
expect(result.length).to.be(1); // the value set above
done();
});
});
it('can update a value', function (done) {
settingsdb.set('somekey', 'someothervalue', function (error) {
expect(error).to.be(null);
done();
});
});
it('can get updated value', function (done) {
settingsdb.get('somekey', function (error, value) {
expect(error).to.be(null);
expect(value).to.be('someothervalue');
done();
});
});
});
2016-04-04 12:41:17 -07:00
describe('importFromFile', function () {
before(function (done) {
async.series([
database.initialize,
database._clear
], done);
});
it('cannot import from non-existent file', function (done) {
database.importFromFile('/does/not/exist', function (error) {
expect(error).to.be.ok();
done();
});
});
2017-11-24 15:31:03 -08:00
it('can export to file', function (done) {
2019-05-07 15:31:32 +02:00
// arch only has maria db which lacks some mysqldump options we need, this is only here to allow running the tests :-/
if (require('child_process').execSync('/usr/bin/mysqldump --version').toString().indexOf('MariaDB') !== -1) return done();
2017-11-24 15:31:03 -08:00
database.exportToFile('/tmp/box.mysqldump', function (error) {
expect(error).to.be(null);
done();
});
});
it('can import from file', function (done) {
2019-05-07 15:31:32 +02:00
// arch only has maria db which lacks some mysqldump options we need, this is only here to allow running the tests :-/
if (require('child_process').execSync('/usr/bin/mysqldump --version').toString().indexOf('MariaDB') !== -1) return done();
2017-11-24 15:31:03 -08:00
database.importFromFile('/tmp/box.mysqldump', function (error) {
expect(error).to.be(null);
done();
});
});
});
2016-05-26 22:34:04 -07:00
describe('mailboxes', function () {
before(function (done) {
2018-02-11 00:04:41 -08:00
async.series([
2021-05-04 21:40:11 -07:00
domaindb.add.bind(null, DOMAIN_0.domain, DOMAIN_0),
2018-02-11 00:04:41 -08:00
], done);
2018-01-26 18:32:13 +01:00
});
2018-01-26 18:32:13 +01:00
after(function (done) {
database._clear(done);
});
2016-09-25 23:21:55 -07:00
it('add user mailbox succeeds', function (done) {
mailboxdb.addMailbox('girish', DOMAIN_0.domain, { ownerId: 'uid-0', ownerType: 'user', active: true }, function (error) {
2016-05-26 22:34:04 -07:00
expect(error).to.be(null);
done();
});
});
it('cannot add dup entry', function (done) {
mailboxdb.addMailbox('girish', DOMAIN_0.domain, { ownerId: 'uid-1', ownerType: 'group', active: true }, function (error) {
2019-10-24 13:34:14 -07:00
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
2016-05-26 22:34:04 -07:00
done();
});
});
2016-09-25 23:21:55 -07:00
it('add app mailbox succeeds', function (done) {
mailboxdb.addMailbox('support', DOMAIN_0.domain, { ownerId: 'osticket', ownerType: 'user', active: true}, function (error) {
2016-09-25 23:21:55 -07:00
expect(error).to.be(null);
done();
});
});
2016-05-26 22:34:04 -07:00
it('get succeeds', function (done) {
2018-01-26 18:32:13 +01:00
mailboxdb.getMailbox('support', DOMAIN_0.domain, function (error, mailbox) {
2016-05-26 22:34:04 -07:00
expect(error).to.be(null);
2017-11-11 01:03:23 +01:00
expect(mailbox.name).to.equal('support');
expect(mailbox.ownerId).to.equal('osticket');
2018-01-26 18:32:13 +01:00
expect(mailbox.domain).to.equal(DOMAIN_0.domain);
2016-05-26 22:34:04 -07:00
expect(mailbox.creationTime).to.be.a(Date);
done();
});
});
2016-09-25 23:21:55 -07:00
it('list mailboxes succeeds', function (done) {
mailboxdb.listMailboxes(DOMAIN_0.domain, null /* search */, 1, 10, function (error, mailboxes) {
expect(error).to.be(null);
2016-09-25 23:21:55 -07:00
expect(mailboxes.length).to.be(2);
expect(mailboxes[0].name).to.be('girish');
expect(mailboxes[1].name).to.be('support');
done();
});
});
2016-09-25 23:21:55 -07:00
it('can set alias', function (done) {
mailboxdb.setAliasesForName('support', DOMAIN_0.domain, [ { name: 'support2', domain: DOMAIN_0.domain }, { name: 'help', domain: DOMAIN_0.domain } ], function (error) {
2016-09-25 23:21:55 -07:00
expect(error).to.be(null);
done();
});
});
it('list all mailboxes succeeds', function (done) {
mailboxdb.listAllMailboxes(1, 10, function (error, mailboxes) {
expect(error).to.be(null);
expect(mailboxes.length).to.be(2);
expect(mailboxes[0].name).to.be('girish');
expect(mailboxes[1].name).to.be('support');
expect(mailboxes[1].domain).to.be(DOMAIN_0.domain);
done();
});
});
2016-09-25 23:21:55 -07:00
it('can get aliases of name', function (done) {
2018-01-26 18:32:13 +01:00
mailboxdb.getAliasesForName('support', DOMAIN_0.domain, function (error, results) {
expect(error).to.be(null);
expect(results.length).to.be(2);
expect(results[0].name).to.be('help');
expect(results[0].domain).to.be(DOMAIN_0.domain);
expect(results[1].name).to.be('support2');
expect(results[1].domain).to.be(DOMAIN_0.domain);
done();
});
});
2016-09-25 23:21:55 -07:00
it('can get alias', function (done) {
2018-01-26 18:32:13 +01:00
mailboxdb.getAlias('support2', DOMAIN_0.domain, function (error, result) {
2016-09-25 23:21:55 -07:00
expect(error).to.be(null);
expect(result.name).to.be('support2');
expect(result.aliasName).to.be('support');
expect(result.aliasDomain).to.be(DOMAIN_0.domain);
2016-09-25 23:21:55 -07:00
done();
});
});
it('can get by owner id', function (done) {
mailboxdb.getByOwnerId('osticket', function (error, results) {
expect(error).to.be(null);
expect(results.length).to.be(3);
expect(results[0].name).to.be('help');
expect(results[1].name).to.be('support');
expect(results[2].name).to.be('support2');
done();
});
});
2016-09-27 16:34:28 -07:00
it('cannot get non-existing group', function (done) {
2019-08-23 15:09:06 -07:00
mailboxdb.getList('random', DOMAIN_0.domain, function (error) {
2019-10-24 13:34:14 -07:00
expect(error.reason).to.be(BoxError.NOT_FOUND);
2016-09-27 16:34:28 -07:00
done();
});
});
2016-12-15 16:13:16 +01:00
it('can change name', function (done) {
2018-01-26 18:32:13 +01:00
mailboxdb.updateName('support', DOMAIN_0.domain, 'support3', DOMAIN_0.domain, function (error) {
2016-12-15 16:13:16 +01:00
expect(error).to.be(null);
2018-01-26 18:32:13 +01:00
mailboxdb.updateName('support3', DOMAIN_0.domain, 'support', DOMAIN_0.domain, done);
2016-12-15 16:13:16 +01:00
});
});
it('cannot change name to existing one', function (done) {
2018-01-26 18:32:13 +01:00
mailboxdb.updateName('support', DOMAIN_0.domain, 'support2', DOMAIN_0.domain, function (error) {
2016-12-15 16:13:16 +01:00
expect(error).to.be.ok();
2019-10-24 13:34:14 -07:00
expect(error.reason).to.eql(BoxError.ALREADY_EXISTS);
2016-12-15 16:13:16 +01:00
done();
});
});
it('unset aliases', function (done) {
2018-01-26 18:32:13 +01:00
mailboxdb.setAliasesForName('support', DOMAIN_0.domain, [], function (error) {
expect(error).to.be(null);
2018-01-26 18:32:13 +01:00
mailboxdb.getAliasesForName('support', DOMAIN_0.domain, function (error, results) {
expect(error).to.be(null);
expect(results.length).to.be(0);
done();
});
});
});
2016-05-26 22:34:04 -07:00
it('del succeeds', function (done) {
2018-01-26 18:32:13 +01:00
mailboxdb.del('girish', DOMAIN_0.domain, function (error) {
2016-05-26 22:34:04 -07:00
expect(error).to.be(null);
done();
});
});
2016-09-25 23:21:55 -07:00
it('del by ownerId succeeds', function (done) {
mailboxdb.delByOwnerId('osticket', function (error) {
expect(error).to.be(null);
mailboxdb.getByOwnerId('osticket', function (error) {
2016-09-25 23:21:55 -07:00
expect(error).to.be.ok();
2019-10-24 13:34:14 -07:00
expect(error.reason).to.be(BoxError.NOT_FOUND);
2016-09-25 23:21:55 -07:00
done();
});
});
});
2016-05-26 22:34:04 -07:00
});
2018-01-20 23:40:59 -08:00
});