test.js 13 KB

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