test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/usr/bin/env node
  2. 'use strict';
  3. require('chromedriver');
  4. var execSync = require('child_process').execSync,
  5. expect = require('expect.js'),
  6. path = require('path'),
  7. superagent = require('superagent'),
  8. webdriver = require('selenium-webdriver');
  9. var by = webdriver.By,
  10. Keys = webdriver.Key,
  11. until = webdriver.until;
  12. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  13. describe('Application life cycle test', function () {
  14. this.timeout(0);
  15. var chrome = require('selenium-webdriver/chrome');
  16. var server, browser = new chrome.Driver(), uploadedImageUrl;
  17. var username = 'admin', password = 'changeme123', oldPassword = 'changeme';
  18. before(function (done) {
  19. var seleniumJar= require('selenium-server-standalone-jar');
  20. var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
  21. server = new SeleniumServer(seleniumJar.path, { port: 4444 });
  22. server.start();
  23. done();
  24. });
  25. after(function (done) {
  26. browser.quit();
  27. server.stop();
  28. done();
  29. });
  30. var LOCATION = 'test';
  31. var TEST_TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 30000;
  32. var app;
  33. var email, token;
  34. function login(username, password, done) {
  35. // browser.manage().window().setSize(1280,768).then(function () {
  36. browser.manage().deleteAllCookies().then(function () {
  37. return browser.get('https://' + app.fqdn + '/login');
  38. }).then(function () {
  39. return browser.sleep(10000); // takes some time for username to be visible
  40. }).then(function () {
  41. return browser.wait(until.elementLocated(by.id('username')), TEST_TIMEOUT);
  42. }).then(function () {
  43. return browser.findElement(by.id('username')).sendKeys(username);
  44. }).then(function () {
  45. return browser.findElement(by.id('password')).sendKeys(password);
  46. }).then(function () {
  47. return browser.findElement(by.id('login')).click();
  48. }).then(function () {
  49. return browser.wait(until.elementLocated(by.xpath('//a[contains(text(), "Announcements")]')), TEST_TIMEOUT);
  50. }).then(function () {
  51. done();
  52. });
  53. }
  54. function checkMailPlugin(done) {
  55. browser.get('https://' + app.fqdn + '/admin/settings/email').then(function () {
  56. return browser.wait(until.elementLocated(by.id('email:smtpTransport:host')), TEST_TIMEOUT);
  57. }).then(function () {
  58. return browser.sleep(10000);
  59. }).then(function () {
  60. return browser.findElement(by.id('email:smtpTransport:host')).getAttribute('value');
  61. }).then(function (val) {
  62. if (val !== 'mail') return done(new Error('Incorrect mail server value: ' + val));
  63. done();
  64. });
  65. }
  66. function restartForum(done) {
  67. browser.get('https://' + app.fqdn + '/admin').then(function () {
  68. return browser.sleep(10000);
  69. }).then(function () {
  70. return browser.findElement(by.xpath('//button[text()="Restart"]')).click();
  71. }).then(function () {
  72. return browser.sleep(10000);
  73. }).then(function () {
  74. return browser.findElement(by.xpath('//button[text()="Confirm"]')).click();
  75. }).then(function () {
  76. console.log('Waiting for forum to restart');
  77. return browser.sleep(50000); // wait for reload
  78. }).then(function () {
  79. return browser.get('https://' + app.fqdn + '/admin');
  80. }).then(function () {
  81. return browser.sleep(10000);
  82. }).then(function () {
  83. return browser.wait(until.elementLocated(by.xpath('//h1[text()="Dashboard"]')), TEST_TIMEOUT);
  84. }).then(function () { done(); });
  85. }
  86. function installCustomPlugin(done) {
  87. execSync('cloudron exec --app ' + app.id + ' -- /usr/local/bin/gosu cloudron:cloudron npm install nodebb-plugin-beep', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  88. done();
  89. }
  90. function activateCustomPlugin(done) {
  91. browser.get('https://' + app.fqdn + '/admin/extend/plugins#installed').then(function () {
  92. return browser.wait(until.elementLocated(by.xpath('//ul[contains(@class, "installed")]//strong[text()="nodebb-plugin-beep"]')), TEST_TIMEOUT);
  93. }).then(function () {
  94. return browser.sleep(10000);
  95. }).then(function () {
  96. var button = browser.findElement(by.xpath('//li[@id="nodebb-plugin-beep"]//button[@data-action="toggleActive"]'));
  97. return browser.executeScript('arguments[0].scrollIntoView(false)', button);
  98. }).then(function () {
  99. return browser.findElement(by.xpath('//li[@id="nodebb-plugin-beep"]//button[@data-action="toggleActive"]')).click(); // activate the plugin
  100. }).then(function () {
  101. return browser.sleep(5000);
  102. }).then(function () {
  103. var button = browser.findElement(by.xpath('//button[text()="Confirm"]'));
  104. return browser.executeScript('arguments[0].scrollIntoView(false)', button);
  105. }).then(function () {
  106. return browser.findElement(by.xpath('//button[text()="Confirm"]')).click();
  107. }).then(function () {
  108. return browser.sleep(20000); // wait for the action to succeed
  109. }).then(function() {
  110. done();
  111. });
  112. }
  113. function listCustomPlugin(done) {
  114. browser.get('https://' + app.fqdn + '/admin/extend/plugins#installed').then(function () {
  115. return browser.wait(until.elementLocated(by.xpath('//strong[text()="nodebb-plugin-beep"]')), TEST_TIMEOUT);
  116. }).then(function () {
  117. done();
  118. });
  119. }
  120. function uploadImage(done) {
  121. browser.get('https://' + app.fqdn + '/user/admin/edit').then(function () {
  122. return browser.sleep(4000);
  123. }).then(function () {
  124. var button = browser.findElement(by.xpath('//a[text()="Change Picture"]'));
  125. return browser.executeScript('arguments[0].scrollIntoView(false)', button);
  126. }).then(function () {
  127. return browser.findElement(by.id('changePictureBtn')).click();
  128. }).then(function () {
  129. return browser.sleep(10000);
  130. }).then(function () {
  131. var button = browser.findElement(by.xpath('//button[@data-action="upload"]'));
  132. return browser.executeScript('arguments[0].scrollIntoView(false)', button);
  133. }).then(function () {
  134. return browser.findElement(by.xpath('//button[@data-action="upload"]')).click();
  135. }).then(function () {
  136. return browser.sleep(10000);
  137. }).then(function () {
  138. return browser.findElement(by.xpath('//input[@type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  139. }).then(function () {
  140. browser.sleep(10000);
  141. }).then(function () { // upload it
  142. return browser.findElement(by.id('fileUploadSubmitBtn')).click();
  143. }).then(function () {
  144. browser.sleep(10000);
  145. }).then(function () {
  146. return browser.findElement(by.xpath('//button[text()="Crop and upload"]')).click();
  147. }).then(function () {
  148. return browser.sleep(10000);
  149. }).then(function () {
  150. done();
  151. });
  152. }
  153. function checkImage(done) {
  154. browser.get('https://' + app.fqdn + '/user/admin').then(function () {
  155. return browser.wait(until.elementLocated(by.xpath('//img[@src="/assets/uploads/profile/1-profileavatar.png"]')), TEST_TIMEOUT);
  156. }).then(function () {
  157. var img = browser.findElement(by.xpath('//img[@src="/assets/uploads/profile/1-profileavatar.png"]'));
  158. return browser.executeScript('return arguments[0].complete && arguments[0].naturalWidth', img);
  159. }).then(function (imageWidth) {
  160. done(imageWidth === 200 ? null : new Error('failed to load image'));
  161. });
  162. }
  163. xit('build app', function () {
  164. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  165. });
  166. it('install app', function () {
  167. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  168. });
  169. it('can get app information', function () {
  170. var inspect = JSON.parse(execSync('cloudron inspect'));
  171. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  172. expect(app).to.be.an('object');
  173. });
  174. it('can login', login.bind(null, username, password));
  175. it('check mail plugin', checkMailPlugin);
  176. it('can install custom plugin', installCustomPlugin);
  177. it('can restart forum', restartForum); // required before activate!
  178. it('can activate custom plugin', activateCustomPlugin);
  179. it('can list custom plugin', listCustomPlugin);
  180. it('can upload image', uploadImage);
  181. it('can check image', checkImage);
  182. it('backup app', function () {
  183. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  184. });
  185. it('restore app', function () {
  186. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  187. });
  188. it('can login', login.bind(null, username, password));
  189. it('can list custom plugin', listCustomPlugin);
  190. it('can check image', checkImage);
  191. it('can restart app', function (done) {
  192. execSync('cloudron restart --wait --app ' + app.id);
  193. done();
  194. });
  195. it('can login', login.bind(null, username, password));
  196. it('can list custom plugin', listCustomPlugin);
  197. it('can check image', checkImage);
  198. it('move to different location', function () {
  199. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  200. var inspect = JSON.parse(execSync('cloudron inspect'));
  201. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  202. expect(app).to.be.an('object');
  203. });
  204. it('can login', login.bind(null, username, password));
  205. it('can list custom plugin', listCustomPlugin);
  206. it('can check image', checkImage);
  207. it('uninstall app', function () {
  208. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  209. });
  210. // test update
  211. it('can install app', function () {
  212. execSync('cloudron install --new --wait --appstore-id org.nodebb.cloudronapp --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  213. var inspect = JSON.parse(execSync('cloudron inspect'));
  214. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  215. expect(app).to.be.an('object');
  216. });
  217. it('can login', login.bind(null, username, oldPassword));
  218. it('check mail plugin', checkMailPlugin);
  219. it('can update', function () {
  220. execSync('cloudron install --wait --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  221. });
  222. it('check mail plugin', checkMailPlugin);
  223. it('uninstall app', function () {
  224. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  225. });
  226. });