test.js 11 KB

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