#!/usr/bin/env node 'use strict'; require('chromedriver'); var execSync = require('child_process').execSync, expect = require('expect.js'), path = require('path'), webdriver = require('selenium-webdriver'); var by = webdriver.By, Keys = webdriver.Key, until = webdriver.until; process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; 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 SSO', function () { this.timeout(0); var chrome = require('selenium-webdriver/chrome'); var server, browser = new chrome.Driver(); 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 = 100000; var PROJECT_NAME = 'testproject'; var PROJECT_DESCRIPTION = 'testdescription'; var USER_STORY_SUBJECT = 'someteststory'; var app; function waitForElement(elem) { return browser.wait(until.elementLocated(elem), TEST_TIMEOUT).then(function () { return browser.wait(until.elementIsVisible(browser.findElement(elem)), TEST_TIMEOUT); }); } function login(callback) { browser.manage().deleteAllCookies(); browser.get('https://' + app.fqdn).then(function () { return browser.executeScript('localStorage.clear();'); }).then(function () { browser.executeScript('sessionStorage.clear();'); }).then(function () { browser.get('https://' + app.fqdn + '/login?next=%252Fprofile'); }).then(function () { return waitForElement(by.name('username')); }).then(function () { return browser.findElement(by.name('username')).sendKeys(process.env.USERNAME); }).then(function () { return browser.findElement(by.name('password')).sendKeys(process.env.PASSWORD); }).then(function () { return browser.findElement(by.className('login-form')).submit(); }).then(function () { return waitForElement(by.xpath('//h4[text()="Your profile"]')); }).then(function () { callback(); }); } function adminLogin(callback) { browser.get('https://' + app.fqdn + '/admin/login/').then(function () { return browser.findElement(by.name('username')).sendKeys('admin'); }).then(function () { return browser.findElement(by.name('password')).sendKeys('123123'); }).then(function () { return browser.findElement(by.id('login-form')).submit(); }).then(function () { return waitForElement(by.xpath('//h1[text()="Site administration"]')); }).then(function () { return browser.findElement(by.xpath('//a[text()="Log out"]')).click(); }).then(function () { return browser.sleep(2000); }).then(function () { callback(); }); } function adminLoginOld(callback) { browser.get('https://' + app.fqdn + '/admin').then(function () { return browser.findElement(by.name('username')).sendKeys('admin'); }).then(function () { return browser.findElement(by.name('password')).sendKeys('123123'); }).then(function () { return browser.findElement(by.id('login-form')).submit(); }).then(function () { return waitForElement(by.xpath('//h1[text()="Site administration"]')); }).then(function () { return browser.findElement(by.xpath('//a[text()="Log out"]')).click(); }).then(function () { return browser.sleep(2000); }).then(function () { callback(); }); } function userStoryExists(callback) { browser.get('https://' + app.fqdn + '/project/' + process.env.USERNAME + '-' + PROJECT_NAME + '/us/1').then(function () { return waitForElement(by.xpath('//span[text()="' + USER_STORY_SUBJECT + '"]')); }).then(function () { callback(); }); } function getAppInfo() { var inspect = JSON.parse(execSync('cloudron inspect')); app = inspect.apps.filter(function (a) { return a.location === LOCATION || a.location === LOCATION + '2'; })[0]; expect(app).to.be.an('object'); } function dismissTutorial(done) { browser.get('https://' + app.fqdn); waitForElement(by.className('introjs-skipbutton')).then(function () { return browser.findElement(by.className('introjs-skipbutton')).sendKeys(Keys.ENTER); }).then(function () { // give some time to ack setTimeout(done, 5000); }); } function createProject(done) { browser.get('https://' + app.fqdn + '/project/new/scrum').then(function () { return waitForElement(by.name('project-name')); }).then(function () { return browser.findElement(by.name('project-name')).sendKeys(PROJECT_NAME); }).then(function () { return browser.sleep(1000); // it needs some time to change focus!! }).then(function () { return browser.findElement(by.xpath('//textarea[@placeholder="Project Description"]')).sendKeys(PROJECT_DESCRIPTION); }).then(function () { return browser.sleep(1000); }).then(function () { return browser.findElement(by.xpath('//button[text()="Create Project"]')).click(); }).then(function () { return browser.wait(until.elementLocated(by.xpath('//span[text()[contains(., "' + PROJECT_NAME + '")]]')), TEST_TIMEOUT); }).then(function () { done(); }); } function createUserStory(done) { browser.get('https://' + app.fqdn + '/project/' + process.env.USERNAME + '-' + PROJECT_NAME + '/backlog').then(function () { return waitForElement(by.xpath('//a[@tg-check-permission="add_us"]')); }).then(function () { // clicks don't work with taiga return browser.findElement(by.xpath('//a[@tg-check-permission="add_us"]')).sendKeys(Keys.ENTER); }).then(function () { return waitForElement(by.name('subject')); }).then(function () { return browser.findElement(by.name('subject')).sendKeys(USER_STORY_SUBJECT); }).then(function () { return browser.findElement(by.xpath('//button[@title="Create"]')).sendKeys(Keys.ENTER); }).then(function () { return waitForElement(by.xpath('//span[text()[contains(., "' + USER_STORY_SUBJECT + '")]]')); }).then(function () { done(); }); } function deleteProject(done) { browser.get('https://' + app.fqdn + '/project/' + process.env.USERNAME + '-' + PROJECT_NAME + '/admin/project-profile/details').then(function () { return waitForElement(by.className('delete-project')); }).then(function () { return browser.findElement(by.className('delete-project')).click(); }).then(function () { return waitForElement(by.xpath('//a[@title="Yes, I\'m really sure"]')); }).then(function () { return browser.findElement(by.xpath('//a[@title="Yes, I\'m really sure"]')).click(); }).then(function () { // give some time to redirect setTimeout(done, 5000); }); } 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', getAppInfo); it('can admin login', adminLogin); it('can login', login); it('can dismiss tutorial', dismissTutorial); it('can create project', createProject); it('can create user story', createUserStory); it('user story exists', userStoryExists); 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('user story is still present', userStoryExists); it('move to different location', function () { execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); }); it('can get app information', getAppInfo); it('can admin login', adminLogin); // origin change requires new login it('can login', login); it('user story is still present', userStoryExists); it('can delete project', deleteProject); 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 --new --wait --appstore-id ' + app.manifest.id + ' --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); }); it('can admin login', adminLoginOld); it('can get app information', getAppInfo); it('can login', login); it('can dismiss tutorial', dismissTutorial); it('can create project', createProject); it('can create user story', createUserStory); it('user story exists', userStoryExists); it('can update', function () { execSync('cloudron install --wait --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); }); it('can admin login', adminLogin); it('user story exists', userStoryExists); it('uninstall app', function () { execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); }); });