test.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. if (app.manifest.version === '1.7.0') {
  87. return browser.wait(until.elementLocated(by.xpath('//h3/a[text()="Hello Cloudron!"]')), TIMEOUT);
  88. } else {
  89. return browser.wait(until.elementLocated(by.xpath('//h2/a[text()="Hello Cloudron!"]')), TIMEOUT);
  90. }
  91. }).then(function () {
  92. done();
  93. });
  94. }
  95. function editPostWordpress4(done) {
  96. browser.get('https://' + app.fqdn + '/wp-admin/post.php?post=1&action=edit').then(function () {
  97. return browser.wait(until.elementLocated(by.xpath('//input[@id="title"]')), TIMEOUT);
  98. }).then(function () {
  99. return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys(Key.chord(Key.CONTROL, 'a'));
  100. }).then(function () {
  101. return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys('Hello Cloudron!');
  102. }).then(function () {
  103. return browser.findElement(by.xpath('//input[@id="publish"]')).click();
  104. }).then(function () {
  105. return browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "Post updated.")]')), TIMEOUT);
  106. }).then(function () {
  107. done();
  108. });
  109. }
  110. function editPost(done) {
  111. browser.get('https://' + app.fqdn + '/wp-admin/post.php?post=1&action=edit').then(function () {
  112. return browser.wait(until.elementLocated(by.xpath('//button[@aria-label="Disable tips"]')), TIMEOUT);
  113. }).then(function () {
  114. return browser.findElement(by.xpath('//button[@aria-label="Disable tips"]')).click();
  115. }).then(function () {
  116. return browser.findElement(by.xpath('//textarea[@id="post-title-0"]')).sendKeys(Key.chord(Key.CONTROL, 'a'));
  117. }).then(function () {
  118. return browser.findElement(by.xpath('//textarea[@id="post-title-0"]')).sendKeys('Hello Cloudron!');
  119. }).then(function () {
  120. return browser.findElement(by.xpath('//button[text()="Update"]')).click();
  121. }).then(function () {
  122. return browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "Post updated.")]')), TIMEOUT);
  123. }).then(function () {
  124. return browser.sleep(3000);
  125. }).then(function () {
  126. done();
  127. });
  128. }
  129. function uploadMedia(done) {
  130. browser.get('https://' + app.fqdn + '/wp-admin/media-new.php?browser-uploader').then(function () {
  131. return browser.wait(until.elementLocated(by.id('async-upload')), TIMEOUT);
  132. }).then(function () {
  133. return browser.findElement(by.xpath('//input[@id="async-upload" and @type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  134. }).then(function () {
  135. return browser.findElement(by.id('html-upload')).click();
  136. }).then(function () {
  137. return browser.wait(function () {
  138. return browser.getCurrentUrl().then(function (url) {
  139. return url === 'https://' + app.fqdn + '/wp-admin/upload.php';
  140. });
  141. }, TIMEOUT);
  142. }).then(function () {
  143. done();
  144. });
  145. }
  146. function checkMedia(item, done) {
  147. browser.get(`https://${app.fqdn}/wp-admin/upload.php?item=${item}`).then(function () { // there's got to be a better way..
  148. return browser.wait(until.elementLocated(by.xpath('//*[text()="Attachment Details"]')), TIMEOUT);
  149. }).then(function () {
  150. return browser.findElement(by.xpath('//img[@class="details-image"]')).getAttribute('src');
  151. }).then(function (srcLink) {
  152. console.log('media is located at ', srcLink);
  153. mediaLink = srcLink;
  154. done();
  155. });
  156. }
  157. function checkMediaLink(done) {
  158. superagent.get(mediaLink).end(function (error, result) {
  159. expect(error).to.be(null);
  160. expect(result.statusCode).to.be(200);
  161. done();
  162. });
  163. }
  164. xit('build app', function () {
  165. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  166. });
  167. it('can login', function (done) {
  168. var inspect = JSON.parse(execSync('cloudron inspect'));
  169. superagent.post('https://' + inspect.apiEndpoint + '/api/v1/developer/login').send({
  170. username: username,
  171. password: password
  172. }).end(function (error, result) {
  173. if (error) return done(error);
  174. if (result.statusCode !== 200) return done(new Error('Login failed with status ' + result.statusCode));
  175. token = result.body.accessToken;
  176. superagent.get('https://' + inspect.apiEndpoint + '/api/v1/profile')
  177. .query({ access_token: token }).end(function (error, result) {
  178. if (error) return done(error);
  179. if (result.statusCode !== 200) return done(new Error('Get profile failed with status ' + result.statusCode));
  180. email = result.body.email;
  181. done();
  182. });
  183. });
  184. });
  185. it('install app', function () {
  186. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  187. });
  188. it('can get app information', function () {
  189. var inspect = JSON.parse(execSync('cloudron inspect'));
  190. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  191. expect(app).to.be.an('object');
  192. });
  193. it('can get the main page', function (done) {
  194. superagent.get('https://' + app.fqdn).end(function (error, result) {
  195. expect(error).to.be(null);
  196. expect(result.status).to.eql(200);
  197. done();
  198. });
  199. });
  200. it('can login', login.bind(null, username, password));
  201. it('is an admin dashboard', function (done) {
  202. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  203. });
  204. it('can edit', editPost);
  205. it('can upload media', uploadMedia);
  206. var mediaLink;
  207. it('can see media', checkMedia.bind(null, 6));
  208. it('can see media link', checkMediaLink);
  209. it('has correct htaccess', checkHtaccess);
  210. it('can access permalink', checkPermalink);
  211. it('can restart app', function (done) {
  212. execSync('cloudron restart --wait --app ' + app.id);
  213. done();
  214. });
  215. it('can login', login.bind(null, username, password));
  216. it('can see updated post', checkPost);
  217. it('can see media link', checkMediaLink);
  218. it('has correct htaccess', checkHtaccess);
  219. it('can access permalink', checkPermalink);
  220. it('backup app', function () {
  221. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  222. });
  223. it('restore app', function () {
  224. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  225. });
  226. it('can login', login.bind(null, username, password));
  227. it('can see updated post', checkPost);
  228. it('can see media link', checkMediaLink);
  229. it('has correct htaccess', checkHtaccess);
  230. it('can access permalink', checkPermalink);
  231. it('can login', login.bind(null, username, password));
  232. it('runs cron jobs', function (done) {
  233. this.timeout(6 * 60 * 1000); // cron runs only every 5 minutes
  234. console.log('It can take upto 6 mins to detect that cron is working');
  235. function checkLogs() {
  236. var logs = execSync('cloudron logs --lines 1000 --app ' + app.id).toString('utf8');
  237. if (logs.indexOf('Executed the cron event \'wp_version_check\'') !== -1) return done();
  238. setTimeout(checkLogs, 45000);
  239. }
  240. setTimeout(checkLogs, 45000);
  241. });
  242. it('move to different location', function () {
  243. browser.manage().deleteAllCookies();
  244. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  245. var inspect = JSON.parse(execSync('cloudron inspect'));
  246. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  247. expect(app).to.be.an('object');
  248. mediaLink = mediaLink.replace(LOCATION, LOCATION + '2');
  249. });
  250. it('can login', login.bind(null, username, password));
  251. it('can see updated post', checkPost);
  252. it('can see media link', checkMediaLink);
  253. it('has correct htaccess', checkHtaccess);
  254. it('can access permalink', checkPermalink);
  255. it('can login', login.bind(null, username, password));
  256. it('can logout', logout);
  257. it('uninstall app', function () {
  258. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  259. });
  260. // check if the _first_ login via email succeeds
  261. it('can login via email', function (done) {
  262. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  263. var inspect = JSON.parse(execSync('cloudron inspect'));
  264. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  265. expect(app).to.be.an('object');
  266. login(email, password, function () {
  267. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  268. logout(done);
  269. });
  270. });
  271. // No SSO
  272. it('install app (no sso)', function () {
  273. execSync('cloudron install --new --wait --no-sso --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  274. });
  275. it('can get app information', function () {
  276. var inspect = JSON.parse(execSync('cloudron inspect'));
  277. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  278. expect(app).to.be.an('object');
  279. });
  280. it('can login (no sso)', login.bind(null, 'admin', 'changeme'));
  281. it('is an admin dashboard (no sso)', function (done) {
  282. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  283. });
  284. it('can logout', logout);
  285. it('uninstall app (no sso)', function () {
  286. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  287. });
  288. // test update
  289. it('can install app', function () {
  290. execSync('cloudron install --new --wait --appstore-id org.wordpress.cloudronapp --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  291. var inspect = JSON.parse(execSync('cloudron inspect'));
  292. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  293. expect(app).to.be.an('object');
  294. });
  295. it('can login', login.bind(null, username, password));
  296. it('can edit', editPost);
  297. it('can upload media', uploadMedia);
  298. it('can update', function () {
  299. execSync('cloudron install --wait --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  300. });
  301. it('can login', login.bind(null, username, password));
  302. it('can see updated post', checkPost);
  303. it('can see media', checkMedia.bind(null, 6));
  304. it('can see media link', checkMediaLink);
  305. it('can access permalink', checkPermalink);
  306. it('uninstall app', function () {
  307. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  308. });
  309. });