diff --git a/test/test.js b/test/test.js index f728659..db24347 100644 --- a/test/test.js +++ b/test/test.js @@ -1,214 +1,359 @@ #!/usr/bin/env node /* jshint esversion: 8 */ -/* global describe */ -/* global before */ -/* global after */ -/* global it */ -/* global xit */ +/* 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, +const 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'); + +if (!process.env.USERNAME || !process.env.PASSWORD) { + console.log('USERNAME and PASSWORD env vars need to be set'); + process.exit(1); +} + describe('Application life cycle test', function () { this.timeout(0); + const ELEMENT_LOCATION = 'element-test'; const LOCATION = 'test'; - const TEST_TIMEOUT = 10000; + const TEST_TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 10000; const EXEC_ARGS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }; + const USERNAME = process.env.USERNAME; + const PASSWORD = process.env.PASSWORD; + const ROOM_ID = Math.floor((Math.random() * 100) + 1); + const ROOM_NAME = 'Test room ' + ROOM_ID; + const MSG_TEXT = 'Test message '; - const username = process.env.USERNAME; - const password = process.env.PASSWORD; - - var app, browser; - var token, roomId; + let browser, app; + let athenticated_by_oidc = false; 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'); + const options = new Options().windowSize({ width: 1280, height: 1024 }); + if (process.env.HEADLESS) options.addArguments('headless'); - browser = new Builder().forBrowser('chrome').setChromeOptions(new Options().windowSize({ width: 1280, height: 1024 })).build(); + browser = new Builder().forBrowser('chrome').setChromeOptions(options).build(); }); after(function () { browser.quit(); }); + function sleep(millis) { + return new Promise(resolve => setTimeout(resolve, millis)); + } + + async function waitForElement(elem) { + await browser.wait(until.elementLocated(elem), TEST_TIMEOUT); + await browser.wait(until.elementIsVisible(browser.findElement(elem)), TEST_TIMEOUT); + } + function getAppInfo() { - var inspect = JSON.parse(execSync('cloudron inspect')); + const 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'); } + function getElementAppInfo() { + const inspect = JSON.parse(execSync('cloudron inspect')); + app = inspect.apps.filter(function (a) { return a.location.indexOf(ELEMENT_LOCATION) === 0; })[0]; + expect(app).to.be.an('object'); + } + + function getMessage() { + return MSG_TEXT + Math.floor((Math.random() * 100) + 1); + } + + async function updateSynapseConfig() { + console.log(`Setting Synapse Matrix server location to "https://${app.fqdn}"`); + execSync(`cloudron exec --app ${ELEMENT_LOCATION} -- bash -c "jq '.default_server_config[\\"m.homeserver\\"].base_url = \\"https://${app.fqdn}\\"' /app/data/config.json | sponge /app/data/config.json"`); + execSync(`cloudron restart --app ${ELEMENT_LOCATION}`); + // wait when all services are up and running + await sleep(15000); + } + 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(); - }); - }); + async function registerUser() { + await browser.get(`https://${app.fqdn}/#/register`); + await waitForElement(By.xpath('//input[@label="Username"]')); + await browser.findElement(By.xpath('//input[@label="Username"]')).sendKeys(USERNAME); + await browser.sleep(2000); + await browser.findElement(By.xpath('//input[@label="Password"]')).sendKeys(PASSWORD); + await browser.sleep(2000); + await browser.findElement(By.xpath('//input[@label="Confirm password"]')).sendKeys(PASSWORD); + await browser.sleep(2000); + await browser.findElement(By.xpath('//input[@value="Register"]')).click(); + await browser.sleep(2000); + await waitForElement(By.xpath('//h1[text()="You\'re in"]')); + await browser.sleep(2000); + await browser.findElement(By.xpath('//div[@role="button" and text()="Skip"]')).click(); + await browser.sleep(2000); + await waitForElement(By.xpath(`//h1[contains(., "Welcome")]`)); } - // 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)); + async function loginOIDC(username, password) { + browser.manage().deleteAllCookies(); + await browser.get(`https://${app.fqdn}/#/login`); + await browser.sleep(6000); - token = result.body.access_token; - if (!token) return done(new Error('No token')); + await waitForElement(By.xpath('//div[@role="button" and contains(., "Continue with Cloudron")]')); + await browser.findElement(By.xpath('//div[@role="button" and contains(., "Continue with Cloudron")]')).click(); + await browser.sleep(2000); - done(); - }); + if (!athenticated_by_oidc) { + await waitForElement(By.xpath('//input[@name="username"]')); + await browser.findElement(By.xpath('//input[@name="username"]')).sendKeys(username); + await browser.findElement(By.xpath('//input[@name="password"]')).sendKeys(password); + await browser.sleep(2000); + await browser.findElement(By.xpath('//button[@type="submit" and contains(text(), "Sign in")]')).click(); + await browser.sleep(2000); + + athenticated_by_oidc = true; + } + + await waitForElement(By.xpath('//p[@class="confirm-trust" and contains(., "Continuing will grant ")]')); + await browser.findElement(By.xpath('//a[contains(., "Continue")]')).click(); + await browser.sleep(2000); + + if (await browser.findElements(By.xpath('//div[@aria-label="Skip verification for now"]')).then(found => !!found.length)) { + await skipVerification(); + } + + await browser.sleep(3000); + await waitForElement(By.xpath(`//h1[contains(., "Welcome")]`)); } - 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)); + async function login() { + await browser.get('https://' + app.fqdn + '/#/login'); + await browser.wait(until.elementLocated(By.xpath('//input[@value="Sign in"]')), TEST_TIMEOUT); + await browser.findElement(By.xpath('//input[@name="username"]')).sendKeys(USERNAME); + await browser.findElement(By.xpath('//input[@name="password"]')).sendKeys(PASSWORD); + await browser.findElement(By.xpath('//input[@value="Sign in"]')).click(); + await browser.sleep(5000); - if (result.body.joined_rooms.length !== 1) return done(new Error('User must have auto-joined discuss channel:' + result.statusCode)); - done(); - }); + if (await browser.findElements(By.xpath('//div[@aria-label="Skip verification for now"]')).then(found => !!found.length)) { + await skipVerification(); + } + + await browser.wait(until.elementLocated(By.xpath('//span[text()="Rooms"]')), TEST_TIMEOUT); } - 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(); - }); + async function skipVerification() { + await browser.wait(until.elementLocated(By.xpath('//div[@aria-label="Skip verification for now"]')), TEST_TIMEOUT); + await browser.sleep(5000); + await browser.findElement(By.xpath('//div[@aria-label="Skip verification for now"]')).click(); + await browser.wait(until.elementLocated(By.xpath('//div[contains(text(), "verify later")]')), TEST_TIMEOUT); + await browser.sleep(5000); + await browser.findElement(By.xpath('//div[contains(text(), "verify later")]')).click(); + await browser.sleep(5000); } - 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)); + async function logout() { + await browser.get('https://' + app.fqdn + '/#/home'); + await browser.sleep(5000); + await waitForElement(By.xpath('//div[@role="button" and @title="User menu"]')); - if (!result.body.joined_rooms.includes(roomId)) return done(new Error('No room in list: ' + JSON.stringify(result.body))); + await browser.findElement(By.xpath('//div[@role="button" and @title="User menu"]')).click(); + await browser.sleep(2000); - done(); - }); + await browser.findElement(By.xpath('//li[@role="menuitem" and @aria-label="Sign out"]')).click(); + await browser.sleep(2000); + + if (await browser.findElements(By.xpath('//button[contains(text(), "I don\'t want my encrypted messages")]')).then(found => !!found.length)) { + await browser.findElement(By.xpath('//button[contains(text(), "I don\'t want my encrypted messages")]')).click(); + await browser.sleep(3000); + } + + await waitForElement(By.xpath('//h1[text()="Sign in"]')); + } + + async function isLoggedIn() { + await browser.get('https://' + app.fqdn + '/#/home'); + await browser.wait(until.elementLocated(By.xpath('//span[text()="Rooms"]')), TEST_TIMEOUT); + } + + async function createRoom() { + await browser.get('https://' + app.fqdn + '/#/home'); + await browser.sleep(4000); + await waitForElement(By.xpath('//div[@role="button" and @aria-label="Add room"]')); + await browser.findElement(By.xpath('//div[@role="button" and @aria-label="Add room"]')).click(); + await browser.sleep(2000); + await browser.findElement(By.xpath('//li[@role="menuitem" and @aria-label="New room"]')).click(); + await browser.sleep(2000); + + await browser.findElement(By.xpath('//input[@label="Name"]')).sendKeys(ROOM_NAME); + await browser.sleep(2000); + + await browser.findElement(By.xpath('//button[text()="Create room"]')).click(); + await browser.sleep(2000); + + await waitForElement(By.xpath('//div[@role="button" and @aria-label="Add room"]')); + + await waitForElement(By.xpath('//div[@class="mx_RoomTile_titleContainer"]/div[@title="' + ROOM_NAME + '"]')); + } + + async function checkRoom() { + await browser.get('https://' + app.fqdn + '/#/home'); + await browser.sleep(4000); + await waitForElement(By.xpath('//div[@role="treeitem" and @aria-label="' + ROOM_NAME + '"]')); + await browser.findElement(By.xpath('//div[@role="treeitem" and @aria-label="' + ROOM_NAME + '"]')).click(); + await browser.sleep(2000); + await waitForElement(By.xpath('//h2[text()="' + ROOM_NAME + '"]')); + } + + async function sendMessage() { + await checkRoom(); + + await browser.findElement(By.xpath('//div[contains(@class, "mx_BasicMessageComposer_input")]')).sendKeys(getMessage()); + await browser.sleep(2000); + + await browser.findElement(By.xpath('//div[@role="button" and @aria-label="Send message"]')).click(); + await browser.sleep(2000); } 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('can install element-web app', function () { execSync('cloudron install --appstore-id im.riot.cloudronapp --location ' + ELEMENT_LOCATION, EXEC_ARGS); }); + it('update element-app config', updateSynapseConfig); + + it('can get Element app info', getElementAppInfo); + it('can register new user', registerUser); + it('create room', createRoom); + it('can send message', sendMessage); + + it('can logout', logout); + + it('can login', login); + it('check room', checkRoom); + it('can logout', logout); + + it('can get app info', getAppInfo); + + it('uninstall element-web app', async function () { + await browser.get('about:blank'); + execSync(`cloudron uninstall --app ${ELEMENT_LOCATION}`, EXEC_ARGS); + }); 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 info', getAppInfo); - it('can get app information', getAppInfo); + it('can install element-web app', function () { execSync('cloudron install --appstore-id im.riot.cloudronapp --location ' + ELEMENT_LOCATION, EXEC_ARGS); }); + it('update element-app config', updateSynapseConfig); - it('check landing page', checkLandingPage); - it('can login', checkLogin); - it('check autojoin', checkAutoJoinRoom); + it('can get Element app info', getElementAppInfo); + it('can login via OIDC', loginOIDC.bind(null, USERNAME, PASSWORD)); it('create room', createRoom); + it('can send message', sendMessage); + it('can get app info', getAppInfo); + + it('can restart app', function () { execSync(`cloudron restart ${app.id}`); }); + + it('backup app', function () { execSync(`cloudron backup create --app ${app.id}`, EXEC_ARGS); }); + + it('can get Element app info', getElementAppInfo); + it('is logged in', isLoggedIn); it('check room', checkRoom); + it('can get app info', getAppInfo); - it('can restart app', function () { execSync('cloudron restart'); }); + it('restore app', async function () { + const backups = JSON.parse(execSync(`cloudron backup list --raw --app ${app.id}`)); - it('check landing page', checkLandingPage); - it('check room', checkRoom); + await browser.get('about:blank'); + execSync(`cloudron uninstall --app ${app.id}`, EXEC_ARGS); + execSync(`cloudron install --location ${LOCATION}`, EXEC_ARGS); - 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('can get Element app info', getElementAppInfo); + it('is logged in', isLoggedIn); it('check room', checkRoom); + it('can send message', sendMessage); + it('can logout', logout); + it('can get app info', getAppInfo); - it('move to different location', function () { + it('move to different location', async function () { browser.manage().deleteAllCookies(); - execSync('cloudron configure --location ' + LOCATION + '2', EXEC_ARGS); + await browser.get('about:blank'); + + execSync(`cloudron configure --location ${LOCATION}2`, EXEC_ARGS); getAppInfo(); + // wait when all services are up and running + await sleep(15000); }); - it('check landing page', checkLandingPage); - it('check room', checkRoom); + it('update element-app config', updateSynapseConfig); - it('uninstall app', function () { execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS); }); + it('can get Element app info', getElementAppInfo); + it('can login via OIDC', loginOIDC.bind(null, USERNAME, PASSWORD)); + it('check room', checkRoom); + it('can send message', sendMessage); + + it('can logout', logout); + it('can get app info', getAppInfo); + + it('uninstall app', async function () { + await browser.get('about:blank'); + 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 info', getAppInfo); + it('update element-app config', updateSynapseConfig); - it('can get app information', getAppInfo); + it('can get Element app info', getElementAppInfo); - it('check landing page', checkLandingPage); - it('can login', checkLogin); + it('can login', login); it('create room', createRoom); + it('can send message', sendMessage); + it('can logout', logout); + it('can get app info', getAppInfo); + + it('can update', async function () { + await browser.get('about:blank'); + execSync(`cloudron update --app ${app.id}`, EXEC_ARGS); + // wait when all services are up and running + await sleep(15000); + }); + + it('can get Element app info', getElementAppInfo); + it('can login via OIDC', loginOIDC.bind(null, USERNAME, PASSWORD)); + it('is logged in', isLoggedIn); it('check room', checkRoom); + it('can send message', sendMessage); + it('can get app info', getAppInfo); - it('can update', function () { execSync('cloudron update --app ' + LOCATION, EXEC_ARGS); }); + it('uninstall app', async function () { + await browser.get('about:blank'); + execSync(`cloudron uninstall --app ${app.id}`, EXEC_ARGS); + }); - it('check landing page', checkLandingPage); - it('check room', checkRoom); - - it('uninstall app', function () { execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS); }); + it('uninstall element-web app', function () { + execSync(`cloudron uninstall --app ${ELEMENT_LOCATION}`, EXEC_ARGS); + }); });