test.js 13 KB

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