5 var execSync
= require('child_process').execSync
,
6 expect
= require('expect.js'),
7 path
= require('path'),
8 util
= require('util'),
10 superagent
= require('superagent'),
11 webdriver
= require('selenium-webdriver');
13 var by
= webdriver
.By
,
15 until
= webdriver
.until
;
17 process
.env
.NODE_TLS_REJECT_UNAUTHORIZED
= '0';
19 if (!process
.env
.USERNAME
|| !process
.env
.PASSWORD
) {
20 console
.log('USERNAME and PASSWORD env vars need to be set');
24 describe('Application life cycle test', function () {
27 var chrome
= require('selenium-webdriver/chrome');
28 var server
, browser
= new chrome
.Driver();
30 before(function (done
) {
31 var seleniumJar
= require('selenium-server-standalone-jar');
32 var SeleniumServer
= require('selenium-webdriver/remote').SeleniumServer
;
33 server
= new SeleniumServer(seleniumJar
.path
, { port: 4444 });
39 after(function (done
) {
45 var LOCATION
= 'test';
46 var TEST_TIMEOUT
= 10000;
47 var TEST_FILE_NAME_0
= 'index.html';
48 var TEST_FILE_NAME_1
= 'test.txt';
51 // tests which are used more than once
52 function login(done
) {
53 browser
.manage().deleteAllCookies();
54 browser
.get('https://' + app
.fqdn
+ '/_admin');
56 browser
.wait(until
.elementLocated(by
.id('inputUsername')), TEST_TIMEOUT
).then(function () {
57 browser
.wait(until
.elementIsVisible(browser
.findElement(by
.id('inputUsername'))), TEST_TIMEOUT
).then(function () {
58 browser
.findElement(by
.id('inputUsername')).sendKeys(process
.env
.USERNAME
);
59 browser
.findElement(by
.id('inputPassword')).sendKeys(process
.env
.PASSWORD
);
60 browser
.findElement(by
.id('loginForm')).submit();
62 browser
.wait(until
.elementIsVisible(browser
.findElement(by
.id('logoutButton'))), TEST_TIMEOUT
).then(function () {
69 function logout(done
) {
70 browser
.get('https://' + app
.fqdn
+ '/_admin');
72 browser
.wait(until
.elementLocated(by
.id('logoutButton')), TEST_TIMEOUT
).then(function () {
73 browser
.wait(until
.elementIsVisible(browser
.findElement(by
.id('logoutButton'))), TEST_TIMEOUT
).then(function () {
74 browser
.findElement(by
.id('logoutButton')).click();
76 browser
.wait(until
.elementIsVisible(browser
.findElement(by
.id('inputPassword'))), TEST_TIMEOUT
).then(function () {
83 function checkFileIsListed(name
, done
) {
84 browser
.get('https://' + app
.fqdn
+ '/_admin');
86 browser
.wait(until
.elementLocated(by
.xpath('//*[text()="' + name
+ '"]')), TEST_TIMEOUT
).then(function () {
91 function checkFileIsPresent(done
) {
92 browser
.get('https://' + app
.fqdn
+ '/' + TEST_FILE_NAME_0
);
94 browser
.wait(until
.elementLocated(by
.xpath('//*[text()="test"]')), TEST_TIMEOUT
).then(function () {
99 function checkIndexFileIsServedUp(done
) {
100 browser
.get('https://' + app
.fqdn
);
102 browser
.wait(until
.elementLocated(by
.xpath('//*[text()="test"]')), TEST_TIMEOUT
).then(function () {
107 function checkFileIsGone(name
, done
) {
108 superagent
.get('https://' + app
.fqdn
+ '/' + name
).end(function (error
, result
) {
109 expect(error
).to
.be
.an('object');
110 expect(result
.statusCode
).to
.equal(404);
115 function cliLogin(done
) {
116 execSync(util
.format('%s login %s --username %s --password %s', path
.join(__dirname
, '/../cli/surfer.js'), app
.fqdn
, process
.env
.USERNAME
, process
.env
.PASSWORD
), { stdio: 'inherit' } );
120 function uploadFile(name
, done
) {
121 // File upload can't be tested with selenium, since the file input is not visible and thus can't be interacted with :-(
123 execSync(path
.join(__dirname
, '/../cli/surfer.js') + ' put ' + path
.join(__dirname
, name
), { stdio: 'inherit' } );
127 xit('build app', function () {
128 execSync('cloudron build', { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });
131 it('install app', function () {
132 execSync('cloudron install --new --wait --location ' + LOCATION
, { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });
135 it('can get app information', function () {
136 var inspect
= JSON
.parse(execSync('cloudron inspect'));
138 app
= inspect
.apps
.filter(function (a
) { return a
.location
=== LOCATION
; })[0];
140 expect(app
).to
.be
.an('object');
143 it('can login', login
);
144 it('can cli login', cliLogin
);
145 it('can upload file', uploadFile
.bind(null, TEST_FILE_NAME_0
));
146 it('file is listed', checkFileIsListed
.bind(null, TEST_FILE_NAME_0
));
147 it('file is served up', checkFileIsPresent
);
148 it('file is served up', checkIndexFileIsServedUp
);
149 it('can upload second file', uploadFile
.bind(null, TEST_FILE_NAME_1
));
150 it('file is listed', checkFileIsListed
.bind(null, TEST_FILE_NAME_1
));
151 it('can delete second file with cli', function (done
) {
152 execSync(path
.join(__dirname
, '/../cli/surfer.js') + ' del ' + TEST_FILE_NAME_1
, { stdio: 'inherit' } );
155 it('second file is gone', checkFileIsGone
.bind(null, TEST_FILE_NAME_1
));
156 it('can logout', logout
);
158 it('backup app', function () {
159 execSync('cloudron backup create --app ' + app
.id
, { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });
162 it('restore app', function () {
163 execSync('cloudron restore --app ' + app
.id
, { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });
166 it('can login', login
);
167 it('file is listed', checkFileIsListed
.bind(null, TEST_FILE_NAME_0
));
168 it('file is served up', checkFileIsPresent
);
169 it('file is served up', checkIndexFileIsServedUp
);
170 it('second file is still gone', checkFileIsGone
.bind(null, TEST_FILE_NAME_1
));
171 it('can logout', logout
);
173 it('move to different location', function () {
174 browser
.manage().deleteAllCookies();
175 execSync('cloudron configure --location ' + LOCATION
+ '2 --app ' + app
.id
, { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });
176 var inspect
= JSON
.parse(execSync('cloudron inspect'));
177 app
= inspect
.apps
.filter(function (a
) { return a
.location
=== LOCATION
+ '2'; })[0];
178 expect(app
).to
.be
.an('object');
181 it('can login', login
);
182 it('file is listed', checkFileIsListed
.bind(null, TEST_FILE_NAME_0
));
183 it('file is served up', checkFileIsPresent
);
184 it('file is served up', checkIndexFileIsServedUp
);
185 it('can logout', logout
);
187 it('uninstall app', function () {
188 execSync('cloudron uninstall --app ' + app
.id
, { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });