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