test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var execSync = require('child_process').execSync,
  4. expect = require('expect.js'),
  5. net = require('net'),
  6. path = require('path'),
  7. superagent = require('superagent'),
  8. util = require('util'),
  9. webdriver = require('selenium-webdriver');
  10. var by = webdriver.By,
  11. until = webdriver.until;
  12. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  13. if (!process.env.USERNAME || !process.env.PASSWORD) {
  14. console.log('USERNAME and PASSWORD env vars need to be set');
  15. process.exit(1);
  16. }
  17. describe('Application life cycle test', function () {
  18. this.timeout(0);
  19. var chrome = require('selenium-webdriver/chrome');
  20. var server, browser = new chrome.Driver();
  21. before(function (done) {
  22. var seleniumJar= require('selenium-server-standalone-jar');
  23. var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
  24. server = new SeleniumServer(seleniumJar.path, { port: 4444 });
  25. server.start();
  26. done();
  27. });
  28. after(function (done) {
  29. browser.quit();
  30. server.stop();
  31. done();
  32. });
  33. var LOCATION = 'test';
  34. var TEST_TIMEOUT = 50000;
  35. var app;
  36. function waitForElement(elem, callback) {
  37. browser.wait(until.elementLocated(elem), TEST_TIMEOUT).then(function () {
  38. browser.wait(until.elementIsVisible(browser.findElement(elem)), TEST_TIMEOUT).then(function () {
  39. callback();
  40. });
  41. });
  42. }
  43. function welcomePage(callback) {
  44. browser.get('https://' + app.fqdn);
  45. waitForElement(by.xpath('//*[text()="Cloudron LAMP App"]'), function () {
  46. waitForElement(by.xpath('//*[text()="PHP Version 7.0.15-0ubuntu0.16.04.4"]'), callback);
  47. });
  48. }
  49. function uploadedFileExists(callback) {
  50. browser.get('https://' + app.fqdn + '/test.php');
  51. waitForElement(by.xpath('//*[text()="this works"]'), function () {
  52. waitForElement(by.xpath('//*[text()="' + app.fqdn + '"]'), callback);
  53. });
  54. }
  55. function checkPhpMyAdmin(callback) {
  56. superagent.get('https://' + app.fqdn + '/phpmyadmin').end(function (error, result) {
  57. if (error && !error.response) return callback(error); // network error
  58. if (result.statusCode !== 401) return callback('Expecting 401 error');
  59. superagent.get('https://' + app.fqdn + '/phpmyadmin')
  60. .auth(process.env.USERNAME, process.env.PASSWORD)
  61. .end(function (error, result) {
  62. if (error) return callback(error);
  63. if (result.text.indexOf('.cloudron.info / mysql | phpMyAdmin') === -1) { // in the title
  64. console.log(result.text);
  65. return callback(new Error('could not detect phpmyadmin'));
  66. }
  67. callback();
  68. });
  69. });
  70. }
  71. xit('build app', function () {
  72. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  73. });
  74. it('install app', function () {
  75. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  76. });
  77. it('can get app information', function () {
  78. var inspect = JSON.parse(execSync('cloudron inspect'));
  79. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  80. expect(app).to.be.an('object');
  81. });
  82. it('can view welcome page', welcomePage);
  83. it('can upload file with sftp', function () {
  84. // remove from known hosts in case this test was run on other apps with the same domain already
  85. execSync(util.format('sed -i \'/%s/d\' -i ~/.ssh/known_hosts', app.fqdn));
  86. 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));
  87. });
  88. it('can get uploaded file', uploadedFileExists);
  89. it('can access phpmyadmin', checkPhpMyAdmin);
  90. it('backup app', function () {
  91. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  92. });
  93. it('restore app', function () {
  94. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  95. });
  96. it('can get uploaded file', uploadedFileExists);
  97. it('move to different location', function () {
  98. browser.manage().deleteAllCookies();
  99. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  100. var inspect = JSON.parse(execSync('cloudron inspect'));
  101. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  102. expect(app).to.be.an('object');
  103. });
  104. it('can get uploaded file', uploadedFileExists);
  105. it('can access phpmyadmin', checkPhpMyAdmin);
  106. // disable SFTP
  107. it('can disable sftp', function () {
  108. execSync('cloudron configure --wait -p SFTP_PORT=', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  109. });
  110. it('(nosftp) can view welcome page', welcomePage);
  111. it('(nosftp cannot upload file with sftp', function (done) {
  112. var client = new net.Socket();
  113. client.setTimeout(10000);
  114. client.connect(2222, app.fqdn, function() {
  115. client.destroy();
  116. done(new Error('Connected'));
  117. });
  118. client.on('timeout', function () { done(); }); // the packet just got dropped (good)
  119. client.on('error', function (error) {
  120. done(new Error('Should have got timeout but got error:' + error.message));
  121. });
  122. });
  123. it('(nosftp) cannot access phpmyadmin', function (done) {
  124. superagent.get('https://' + app.fqdn + '/phpmyadmin').end(function (error, result) {
  125. if (error && !error.response) return done(error); // network error
  126. if (result.statusCode !== 404) return done('Expecting 404 error');
  127. done();
  128. });
  129. });
  130. it('uninstall app', function () {
  131. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  132. });
  133. });