]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - test/test.js
Make listen port and ldap filter more flexible
[perso/Immae/Projets/Nodejs/Surfer.git] / test / test.js
1 #!/usr/bin/env node
2
3 'use strict';
4
5 /* global describe */
6 /* global before */
7 /* global after */
8 /* global it */
9
10 var execSync = require('child_process').execSync,
11 expect = require('expect.js'),
12 path = require('path'),
13 util = require('util'),
14 superagent = require('superagent'),
15 { Builder, By, until } = require('selenium-webdriver'),
16 { Options } = require('selenium-webdriver/chrome');
17
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
23 describe('Application life cycle test', function () {
24 this.timeout(0);
25
26 const EXEC_ARGS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' };
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';
31
32 var app;
33 var browser;
34
35 before(function () {
36 browser = new Builder().forBrowser('chrome').setChromeOptions(new Options().windowSize({ width: 1280, height: 1024 })).build();
37 });
38
39 after(function () {
40 browser.quit();
41 });
42
43 function getAppInfo(location, done) {
44 if (!done) {
45 done = location;
46 location = LOCATION;
47 }
48
49 var inspect = JSON.parse(execSync('cloudron inspect'));
50 app = inspect.apps.filter(function (a) { return a.location === location; })[0];
51 expect(app).to.be.an('object');
52
53 done();
54 }
55
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
62 // tests which are used more than once
63 function login(done) {
64 browser.manage().deleteAllCookies();
65 browser.get('https://' + app.fqdn + '/_admin');
66
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();
71
72 waitForElement(By.id('burgerMenuButton')).then(function () {
73 done();
74 });
75 });
76 }
77
78 function logout(done) {
79 browser.get('https://' + app.fqdn + '/_admin');
80
81 waitForElement(By.id('burgerMenuButton')).then(function () {
82 browser.findElement(By.id('burgerMenuButton')).click();
83
84 // wait for open animation
85 browser.sleep(5000);
86
87 waitForElement(By.id('logoutButton')).then(function () {
88 browser.findElement(By.id('logoutButton')).click();
89
90 waitForElement(By.id('loginUsernameInput')).then(function () {
91 done();
92 });
93 });
94 });
95 }
96
97 function checkFileIsListed(name, done) {
98 browser.get('https://' + app.fqdn + '/_admin');
99
100 waitForElement(By.xpath('//*[text()="' + name + '"]')).then(function () {
101 done();
102 });
103 }
104
105 function checkFileIsPresent(done) {
106 browser.get('https://' + app.fqdn + '/' + TEST_FILE_NAME_0);
107
108 waitForElement(By.xpath('//*[text()="test"]')).then(function () {
109 done();
110 });
111 }
112
113 function checkIndexFileIsServedUp(done) {
114 browser.get('https://' + app.fqdn);
115
116 waitForElement(By.xpath('//*[text()="test"]')).then(function () {
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');
124 expect(error.response.status).to.equal(404);
125 expect(result).to.be.an('object');
126 done();
127 });
128 }
129
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
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
138 execSync(path.join(__dirname, '/../cli/surfer.js') + ' put ' + path.join(__dirname, name) + ' /', { stdio: 'inherit' } );
139 done();
140 }
141
142 xit('build app', function () { execSync('cloudron build', EXEC_ARGS); });
143
144 it('install app', function () { execSync(`cloudron install --location ${LOCATION}`, EXEC_ARGS); });
145 it('can get app information', getAppInfo);
146
147 it('can login', login);
148 it('can cli login', cliLogin);
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));
155 it('can delete second file with cli', function () {
156 execSync(path.join(__dirname, '/../cli/surfer.js') + ' del ' + TEST_FILE_NAME_1, { stdio: 'inherit' } );
157 });
158 it('second file is gone', checkFileIsGone.bind(null, TEST_FILE_NAME_1));
159 it('can logout', logout);
160
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); });
163
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
171 it('move to different location', function (done) {
172 browser.manage().deleteAllCookies();
173
174 // ensure we don't hit NXDOMAIN in the mean time
175 browser.get('about:blank').then(function () {
176 execSync(`cloudron configure --location ${LOCATION}2 --app ${app.id}`, EXEC_ARGS);
177 getAppInfo(`${LOCATION}2`, done);
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);
186
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);
191 done();
192 });
193 });
194
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
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
219 it('uninstall app', function (done) {
220 // ensure we don't hit NXDOMAIN in the mean time
221 browser.get('about:blank').then(function () {
222 execSync(`cloudron uninstall --app ${app.id}`, EXEC_ARGS);
223 done();
224 });
225 });
226 });