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

216 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-09-29 09:56:01 -07:00
'use strict';
2021-08-20 09:19:44 -07:00
const apps = require('../apps.js'),
2021-06-03 12:20:44 -07:00
async = require('async'),
constants = require('../constants.js'),
2021-06-03 12:20:44 -07:00
database = require('../database.js'),
2021-08-13 14:43:08 -07:00
delay = require('delay'),
2021-06-03 12:20:44 -07:00
domains = require('../domains.js'),
2021-08-13 14:43:08 -07:00
expect = require('expect.js'),
2021-06-03 12:20:44 -07:00
fs = require('fs'),
mailer = require('../mailer.js'),
nock = require('nock'),
2017-09-29 09:56:01 -07:00
path = require('path'),
2021-06-03 12:20:44 -07:00
rimraf = require('rimraf'),
settings = require('../settings.js'),
2021-07-14 19:03:12 -07:00
tasks = require('../tasks.js'),
2021-06-03 12:20:44 -07:00
users = require('../users.js');
2021-08-13 15:49:59 -07:00
const manifest = {
2021-06-03 12:20:44 -07:00
'id': 'io.cloudron.test',
'author': 'The Presidents Of the United States Of America',
'title': 'test title',
'description': 'test description',
'tagline': 'test rocks',
'website': 'http://test.cloudron.io',
'contactEmail': 'test@cloudron.io',
'version': '0.1.0',
'manifestVersion': 2,
2021-06-03 12:20:44 -07:00
'dockerImage': 'cloudron/test:25.2.0',
'healthCheckPath': '/',
'httpPort': 7777,
'tcpPorts': {
'ECHO_SERVER_PORT': {
'title': 'Echo Server Port',
'description': 'Echo server',
'containerPort': 7778
}
},
'addons': {
'oauth': { },
'redis': { },
'mysql': { },
'postgresql': { }
}
};
2021-08-13 10:41:10 -07:00
const domain = {
2021-06-03 12:20:44 -07:00
domain: 'example.com',
zoneName: 'example.com',
2021-06-03 16:16:04 -07:00
provider: 'noop',
config: {},
2021-06-03 12:20:44 -07:00
fallbackCertificate: null,
2021-06-03 16:16:04 -07:00
tlsConfig: { provider: 'fallback' },
2021-06-03 12:20:44 -07:00
wellKnown: null
};
2021-08-20 09:19:44 -07:00
Object.freeze(domain);
2021-06-03 12:20:44 -07:00
2021-08-13 10:41:10 -07:00
const auditSource = { ip: '1.2.3.4' };
2021-06-03 12:20:44 -07:00
2021-08-13 10:41:10 -07:00
const admin = {
2021-06-28 15:15:28 -07:00
id: null,
2021-07-20 09:05:21 -07:00
username: 'testadmin',
2021-06-03 12:20:44 -07:00
password: 'secret123',
email: 'admin@me.com',
fallbackEmail: 'admin@external.com',
2021-06-03 12:20:44 -07:00
salt: 'morton',
createdAt: 'sometime back',
resetToken: '',
2021-07-20 09:05:21 -07:00
displayName: 'Administrator',
groupIds: [],
2021-06-03 12:20:44 -07:00
role: 'owner',
source: '',
2021-07-19 12:43:30 -07:00
avatar: constants.AVATAR_GRAVATAR,
active: true,
2021-06-03 12:20:44 -07:00
};
2021-08-13 10:41:10 -07:00
const user = {
2021-07-20 09:05:21 -07:00
id: null,
username: 'user',
2021-07-07 12:59:17 -07:00
password: '123secret',
2021-07-20 09:05:21 -07:00
email: 'user@me.com',
fallbackEmail: 'user@external.com',
2021-06-28 15:15:28 -07:00
role: 'user',
salt: 'morton',
createdAt: 'sometime back',
2021-07-20 09:05:21 -07:00
resetToken: '',
groupIds: [],
2021-07-20 09:05:21 -07:00
displayName: 'Normal User',
2021-06-28 15:15:28 -07:00
source: '',
2021-07-19 12:43:30 -07:00
avatar: constants.AVATAR_NONE,
active: true,
2021-06-28 15:15:28 -07:00
};
2021-08-13 10:41:10 -07:00
const app = {
2021-06-03 12:20:44 -07:00
id: 'appid',
appStoreId: 'appStoreId',
installationState: apps.ISTATE_PENDING_INSTALL,
runState: 'running',
subdomain: 'applocation',
2021-08-13 10:41:10 -07:00
domain: domain.domain,
fqdn: domain.domain + '.' + 'applocation',
2021-08-13 15:49:59 -07:00
manifest,
2021-06-03 12:20:44 -07:00
containerId: 'someid',
portBindings: null,
accessRestriction: null,
memoryLimit: 0,
2021-08-13 10:41:10 -07:00
mailboxDomain: domain.domain,
secondaryDomains: [],
redirectDomains: [],
2021-06-03 12:20:44 -07:00
aliasDomains: []
};
2021-08-20 09:19:44 -07:00
Object.freeze(app);
2017-09-29 09:56:01 -07:00
exports = module.exports = {
2021-06-03 12:20:44 -07:00
createTree,
2021-07-19 12:43:30 -07:00
domainSetup,
2021-08-13 17:22:28 -07:00
databaseSetup,
2021-06-03 12:20:44 -07:00
setup,
cleanup,
2021-08-13 14:43:08 -07:00
checkMails,
clearMailQueue,
2021-06-03 12:20:44 -07:00
2021-08-13 10:41:10 -07:00
mockApiServerOrigin: 'http://localhost:6060',
2021-08-13 17:22:28 -07:00
dashboardDomain: domain.domain,
dashboardFqdn: `my.${domain.domain}`,
2021-06-03 12:20:44 -07:00
2021-08-13 10:41:10 -07:00
app,
admin,
auditSource,
2021-08-13 17:22:28 -07:00
domain, // the domain object
2021-08-13 15:49:59 -07:00
manifest,
2021-08-13 10:41:10 -07:00
user,
appstoreToken: 'atoken',
serverUrl: `http://localhost:${constants.PORT}`,
2017-09-29 09:56:01 -07:00
};
function createTree(root, obj) {
rimraf.sync(root);
fs.mkdirSync(root, { recursive: true });
2017-09-29 09:56:01 -07:00
function createSubTree(tree, curpath) {
for (var key in tree) {
if (typeof tree[key] === 'string') {
if (key.startsWith('link:')) {
fs.symlinkSync(tree[key], path.join(curpath, key.slice(5)));
} else {
fs.writeFileSync(path.join(curpath, key), tree[key], 'utf8');
}
} else {
fs.mkdirSync(path.join(curpath, key));
createSubTree(tree[key], path.join(curpath, key));
}
}
}
createSubTree(obj, root);
}
2021-08-20 09:19:44 -07:00
async function databaseSetup() {
2021-06-03 12:20:44 -07:00
nock.cleanAll();
2021-08-20 09:19:44 -07:00
await database.initialize();
await database._clear();
await settings._setApiServerOrigin(exports.mockApiServerOrigin);
await settings.setDashboardLocation(exports.dashboardDomain, exports.dashboardFqdn);
await settings.initCache();
2021-08-13 17:22:28 -07:00
}
2021-08-20 09:19:44 -07:00
async function domainSetup() {
2021-08-13 17:22:28 -07:00
nock.cleanAll();
2021-08-20 09:19:44 -07:00
await databaseSetup();
await domains.add(domain.domain, domain, auditSource);
2021-07-19 12:43:30 -07:00
}
function setup(done) {
async.series([
domainSetup,
2021-07-15 09:50:11 -07:00
async function createOwner() {
2021-08-13 10:41:10 -07:00
const result = await users.createOwner(admin.email, admin.username, admin.password, admin.displayName, auditSource);
admin.id = result;
2021-06-25 22:11:17 -07:00
},
apps.add.bind(null, app.id, app.appStoreId, app.manifest, app.subdomain, app.domain, app.portBindings, app),
settings._set.bind(null, settings.APPSTORE_API_TOKEN_KEY, exports.appstoreToken), // appstore token
2021-07-15 09:50:11 -07:00
async function createUser() {
2021-08-13 10:41:10 -07:00
const result = await users.add(user.email, user, auditSource);
user.id = result;
2021-07-15 09:50:11 -07:00
},
2021-07-07 12:59:17 -07:00
2021-07-14 19:03:12 -07:00
tasks.stopAllTasks,
2021-06-03 12:20:44 -07:00
], done);
}
2021-08-20 09:19:44 -07:00
async function cleanup() {
2021-06-03 12:20:44 -07:00
nock.cleanAll();
mailer._mailQueue = [];
2021-08-20 09:19:44 -07:00
await database._clear();
await database.uninitialize();
2021-06-03 12:20:44 -07:00
}
2021-07-19 12:43:30 -07:00
2021-08-13 14:43:08 -07:00
function clearMailQueue() {
mailer._mailQueue = [];
}
async function checkMails(number) {
await delay(1000);
expect(mailer._mailQueue.length).to.equal(number);
const emails = mailer._mailQueue;
2021-08-13 14:43:08 -07:00
clearMailQueue();
// return for further investigation
return emails;
2021-08-13 14:43:08 -07:00
}