2021-05-17 22:23:18 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
2025-02-12 14:09:09 +01:00
|
|
|
const apps = require('../../apps.js'),
|
|
|
|
|
appstore = require('../../appstore.js'),
|
2025-02-06 15:01:59 +01:00
|
|
|
debug = require('debug')('box:test/common'),
|
2023-08-04 15:34:38 +05:30
|
|
|
constants = require('../../constants.js'),
|
2021-05-17 22:23:18 -07:00
|
|
|
database = require('../../database.js'),
|
|
|
|
|
expect = require('expect.js'),
|
2021-08-12 16:27:31 -07:00
|
|
|
fs = require('fs'),
|
2021-06-05 11:46:34 -07:00
|
|
|
mailer = require('../../mailer.js'),
|
2025-06-06 09:31:31 +02:00
|
|
|
nock = require('nock'),
|
2023-03-26 16:34:14 +02:00
|
|
|
oidc = require('../../oidc.js'),
|
2021-08-12 16:27:31 -07:00
|
|
|
safe = require('safetydance'),
|
2021-05-17 22:23:18 -07:00
|
|
|
server = require('../../server.js'),
|
2021-06-03 22:39:26 -07:00
|
|
|
settings = require('../../settings.js'),
|
2021-08-12 16:27:31 -07:00
|
|
|
support = require('../../support.js'),
|
2025-02-14 17:26:54 +01:00
|
|
|
superagent = require('../../superagent.js'),
|
2022-04-02 17:09:08 -07:00
|
|
|
tasks = require('../../tasks.js'),
|
2023-05-14 10:53:50 +02:00
|
|
|
timers = require('timers/promises'),
|
2021-09-07 09:57:49 -07:00
|
|
|
tokens = require('../../tokens.js');
|
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': { }
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-17 22:23:18 -07:00
|
|
|
exports = module.exports = {
|
|
|
|
|
setup,
|
2021-08-12 21:24:18 -07:00
|
|
|
setupServer,
|
2021-05-17 22:23:18 -07:00
|
|
|
cleanup,
|
2021-06-05 11:46:34 -07:00
|
|
|
clearMailQueue,
|
|
|
|
|
checkMails,
|
2022-04-02 17:09:08 -07:00
|
|
|
waitForTask,
|
2021-05-17 22:23:18 -07:00
|
|
|
|
|
|
|
|
owner: {
|
2021-06-01 09:35:20 -07:00
|
|
|
id: null,
|
2021-05-17 22:23:18 -07:00
|
|
|
username: 'superadmin',
|
|
|
|
|
password: 'Foobar?1337',
|
2021-06-01 09:35:20 -07:00
|
|
|
email: 'superadmin@cloudron.local',
|
2021-08-12 21:24:18 -07:00
|
|
|
displayName: 'Super Admin',
|
2021-06-01 09:35:20 -07:00
|
|
|
token: null
|
|
|
|
|
},
|
|
|
|
|
|
2022-11-23 17:48:05 +01:00
|
|
|
admin: {
|
|
|
|
|
id: null,
|
|
|
|
|
username: 'administrator',
|
|
|
|
|
password: 'Foobar?1339',
|
|
|
|
|
email: 'admin@cloudron.local',
|
|
|
|
|
token: null
|
|
|
|
|
},
|
|
|
|
|
|
2021-06-01 09:35:20 -07:00
|
|
|
user: {
|
|
|
|
|
id: null,
|
|
|
|
|
username: 'user',
|
|
|
|
|
password: 'Foobar?1338',
|
|
|
|
|
email: 'user@cloudron.local',
|
2021-05-17 22:23:18 -07:00
|
|
|
token: null
|
|
|
|
|
},
|
|
|
|
|
|
2025-02-12 14:09:09 +01:00
|
|
|
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: []
|
|
|
|
|
},
|
|
|
|
|
|
2021-08-12 16:27:31 -07:00
|
|
|
mockApiServerOrigin: 'http://localhost:6060',
|
2021-06-29 14:26:34 -07:00
|
|
|
dashboardDomain: 'test.example.com',
|
|
|
|
|
dashboardFqdn: 'my.test.example.com',
|
2021-08-12 16:27:31 -07:00
|
|
|
appstoreToken: 'toktok',
|
2023-08-04 21:37:38 +05:30
|
|
|
mailFqdn: 'my.test.example.com',
|
2021-06-03 22:39:26 -07:00
|
|
|
|
2021-06-05 11:46:34 -07:00
|
|
|
serverUrl: `http://localhost:${constants.PORT}`,
|
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();
|
2023-08-04 15:34:38 +05:30
|
|
|
await appstore._setApiServerOrigin(exports.mockApiServerOrigin);
|
2023-08-14 11:08:38 +05:30
|
|
|
await oidc.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 21:24:18 -07:00
|
|
|
}
|
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');
|
|
|
|
|
|
2022-11-23 17:48:05 +01:00
|
|
|
const owner = exports.owner, serverUrl = exports.serverUrl, user = exports.user, admin = exports.admin;
|
2021-08-12 21:24:18 -07:00
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
await setupServer();
|
|
|
|
|
await safe(fs.promises.unlink(support._sshInfo().filePath));
|
|
|
|
|
|
|
|
|
|
// setup
|
2023-08-10 16:50:29 +05:30
|
|
|
let response = await superagent.post(`${serverUrl}/api/v1/provision/setup`)
|
2022-01-05 22:41:41 -08:00
|
|
|
.send({ domainConfig: { provider: 'noop', domain: exports.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
|
|
|
|
2025-05-07 14:09:10 +02:00
|
|
|
// create owner
|
2025-06-06 09:31:31 +02:00
|
|
|
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' });
|
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' })
|
|
|
|
|
.send({ username: owner.username, password: owner.password, email: owner.email });
|
|
|
|
|
expect(response.status).to.eql(201);
|
|
|
|
|
owner.token = response.body.token;
|
|
|
|
|
owner.id = response.body.userId;
|
2025-06-06 09:31:31 +02:00
|
|
|
expect(scope1.isDone()).to.be.ok();
|
2025-06-06 11:25:57 +02:00
|
|
|
scope1.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 })
|
|
|
|
|
.send({ username: admin.username, email: admin.email, password: admin.password });
|
|
|
|
|
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-03-07 11:53:03 +01:00
|
|
|
const token1 = await tokens.add({ identifier: admin.id, clientId: tokens.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-03-07 11:53:03 +01:00
|
|
|
const token2 = await tokens.add({ identifier: user.id, clientId: tokens.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(exports.app.id, exports.app.appStoreId, exports.app.manifest, exports.app.subdomain, exports.app.domain, exports.app.portBindings, exports.app);
|
|
|
|
|
|
2022-03-31 21:50:53 -07:00
|
|
|
await settings._set(settings.APPSTORE_API_TOKEN_KEY, exports.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-08-20 09:19:44 -07:00
|
|
|
await database._clear();
|
2021-09-07 09:57:49 -07:00
|
|
|
await server.stop();
|
2023-03-26 16:34:14 +02:00
|
|
|
await oidc.stop();
|
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._mailQueue = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2022-04-02 17:09:08 -07:00
|
|
|
|
|
|
|
|
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++) {
|
2022-04-02 17:09:08 -07:00
|
|
|
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`);
|
2022-04-02 17:09:08 -07:00
|
|
|
}
|
|
|
|
|
throw new Error(`Task ${taskId} never finished`);
|
|
|
|
|
}
|