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

252 lines
7.9 KiB
JavaScript
Raw Normal View History

import apps from '../../apps.js';
import * as appstore from '../../appstore.js';
import * as backupSites from '../../backupsites.js';
import debugModule from 'debug';
import constants from '../../constants.js';
import * as database from '../../database.js';
import expect from 'expect.js';
import * as mailer from '../../mailer.js';
import nock from 'nock';
import oidcClients from '../../oidcclients.js';
import * as oidcServer from '../../oidcserver.js';
import * as server from '../../server.js';
import * as settings from '../../settings.js';
import superagent from '@cloudron/superagent';
import tasks from '../../tasks.js';
import timers from 'timers/promises';
import * as tokens from '../../tokens.js';
const debug = debugModule('box:test/common');
2021-05-17 22:23:18 -07:00
2025-02-12 14:09:09 +01:00
const manifest = {
'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,
'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': { }
}
};
const mockApiServerOrigin = 'http://localhost:6060';
const dashboardDomain = 'test.example.com';
const appstoreToken = 'toktok';
const owner = {
id: null,
username: 'superadmin',
password: 'Foobar?1337',
email: 'superadmin@cloudron.local',
displayName: 'Super Admin',
token: null
};
const admin = {
id: null,
username: 'administrator',
password: 'Foobar?1339',
email: 'admin@cloudron.local',
token: null
};
const user = {
id: null,
username: 'user',
password: 'Foobar?1338',
email: 'user@cloudron.local',
token: null
};
const app = {
id: 'appid',
appStoreId: 'appStoreId',
installationState: apps.ISTATE_PENDING_INSTALL,
runState: 'running',
subdomain: 'app',
domain: 'test.example.com',
fqdn: 'app.test.example.com',
manifest,
containerId: 'someid',
portBindings: {},
accessRestriction: null,
memoryLimit: 0,
mailboxDomain: 'test.example.com',
secondaryDomains: [],
redirectDomains: [],
aliasDomains: []
};
const serverUrl = `http://localhost:${constants.PORT}`;
export default {
2021-05-17 22:23:18 -07:00
setup,
setupServer,
2021-05-17 22:23:18 -07:00
cleanup,
2021-06-05 11:46:34 -07:00
clearMailQueue,
checkMails,
waitForTask,
2025-07-17 01:16:24 +02:00
waitForAsyncTask,
owner,
admin,
user,
app,
2025-09-12 09:48:37 +02:00
getDefaultBackupSite,
mockApiServerOrigin,
dashboardDomain,
2021-06-29 14:26:34 -07:00
dashboardFqdn: 'my.test.example.com',
appstoreToken,
mailFqdn: 'my.test.example.com',
serverUrl,
2024-12-10 11:01:10 +01:00
auditSource: { ip: '5.6.7.8' }
2021-05-17 22:23:18 -07:00
};
2021-09-07 09:57:49 -07:00
async function setupServer() {
2025-02-06 15:01:59 +01:00
debug('Setting up server');
2023-03-21 13:54:40 +01:00
await database.initialize();
2023-08-14 11:08:38 +05:30
await database._clear();
await appstore._setApiServerOrigin(mockApiServerOrigin);
2025-06-11 22:00:09 +02:00
await oidcServer.stop();
2023-03-21 13:54:40 +01:00
await server.start();
2025-02-06 15:01:59 +01:00
debug('Set up server complete');
}
2021-08-12 16:27:31 -07:00
2021-09-07 09:57:49 -07:00
async function setup() {
2025-02-06 15:01:59 +01:00
debug('Setting up');
2021-09-07 09:57:49 -07:00
await setupServer();
// setup
2023-08-10 16:50:29 +05:30
let response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
.send({ domainConfig: { provider: 'noop', domain: dashboardDomain, config: {}, tlsConfig: { provider: 'fallback' } } });
2021-09-07 09:57:49 -07:00
expect(response.status).to.eql(200);
2023-05-14 10:53:50 +02:00
await timers.setTimeout(2000);
2021-09-07 09:57:49 -07:00
// create owner
const scope1 = nock(await appstore.getApiServerOrigin())
.post('/api/v1/register_cloudron3', (body) => typeof body.domain === 'string' && typeof body.version === 'string')
2025-06-06 11:09:48 +02:00
.reply(201, { cloudronId: 'cid', cloudronToken: 'CLOUDRON_TOKEN' });
2025-10-07 09:34:35 +02:00
const scope2 = nock(await appstore.getApiServerOrigin())
.post('/api/v1/subscription3?accessToken=CLOUDRON_TOKEN', (body) => typeof body.state === 'object' && typeof body.state.userCount === 'number')
.reply(200, { features: {} });
2023-08-10 16:50:29 +05:30
response = await superagent.post(`${serverUrl}/api/v1/provision/activate`)
2021-09-07 09:57:49 -07:00
.query({ setupToken: 'somesetuptoken' })
2025-10-07 09:34:35 +02:00
.send({ username: owner.username, password: owner.password, email: owner.email })
.ok(() => true);
2021-09-07 09:57:49 -07:00
expect(response.status).to.eql(201);
owner.token = response.body.token;
owner.id = response.body.userId;
expect(scope1.isDone()).to.be.ok();
2025-06-06 11:25:57 +02:00
scope1.persist(false);
2025-10-07 09:34:35 +02:00
expect(scope2.isDone()).to.be.ok();
scope2.persist(false);
2021-09-07 09:57:49 -07:00
2022-11-23 17:48:05 +01:00
// create an admin
response = await superagent.post(`${serverUrl}/api/v1/users`)
.query({ access_token: owner.token })
2025-07-25 11:29:00 +02:00
.send({ username: admin.username, email: admin.email, password: admin.password, role: 'admin' });
2022-11-23 17:48:05 +01:00
expect(response.status).to.equal(201);
admin.id = response.body.id;
// HACK to get a token for second user (passwords are generated and the user should have gotten a password setup link...)
2025-06-11 22:53:29 +02:00
const token1 = await tokens.add({ identifier: admin.id, clientId: oidcClients.ID_WEBADMIN, expires: Date.now() + (60 * 60 * 1000), name: 'fromtest', allowedIpRanges: '' });
2022-11-23 17:48:05 +01:00
admin.token = token1.accessToken;
2021-09-07 09:57:49 -07:00
// create user
response = await superagent.post(`${serverUrl}/api/v1/users`)
.query({ access_token: owner.token })
.send({ username: user.username, email: user.email, password: user.password });
expect(response.status).to.equal(201);
user.id = response.body.id;
// HACK to get a token for second user (passwords are generated and the user should have gotten a password setup link...)
2025-06-11 22:53:29 +02:00
const token2 = await tokens.add({ identifier: user.id, clientId: oidcClients.ID_WEBADMIN, expires: Date.now() + (60 * 60 * 1000), name: 'fromtest', allowedIpRanges: '' });
2022-11-23 17:48:05 +01:00
user.token = token2.accessToken;
2021-11-17 11:14:33 -08:00
2025-02-12 14:09:09 +01:00
// create app object
await apps.add(app.id, app.appStoreId, '', app.manifest, app.subdomain, app.domain, app.portBindings, app);
2025-02-12 14:09:09 +01:00
await settings._set(settings.APPSTORE_API_TOKEN_KEY, appstoreToken); // appstore token
2025-02-06 15:01:59 +01:00
debug('Setup complete');
2021-05-17 22:23:18 -07:00
}
2021-08-20 09:19:44 -07:00
async function cleanup() {
2025-02-06 15:01:59 +01:00
debug('Cleaning up');
2021-09-07 09:57:49 -07:00
await server.stop();
2025-06-11 22:00:09 +02:00
await oidcServer.stop();
2025-07-17 01:16:24 +02:00
if (!nock.isActive()) nock.activate();
2025-02-06 15:01:59 +01:00
debug('Cleaned up');
2021-05-17 22:23:18 -07:00
}
2021-06-05 11:46:34 -07:00
function clearMailQueue() {
mailer.clearMailQueue();
2021-06-05 11:46:34 -07:00
}
async function checkMails(number) {
2023-05-14 10:53:50 +02:00
await timers.setTimeout(1000);
2021-06-05 11:46:34 -07:00
expect(mailer._mailQueue.length).to.equal(number);
clearMailQueue();
}
async function waitForTask(taskId) {
2025-02-06 15:01:59 +01:00
debug(`Waiting for task: ${taskId}`);
2024-01-11 16:31:12 +01:00
for (let i = 0; i < 30; i++) {
const result = await tasks.get(taskId);
expect(result).to.not.be(null);
2022-09-19 18:20:27 +02:00
if (!result.active) {
if (result.success) return result;
throw new Error(`Task ${taskId} failed: ${result.error.message} - ${result.error.stack}`);
}
2023-05-14 10:53:50 +02:00
await timers.setTimeout(2000);
2025-02-06 15:01:59 +01:00
debug(`Waiting for task to ${taskId} finish`);
}
throw new Error(`Task ${taskId} never finished`);
}
2025-07-17 01:16:24 +02:00
async function waitForAsyncTask(es) {
return new Promise((resolve, reject) => {
const messages = [];
es.addEventListener('message', function (message) {
debug(`waitForAsyncTask: ${message.data}`);
messages.push(JSON.parse(message.data));
if (messages[messages.length-1].type === 'done') {
debug('waitForAsyncTask: finished');
es.close();
resolve(messages);
}
});
es.addEventListener('error', function (error) {
debug('waitForAsyncTask: errored', error);
es.close();
const e = new Error(error.message);
e.code = error.code;
reject(e);
});
});
}
2025-07-25 07:44:25 +02:00
2025-09-12 09:48:37 +02:00
async function getDefaultBackupSite() {
2025-09-29 14:44:42 +02:00
const result = await backupSites.list();
2025-08-04 15:00:21 +02:00
return result.find(r => r.name === 'Default');
2025-07-25 07:44:25 +02:00
}