test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 checkPost(done) {
  85. browser.get('https://' + app.fqdn).then(function () {
  86. return browser.wait(until.elementLocated(by.xpath('//h3/a[text()="Hello Cloudron!"]')), TIMEOUT);
  87. }).then(function () {
  88. done();
  89. });
  90. }
  91. function editPost(done) {
  92. browser.get('https://' + app.fqdn + '/wp-admin/post.php?post=1&action=edit').then(function () {
  93. return browser.wait(until.elementLocated(by.xpath('//input[@id="title"]')), TIMEOUT);
  94. }).then(function () {
  95. return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys(Key.chord(Key.CONTROL, 'a'));
  96. }).then(function () {
  97. return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys('Hello Cloudron!');
  98. }).then(function () {
  99. return browser.findElement(by.xpath('//input[@id="publish"]')).click();
  100. }).then(function () {
  101. return browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "Post updated.")]')), TIMEOUT);
  102. }).then(function () {
  103. done();
  104. });
  105. }
  106. function uploadMedia(done) {
  107. browser.get('https://' + app.fqdn + '/wp-admin/media-new.php?browser-uploader').then(function () {
  108. return browser.wait(until.elementLocated(by.id('async-upload')), TIMEOUT);
  109. }).then(function () {
  110. return browser.findElement(by.xpath('//input[@id="async-upload" and @type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  111. }).then(function () {
  112. return browser.findElement(by.id('html-upload')).click();
  113. }).then(function () {
  114. return browser.wait(function () {
  115. return browser.getCurrentUrl().then(function (url) {
  116. return url === 'https://' + app.fqdn + '/wp-admin/upload.php';
  117. });
  118. }, TIMEOUT);
  119. }).then(function () {
  120. done();
  121. });
  122. }
  123. function checkMedia(item, done) {
  124. browser.get(`https://${app.fqdn}/wp-admin/upload.php?item=${item}`).then(function () { // there's got to be a better way..
  125. return browser.wait(until.elementLocated(by.xpath('//*[text()="Attachment Details"]')), TIMEOUT);
  126. }).then(function () {
  127. return browser.findElement(by.xpath('//img[@class="details-image"]')).getAttribute('src');
  128. }).then(function (srcLink) {
  129. console.log('media is located at ', srcLink);
  130. mediaLink = srcLink;
  131. done();
  132. });
  133. }
  134. function checkMediaLink(done) {
  135. superagent.get(mediaLink).end(function (error, result) {
  136. expect(error).to.be(null);
  137. expect(result.statusCode).to.be(200);
  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.bind(null, 6));
  185. it('can see media link', checkMediaLink);
  186. it('has correct htaccess', checkHtaccess);
  187. it('can access permalink', checkPermalink);
  188. it('can restart app', function (done) {
  189. execSync('cloudron restart --wait --app ' + app.id);
  190. done();
  191. });
  192. it('can login', login.bind(null, username, password));
  193. it('can see updated post', checkPost);
  194. it('can see media link', checkMediaLink);
  195. it('has correct htaccess', checkHtaccess);
  196. it('can access permalink', checkPermalink);
  197. it('backup app', function () {
  198. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  199. });
  200. it('restore app', function () {
  201. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  202. });
  203. it('can login', login.bind(null, username, password));
  204. it('can see updated post', checkPost);
  205. it('can see media link', checkMediaLink);
  206. it('has correct htaccess', checkHtaccess);
  207. it('can access permalink', checkPermalink);
  208. it('can login', login.bind(null, username, password));
  209. it('runs cron jobs', function (done) {
  210. this.timeout(6 * 60 * 1000); // cron runs only every 5 minutes
  211. console.log('It can take upto 6 mins to detect that cron is working');
  212. function checkLogs() {
  213. var logs = execSync('cloudron logs --lines 1000 --app ' + app.id).toString('utf8');
  214. if (logs.indexOf('Executed the cron event \'wp_version_check\'') !== -1) return done();
  215. setTimeout(checkLogs, 45000);
  216. }
  217. setTimeout(checkLogs, 45000);
  218. });
  219. it('move to different location', function () {
  220. browser.manage().deleteAllCookies();
  221. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  222. var inspect = JSON.parse(execSync('cloudron inspect'));
  223. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  224. expect(app).to.be.an('object');
  225. mediaLink = mediaLink.replace(LOCATION, LOCATION + '2');
  226. });
  227. it('can login', login.bind(null, username, password));
  228. it('can see updated post', checkPost);
  229. it('can see media link', checkMediaLink);
  230. it('has correct htaccess', checkHtaccess);
  231. it('can access permalink', checkPermalink);
  232. it('can login', login.bind(null, username, password));
  233. it('can logout', logout);
  234. it('uninstall app', function () {
  235. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  236. });
  237. // check if the _first_ login via email succeeds
  238. it('can login via email', function (done) {
  239. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  240. var inspect = JSON.parse(execSync('cloudron inspect'));
  241. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  242. expect(app).to.be.an('object');
  243. login(email, password, function () {
  244. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  245. logout(done);
  246. });
  247. });
  248. // No SSO
  249. it('install app (no sso)', function () {
  250. execSync('cloudron install --new --wait --no-sso --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  251. });
  252. it('can get app information', function () {
  253. var inspect = JSON.parse(execSync('cloudron inspect'));
  254. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  255. expect(app).to.be.an('object');
  256. });
  257. it('can login (no sso)', login.bind(null, 'admin', 'changeme'));
  258. it('is an admin dashboard (no sso)', function (done) {
  259. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  260. });
  261. it('can logout', logout);
  262. it('uninstall app (no sso)', function () {
  263. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  264. });
  265. // test update
  266. it('can install app', function () {
  267. execSync('cloudron install --new --wait --appstore-id org.wordpress.cloudronapp --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  268. var inspect = JSON.parse(execSync('cloudron inspect'));
  269. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  270. expect(app).to.be.an('object');
  271. });
  272. it('can login', login.bind(null, username, password));
  273. it('can edit', editPost);
  274. it('can upload media', uploadMedia);
  275. it('can update', function () {
  276. execSync('cloudron install --wait --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  277. });
  278. it('can login', login.bind(null, username, password));
  279. it('can see updated post', checkPost);
  280. it('can see media', checkMedia.bind(null, 5));
  281. it('can see media link', checkMediaLink);
  282. it('can access permalink', checkPermalink);
  283. it('uninstall app', function () {
  284. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  285. });
  286. });