257 lines
9.4 KiB
JavaScript
257 lines
9.4 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/* jslint node:true */
|
|
/* global it:false */
|
|
/* global xit:false */
|
|
/* global describe:false */
|
|
/* global before:false */
|
|
/* global after:false */
|
|
|
|
'use strict';
|
|
|
|
require('chromedriver');
|
|
|
|
var execSync = require('child_process').execSync,
|
|
expect = require('expect.js'),
|
|
path = require('path'),
|
|
superagent = require('superagent'),
|
|
webdriver = require('selenium-webdriver');
|
|
|
|
var by = require('selenium-webdriver').By,
|
|
until = require('selenium-webdriver').until,
|
|
Key = require('selenium-webdriver').Key;
|
|
|
|
describe('Application life cycle test', function () {
|
|
this.timeout(0);
|
|
var server, browser = new webdriver.Builder().forBrowser('chrome').build();
|
|
|
|
var LOCATION = 'test';
|
|
var app;
|
|
var username = process.env.USERNAME;
|
|
var password = process.env.PASSWORD;
|
|
var TIMEOUT = process.env.TIMEOUT | 30000;
|
|
var token, roomId;
|
|
|
|
before(function (done) {
|
|
if (!process.env.USERNAME) return done(new Error('USERNAME env var not set'));
|
|
if (!process.env.PASSWORD) return done(new Error('PASSWORD env var not set'));
|
|
|
|
var seleniumJar= require('selenium-server-standalone-jar');
|
|
var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
|
|
server = new SeleniumServer(seleniumJar.path, { port: 4444 });
|
|
server.start();
|
|
|
|
done();
|
|
});
|
|
|
|
after(function (done) {
|
|
browser.quit();
|
|
server.stop();
|
|
done();
|
|
});
|
|
|
|
function checkLandingPage(done) {
|
|
browser.get('https://' + app.fqdn).then(function () {
|
|
return browser.wait(until.elementLocated(by.xpath('//h1[contains(text(),"Synapse is running")]')), TIMEOUT);
|
|
}).then(function () {
|
|
done();
|
|
});
|
|
}
|
|
|
|
// https://matrix.org/docs/spec/client_server/latest#user-interactive-api-in-the-rest-api
|
|
function registerUser(done) {
|
|
superagent.post('https://' + app.fqdn + '/_matrix/client/r0/register?kind=user').send({
|
|
username: username,
|
|
password: password,
|
|
inhibit_login: false
|
|
}).end(function (error, result) {
|
|
// we will first get a 401
|
|
let session = result.body.session;
|
|
console.log('session is', session);
|
|
if (result.statusCode !== 401) return done(new Error('Expecting a 401 ' + result.statusCode));
|
|
|
|
superagent.post('https://' + app.fqdn + '/_matrix/client/r0/register?kind=user').send({
|
|
auth: {
|
|
type: 'm.login.dummy',
|
|
session: session
|
|
},
|
|
username: username,
|
|
password: password,
|
|
inhibit_login: false
|
|
}).end(function (error, result) {
|
|
if (error) return done(error);
|
|
if (result.statusCode !== 200) return done(new Error('Login failed with status ' + result.statusCode));
|
|
|
|
console.log('registered user with id', result.user_id);
|
|
|
|
done();
|
|
});
|
|
});
|
|
}
|
|
|
|
// https://matrix.org/docs/spec/client_server/latest
|
|
function checkLogin(done) {
|
|
superagent.post('https://' + app.fqdn + '/_matrix/client/r0/login').send({
|
|
type: 'm.login.password',
|
|
user: username,
|
|
password: password
|
|
}).end(function (error, result) {
|
|
if (error) return done(error);
|
|
if (result.statusCode !== 200) return done(new Error('Login failed with status ' + result.statusCode));
|
|
|
|
token = result.body.access_token;
|
|
if (!token) return done(new Error('No token'));
|
|
|
|
done();
|
|
});
|
|
}
|
|
|
|
function checkAutoJoinRoom(done) {
|
|
superagent.get('https://' + app.fqdn + '/_matrix/client/r0/joined_rooms?access_token=' + token).end(function (error, result) {
|
|
if (error) return done(error);
|
|
if (result.statusCode !== 200) return done(new Error('Room listing failed with status ' + result.statusCode));
|
|
|
|
if (result.body.joined_rooms.length !== 1) return done(new Error('User must have auto-joined discuss channel:' + result.statusCode));
|
|
done();
|
|
});
|
|
}
|
|
|
|
function createRoom(done) {
|
|
superagent.post('https://' + app.fqdn + '/_matrix/client/r0/createRoom?access_token=' + token).send({
|
|
room_alias_name: 'general'
|
|
}).end(function (error, result) {
|
|
if (error) return done(error);
|
|
if (result.statusCode !== 200) return done(new Error('Room creation failed with status ' + result.statusCode));
|
|
|
|
roomId = result.body.room_id;
|
|
if (!roomId) return done(new Error('No room id'));
|
|
|
|
done();
|
|
});
|
|
}
|
|
|
|
function checkRoom(done) {
|
|
superagent.get('https://' + app.fqdn + '/_matrix/client/r0/joined_rooms?access_token=' + token).end(function (error, result) {
|
|
if (error) return done(error);
|
|
if (result.statusCode !== 200) return done(new Error('Room listing failed with status ' + result.statusCode));
|
|
|
|
if (!result.body.joined_rooms.includes(roomId)) return done(new Error('No room in list: ' + JSON.stringify(result.body)));
|
|
|
|
done();
|
|
});
|
|
}
|
|
|
|
xit('build app', function () {
|
|
execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
});
|
|
|
|
// No SSO
|
|
it('install app (no sso)', function () {
|
|
execSync('cloudron install --no-sso --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
});
|
|
|
|
it('can get app information', function () {
|
|
var inspect = JSON.parse(execSync('cloudron inspect'));
|
|
|
|
app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
|
|
|
|
expect(app).to.be.an('object');
|
|
});
|
|
|
|
it('check landing page', checkLandingPage);
|
|
it('can register new user', registerUser);
|
|
it('can login', checkLogin);
|
|
it('check autojoin', checkAutoJoinRoom);
|
|
it('create room', createRoom);
|
|
it('check room', checkRoom);
|
|
|
|
it('uninstall app', function () {
|
|
execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
});
|
|
|
|
// SSO
|
|
it('install app', function () {
|
|
execSync('cloudron install --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
});
|
|
|
|
it('can get app information', function () {
|
|
var inspect = JSON.parse(execSync('cloudron inspect'));
|
|
|
|
app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
|
|
|
|
expect(app).to.be.an('object');
|
|
});
|
|
|
|
it('check landing page', checkLandingPage);
|
|
it('can login', checkLogin);
|
|
it('check autojoin', checkAutoJoinRoom);
|
|
it('create room', createRoom);
|
|
it('check room', checkRoom);
|
|
|
|
it('can restart app', function (done) {
|
|
execSync('cloudron restart');
|
|
done();
|
|
});
|
|
|
|
it('check landing page', checkLandingPage);
|
|
it('check room', checkRoom);
|
|
|
|
it('backup app', function () {
|
|
execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
});
|
|
|
|
it('check landing page', checkLandingPage);
|
|
it('check room', checkRoom);
|
|
|
|
it('restore app', function () {
|
|
const backups = JSON.parse(execSync('cloudron backup list --raw'));
|
|
execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
execSync('cloudron install --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
var inspect = JSON.parse(execSync('cloudron inspect'));
|
|
app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
|
|
execSync(`cloudron restore --backup ${backups[0].id} --app ${app.id}`, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
});
|
|
|
|
it('check landing page', checkLandingPage);
|
|
it('check room', checkRoom);
|
|
|
|
it('move to different location', function () {
|
|
browser.manage().deleteAllCookies();
|
|
execSync('cloudron configure --location ' + LOCATION + '2', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
var inspect = JSON.parse(execSync('cloudron inspect'));
|
|
app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
|
|
expect(app).to.be.an('object');
|
|
});
|
|
|
|
it('check landing page', checkLandingPage);
|
|
it('check room', checkRoom);
|
|
|
|
it('uninstall app', function () {
|
|
execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
});
|
|
|
|
// test update
|
|
it('can install app', function () {
|
|
execSync('cloudron install --appstore-id org.matrix.synapse --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
var inspect = JSON.parse(execSync('cloudron inspect'));
|
|
app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
|
|
expect(app).to.be.an('object');
|
|
});
|
|
|
|
it('check landing page', checkLandingPage);
|
|
it('can login', checkLogin);
|
|
it('create room', createRoom);
|
|
it('check room', checkRoom);
|
|
|
|
it('can update', function () {
|
|
execSync('cloudron update --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
});
|
|
|
|
it('check landing page', checkLandingPage);
|
|
it('check room', checkRoom);
|
|
it('uninstall app', function () {
|
|
execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
|
|
});
|
|
});
|
|
|