test.js 11 KB

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