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