]> git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - test/test.js
dace05d0119dca5a8b11cbb1ba7b2cae09d95835
[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 webdriver = require('selenium-webdriver');
16
17 var by = webdriver.By,
18 until = webdriver.until;
19
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
25 const EXEC_OPTIONS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' };
26
27 describe('Application life cycle test', function () {
28 this.timeout(0);
29
30 var browser;
31
32 before(function (done) {
33 browser = new webdriver.Builder()
34 .forBrowser('chrome')
35 // .setChromeOptions(new chrome.Options().addArguments(['no-sandbox', 'headless']))
36 .build();
37
38 done();
39 });
40
41 after(function (done) {
42 browser.quit();
43 done();
44 });
45
46 var LOCATION = 'test';
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
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
58 // tests which are used more than once
59 function login(done) {
60 browser.manage().deleteAllCookies();
61 browser.get('https://' + app.fqdn + '/_admin');
62
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();
67
68 waitForElement(by.id('burgerMenuButton')).then(function () {
69 done();
70 });
71 });
72 }
73
74 function logout(done) {
75 browser.get('https://' + app.fqdn + '/_admin');
76
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 () {
84 browser.findElement(by.id('logoutButton')).click();
85
86 waitForElement(by.id('loginUsernameInput')).then(function () {
87 done();
88 });
89 });
90 });
91 }
92
93 function checkFileIsListed(name, done) {
94 browser.get('https://' + app.fqdn + '/_admin');
95
96 waitForElement(by.xpath('//*[text()="' + name + '"]')).then(function () {
97 done();
98 });
99 }
100
101 function checkFileIsPresent(done) {
102 browser.get('https://' + app.fqdn + '/' + TEST_FILE_NAME_0);
103
104 waitForElement(by.xpath('//*[text()="test"]')).then(function () {
105 done();
106 });
107 }
108
109 function checkIndexFileIsServedUp(done) {
110 browser.get('https://' + app.fqdn);
111
112 waitForElement(by.xpath('//*[text()="test"]')).then(function () {
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');
120 expect(error.response.status).to.equal(404);
121 expect(result).to.be.an('object');
122 done();
123 });
124 }
125
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
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
134 execSync(path.join(__dirname, '/../cli/surfer.js') + ' put ' + path.join(__dirname, name), { stdio: 'inherit' } );
135 done();
136 }
137
138 xit('build app', function () {
139 execSync('cloudron build', EXEC_OPTIONS);
140 });
141
142 it('install app', function () {
143 execSync('cloudron install --new --wait --location ' + LOCATION, EXEC_OPTIONS);
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
154 it('can login', login);
155 it('can cli login', cliLogin);
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) {
163 execSync(path.join(__dirname, '/../cli/surfer.js') + ' del ' + TEST_FILE_NAME_1, { stdio: 'inherit' } );
164 done();
165 });
166 it('second file is gone', checkFileIsGone.bind(null, TEST_FILE_NAME_1));
167 it('can logout', logout);
168
169 it('backup app', function () {
170 execSync('cloudron backup create --app ' + app.id, EXEC_OPTIONS);
171 });
172
173 it('restore app', function () {
174 execSync('cloudron restore --app ' + app.id, EXEC_OPTIONS);
175 });
176
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
184 it('move to different location', function (done) {
185 browser.manage().deleteAllCookies();
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 });
195 });
196
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
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 });
209 });
210 });