2021-05-17 22:23:18 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const async = require('async'),
|
|
|
|
|
constants = require('../../constants.js'),
|
|
|
|
|
database = require('../../database.js'),
|
2021-06-05 11:46:34 -07:00
|
|
|
delay = require('delay'),
|
2021-05-17 22:23:18 -07:00
|
|
|
expect = require('expect.js'),
|
2021-06-05 11:46:34 -07:00
|
|
|
mailer = require('../../mailer.js'),
|
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-06-01 09:35:20 -07:00
|
|
|
superagent = require('superagent'),
|
2021-06-04 09:28:40 -07:00
|
|
|
tokens = require('../../tokens.js');
|
2021-05-17 22:23:18 -07:00
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
setup,
|
|
|
|
|
cleanup,
|
2021-06-05 11:46:34 -07:00
|
|
|
clearMailQueue,
|
|
|
|
|
checkMails,
|
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',
|
|
|
|
|
token: null
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
user: {
|
|
|
|
|
id: null,
|
|
|
|
|
username: 'user',
|
|
|
|
|
password: 'Foobar?1338',
|
|
|
|
|
email: 'user@cloudron.local',
|
2021-05-17 22:23:18 -07:00
|
|
|
token: null
|
|
|
|
|
},
|
|
|
|
|
|
2021-06-03 22:39:26 -07:00
|
|
|
MOCK_API_SERVER_ORIGIN: 'http://localhost:6060',
|
|
|
|
|
DASHBOARD_DOMAIN: 'test.example.com',
|
|
|
|
|
DASHBOARD_FQDN: 'my.test.example.com',
|
|
|
|
|
|
2021-06-05 11:46:34 -07:00
|
|
|
serverUrl: `http://localhost:${constants.PORT}`,
|
2021-05-17 22:23:18 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function setup(done) {
|
2021-06-01 09:35:20 -07:00
|
|
|
const owner = exports.owner, serverUrl = exports.serverUrl, user = exports.user;
|
2021-05-17 22:23:18 -07:00
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
|
server.start.bind(null),
|
|
|
|
|
database._clear.bind(null),
|
|
|
|
|
|
2021-06-03 22:39:26 -07:00
|
|
|
settings._setApiServerOrigin.bind(null, exports.MOCK_API_SERVER_ORIGIN),
|
|
|
|
|
settings.setDashboardLocation.bind(null, exports.DASHBOARD_DOMAIN, exports.DASHBOARD_FQDN),
|
|
|
|
|
|
2021-05-17 22:23:18 -07:00
|
|
|
function createAdmin(callback) {
|
|
|
|
|
superagent.post(`${serverUrl}/api/v1/cloudron/activate`)
|
|
|
|
|
.query({ setupToken: 'somesetuptoken' })
|
|
|
|
|
.send({ username: owner.username, password: owner.password, email: owner.email })
|
|
|
|
|
.end(function (error, result) {
|
|
|
|
|
expect(result).to.be.ok();
|
|
|
|
|
expect(result.statusCode).to.eql(201);
|
|
|
|
|
|
|
|
|
|
// stash token for further use
|
|
|
|
|
owner.token = result.body.token;
|
2021-06-04 14:24:39 -07:00
|
|
|
owner.id = result.body.userId;
|
2021-05-17 22:23:18 -07:00
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
2021-06-01 09:35:20 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
function createUser(callback) {
|
|
|
|
|
superagent.post(`${serverUrl}/api/v1/users`)
|
|
|
|
|
.query({ access_token: owner.token })
|
2021-06-05 10:37:11 -07:00
|
|
|
.send({ username: user.username, email: user.email, password: user.password })
|
2021-06-04 09:28:40 -07:00
|
|
|
.end(async function (error, result) {
|
2021-06-01 09:35:20 -07:00
|
|
|
expect(error).to.not.be.ok();
|
|
|
|
|
expect(result.statusCode).to.equal(201);
|
|
|
|
|
|
|
|
|
|
user.id = result.body.id;
|
|
|
|
|
|
|
|
|
|
// HACK to get a token for second user (passwords are generated and the user should have gotten a password setup link...)
|
2021-06-04 09:28:40 -07:00
|
|
|
const token = await tokens.add({ identifier: user.id, clientId: 'test-client-id', expires: Date.now() + 10000, name: 'fromtest' });
|
|
|
|
|
user.token = token.accessToken;
|
|
|
|
|
callback();
|
2021-06-01 09:35:20 -07:00
|
|
|
});
|
2021-05-17 22:23:18 -07:00
|
|
|
}
|
2021-06-01 09:35:20 -07:00
|
|
|
|
2021-05-17 22:23:18 -07:00
|
|
|
], done);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cleanup(done) {
|
|
|
|
|
database._clear(function (error) {
|
|
|
|
|
expect(!error).to.be.ok();
|
|
|
|
|
|
|
|
|
|
server.stop(done);
|
|
|
|
|
});
|
|
|
|
|
}
|
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();
|
|
|
|
|
}
|