test.js 5.0 KB

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