test.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/usr/bin/env node
  2. 'use strict';
  3. require('chromedriver');
  4. var execSync = require('child_process').execSync,
  5. expect = require('expect.js'),
  6. fs = require('fs'),
  7. net = require('net'),
  8. path = require('path'),
  9. superagent = require('superagent'),
  10. util = require('util'),
  11. webdriver = require('selenium-webdriver');
  12. var by = webdriver.By,
  13. until = webdriver.until,
  14. Builder = require('selenium-webdriver').Builder;
  15. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  16. if (!process.env.USERNAME || !process.env.PASSWORD) {
  17. console.log('USERNAME and PASSWORD env vars need to be set');
  18. process.exit(1);
  19. }
  20. describe('Application life cycle test', function () {
  21. this.timeout(0);
  22. var server, browser = new Builder().forBrowser('chrome').build();
  23. before(function (done) {
  24. var seleniumJar= require('selenium-server-standalone-jar');
  25. var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
  26. server = new SeleniumServer(seleniumJar.path, { port: 4444 });
  27. server.start();
  28. done();
  29. });
  30. after(function (done) {
  31. browser.quit();
  32. server.stop();
  33. done();
  34. });
  35. var LOCATION = 'test';
  36. var TEST_TIMEOUT = 50000;
  37. var app;
  38. function waitForElement(elem) {
  39. return browser.wait(until.elementLocated(elem), TEST_TIMEOUT).then(function () {
  40. return browser.wait(until.elementIsVisible(browser.findElement(elem)), TEST_TIMEOUT);
  41. });
  42. }
  43. function welcomePage(callback) {
  44. browser.get('https://' + app.fqdn).then(function () {
  45. return waitForElement(by.xpath('//*[text()="Cloudron LAMP App"]'));
  46. }).then(function () {
  47. return waitForElement(by.xpath('//h1[contains(text(), "7.2.10-0ubuntu0.18.04.1")]'));
  48. }).then(function () {
  49. callback();
  50. });
  51. }
  52. function uploadedFileExists(callback) {
  53. browser.get('https://' + app.fqdn + '/test.php').then(function () {
  54. return waitForElement(by.xpath('//*[text()="this works"]'));
  55. }).then(function () {
  56. return waitForElement(by.xpath('//*[text()="' + app.fqdn + '"]'));
  57. }).then(function () {
  58. callback();
  59. });
  60. }
  61. function checkIonCube(callback) {
  62. browser.get('https://' + app.fqdn).then(function () {
  63. return waitForElement(by.xpath('//td[contains(text(), "ionCube Loader")]'));
  64. // return waitForElement(by.xpath('//*[contains(text(), "Intrusion Protection&nsbp;from ioncube24.com")]'));
  65. }).then(function () {
  66. callback();
  67. });
  68. }
  69. function checkPhpMyAdmin(callback) {
  70. superagent.get('https://' + app.fqdn + '/phpmyadmin').end(function (error, result) {
  71. if (error && !error.response) return callback(error); // network error
  72. if (result.statusCode !== 401) return callback('Expecting 401 error');
  73. superagent.get('https://' + app.fqdn + '/phpmyadmin')
  74. .auth(process.env.USERNAME, process.env.PASSWORD)
  75. .end(function (error, result) {
  76. if (error) return callback(error);
  77. if (result.text.indexOf(`${app.fqdn} / mysql | phpMyAdmin`) === -1) { // in the <title>
  78. console.log(result.text);
  79. return callback(new Error('could not detect phpmyadmin'));
  80. }
  81. callback();
  82. });
  83. });
  84. }
  85. function checkCron(callback) {
  86. this.timeout(60000 * 2);
  87. fs.writeFileSync('/tmp/crontab', '* * * * * echo -n "$MYSQL_HOST" > /app/data/public/cron\n', 'utf8');
  88. execSync('cloudron push /tmp/crontab /app/data/crontab');
  89. fs.unlinkSync('/tmp/crontab');
  90. execSync('cloudron restart --wait');
  91. console.log('Waiting for crontab to trigger');
  92. setTimeout(function () {
  93. superagent.get('https://' + app.fqdn + '/cron').end(function (error, result) {
  94. if (error && !error.response) return callback(error); // network error
  95. if (result.statusCode !== 200) return callback('Expecting 200, got ' + result.statusCode);
  96. if (result.text !== 'mysql') return callback('Unexpected text: ' + result.text);
  97. callback();
  98. });
  99. }, 60 * 1000); // give it a minute to run the crontab
  100. }
  101. xit('build app', function () {
  102. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  103. });
  104. it('install app', function () {
  105. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  106. });
  107. it('can get app information', function () {
  108. var inspect = JSON.parse(execSync('cloudron inspect'));
  109. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  110. expect(app).to.be.an('object');
  111. });
  112. it('can view welcome page', welcomePage);
  113. it('can access ioncube', checkIonCube);
  114. it('can upload file with sftp', function () {
  115. // remove from known hosts in case this test was run on other apps with the same domain already
  116. // if the tests fail here you want below in ~/.ssh/config
  117. // Host test.cloudron.xyz
  118. // StrictHostKeyChecking no
  119. // HashKnownHosts no
  120. console.log('If this test fails, see the comment above this log message');
  121. execSync(util.format('sed -i \'/%s/d\' -i ~/.ssh/known_hosts', app.fqdn));
  122. execSync(util.format('lftp sftp://%s:%s@%s:%s -e "set sftp:auto-confirm yes; cd public/; put test.php; bye"', process.env.USERNAME, process.env.PASSWORD, app.fqdn, app.portBindings.SFTP_PORT));
  123. });
  124. it('can get uploaded file', uploadedFileExists);
  125. it('can access phpmyadmin', checkPhpMyAdmin);
  126. it('executes cron tasks', checkCron);
  127. it('backup app', function () {
  128. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  129. });
  130. it('restore app', function () {
  131. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  132. });
  133. it('can get uploaded file', uploadedFileExists);
  134. it('move to different location', function () {
  135. browser.manage().deleteAllCookies();
  136. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  137. var inspect = JSON.parse(execSync('cloudron inspect'));
  138. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  139. expect(app).to.be.an('object');
  140. });
  141. it('can get uploaded file', uploadedFileExists);
  142. it('can access phpmyadmin', checkPhpMyAdmin);
  143. it('can access ioncube', checkIonCube);
  144. // disable SFTP
  145. it('can disable sftp', function () {
  146. execSync('cloudron configure --wait -p SFTP_PORT=', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  147. });
  148. it('(nosftp) can view welcome page', welcomePage);
  149. it('(nosftp cannot upload file with sftp', function (done) {
  150. var client = new net.Socket();
  151. client.setTimeout(10000);
  152. client.connect(2222, app.fqdn, function() {
  153. client.destroy();
  154. done(new Error('Connected'));
  155. });
  156. client.on('timeout', function () { client.destroy(); done(); }); // the packet just got dropped (good)
  157. client.on('error', function (error) {
  158. client.destroy();
  159. done(new Error('Should have got timeout but got error:' + error.message));
  160. });
  161. });
  162. it('(nosftp) cannot access phpmyadmin', function (done) {
  163. superagent.get('https://' + app.fqdn + '/phpmyadmin').end(function (error, result) {
  164. if (error && !error.response) return done(error); // network error
  165. if (result.statusCode !== 404) return done('Expecting 404 error');
  166. done();
  167. });
  168. });
  169. it('uninstall app', function () {
  170. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  171. });
  172. // test update
  173. it('can install app', function () {
  174. execSync('cloudron install --new --wait --appstore-id lamp.cloudronapp --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  175. var inspect = JSON.parse(execSync('cloudron inspect'));
  176. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  177. expect(app).to.be.an('object');
  178. });
  179. it('can upload file with sftp', function () {
  180. // remove from known hosts in case this test was run on other apps with the same domain already
  181. // if the tests fail here you want to set "HashKnownHosts no" in ~/.ssh/config
  182. execSync(util.format('sed -i \'/%s/d\' -i ~/.ssh/known_hosts', app.fqdn));
  183. execSync(util.format('lftp sftp://%s:%s@%s:%s -e "set sftp:auto-confirm yes; cd public/; put test.php; bye"', process.env.USERNAME, process.env.PASSWORD, app.fqdn, app.portBindings.SFTP_PORT));
  184. });
  185. it('can update', function () {
  186. execSync('cloudron install --wait --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  187. });
  188. it('can get uploaded file', uploadedFileExists);
  189. it('can access phpmyadmin', checkPhpMyAdmin);
  190. it('can access ioncube', checkIonCube);
  191. it('uninstall app', function () {
  192. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  193. });
  194. });