]>
Commit | Line | Data |
---|---|---|
ebd7ed7a GR |
1 | #!/usr/bin/env node |
2 | ||
3 | 'use strict'; | |
4 | ||
a7317b4b JZ |
5 | /* global describe */ |
6 | /* global before */ | |
7 | /* global after */ | |
8 | /* global it */ | |
9 | ||
ebd7ed7a GR |
10 | var execSync = require('child_process').execSync, |
11 | expect = require('expect.js'), | |
12 | path = require('path'), | |
1c3b94a7 | 13 | util = require('util'), |
5158ef83 JZ |
14 | superagent = require('superagent'), |
15 | webdriver = require('selenium-webdriver'); | |
16 | ||
17 | var by = webdriver.By, | |
5158ef83 | 18 | until = webdriver.until; |
ebd7ed7a | 19 | |
5158ef83 JZ |
20 | if (!process.env.USERNAME || !process.env.PASSWORD) { |
21 | console.log('USERNAME and PASSWORD env vars need to be set'); | |
22 | process.exit(1); | |
23 | } | |
24 | ||
a7317b4b JZ |
25 | const EXEC_OPTIONS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }; |
26 | ||
ebd7ed7a GR |
27 | describe('Application life cycle test', function () { |
28 | this.timeout(0); | |
5158ef83 | 29 | |
a7317b4b | 30 | var browser; |
ebd7ed7a GR |
31 | |
32 | before(function (done) { | |
a7317b4b JZ |
33 | browser = new webdriver.Builder() |
34 | .forBrowser('chrome') | |
35 | // .setChromeOptions(new chrome.Options().addArguments(['no-sandbox', 'headless'])) | |
36 | .build(); | |
ebd7ed7a GR |
37 | |
38 | done(); | |
39 | }); | |
40 | ||
41 | after(function (done) { | |
5158ef83 | 42 | browser.quit(); |
ebd7ed7a GR |
43 | done(); |
44 | }); | |
45 | ||
f173d2d7 | 46 | var LOCATION = 'test'; |
5158ef83 JZ |
47 | var TEST_TIMEOUT = 10000; |
48 | var TEST_FILE_NAME_0 = 'index.html'; | |
49 | var TEST_FILE_NAME_1 = 'test.txt'; | |
50 | var app; | |
51 | ||
a7317b4b JZ |
52 | function waitForElement(elem) { |
53 | return browser.wait(until.elementLocated(elem), TEST_TIMEOUT).then(function () { | |
54 | return browser.wait(until.elementIsVisible(browser.findElement(elem)), TEST_TIMEOUT); | |
55 | }); | |
56 | } | |
57 | ||
5158ef83 JZ |
58 | // tests which are used more than once |
59 | function login(done) { | |
60 | browser.manage().deleteAllCookies(); | |
61 | browser.get('https://' + app.fqdn + '/_admin'); | |
62 | ||
a7317b4b JZ |
63 | waitForElement(by.id('loginUsernameInput')).then(function () { |
64 | browser.findElement(by.id('loginUsernameInput')).sendKeys(process.env.USERNAME); | |
65 | browser.findElement(by.id('loginPasswordInput')).sendKeys(process.env.PASSWORD); | |
66 | browser.findElement(by.id('loginSubmitButton')).click(); | |
5158ef83 | 67 | |
a7317b4b JZ |
68 | waitForElement(by.id('burgerMenuButton')).then(function () { |
69 | done(); | |
5158ef83 JZ |
70 | }); |
71 | }); | |
72 | } | |
73 | ||
74 | function logout(done) { | |
75 | browser.get('https://' + app.fqdn + '/_admin'); | |
76 | ||
a7317b4b JZ |
77 | waitForElement(by.id('burgerMenuButton')).then(function () { |
78 | browser.findElement(by.id('burgerMenuButton')).click(); | |
79 | ||
80 | // wait for open animation | |
81 | browser.sleep(5000); | |
82 | ||
83 | waitForElement(by.id('logoutButton')).then(function () { | |
5158ef83 JZ |
84 | browser.findElement(by.id('logoutButton')).click(); |
85 | ||
a7317b4b | 86 | waitForElement(by.id('loginUsernameInput')).then(function () { |
5158ef83 JZ |
87 | done(); |
88 | }); | |
89 | }); | |
90 | }); | |
91 | } | |
92 | ||
93 | function checkFileIsListed(name, done) { | |
94 | browser.get('https://' + app.fqdn + '/_admin'); | |
95 | ||
a7317b4b | 96 | waitForElement(by.xpath('//*[text()="' + name + '"]')).then(function () { |
5158ef83 JZ |
97 | done(); |
98 | }); | |
99 | } | |
100 | ||
101 | function checkFileIsPresent(done) { | |
102 | browser.get('https://' + app.fqdn + '/' + TEST_FILE_NAME_0); | |
103 | ||
a7317b4b | 104 | waitForElement(by.xpath('//*[text()="test"]')).then(function () { |
5158ef83 JZ |
105 | done(); |
106 | }); | |
107 | } | |
108 | ||
109 | function checkIndexFileIsServedUp(done) { | |
110 | browser.get('https://' + app.fqdn); | |
111 | ||
a7317b4b | 112 | waitForElement(by.xpath('//*[text()="test"]')).then(function () { |
5158ef83 JZ |
113 | done(); |
114 | }); | |
115 | } | |
116 | ||
117 | function checkFileIsGone(name, done) { | |
118 | superagent.get('https://' + app.fqdn + '/' + name).end(function (error, result) { | |
119 | expect(error).to.be.an('object'); | |
a7317b4b JZ |
120 | expect(error.response.status).to.equal(404); |
121 | expect(result).to.be.an('object'); | |
5158ef83 JZ |
122 | done(); |
123 | }); | |
124 | } | |
125 | ||
1c3b94a7 JZ |
126 | function cliLogin(done) { |
127 | 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' } ); | |
128 | done(); | |
129 | } | |
130 | ||
5158ef83 JZ |
131 | function uploadFile(name, done) { |
132 | // File upload can't be tested with selenium, since the file input is not visible and thus can't be interacted with :-( | |
133 | ||
5158ef83 JZ |
134 | execSync(path.join(__dirname, '/../cli/surfer.js') + ' put ' + path.join(__dirname, name), { stdio: 'inherit' } ); |
135 | done(); | |
136 | } | |
137 | ||
d9973759 | 138 | xit('build app', function () { |
a7317b4b | 139 | execSync('cloudron build', EXEC_OPTIONS); |
ebd7ed7a GR |
140 | }); |
141 | ||
052333b5 | 142 | it('install app', function () { |
a7317b4b | 143 | execSync('cloudron install --new --wait --location ' + LOCATION, EXEC_OPTIONS); |
ebd7ed7a GR |
144 | }); |
145 | ||
146 | it('can get app information', function () { | |
147 | var inspect = JSON.parse(execSync('cloudron inspect')); | |
148 | ||
149 | app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0]; | |
150 | ||
151 | expect(app).to.be.an('object'); | |
152 | }); | |
153 | ||
5158ef83 | 154 | it('can login', login); |
1c3b94a7 | 155 | it('can cli login', cliLogin); |
5158ef83 JZ |
156 | it('can upload file', uploadFile.bind(null, TEST_FILE_NAME_0)); |
157 | it('file is listed', checkFileIsListed.bind(null, TEST_FILE_NAME_0)); | |
158 | it('file is served up', checkFileIsPresent); | |
159 | it('file is served up', checkIndexFileIsServedUp); | |
160 | it('can upload second file', uploadFile.bind(null, TEST_FILE_NAME_1)); | |
161 | it('file is listed', checkFileIsListed.bind(null, TEST_FILE_NAME_1)); | |
162 | it('can delete second file with cli', function (done) { | |
5158ef83 | 163 | execSync(path.join(__dirname, '/../cli/surfer.js') + ' del ' + TEST_FILE_NAME_1, { stdio: 'inherit' } ); |
ebd7ed7a GR |
164 | done(); |
165 | }); | |
5158ef83 JZ |
166 | it('second file is gone', checkFileIsGone.bind(null, TEST_FILE_NAME_1)); |
167 | it('can logout', logout); | |
ebd7ed7a GR |
168 | |
169 | it('backup app', function () { | |
a7317b4b | 170 | execSync('cloudron backup create --app ' + app.id, EXEC_OPTIONS); |
ebd7ed7a GR |
171 | }); |
172 | ||
173 | it('restore app', function () { | |
a7317b4b | 174 | execSync('cloudron restore --app ' + app.id, EXEC_OPTIONS); |
ebd7ed7a GR |
175 | }); |
176 | ||
5158ef83 JZ |
177 | it('can login', login); |
178 | it('file is listed', checkFileIsListed.bind(null, TEST_FILE_NAME_0)); | |
179 | it('file is served up', checkFileIsPresent); | |
180 | it('file is served up', checkIndexFileIsServedUp); | |
181 | it('second file is still gone', checkFileIsGone.bind(null, TEST_FILE_NAME_1)); | |
182 | it('can logout', logout); | |
183 | ||
a7317b4b | 184 | it('move to different location', function (done) { |
5158ef83 | 185 | browser.manage().deleteAllCookies(); |
a7317b4b JZ |
186 | |
187 | // ensure we don't hit NXDOMAIN in the mean time | |
188 | browser.get('about:blank').then(function () { | |
189 | execSync('cloudron configure --location ' + LOCATION + '2 --app ' + app.id, EXEC_OPTIONS); | |
190 | var inspect = JSON.parse(execSync('cloudron inspect')); | |
191 | app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0]; | |
192 | expect(app).to.be.an('object'); | |
193 | done(); | |
194 | }); | |
ebd7ed7a GR |
195 | }); |
196 | ||
5158ef83 JZ |
197 | it('can login', login); |
198 | it('file is listed', checkFileIsListed.bind(null, TEST_FILE_NAME_0)); | |
199 | it('file is served up', checkFileIsPresent); | |
200 | it('file is served up', checkIndexFileIsServedUp); | |
201 | it('can logout', logout); | |
202 | ||
a7317b4b JZ |
203 | it('uninstall app', function (done) { |
204 | // ensure we don't hit NXDOMAIN in the mean time | |
205 | browser.get('about:blank').then(function () { | |
206 | execSync('cloudron uninstall --app ' + app.id, EXEC_OPTIONS); | |
207 | done(); | |
208 | }); | |
ebd7ed7a GR |
209 | }); |
210 | }); |