test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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()="Confirm"]')).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.wait(until.elementLocated(by.xpath('//a[text()="Change Picture"]')), TEST_TIMEOUT.then(function () {
  95. browser.findElement(by.xpath('//a[text()="Change Picture"]')).click().then(function () {
  96. return browser.sleep(4000);
  97. }).then(function () {
  98. return browser.findElement(by.xpath('//span[contains(text(), "Upload New Picture")]')).click();
  99. }).then(function () {
  100. return browser.sleep(4000);
  101. }).then(function () {
  102. return browser.findElement(by.xpath('//input[@type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  103. }).then(function () {
  104. browser.sleep(3000);
  105. }).then(function () {
  106. return browser.findElement(by.id('fileUploadSubmitBtn')).click();
  107. }).then(function () {
  108. browser.sleep(3000).then(function () { done(); });
  109. });
  110. }));
  111. }
  112. function checkImage(done) {
  113. browser.get('https://' + app.fqdn + '/user/admin');
  114. browser.wait(until.elementLocated(by.xpath('//img[@src="/uploads/profile/1-profileimg.png"]')), TEST_TIMEOUT);
  115. var img = browser.findElement(by.xpath('//img[@src="/uploads/profile/1-profileimg.png"]'));
  116. browser.executeScript('return arguments[0].complete && arguments[0].naturalWidth', img).then(function (imageWidth) {
  117. done(imageWidth === 128 ? null : new Error('failed to load image'));
  118. });
  119. }
  120. xit('build app', function () {
  121. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  122. });
  123. it('install app', function () {
  124. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  125. });
  126. it('can get app information', function () {
  127. var inspect = JSON.parse(execSync('cloudron inspect'));
  128. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  129. expect(app).to.be.an('object');
  130. });
  131. it('can login', login);
  132. it('check mail plugin', checkMailPlugin);
  133. it('can install custom plugin', installCustomPlugin);
  134. it('can restart forum', restartForum); // required before activate!
  135. it('can activate custom plugin', activateCustomPlugin);
  136. it('can list custom plugin', listCustomPlugin);
  137. it('can upload image', uploadImage);
  138. it('can check image', checkImage);
  139. it('backup app', function () {
  140. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  141. });
  142. it('restore app', function () {
  143. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  144. });
  145. it('can login', login);
  146. it('can list custom plugin', listCustomPlugin);
  147. it('can check image', checkImage);
  148. it('can restart app', function (done) {
  149. execSync('cloudron restart --app ' + app.id);
  150. done();
  151. });
  152. it('can login', login);
  153. it('can list custom plugin', listCustomPlugin);
  154. it('can check image', checkImage);
  155. it('move to different location', function () {
  156. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  157. var inspect = JSON.parse(execSync('cloudron inspect'));
  158. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  159. expect(app).to.be.an('object');
  160. });
  161. it('can login', login);
  162. it('can list custom plugin', listCustomPlugin);
  163. it('can check image', checkImage);
  164. it('uninstall app', function () {
  165. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  166. });
  167. });