]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env node | |
2 | ||
3 | 'use strict'; | |
4 | ||
5 | var execSync = require('child_process').execSync, | |
6 | expect = require('expect.js'), | |
7 | path = require('path'), | |
8 | util = require('util'), | |
9 | fs = require('fs'), | |
10 | superagent = require('superagent'), | |
11 | webdriver = require('selenium-webdriver'); | |
12 | ||
13 | var by = webdriver.By, | |
14 | Keys = webdriver.Key, | |
15 | until = webdriver.until; | |
16 | ||
17 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | |
18 | ||
19 | if (!process.env.USERNAME || !process.env.PASSWORD) { | |
20 | console.log('USERNAME and PASSWORD env vars need to be set'); | |
21 | process.exit(1); | |
22 | } | |
23 | ||
24 | describe('Application life cycle test', function () { | |
25 | this.timeout(0); | |
26 | ||
27 | var chrome = require('selenium-webdriver/chrome'); | |
28 | var server, browser = new chrome.Driver(); | |
29 | ||
30 | before(function (done) { | |
31 | var seleniumJar= require('selenium-server-standalone-jar'); | |
32 | var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer; | |
33 | server = new SeleniumServer(seleniumJar.path, { port: 4444 }); | |
34 | server.start(); | |
35 | ||
36 | done(); | |
37 | }); | |
38 | ||
39 | after(function (done) { | |
40 | browser.quit(); | |
41 | server.stop(); | |
42 | done(); | |
43 | }); | |
44 | ||
45 | var LOCATION = 'test'; | |
46 | var TEST_TIMEOUT = 10000; | |
47 | var TEST_FILE_NAME_0 = 'index.html'; | |
48 | var TEST_FILE_NAME_1 = 'test.txt'; | |
49 | var app; | |
50 | ||
51 | // tests which are used more than once | |
52 | function login(done) { | |
53 | browser.manage().deleteAllCookies(); | |
54 | browser.get('https://' + app.fqdn + '/_admin'); | |
55 | ||
56 | browser.wait(until.elementLocated(by.id('inputUsername')), TEST_TIMEOUT).then(function () { | |
57 | browser.wait(until.elementIsVisible(browser.findElement(by.id('inputUsername'))), TEST_TIMEOUT).then(function () { | |
58 | browser.findElement(by.id('inputUsername')).sendKeys(process.env.USERNAME); | |
59 | browser.findElement(by.id('inputPassword')).sendKeys(process.env.PASSWORD); | |
60 | browser.findElement(by.id('loginForm')).submit(); | |
61 | ||
62 | browser.wait(until.elementIsVisible(browser.findElement(by.id('logoutButton'))), TEST_TIMEOUT).then(function () { | |
63 | done(); | |
64 | }); | |
65 | }); | |
66 | }); | |
67 | } | |
68 | ||
69 | function logout(done) { | |
70 | browser.get('https://' + app.fqdn + '/_admin'); | |
71 | ||
72 | browser.wait(until.elementLocated(by.id('logoutButton')), TEST_TIMEOUT).then(function () { | |
73 | browser.wait(until.elementIsVisible(browser.findElement(by.id('logoutButton'))), TEST_TIMEOUT).then(function () { | |
74 | browser.findElement(by.id('logoutButton')).click(); | |
75 | ||
76 | browser.wait(until.elementIsVisible(browser.findElement(by.id('inputPassword'))), TEST_TIMEOUT).then(function () { | |
77 | done(); | |
78 | }); | |
79 | }); | |
80 | }); | |
81 | } | |
82 | ||
83 | function checkFileIsListed(name, done) { | |
84 | browser.get('https://' + app.fqdn + '/_admin'); | |
85 | ||
86 | browser.wait(until.elementLocated(by.xpath('//*[text()="' + name + '"]')), TEST_TIMEOUT).then(function () { | |
87 | done(); | |
88 | }); | |
89 | } | |
90 | ||
91 | function checkFileIsPresent(done) { | |
92 | browser.get('https://' + app.fqdn + '/' + TEST_FILE_NAME_0); | |
93 | ||
94 | browser.wait(until.elementLocated(by.xpath('//*[text()="test"]')), TEST_TIMEOUT).then(function () { | |
95 | done(); | |
96 | }); | |
97 | } | |
98 | ||
99 | function checkIndexFileIsServedUp(done) { | |
100 | browser.get('https://' + app.fqdn); | |
101 | ||
102 | browser.wait(until.elementLocated(by.xpath('//*[text()="test"]')), TEST_TIMEOUT).then(function () { | |
103 | done(); | |
104 | }); | |
105 | } | |
106 | ||
107 | function checkFileIsGone(name, done) { | |
108 | superagent.get('https://' + app.fqdn + '/' + name).end(function (error, result) { | |
109 | expect(error).to.be.an('object'); | |
110 | expect(result.statusCode).to.equal(404); | |
111 | done(); | |
112 | }); | |
113 | } | |
114 | ||
115 | function cliLogin(done) { | |
116 | 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' } ); | |
117 | done(); | |
118 | } | |
119 | ||
120 | function uploadFile(name, done) { | |
121 | // File upload can't be tested with selenium, since the file input is not visible and thus can't be interacted with :-( | |
122 | ||
123 | execSync(path.join(__dirname, '/../cli/surfer.js') + ' put ' + path.join(__dirname, name), { stdio: 'inherit' } ); | |
124 | done(); | |
125 | } | |
126 | ||
127 | xit('build app', function () { | |
128 | execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); | |
129 | }); | |
130 | ||
131 | it('install app', function () { | |
132 | execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); | |
133 | }); | |
134 | ||
135 | it('can get app information', function () { | |
136 | var inspect = JSON.parse(execSync('cloudron inspect')); | |
137 | ||
138 | app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0]; | |
139 | ||
140 | expect(app).to.be.an('object'); | |
141 | }); | |
142 | ||
143 | it('can login', login); | |
144 | it('can cli login', cliLogin); | |
145 | it('can upload file', uploadFile.bind(null, TEST_FILE_NAME_0)); | |
146 | it('file is listed', checkFileIsListed.bind(null, TEST_FILE_NAME_0)); | |
147 | it('file is served up', checkFileIsPresent); | |
148 | it('file is served up', checkIndexFileIsServedUp); | |
149 | it('can upload second file', uploadFile.bind(null, TEST_FILE_NAME_1)); | |
150 | it('file is listed', checkFileIsListed.bind(null, TEST_FILE_NAME_1)); | |
151 | it('can delete second file with cli', function (done) { | |
152 | execSync(path.join(__dirname, '/../cli/surfer.js') + ' del ' + TEST_FILE_NAME_1, { stdio: 'inherit' } ); | |
153 | done(); | |
154 | }); | |
155 | it('second file is gone', checkFileIsGone.bind(null, TEST_FILE_NAME_1)); | |
156 | it('can logout', logout); | |
157 | ||
158 | it('backup app', function () { | |
159 | execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); | |
160 | }); | |
161 | ||
162 | it('restore app', function () { | |
163 | execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); | |
164 | }); | |
165 | ||
166 | it('can login', login); | |
167 | it('file is listed', checkFileIsListed.bind(null, TEST_FILE_NAME_0)); | |
168 | it('file is served up', checkFileIsPresent); | |
169 | it('file is served up', checkIndexFileIsServedUp); | |
170 | it('second file is still gone', checkFileIsGone.bind(null, TEST_FILE_NAME_1)); | |
171 | it('can logout', logout); | |
172 | ||
173 | it('move to different location', function () { | |
174 | browser.manage().deleteAllCookies(); | |
175 | execSync('cloudron configure --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); | |
176 | var inspect = JSON.parse(execSync('cloudron inspect')); | |
177 | app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0]; | |
178 | expect(app).to.be.an('object'); | |
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 () { | |
188 | execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); | |
189 | }); | |
190 | }); |