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