test.js 10 KB

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