5 var execSync
= require('child_process').execSync
,
6 expect
= require('expect.js'),
7 path
= require('path'),
9 superagent
= require('superagent'),
10 webdriver
= require('selenium-webdriver');
12 var by
= webdriver
.By
,
14 until
= webdriver
.until
;
16 process
.env
.NODE_TLS_REJECT_UNAUTHORIZED
= '0';
18 if (!process
.env
.USERNAME
|| !process
.env
.PASSWORD
) {
19 console
.log('USERNAME and PASSWORD env vars need to be set');
23 describe('Application life cycle test', function () {
26 var chrome
= require('selenium-webdriver/chrome');
27 var server
, browser
= new chrome
.Driver();
29 before(function (done
) {
30 var seleniumJar
= require('selenium-server-standalone-jar');
31 var SeleniumServer
= require('selenium-webdriver/remote').SeleniumServer
;
32 server
= new SeleniumServer(seleniumJar
.path
, { port: 4444 });
38 after(function (done
) {
44 var LOCATION
= 'surfer';
45 var TEST_TIMEOUT
= 10000;
46 var TEST_FILE_NAME_0
= 'index.html';
47 var TEST_FILE_NAME_1
= 'test.txt';
50 // tests which are used more than once
51 function login(done
) {
52 browser
.manage().deleteAllCookies();
53 browser
.get('https://' + app
.fqdn
+ '/_admin');
55 browser
.wait(until
.elementLocated(by
.id('inputUsername')), TEST_TIMEOUT
).then(function () {
56 browser
.wait(until
.elementIsVisible(browser
.findElement(by
.id('inputUsername'))), TEST_TIMEOUT
).then(function () {
57 browser
.findElement(by
.id('inputUsername')).sendKeys(process
.env
.USERNAME
);
58 browser
.findElement(by
.id('inputPassword')).sendKeys(process
.env
.PASSWORD
);
59 browser
.findElement(by
.id('loginForm')).submit();
61 browser
.wait(until
.elementIsVisible(browser
.findElement(by
.id('logoutButton'))), TEST_TIMEOUT
).then(function () {
68 function logout(done
) {
69 browser
.get('https://' + app
.fqdn
+ '/_admin');
71 browser
.wait(until
.elementLocated(by
.id('logoutButton')), TEST_TIMEOUT
).then(function () {
72 browser
.wait(until
.elementIsVisible(browser
.findElement(by
.id('logoutButton'))), TEST_TIMEOUT
).then(function () {
73 browser
.findElement(by
.id('logoutButton')).click();
75 browser
.wait(until
.elementIsVisible(browser
.findElement(by
.id('inputPassword'))), TEST_TIMEOUT
).then(function () {
82 function checkFileIsListed(name
, done
) {
83 browser
.get('https://' + app
.fqdn
+ '/_admin');
85 browser
.wait(until
.elementLocated(by
.xpath('//*[text()="' + name
+ '"]')), TEST_TIMEOUT
).then(function () {
90 function checkFileIsPresent(done
) {
91 browser
.get('https://' + app
.fqdn
+ '/' + TEST_FILE_NAME_0
);
93 browser
.wait(until
.elementLocated(by
.xpath('//*[text()="test"]')), TEST_TIMEOUT
).then(function () {
98 function checkIndexFileIsServedUp(done
) {
99 browser
.get('https://' + app
.fqdn
);
101 browser
.wait(until
.elementLocated(by
.xpath('//*[text()="test"]')), TEST_TIMEOUT
).then(function () {
106 function checkFileIsGone(name
, done
) {
107 superagent
.get('https://' + app
.fqdn
+ '/' + name
).end(function (error
, result
) {
108 expect(error
).to
.be
.an('object');
109 expect(result
.statusCode
).to
.equal(404);
114 function uploadFile(name
, done
) {
115 // File upload can't be tested with selenium, since the file input is not visible and thus can't be interacted with :-(
117 fs
.writeFileSync(process
.env
.HOME
+ '/.surfer.json', JSON
.stringify({ server: 'https://' + app
.fqdn
, username: process
.env
.USERNAME
, password: process
.env
.PASSWORD
}));
118 execSync(path
.join(__dirname
, '/../cli/surfer.js') + ' put ' + path
.join(__dirname
, name
), { stdio: 'inherit' } );
122 xit('build app', function () {
123 execSync('cloudron build', { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });
126 it('install app', function () {
127 execSync('cloudron install --new --wait --location ' + LOCATION
, { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });
130 it('can get app information', function () {
131 var inspect
= JSON
.parse(execSync('cloudron inspect'));
133 app
= inspect
.apps
.filter(function (a
) { return a
.location
=== LOCATION
; })[0];
135 expect(app
).to
.be
.an('object');
138 it('can login', login
);
139 it('can upload file', uploadFile
.bind(null, TEST_FILE_NAME_0
));
140 it('file is listed', checkFileIsListed
.bind(null, TEST_FILE_NAME_0
));
141 it('file is served up', checkFileIsPresent
);
142 it('file is served up', checkIndexFileIsServedUp
);
143 it('can upload second file', uploadFile
.bind(null, TEST_FILE_NAME_1
));
144 it('file is listed', checkFileIsListed
.bind(null, TEST_FILE_NAME_1
));
145 it('can delete second file with cli', function (done
) {
146 fs
.writeFileSync(process
.env
.HOME
+ '/.surfer.json', JSON
.stringify({ server: 'https://' + app
.fqdn
, username: process
.env
.USERNAME
, password: process
.env
.PASSWORD
}));
147 execSync(path
.join(__dirname
, '/../cli/surfer.js') + ' del ' + TEST_FILE_NAME_1
, { stdio: 'inherit' } );
150 it('second file is gone', checkFileIsGone
.bind(null, TEST_FILE_NAME_1
));
151 it('can logout', logout
);
153 it('backup app', function () {
154 execSync('cloudron backup create --app ' + app
.id
, { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });
157 it('restore app', function () {
158 execSync('cloudron restore --app ' + app
.id
, { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });
161 it('can login', login
);
162 it('file is listed', checkFileIsListed
.bind(null, TEST_FILE_NAME_0
));
163 it('file is served up', checkFileIsPresent
);
164 it('file is served up', checkIndexFileIsServedUp
);
165 it('second file is still gone', checkFileIsGone
.bind(null, TEST_FILE_NAME_1
));
166 it('can logout', logout
);
168 it('move to different location', function () {
169 browser
.manage().deleteAllCookies();
170 execSync('cloudron install --location ' + LOCATION
+ '2 --app ' + app
.id
, { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });
171 var inspect
= JSON
.parse(execSync('cloudron inspect'));
172 app
= inspect
.apps
.filter(function (a
) { return a
.location
=== LOCATION
+ '2'; })[0];
173 expect(app
).to
.be
.an('object');
176 it('can login', login
);
177 it('file is listed', checkFileIsListed
.bind(null, TEST_FILE_NAME_0
));
178 it('file is served up', checkFileIsPresent
);
179 it('file is served up', checkIndexFileIsServedUp
);
180 it('can logout', logout
);
182 it('uninstall app', function () {
183 execSync('cloudron uninstall --app ' + app
.id
, { cwd: path
.resolve(__dirname
, '..'), stdio: 'inherit' });