test.js 11 KB

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