test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #!/usr/bin/env node
  2. /* jslint node:true */
  3. /* global it:false */
  4. /* global xit:false */
  5. /* global describe:false */
  6. /* global before:false */
  7. /* global after:false */
  8. 'use strict';
  9. var execSync = require('child_process').execSync,
  10. ejs = require('ejs'),
  11. expect = require('expect.js'),
  12. fs = require('fs'),
  13. mkdirp = require('mkdirp'),
  14. path = require('path'),
  15. rimraf = require('rimraf'),
  16. superagent = require('superagent'),
  17. util = require('util'),
  18. webdriver = require('selenium-webdriver');
  19. var by = require('selenium-webdriver').By,
  20. until = require('selenium-webdriver').until,
  21. Key = require('selenium-webdriver').Key,
  22. Builder = require('selenium-webdriver').Builder;
  23. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  24. describe('Application life cycle test', function () {
  25. this.timeout(0);
  26. var server, browser = new Builder().forBrowser('chrome').build();
  27. var LOCATION = 'test';
  28. var app;
  29. var username = process.env.USERNAME;
  30. var password = process.env.PASSWORD;
  31. var TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 5000;
  32. var email, token;
  33. before(function (done) {
  34. if (!process.env.USERNAME) return done(new Error('USERNAME env var not set'));
  35. if (!process.env.PASSWORD) return done(new Error('PASSWORD env var not set'));
  36. var seleniumJar= require('selenium-server-standalone-jar');
  37. var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
  38. server = new SeleniumServer(seleniumJar.path, { port: 4444 });
  39. server.start();
  40. done();
  41. });
  42. after(function (done) {
  43. browser.quit();
  44. server.stop();
  45. done();
  46. });
  47. function login(username, password, done) {
  48. browser.manage().deleteAllCookies().then(function () {
  49. return browser.get('https://' + app.fqdn + '/wp-login.php');
  50. }).then(function () {
  51. return browser.sleep(2000); // there seems to be some javascript that gives auto-focus to username
  52. }).then(function () {
  53. return browser.findElement(by.id('user_login')).sendKeys(username);
  54. }).then(function () {
  55. return browser.findElement(by.id('user_pass')).sendKeys(password);
  56. }).then(function () {
  57. return browser.findElement(by.tagName('form')).submit();
  58. }).then(function () {
  59. return browser.wait(until.elementLocated(by.xpath('//h1[text()="Dashboard"]')), TIMEOUT);
  60. }).then(function () {
  61. done();
  62. });
  63. }
  64. function logout(done) {
  65. browser.manage().deleteAllCookies().then(function () {
  66. browser.executeScript('localStorage.clear();');
  67. browser.executeScript('sessionStorage.clear();');
  68. done();
  69. });
  70. }
  71. function checkHtaccess(done) {
  72. var out = execSync(util.format('cloudron exec --app %s -- cat /app/data/htaccess', app.id));
  73. expect(out.toString('utf8').indexOf('RewriteEngine On')).to.not.be(-1); // wp generates this with permalinks in hard mode
  74. done();
  75. }
  76. function checkPermalink(done) {
  77. browser.get('https://' + app.fqdn + '/hello-world').then(function () {
  78. return browser.findElement(by.xpath('//h1[text()="Hello Cloudron!"]'));
  79. }).then(function () {
  80. done();
  81. });
  82. }
  83. function checkMedia(done) {
  84. superagent.get(mediaLink).end(function (error, result) {
  85. expect(error).to.be(null);
  86. expect(result.statusCode).to.be(200);
  87. done();
  88. });
  89. }
  90. function checkPost(done) {
  91. browser.get('https://' + app.fqdn).then(function () {
  92. return browser.wait(until.elementLocated(by.xpath('//h3/a[text()="Hello Cloudron!"]')), TIMEOUT);
  93. }).then(function () {
  94. done();
  95. });
  96. }
  97. function editPost(done) {
  98. browser.get('https://' + app.fqdn + '/wp-admin/post.php?post=1&action=edit').then(function () {
  99. return browser.wait(until.elementLocated(by.xpath('//input[@id="title"]')), TIMEOUT);
  100. }).then(function () {
  101. return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys(Key.chord(Key.CONTROL, 'a'));
  102. }).then(function () {
  103. return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys('Hello Cloudron!');
  104. }).then(function () {
  105. return browser.findElement(by.xpath('//input[@id="publish"]')).click();
  106. }).then(function () {
  107. return browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "Post updated.")]')), TIMEOUT);
  108. }).then(function () {
  109. done();
  110. });
  111. }
  112. function uploadMedia(done) {
  113. browser.get('https://' + app.fqdn + '/wp-admin/media-new.php?browser-uploader').then(function () {
  114. return browser.wait(until.elementLocated(by.id('async-upload')), TIMEOUT);
  115. }).then(function () {
  116. return browser.findElement(by.xpath('//input[@id="async-upload" and @type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  117. }).then(function () {
  118. return browser.findElement(by.id('html-upload')).click();
  119. }).then(function () {
  120. return browser.wait(function () {
  121. return browser.getCurrentUrl().then(function (url) {
  122. return url === 'https://' + app.fqdn + '/wp-admin/upload.php';
  123. });
  124. }, TIMEOUT);
  125. }).then(function () {
  126. done();
  127. });
  128. }
  129. function checkMedia(done) {
  130. browser.get('https://' + app.fqdn + '/wp-admin/upload.php?item=5').then(function () { // there's got to be a better way..
  131. return browser.wait(until.elementLocated(by.xpath('//*[text()="Attachment Details"]')), TIMEOUT);
  132. }).then(function () {
  133. return browser.findElement(by.xpath('//img[@class="details-image"]')).getAttribute('src');
  134. }).then(function (srcLink) {
  135. console.log('media is located at ', srcLink);
  136. mediaLink = srcLink;
  137. done();
  138. });
  139. }
  140. xit('build app', function () {
  141. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  142. });
  143. it('can login', function (done) {
  144. var inspect = JSON.parse(execSync('cloudron inspect'));
  145. superagent.post('https://' + inspect.apiEndpoint + '/api/v1/developer/login').send({
  146. username: username,
  147. password: password
  148. }).end(function (error, result) {
  149. if (error) return done(error);
  150. if (result.statusCode !== 200) return done(new Error('Login failed with status ' + result.statusCode));
  151. token = result.body.token;
  152. superagent.get('https://' + inspect.apiEndpoint + '/api/v1/profile')
  153. .query({ access_token: token }).end(function (error, result) {
  154. if (error) return done(error);
  155. if (result.statusCode !== 200) return done(new Error('Get profile failed with status ' + result.statusCode));
  156. email = result.body.email;
  157. done();
  158. });
  159. });
  160. });
  161. it('install app', function () {
  162. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  163. });
  164. it('can get app information', function () {
  165. var inspect = JSON.parse(execSync('cloudron inspect'));
  166. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  167. expect(app).to.be.an('object');
  168. });
  169. it('can get the main page', function (done) {
  170. superagent.get('https://' + app.fqdn).end(function (error, result) {
  171. expect(error).to.be(null);
  172. expect(result.status).to.eql(200);
  173. done();
  174. });
  175. });
  176. it('can login', login.bind(null, username, password));
  177. it('is an admin dashboard', function (done) {
  178. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  179. });
  180. it('can edit', editPost);
  181. it('can upload media', uploadMedia);
  182. var mediaLink;
  183. it('can see media', checkMedia);
  184. it('has correct htaccess', checkHtaccess);
  185. it('can access permalink', checkPermalink);
  186. it('can restart app', function (done) {
  187. execSync('cloudron restart --wait --app ' + app.id);
  188. done();
  189. });
  190. it('can login', login.bind(null, username, password));
  191. it('can see updated post', checkPost);
  192. it('can see media', checkMedia);
  193. it('has correct htaccess', checkHtaccess);
  194. it('can access permalink', checkPermalink);
  195. it('backup app', function () {
  196. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  197. });
  198. it('restore app', function () {
  199. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  200. });
  201. it('can login', login.bind(null, username, password));
  202. it('can see updated post', checkPost);
  203. it('can see media', checkMedia);
  204. it('has correct htaccess', checkHtaccess);
  205. it('can access permalink', checkPermalink);
  206. it('can login', login.bind(null, username, password));
  207. it('runs cron jobs', function (done) {
  208. this.timeout(120000);
  209. // cron jobs are only executed when app is running and healthy
  210. setTimeout(function () {
  211. var logs = execSync('cloudron logs --lines 1000 --app ' + app.id).toString('utf8');
  212. if (logs.indexOf('Executed the cron event \'wp_version_check\'') !== -1) return done();
  213. console.log(logs);
  214. done(new Error('cron jobs are possibly not running'));
  215. }, 45000);
  216. });
  217. it('move to different location', function () {
  218. browser.manage().deleteAllCookies();
  219. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  220. var inspect = JSON.parse(execSync('cloudron inspect'));
  221. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  222. expect(app).to.be.an('object');
  223. mediaLink = mediaLink.replace(LOCATION, LOCATION + '2');
  224. });
  225. it('can login', login.bind(null, username, password));
  226. it('can see updated post', checkPost);
  227. it('can see media', checkMedia);
  228. it('has correct htaccess', checkHtaccess);
  229. it('can access permalink', checkPermalink);
  230. it('can login', login.bind(null, username, password));
  231. it('can logout', logout);
  232. it('uninstall app', function () {
  233. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  234. });
  235. // check if the _first_ login via email succeeds
  236. it('can login via email', function (done) {
  237. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  238. var inspect = JSON.parse(execSync('cloudron inspect'));
  239. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  240. expect(app).to.be.an('object');
  241. login(email, password, function () {
  242. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  243. logout(done);
  244. });
  245. });
  246. // No SSO
  247. it('install app (no sso)', function () {
  248. execSync('cloudron install --new --wait --no-sso --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  249. });
  250. it('can get app information', function () {
  251. var inspect = JSON.parse(execSync('cloudron inspect'));
  252. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  253. expect(app).to.be.an('object');
  254. });
  255. it('can login (no sso)', login.bind(null, 'admin', 'changeme'));
  256. it('is an admin dashboard (no sso)', function (done) {
  257. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  258. });
  259. it('can logout', logout);
  260. it('uninstall app (no sso)', function () {
  261. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  262. });
  263. // test update
  264. it('can install app', function () {
  265. execSync('cloudron install --new --wait --appstore-id org.wordpress.cloudronapp --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  266. var inspect = JSON.parse(execSync('cloudron inspect'));
  267. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  268. expect(app).to.be.an('object');
  269. });
  270. it('can login', login.bind(null, username, password));
  271. it('can edit', editPost);
  272. it('can upload media', uploadMedia);
  273. it('can update', function () {
  274. execSync('cloudron install --wait --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  275. });
  276. it('can login', login.bind(null, username, password));
  277. it('can see updated post', checkPost);
  278. it('can see media', checkMedia);
  279. it('can access permalink', checkPermalink);
  280. it('uninstall app', function () {
  281. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  282. });
  283. });