| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 | #!/usr/bin/env node/* jslint node:true *//* global it:false *//* global xit:false *//* global describe:false *//* global before:false *//* global after:false */'use strict';var execSync = require('child_process').execSync,    ejs = require('ejs'),    expect = require('expect.js'),    fs = require('fs'),    mkdirp = require('mkdirp'),    path = require('path'),    rimraf = require('rimraf'),    superagent = require('superagent'),    webdriver = require('selenium-webdriver');var by = require('selenium-webdriver').By,    until = require('selenium-webdriver').until,    Key = require('selenium-webdriver').Key,    Builder = require('selenium-webdriver').Builder;process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';describe('Application life cycle test', function () {    this.timeout(0);    var server, browser = new Builder().forBrowser('chrome').build();    var LOCATION = 'test';    var app;    var username = process.env.USERNAME;    var password = process.env.PASSWORD;    var TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 5000;    var email, token;    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 login(username, password, done) {        browser.manage().deleteAllCookies().then(function () {            return browser.get('https://' + app.fqdn + '/wp-login.php');        }).then(function () {            return browser.sleep(2000); // there seems to be some javascript that gives auto-focus to username        }).then(function () {            return browser.findElement(by.id('user_login')).sendKeys(username);        }).then(function () {            return browser.findElement(by.id('user_pass')).sendKeys(password);        }).then(function () {            return browser.findElement(by.tagName('form')).submit();        }).then(function () {            return browser.wait(until.elementLocated(by.xpath('//h1[text()="Dashboard"]')), TIMEOUT);        }).then(function () {            done();        });    }    function logout(done) {        browser.manage().deleteAllCookies().then(function () {            browser.executeScript('localStorage.clear();');            browser.executeScript('sessionStorage.clear();');            done();        });    }    function checkHtaccess(done) {        var out = execSync('cloudron exec -- cat /app/data/htaccess');        expect(out.toString('utf8').indexOf('RewriteEngine On')).to.not.be(-1); // wp generates this with permalinks in hard mode        done();    }    function checkPermalink(done) {        browser.get('https://' + app.fqdn + '/hello-world').then(function () {            return browser.findElement(by.xpath('//h1[text()="Hello Cloudron!"]'));        }).then(function () {            done();        });    }    function checkMedia(done) {        superagent.get(mediaLink).end(function (error, result) {            expect(error).to.be(null);            expect(result.statusCode).to.be(200);            done();        });    }    function checkPost(done) {        browser.get('https://' + app.fqdn).then(function () {            return browser.wait(until.elementLocated(by.xpath('//h3/a[text()="Hello Cloudron!"]')), TIMEOUT);        }).then(function () {            done();        });    }    function editPost(done) {        browser.get('https://' + app.fqdn + '/wp-admin/post.php?post=1&action=edit').then(function () {            return browser.wait(until.elementLocated(by.xpath('//input[@id="title"]')), TIMEOUT);        }).then(function () {            return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys(Key.chord(Key.CONTROL, 'a'));        }).then(function () {            return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys('Hello Cloudron!');        }).then(function () {            return browser.findElement(by.xpath('//input[@id="publish"]')).click();        }).then(function () {            return browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "Post updated.")]')), TIMEOUT);        }).then(function () {            done();        });    }    function uploadMedia(done) {        browser.get('https://' + app.fqdn + '/wp-admin/media-new.php?browser-uploader').then(function () {            return browser.wait(until.elementLocated(by.id('async-upload')), TIMEOUT);        }).then(function () {            return browser.findElement(by.xpath('//input[@id="async-upload" and @type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));        }).then(function () {            return browser.findElement(by.id('html-upload')).click();        }).then(function () {            return browser.wait(function () {                return browser.getCurrentUrl().then(function (url) {                    return url === 'https://' + app.fqdn + '/wp-admin/upload.php';                });            }, TIMEOUT);        }).then(function () {            done();        });    }    function checkMedia(done) {        browser.get('https://' + app.fqdn + '/wp-admin/upload.php?item=5').then(function () { // there's got to be a better way..            return browser.wait(until.elementLocated(by.xpath('//*[text()="Attachment Details"]')), TIMEOUT);        }).then(function () {            return browser.findElement(by.xpath('//img[@class="details-image"]')).getAttribute('src');        }).then(function (srcLink) {            console.log('media is located at ', srcLink);            mediaLink = srcLink;            done();        });    }    xit('build app', function () {        execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });    });    it('can login', function (done) {        var inspect = JSON.parse(execSync('cloudron inspect'));        superagent.post('https://' + inspect.apiEndpoint + '/api/v1/developer/login').send({            username: 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.token;            superagent.get('https://' + inspect.apiEndpoint + '/api/v1/profile')                .query({ access_token: token }).end(function (error, result) {                if (error) return done(error);                if (result.statusCode !== 200) return done(new Error('Get profile failed with status ' + result.statusCode));                email = result.body.email;                done();            });        });    });    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 get the main page', function (done) {        superagent.get('https://' + app.fqdn).end(function (error, result) {            expect(error).to.be(null);            expect(result.status).to.eql(200);            done();        });    });    it('can login', login.bind(null, username, password));    it('is an admin dashboard', function (done) {        browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });    });    it('can edit', editPost);    it('can upload media', uploadMedia);    var mediaLink;    it('can see media', checkMedia);    it('has correct htaccess', checkHtaccess);    it('can access permalink', checkPermalink);    it('can restart app', function (done) {        execSync('cloudron restart --wait');        done();    });    it('can login', login.bind(null, username, password));    it('can see updated post', checkPost);    it('can see media', checkMedia);    it('has correct htaccess', checkHtaccess);    it('can access permalink', checkPermalink);    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.bind(null, username, password));    it('can see updated post', checkPost);    it('can see media', checkMedia);    it('has correct htaccess', checkHtaccess);    it('can access permalink', checkPermalink);    it('can login', login.bind(null, username, password));    it('runs cron jobs', function (done) {        this.timeout(120000);        // cron jobs are only executed when app is running and healthy        setTimeout(function () {            var logs = execSync('cloudron logs --lines 1000 --app ' + app.id).toString('utf8');            if (logs.indexOf('Executed the cron event \'wp_version_check\'') !== -1) return done();            console.log(logs);            done(new Error('cron jobs are possibly not running'));        }, 45000);    });    it('move to different location', function () {        browser.manage().deleteAllCookies();        execSync('cloudron configure --wait --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');        mediaLink = mediaLink.replace(LOCATION, LOCATION + '2');    });    it('can login', login.bind(null, username, password));    it('can see updated post', checkPost);    it('can see media', checkMedia);    it('has correct htaccess', checkHtaccess);    it('can access permalink', checkPermalink);    it('can login', login.bind(null, username, password));    it('can logout', logout);    it('uninstall app', function () {        execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });    });    // check if the _first_ login via email succeeds    it('can login via email', function (done) {        execSync('cloudron install --new --wait --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');        login(email, password, function () {            execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });            logout(done);        });    });    // No SSO    it('install app (no sso)', function () {        execSync('cloudron install --new --wait --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('can login (no sso)', login.bind(null, 'admin', 'changeme'));    it('is an admin dashboard (no sso)', function (done) {        browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });    });    it('can logout', logout);    it('uninstall app (no sso)', function () {        execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });    });    // test update    it('can install app', function () {        execSync('cloudron install --new --wait --appstore-id org.wordpress.cloudronapp --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('can login', login.bind(null, username, password));    it('can edit', editPost);    it('can upload media', uploadMedia);    it('can update', function () {        execSync('cloudron install --wait --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });    });    it('can login', login.bind(null, username, password));    it('can see updated post', checkPost);    it('can see media', checkMedia);    it('can access permalink', checkPermalink);     it('uninstall app', function () {        execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });    });});
 |