2021-05-17 22:23:18 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
const constants = require('../../constants.js'),
|
2021-05-17 22:23:18 -07:00
|
|
|
database = require('../../database.js'),
|
2022-04-15 07:52:35 -05:00
|
|
|
delay = require('../../delay.js'),
|
2021-05-17 22:23:18 -07:00
|
|
|
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'),
|
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'),
|
2021-06-01 09:35:20 -07:00
|
|
|
superagent = require('superagent'),
|
2022-04-02 17:09:08 -07:00
|
|
|
tasks = require('../../tasks.js'),
|
2021-09-07 09:57:49 -07:00
|
|
|
tokens = require('../../tokens.js');
|
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
|
|
|
|
|
},
|
|
|
|
|
|
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',
|
2021-06-03 22:39:26 -07:00
|
|
|
|
2021-06-05 11:46:34 -07:00
|
|
|
serverUrl: `http://localhost:${constants.PORT}`,
|
2021-05-17 22:23:18 -07:00
|
|
|
};
|
|
|
|
|
|
2021-09-07 09:57:49 -07:00
|
|
|
async function setupServer() {
|
|
|
|
|
await database._clear();
|
2023-03-27 11:31:37 +02:00
|
|
|
await oidc.stop();
|
2023-03-21 13:54:40 +01:00
|
|
|
await database.initialize();
|
2021-09-07 09:57:49 -07:00
|
|
|
await settings._setApiServerOrigin(exports.mockApiServerOrigin);
|
2023-03-21 13:54:40 +01:00
|
|
|
await server.start();
|
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() {
|
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
|
|
|
|
|
let response = await superagent.post(`${serverUrl}/api/v1/cloudron/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);
|
|
|
|
|
|
|
|
|
|
await delay(2000);
|
|
|
|
|
|
|
|
|
|
// create admin
|
|
|
|
|
response = await superagent.post(`${serverUrl}/api/v1/cloudron/activate`)
|
|
|
|
|
.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;
|
|
|
|
|
|
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...)
|
|
|
|
|
const token1 = await tokens.add({ identifier: admin.id, clientId: 'test-client-id', expires: Date.now() + (60 * 60 * 1000), name: 'fromtest' });
|
|
|
|
|
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...)
|
2022-11-23 17:48:05 +01:00
|
|
|
const token2 = await tokens.add({ identifier: user.id, clientId: 'test-client-id', expires: Date.now() + (60 * 60 * 1000), name: 'fromtest' });
|
|
|
|
|
user.token = token2.accessToken;
|
2021-11-17 11:14:33 -08:00
|
|
|
|
2022-03-31 21:50:53 -07:00
|
|
|
await settings._set(settings.APPSTORE_API_TOKEN_KEY, exports.appstoreToken); // appstore token
|
2021-05-17 22:23:18 -07:00
|
|
|
}
|
|
|
|
|
|
2021-08-20 09:19:44 -07:00
|
|
|
async function cleanup() {
|
|
|
|
|
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();
|
2021-05-17 22:23:18 -07:00
|
|
|
}
|
2021-06-05 11:46:34 -07:00
|
|
|
|
|
|
|
|
function clearMailQueue() {
|
|
|
|
|
mailer._mailQueue = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function checkMails(number) {
|
|
|
|
|
await delay(1000);
|
|
|
|
|
expect(mailer._mailQueue.length).to.equal(number);
|
|
|
|
|
clearMailQueue();
|
|
|
|
|
}
|
2022-04-02 17:09:08 -07:00
|
|
|
|
|
|
|
|
async function waitForTask(taskId) {
|
|
|
|
|
// eslint-disable-next-line no-constant-condition
|
|
|
|
|
for (let i = 0; i < 10; 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}`);
|
|
|
|
|
}
|
2022-04-02 17:09:08 -07:00
|
|
|
await delay(2000);
|
|
|
|
|
console.log(`Waiting for task to ${taskId} finish`);
|
|
|
|
|
}
|
|
|
|
|
throw new Error(`Task ${taskId} never finished`);
|
|
|
|
|
}
|