test.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 = 'nodebbtest';
  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().deleteAllCookies();
  35. browser.get('https://' + app.fqdn + '/login');
  36. browser.executeScript('localStorage.clear();')
  37. browser.get('https://' + app.fqdn + '/login');
  38. browser.sleep(5000); // takes some time for username to be visible
  39. browser.wait(until.elementLocated(by.id('username')), TEST_TIMEOUT).then(function () {
  40. browser.findElement(by.id('username')).sendKeys(username);
  41. browser.findElement(by.id('password')).sendKeys(password);
  42. browser.findElement(by.id('login')).click();
  43. browser.wait(until.elementLocated(by.xpath('//a[contains(text(), "Announcements")]')), TEST_TIMEOUT).then(function () { done(); });
  44. });
  45. }
  46. function checkMailPlugin(done) {
  47. browser.get('https://' + app.fqdn + '/admin/emailers/local');
  48. browser.sleep(4000);
  49. browser.wait(until.elementLocated(by.id('emailer:local:host')), TEST_TIMEOUT).then(function () {
  50. browser.findElement(by.id('emailer:local:host')).getAttribute('value').then(function (val) {
  51. if (val !== 'mail') return done(new Error('Incorrect mail server value: ' + val));
  52. done();
  53. });
  54. });
  55. }
  56. function restartForum(done) {
  57. browser.get('https://' + app.fqdn + '/admin').then(function () {
  58. return browser.findElement(by.xpath('//button[text()="Restart"]')).click();
  59. }).then(function () {
  60. return browser.sleep(3000);
  61. }).then(function () {
  62. return browser.findElement(by.xpath('//button[text()="OK"]')).click();
  63. }).then(function () {
  64. return browser.sleep(20000); // wait 20secs for reload
  65. }).then(function () {
  66. return browser.get('https://' + app.fqdn + '/admin');
  67. }).then(function () {
  68. return browser.findElement(by.xpath('//h1[text()="Dashboard"]'))
  69. }).then(function () { done(); });
  70. }
  71. function installCustomPlugin(done) {
  72. execSync('cloudron exec -- /usr/local/bin/gosu cloudron:cloudron npm install nodebb-plugin-beep', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  73. done();
  74. }
  75. function activateCustomPlugin(done) {
  76. browser.get('https://' + app.fqdn + '/admin/extend/plugins#installed');
  77. browser.wait(until.elementLocated(by.xpath('//ul[contains(@class, "installed")]//strong[text()="nodebb-plugin-beep"]')), TEST_TIMEOUT).then(function () {
  78. return browser.sleep(10000);
  79. }).then(function () {
  80. return browser.findElement(by.xpath('//li[@id="nodebb-plugin-beep"]//button[@data-action="toggleActive"]')).click(); // activate the plugin
  81. }).then(function () {
  82. browser.sleep(10000).then(function() { done(); }); // wait for the action to succeed
  83. });
  84. }
  85. function listCustomPlugin(done) {
  86. browser.get('https://' + app.fqdn + '/admin/extend/plugins#installed');
  87. browser.wait(until.elementLocated(by.xpath('//ul[contains(@class, "installed")]//strong[text()="nodebb-plugin-beep"]')), TEST_TIMEOUT).then(function () {
  88. done();
  89. });
  90. }
  91. function uploadImage(done) {
  92. browser.get('https://' + app.fqdn + '/user/admin/edit');
  93. browser.findElement(by.xpath('//a[text()="Change Picture"]')).click().then(function () {
  94. return browser.sleep(4000);
  95. }).then(function () {
  96. return browser.findElement(by.xpath('//span[contains(text(), "Upload New Picture")]')).click();
  97. }).then(function () {
  98. return browser.sleep(4000);
  99. }).then(function () {
  100. return browser.findElement(by.xpath('//input[@type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  101. }).then(function () {
  102. browser.sleep(3000);
  103. }).then(function () {
  104. return browser.findElement(by.id('fileUploadSubmitBtn')).click();
  105. }).then(function () {
  106. browser.sleep(3000).then(function () { done(); });
  107. });
  108. }
  109. function checkImage(done) {
  110. browser.get('https://' + app.fqdn + '/user/admin');
  111. browser.wait(until.elementLocated(by.xpath('//img[@src="/uploads/profile/1-profileimg.png"]')), TEST_TIMEOUT);
  112. var img = browser.findElement(by.xpath('//img[@src="/uploads/profile/1-profileimg.png"]'));
  113. browser.executeScript('return arguments[0].complete && arguments[0].naturalWidth', img).then(function (imageWidth) {
  114. done(imageWidth === 128 ? null : new Error('failed to load image'));
  115. });
  116. }
  117. xit('build app', function () {
  118. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  119. });
  120. console.log('IMPORTANT: make the browser window full screen. Otherwise the responsive UI hides elements required for this test');
  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');
  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 install --wait --location ' + LOCATION + '2', { 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. });