test.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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').then(function () {
  49. return browser.wait(until.elementLocated(by.id('host')), TEST_TIMEOUT);
  50. }).then(function () {
  51. return browser.sleep(5000);
  52. }).then(function () {
  53. return browser.findElement(by.id('host')).getAttribute('value');
  54. }).then(function (val) {
  55. if (val !== 'mail') return done(new Error('Incorrect mail server value: ' + val));
  56. done();
  57. });
  58. }
  59. function restartForum(done) {
  60. browser.get('https://' + app.fqdn + '/admin').then(function () {
  61. return browser.sleep(3000);
  62. }).then(function () {
  63. return browser.findElement(by.xpath('//button[text()="Restart"]')).click();
  64. }).then(function () {
  65. return browser.sleep(3000);
  66. }).then(function () {
  67. return browser.findElement(by.xpath('//button[text()="Confirm"]')).click();
  68. }).then(function () {
  69. console.log('Waiting for forum to restart');
  70. return browser.sleep(40000); // wait 30secs for reload
  71. }).then(function () {
  72. return browser.get('https://' + app.fqdn + '/admin');
  73. }).then(function () {
  74. return browser.sleep(3000);
  75. }).then(function () {
  76. return browser.findElement(by.xpath('//h1[text()="Dashboard"]'))
  77. }).then(function () { done(); });
  78. }
  79. function installCustomPlugin(done) {
  80. execSync('cloudron exec --app ' + app.id + ' -- /usr/local/bin/gosu cloudron:cloudron npm install nodebb-plugin-beep', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  81. done();
  82. }
  83. function activateCustomPlugin(done) {
  84. browser.get('https://' + app.fqdn + '/admin/extend/plugins#installed');
  85. browser.wait(until.elementLocated(by.xpath('//ul[contains(@class, "installed")]//strong[text()="nodebb-plugin-beep"]')), TEST_TIMEOUT).then(function () {
  86. return browser.sleep(10000);
  87. }).then(function () {
  88. return browser.findElement(by.xpath('//li[@id="nodebb-plugin-beep"]//button[@data-action="toggleActive"]')).click(); // activate the plugin
  89. }).then(function () {
  90. browser.sleep(20000).then(function() { done(); }); // wait for the action to succeed
  91. });
  92. }
  93. function listCustomPlugin(done) {
  94. browser.get('https://' + app.fqdn + '/admin/extend/plugins#installed');
  95. browser.wait(until.elementLocated(by.xpath('//strong[text()="nodebb-plugin-beep"]')), TEST_TIMEOUT).then(function () {
  96. done();
  97. });
  98. }
  99. function uploadImage(done) {
  100. browser.get('https://' + app.fqdn + '/user/admin/edit');
  101. browser.wait(until.elementLocated(by.xpath('//a[text()="Change Picture"]')), TEST_TIMEOUT).then(function () {
  102. browser.findElement(by.xpath('//a[text()="Change Picture"]')).click().then(function () {
  103. return browser.sleep(4000);
  104. }).then(function () {
  105. return browser.findElement(by.xpath('//button[@data-action="upload"]')).click();
  106. }).then(function () {
  107. return browser.sleep(4000);
  108. }).then(function () {
  109. return browser.findElement(by.xpath('//input[@type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  110. }).then(function () {
  111. browser.sleep(3000);
  112. }).then(function () { // upload it
  113. return browser.findElement(by.id('fileUploadSubmitBtn')).click();
  114. }).then(function () {
  115. browser.sleep(3000);
  116. }).then(function () {
  117. return browser.findElement(by.xpath('//button[text()="Crop and upload"]')).click();
  118. }).then(function () {
  119. browser.sleep(3000).then(function () { done(); });
  120. });
  121. });
  122. }
  123. function checkImage(done) {
  124. browser.get('https://' + app.fqdn + '/user/admin');
  125. browser.wait(until.elementLocated(by.xpath('//img[@src="/assets/uploads/profile/1-profileavatar.png"]')), TEST_TIMEOUT).then(function () {
  126. var img = browser.findElement(by.xpath('//img[@src="/assets/uploads/profile/1-profileavatar.png"]'));
  127. return browser.executeScript('return arguments[0].complete && arguments[0].naturalWidth', img);
  128. }).then(function (imageWidth) {
  129. done(imageWidth === 128 ? null : new Error('failed to load image'));
  130. });
  131. }
  132. xit('build app', function () {
  133. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  134. });
  135. it('install app', function () {
  136. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  137. });
  138. it('can get app information', function () {
  139. var inspect = JSON.parse(execSync('cloudron inspect'));
  140. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  141. expect(app).to.be.an('object');
  142. });
  143. it('can login', login);
  144. it('check mail plugin', checkMailPlugin);
  145. it('can install custom plugin', installCustomPlugin);
  146. it('can restart forum', restartForum); // required before activate!
  147. it('can activate custom plugin', activateCustomPlugin);
  148. it('can list custom plugin', listCustomPlugin);
  149. it('can upload image', uploadImage);
  150. it('can check image', checkImage);
  151. it('backup app', function () {
  152. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  153. });
  154. it('restore app', function () {
  155. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  156. });
  157. it('can login', login);
  158. it('can list custom plugin', listCustomPlugin);
  159. it('can check image', checkImage);
  160. it('can restart app', function (done) {
  161. execSync('cloudron restart --app ' + app.id);
  162. done();
  163. });
  164. it('can login', login);
  165. it('can list custom plugin', listCustomPlugin);
  166. it('can check image', checkImage);
  167. it('move to different location', function () {
  168. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  169. var inspect = JSON.parse(execSync('cloudron inspect'));
  170. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  171. expect(app).to.be.an('object');
  172. });
  173. it('can login', login);
  174. it('can list custom plugin', listCustomPlugin);
  175. it('can check image', checkImage);
  176. it('uninstall app', function () {
  177. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  178. });
  179. });