#!/usr/bin/env node /* jshint esversion: 8 */ /* global describe */ /* global before */ /* global after */ /* global it */ /* global xit */ 'use strict'; require('chromedriver'); var execSync = require('child_process').execSync, expect = require('expect.js'), path = require('path'), superagent = require('superagent'), { Builder, By, Key, until } = require('selenium-webdriver'), { Options } = require('selenium-webdriver/chrome'); describe('Application life cycle test', function () { this.timeout(0); const LOCATION = 'test'; const TEST_TIMEOUT = 10000; const EXEC_ARGS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }; const username = process.env.USERNAME; const password = process.env.PASSWORD; var app, browser; var token, roomId; before(function () { if (!process.env.USERNAME) throw new Error('USERNAME env var not set'); if (!process.env.PASSWORD) throw new Error('PASSWORD env var not set'); browser = new Builder().forBrowser('chrome').setChromeOptions(new Options().windowSize({ width: 1280, height: 1024 })).build(); }); after(function () { browser.quit(); }); function getAppInfo() { var inspect = JSON.parse(execSync('cloudron inspect')); app = inspect.apps.filter(function (a) { return a.location.indexOf(LOCATION) === 0; })[0]; expect(app).to.be.an('object'); } async function checkLandingPage() { await browser.get(`https://${app.fqdn}`); await browser.wait(until.elementLocated(By.xpath('//h1[contains(text(),"Synapse is running")]')), TEST_TIMEOUT); } // 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.body.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', EXEC_ARGS); }); // No SSO it('install app (no sso)', function () { execSync('cloudron install --no-sso --location ' + LOCATION, EXEC_ARGS); }); it('can get app information', getAppInfo); 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, EXEC_ARGS); }); // SSO it('install app', function () { execSync('cloudron install --location ' + LOCATION, EXEC_ARGS); }); it('can get app information', getAppInfo); 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 () { execSync('cloudron restart'); }); it('check landing page', checkLandingPage); it('check room', checkRoom); it('backup app', function () { execSync('cloudron backup create --app ' + app.id, EXEC_ARGS); }); 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, EXEC_ARGS); execSync('cloudron install --location ' + LOCATION, EXEC_ARGS); getAppInfo(); execSync(`cloudron restore --backup ${backups[0].id} --app ${app.id}`, EXEC_ARGS); }); it('check landing page', checkLandingPage); it('check room', checkRoom); it('move to different location', function () { browser.manage().deleteAllCookies(); execSync('cloudron configure --location ' + LOCATION + '2', EXEC_ARGS); getAppInfo(); }); it('check landing page', checkLandingPage); it('check room', checkRoom); it('uninstall app', function () { execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS); }); // test update it('can install app', function () { execSync('cloudron install --appstore-id org.matrix.synapse --location ' + LOCATION, EXEC_ARGS); }); it('can get app information', getAppInfo); 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, EXEC_ARGS); }); it('check landing page', checkLandingPage); it('check room', checkRoom); it('uninstall app', function () { execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS); }); });