test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. webdriver = require('selenium-webdriver');
  8. var by = webdriver.By,
  9. Keys = webdriver.Key,
  10. until = webdriver.until;
  11. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  12. describe('Application life cycle test', function () {
  13. this.timeout(0);
  14. var chrome = require('selenium-webdriver/chrome');
  15. var server, browser = new chrome.Driver(), uploadedImageUrl;
  16. var username = 'admin', password = 'changeme';
  17. before(function (done) {
  18. var seleniumJar= require('selenium-server-standalone-jar');
  19. var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
  20. server = new SeleniumServer(seleniumJar.path, { port: 4444 });
  21. server.start();
  22. done();
  23. });
  24. after(function (done) {
  25. browser.quit();
  26. server.stop();
  27. done();
  28. });
  29. var LOCATION = 'test';
  30. var TEST_TIMEOUT = parseInt(process.env.TEST_TIMEOUT, 10) || 20000;
  31. var app;
  32. var email, token;
  33. function login(done) {
  34. browser.manage().window().setSize(1280,768);
  35. browser.manage().deleteAllCookies();
  36. browser.get('https://' + app.fqdn + '/login');
  37. browser.executeScript('localStorage.clear();')
  38. browser.get('https://' + app.fqdn + '/login');
  39. browser.sleep(5000); // takes some time for username to be visible
  40. browser.wait(until.elementLocated(by.id('username')), TEST_TIMEOUT).then(function () {
  41. browser.findElement(by.id('username')).sendKeys(username);
  42. browser.findElement(by.id('password')).sendKeys(password);
  43. browser.findElement(by.id('login')).click();
  44. browser.wait(until.elementLocated(by.xpath('//a[contains(text(), "Announcements")]')), TEST_TIMEOUT).then(function () { done(); });
  45. });
  46. }
  47. function checkMailPlugin(done) {
  48. browser.get('https://' + app.fqdn + '/admin/emailers/local');
  49. browser.sleep(4000);
  50. browser.wait(until.elementLocated(by.id('host')), TEST_TIMEOUT).then(function () {
  51. browser.findElement(by.id('host')).getAttribute('value').then(function (val) {
  52. if (val !== 'mail') return done(new Error('Incorrect mail server value: ' + val));
  53. done();
  54. });
  55. });
  56. }
  57. function restartForum(done) {
  58. browser.get('https://' + app.fqdn + '/admin').then(function () {
  59. return browser.findElement(by.xpath('//button[text()="Restart"]')).click();
  60. }).then(function () {
  61. return browser.sleep(3000);
  62. }).then(function () {
  63. return browser.findElement(by.xpath('//button[text()="OK"]')).click();
  64. }).then(function () {
  65. return browser.sleep(30000); // wait 30secs for reload
  66. }).then(function () {
  67. return browser.get('https://' + app.fqdn + '/admin');
  68. }).then(function () {
  69. return browser.findElement(by.xpath('//h1[text()="Dashboard"]'))
  70. }).then(function () { done(); });
  71. }
  72. function installCustomPlugin(done) {
  73. execSync('cloudron exec --app ' + app.id + ' -- /usr/local/bin/gosu cloudron:cloudron npm install nodebb-plugin-beep', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  74. done();
  75. }
  76. function activateCustomPlugin(done) {
  77. browser.get('https://' + app.fqdn + '/admin/extend/plugins#installed');
  78. browser.wait(until.elementLocated(by.xpath('//ul[contains(@class, "installed")]//strong[text()="nodebb-plugin-beep"]')), TEST_TIMEOUT).then(function () {
  79. return browser.sleep(10000);
  80. }).then(function () {
  81. return browser.findElement(by.xpath('//li[@id="nodebb-plugin-beep"]//button[@data-action="toggleActive"]')).click(); // activate the plugin
  82. }).then(function () {
  83. browser.sleep(10000).then(function() { done(); }); // wait for the action to succeed
  84. });
  85. }
  86. function listCustomPlugin(done) {
  87. browser.get('https://' + app.fqdn + '/admin/extend/plugins#installed');
  88. browser.wait(until.elementLocated(by.xpath('//ul[contains(@class, "installed")]//strong[text()="nodebb-plugin-beep"]')), TEST_TIMEOUT).then(function () {
  89. done();
  90. });
  91. }
  92. function uploadImage(done) {
  93. browser.get('https://' + app.fqdn + '/user/admin/edit');
  94. browser.findElement(by.xpath('//a[text()="Change Picture"]')).click().then(function () {
  95. return browser.sleep(4000);
  96. }).then(function () {
  97. return browser.findElement(by.xpath('//span[contains(text(), "Upload New Picture")]')).click();
  98. }).then(function () {
  99. return browser.sleep(4000);
  100. }).then(function () {
  101. return browser.findElement(by.xpath('//input[@type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  102. }).then(function () {
  103. browser.sleep(3000);
  104. }).then(function () {
  105. return browser.findElement(by.id('fileUploadSubmitBtn')).click();
  106. }).then(function () {
  107. browser.sleep(3000).then(function () { done(); });
  108. });
  109. }
  110. function checkImage(done) {
  111. browser.get('https://' + app.fqdn + '/user/admin');
  112. browser.wait(until.elementLocated(by.xpath('//img[@src="/uploads/profile/1-profileimg.png"]')), TEST_TIMEOUT);
  113. var img = browser.findElement(by.xpath('//img[@src="/uploads/profile/1-profileimg.png"]'));
  114. browser.executeScript('return arguments[0].complete && arguments[0].naturalWidth', img).then(function (imageWidth) {
  115. done(imageWidth === 128 ? null : new Error('failed to load image'));
  116. });
  117. }
  118. xit('build app', function () {
  119. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  120. });
  121. it('install app', function () {
  122. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  123. });
  124. it('can get app information', function () {
  125. var inspect = JSON.parse(execSync('cloudron inspect'));
  126. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  127. expect(app).to.be.an('object');
  128. });
  129. it('can login', login);
  130. it('check mail plugin', checkMailPlugin);
  131. it('can install custom plugin', installCustomPlugin);
  132. it('can restart forum', restartForum); // required before activate!
  133. it('can activate custom plugin', activateCustomPlugin);
  134. it('can list custom plugin', listCustomPlugin);
  135. it('can upload image', uploadImage);
  136. it('can check image', checkImage);
  137. it('backup app', function () {
  138. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  139. });
  140. it('restore app', function () {
  141. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  142. });
  143. it('can login', login);
  144. it('can list custom plugin', listCustomPlugin);
  145. it('can check image', checkImage);
  146. it('can restart app', function (done) {
  147. execSync('cloudron restart --app ' + app.id);
  148. done();
  149. });
  150. it('can login', login);
  151. it('can list custom plugin', listCustomPlugin);
  152. it('can check image', checkImage);
  153. it('move to different location', function () {
  154. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  155. var inspect = JSON.parse(execSync('cloudron inspect'));
  156. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  157. expect(app).to.be.an('object');
  158. });
  159. it('can login', login);
  160. it('can list custom plugin', listCustomPlugin);
  161. it('can check image', checkImage);
  162. it('uninstall app', function () {
  163. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  164. });
  165. });