#!/usr/bin/env node 'use strict'; var execSync = require('child_process').execSync, expect = require('expect.js'), path = require('path'), superagent = require('superagent'), webdriver = require('selenium-webdriver'); var by = webdriver.By, Keys = webdriver.Key, until = webdriver.until; process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; describe('Application life cycle test', function () { this.timeout(0); var chrome = require('selenium-webdriver/chrome'); var server, browser = new chrome.Driver(), uploadedImageUrl; var username = 'admin', password = 'changeme'; before(function (done) { 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(); }); var LOCATION = 'test'; var TEST_TIMEOUT = 30000; var app; var email, token; function login(done) { browser.manage().window().setSize(1280,768); browser.manage().deleteAllCookies(); browser.get('https://' + app.fqdn + '/login'); browser.executeScript('localStorage.clear();') browser.get('https://' + app.fqdn + '/login'); browser.sleep(5000); // takes some time for username to be visible browser.wait(until.elementLocated(by.id('username')), TEST_TIMEOUT).then(function () { browser.findElement(by.id('username')).sendKeys(username); browser.findElement(by.id('password')).sendKeys(password); browser.findElement(by.id('login')).click(); browser.wait(until.elementLocated(by.xpath('//a[contains(text(), "Announcements")]')), TEST_TIMEOUT).then(function () { done(); }); }); } function checkMailPlugin(done) { browser.get('https://' + app.fqdn + '/admin/emailers/local'); browser.wait(until.elementLocated(by.id('host')), TEST_TIMEOUT).then(function () { browser.findElement(by.id('host')).getAttribute('value').then(function (val) { if (val !== 'mail') return done(new Error('Incorrect mail server value: ' + val)); done(); }); }); } function restartForum(done) { browser.get('https://' + app.fqdn + '/admin').then(function () { return browser.findElement(by.xpath('//button[text()="Restart"]')).click(); }).then(function () { return browser.sleep(3000); }).then(function () { return browser.findElement(by.xpath('//button[text()="Confirm"]')).click(); }).then(function () { return browser.sleep(30000); // wait 30secs for reload }).then(function () { return browser.get('https://' + app.fqdn + '/admin'); }).then(function () { return browser.findElement(by.xpath('//h1[text()="Dashboard"]')) }).then(function () { done(); }); } function installCustomPlugin(done) { execSync('cloudron exec --app ' + app.id + ' -- /usr/local/bin/gosu cloudron:cloudron npm install nodebb-plugin-beep', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); done(); } function activateCustomPlugin(done) { browser.get('https://' + app.fqdn + '/admin/extend/plugins#installed'); browser.wait(until.elementLocated(by.xpath('//ul[contains(@class, "installed")]//strong[text()="nodebb-plugin-beep"]')), TEST_TIMEOUT).then(function () { return browser.sleep(10000); }).then(function () { return browser.findElement(by.xpath('//li[@id="nodebb-plugin-beep"]//button[@data-action="toggleActive"]')).click(); // activate the plugin }).then(function () { browser.sleep(20000).then(function() { done(); }); // wait for the action to succeed }); } function listCustomPlugin(done) { browser.get('https://' + app.fqdn + '/admin/extend/plugins#installed'); browser.wait(until.elementLocated(by.xpath('//strong[text()="nodebb-plugin-beep"]')), TEST_TIMEOUT).then(function () { done(); }); } function uploadImage(done) { browser.get('https://' + app.fqdn + '/user/admin/edit'); browser.wait(until.elementLocated(by.xpath('//a[text()="Change Picture"]')), TEST_TIMEOUT).then(function () { browser.findElement(by.xpath('//a[text()="Change Picture"]')).click().then(function () { return browser.sleep(4000); }).then(function () { return browser.findElement(by.xpath('//button[@data-action="upload"]')).click(); }).then(function () { return browser.sleep(4000); }).then(function () { return browser.findElement(by.xpath('//input[@type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png')); }).then(function () { browser.sleep(3000); }).then(function () { return browser.findElement(by.id('fileUploadSubmitBtn')).click(); }).then(function () { browser.sleep(3000).then(function () { done(); }); }); }); } function checkImage(done) { browser.get('https://' + app.fqdn + '/user/admin'); browser.wait(until.elementLocated(by.xpath('//img[@src="/uploads/profile/1-profileimg.png"]')), TEST_TIMEOUT); var img = browser.findElement(by.xpath('//img[@src="/uploads/profile/1-profileimg.png"]')); browser.executeScript('return arguments[0].complete && arguments[0].naturalWidth', img).then(function (imageWidth) { done(imageWidth === 128 ? null : new Error('failed to load image')); }); } xit('build app', function () { execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); }); it('install app', function () { execSync('cloudron install --new --wait --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('can login', login); it('check mail plugin', checkMailPlugin); it('can install custom plugin', installCustomPlugin); it('can restart forum', restartForum); // required before activate! it('can activate custom plugin', activateCustomPlugin); it('can list custom plugin', listCustomPlugin); it('can upload image', uploadImage); it('can check image', checkImage); it('backup app', function () { execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); }); it('restore app', function () { execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); }); it('can login', login); it('can list custom plugin', listCustomPlugin); it('can check image', checkImage); it('can restart app', function (done) { execSync('cloudron restart --app ' + app.id); done(); }); it('can login', login); it('can list custom plugin', listCustomPlugin); it('can check image', checkImage); it('move to different location', function () { execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { 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('can login', login); it('can list custom plugin', listCustomPlugin); it('can check image', checkImage); it('uninstall app', function () { execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); }); });