]>
Commit | Line | Data |
---|---|---|
ebd7ed7a GR |
1 | #!/usr/bin/env node |
2 | ||
3 | 'use strict'; | |
4 | ||
5 | var execSync = require('child_process').execSync, | |
6 | expect = require('expect.js'), | |
7 | path = require('path'), | |
5158ef83 JZ |
8 | fs = require('fs'), |
9 | superagent = require('superagent'), | |
10 | webdriver = require('selenium-webdriver'); | |
11 | ||
12 | var by = webdriver.By, | |
13 | Keys = webdriver.Key, | |
14 | until = webdriver.until; | |
ebd7ed7a GR |
15 | |
16 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | |
17 | ||
5158ef83 JZ |
18 | if (!process.env.USERNAME || !process.env.PASSWORD) { |
19 | console.log('USERNAME and PASSWORD env vars need to be set'); | |
20 | process.exit(1); | |
21 | } | |
22 | ||
ebd7ed7a GR |
23 | describe('Application life cycle test', function () { |
24 | this.timeout(0); | |
5158ef83 JZ |
25 | |
26 | var firefox = require('selenium-webdriver/firefox'); | |
27 | var server, browser = new firefox.Driver(); | |
ebd7ed7a GR |
28 | |
29 | before(function (done) { | |
5158ef83 JZ |
30 | var seleniumJar= require('selenium-server-standalone-jar'); |
31 | var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer; | |
32 | server = new SeleniumServer(seleniumJar.path, { port: 4444 }); | |
33 | server.start(); | |
ebd7ed7a GR |
34 | |
35 | done(); | |
36 | }); | |
37 | ||
38 | after(function (done) { | |
5158ef83 JZ |
39 | browser.quit(); |
40 | server.stop(); | |
ebd7ed7a GR |
41 | done(); |
42 | }); | |
43 | ||
5158ef83 JZ |
44 | var LOCATION = 'rctest'; |
45 | var TEST_TIMEOUT = 10000; | |
46 | var TEST_FILE_NAME_0 = 'index.html'; | |
47 | var TEST_FILE_NAME_1 = 'test.txt'; | |
48 | var app; | |
49 | ||
50 | // tests which are used more than once | |
51 | function login(done) { | |
52 | browser.manage().deleteAllCookies(); | |
53 | browser.get('https://' + app.fqdn + '/_admin'); | |
54 | ||
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(); | |
60 | ||
61 | browser.wait(until.elementIsVisible(browser.findElement(by.id('logoutButton'))), TEST_TIMEOUT).then(function () { | |
62 | done(); | |
63 | }); | |
64 | }); | |
65 | }); | |
66 | } | |
67 | ||
68 | function logout(done) { | |
69 | browser.get('https://' + app.fqdn + '/_admin'); | |
70 | ||
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(); | |
74 | ||
75 | browser.wait(until.elementIsVisible(browser.findElement(by.id('inputPassword'))), TEST_TIMEOUT).then(function () { | |
76 | done(); | |
77 | }); | |
78 | }); | |
79 | }); | |
80 | } | |
81 | ||
82 | function checkFileIsListed(name, done) { | |
83 | browser.get('https://' + app.fqdn + '/_admin'); | |
84 | ||
85 | browser.wait(until.elementLocated(by.xpath('//*[text()="' + name + '"]')), TEST_TIMEOUT).then(function () { | |
86 | done(); | |
87 | }); | |
88 | } | |
89 | ||
90 | function checkFileIsPresent(done) { | |
91 | browser.get('https://' + app.fqdn + '/' + TEST_FILE_NAME_0); | |
92 | ||
93 | browser.wait(until.elementLocated(by.xpath('//*[text()="test"]')), TEST_TIMEOUT).then(function () { | |
94 | done(); | |
95 | }); | |
96 | } | |
97 | ||
98 | function checkIndexFileIsServedUp(done) { | |
99 | browser.get('https://' + app.fqdn); | |
100 | ||
101 | browser.wait(until.elementLocated(by.xpath('//*[text()="test"]')), TEST_TIMEOUT).then(function () { | |
102 | done(); | |
103 | }); | |
104 | } | |
105 | ||
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); | |
110 | done(); | |
111 | }); | |
112 | } | |
113 | ||
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 :-( | |
116 | ||
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' } ); | |
119 | done(); | |
120 | } | |
121 | ||
de9b8cee | 122 | it('build app', function () { |
ebd7ed7a GR |
123 | execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); |
124 | }); | |
125 | ||
052333b5 | 126 | it('install app', function () { |
5158ef83 | 127 | execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); |
ebd7ed7a GR |
128 | }); |
129 | ||
130 | it('can get app information', function () { | |
131 | var inspect = JSON.parse(execSync('cloudron inspect')); | |
132 | ||
133 | app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0]; | |
134 | ||
135 | expect(app).to.be.an('object'); | |
136 | }); | |
137 | ||
5158ef83 JZ |
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' } ); | |
ebd7ed7a GR |
148 | done(); |
149 | }); | |
5158ef83 JZ |
150 | it('second file is gone', checkFileIsGone.bind(null, TEST_FILE_NAME_1)); |
151 | it('can logout', logout); | |
ebd7ed7a GR |
152 | |
153 | it('backup app', function () { | |
154 | execSync('cloudron backup --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); | |
155 | }); | |
156 | ||
157 | it('restore app', function () { | |
158 | execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); | |
159 | }); | |
160 | ||
5158ef83 JZ |
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); | |
167 | ||
168 | it('move to different location', function () { | |
169 | browser.manage().deleteAllCookies(); | |
170 | execSync('cloudron install --location ' + LOCATION + '2', { 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'); | |
ebd7ed7a GR |
174 | }); |
175 | ||
5158ef83 JZ |
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); | |
181 | ||
052333b5 | 182 | it('uninstall app', function () { |
ebd7ed7a GR |
183 | execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); |
184 | }); | |
185 | }); |