test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 checkHtaccess(done) {
  64. var out = execSync('cloudron exec -- cat /app/data/htaccess');
  65. expect(out.toString('utf8').indexOf('RewriteEngine On')).to.not.be(-1); // wp generates this with permalinks in hard mode
  66. done();
  67. }
  68. function checkPermalink(done) {
  69. browser.get('https://' + app.fqdn + '/hello-world').then(function () {
  70. return browser.findElement(by.xpath('//h1[text()="Hello Cloudron!"]'));
  71. }).then(function () {
  72. done();
  73. });
  74. }
  75. function checkMedia(done) {
  76. superagent.get(mediaLink).end(function (error, result) {
  77. expect(error).to.be(null);
  78. expect(result.statusCode).to.be(200);
  79. done();
  80. });
  81. }
  82. function checkPost(done) {
  83. browser.get('https://' + app.fqdn).then(function () {
  84. return browser.wait(until.elementLocated(by.xpath('//h3/a[text()="Hello Cloudron!"]')), TIMEOUT);
  85. }).then(function () {
  86. done();
  87. });
  88. }
  89. function editPost(done) {
  90. browser.get('https://' + app.fqdn + '/wp-admin/post.php?post=1&action=edit').then(function () {
  91. return browser.wait(until.elementLocated(by.xpath('//input[@id="title"]')), TIMEOUT);
  92. }).then(function () {
  93. return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys(Key.chord(Key.CONTROL, 'a'));
  94. }).then(function () {
  95. return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys('Hello Cloudron!');
  96. }).then(function () {
  97. return browser.findElement(by.xpath('//input[@id="publish"]')).click();
  98. }).then(function () {
  99. return browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "Post updated.")]')), TIMEOUT);
  100. }).then(function () {
  101. done();
  102. });
  103. }
  104. function uploadMedia(done) {
  105. browser.get('https://' + app.fqdn + '/wp-admin/media-new.php?browser-uploader').then(function () {
  106. return browser.wait(until.elementLocated(by.id('async-upload')), TIMEOUT);
  107. }).then(function () {
  108. return browser.findElement(by.xpath('//input[@id="async-upload" and @type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  109. }).then(function () {
  110. return browser.findElement(by.id('html-upload')).click();
  111. }).then(function () {
  112. return browser.wait(function () {
  113. return browser.getCurrentUrl().then(function (url) {
  114. return url === 'https://' + app.fqdn + '/wp-admin/upload.php';
  115. });
  116. }, TIMEOUT);
  117. }).then(function () {
  118. done();
  119. });
  120. }
  121. function checkMedia(done) {
  122. browser.get('https://' + app.fqdn + '/wp-admin/upload.php?item=5').then(function () { // there's got to be a better way..
  123. return browser.wait(until.elementLocated(by.xpath('//*[text()="Attachment Details"]')), TIMEOUT);
  124. }).then(function () {
  125. return browser.findElement(by.xpath('//img[@class="details-image"]')).getAttribute('src');
  126. }).then(function (srcLink) {
  127. console.log('media is located at ', srcLink);
  128. mediaLink = srcLink;
  129. done();
  130. });
  131. }
  132. xit('build app', function () {
  133. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  134. });
  135. it('can login', function (done) {
  136. var inspect = JSON.parse(execSync('cloudron inspect'));
  137. superagent.post('https://' + inspect.apiEndpoint + '/api/v1/developer/login').send({
  138. username: username,
  139. password: password
  140. }).end(function (error, result) {
  141. if (error) return done(error);
  142. if (result.statusCode !== 200) return done(new Error('Login failed with status ' + result.statusCode));
  143. token = result.body.token;
  144. superagent.get('https://' + inspect.apiEndpoint + '/api/v1/profile')
  145. .query({ access_token: token }).end(function (error, result) {
  146. if (error) return done(error);
  147. if (result.statusCode !== 200) return done(new Error('Get profile failed with status ' + result.statusCode));
  148. email = result.body.email;
  149. done();
  150. });
  151. });
  152. });
  153. it('install app', function () {
  154. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  155. });
  156. it('can get app information', function () {
  157. var inspect = JSON.parse(execSync('cloudron inspect'));
  158. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  159. expect(app).to.be.an('object');
  160. });
  161. it('can get the main page', function (done) {
  162. superagent.get('https://' + app.fqdn).end(function (error, result) {
  163. expect(error).to.be(null);
  164. expect(result.status).to.eql(200);
  165. done();
  166. });
  167. });
  168. it('can login', login.bind(null, username, password));
  169. it('is an admin dashboard', function (done) {
  170. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  171. });
  172. it('can edit', editPost);
  173. it('can upload media', uploadMedia);
  174. var mediaLink;
  175. it('can see media', checkMedia);
  176. it('has correct htaccess', checkHtaccess);
  177. it('can access permalink', checkPermalink);
  178. it('runs cron jobs', function (done) {
  179. console.log('sleeping for a minute to ensure that cron job has run');
  180. setTimeout(function () {
  181. var logs = execSync('cloudron logs --lines 1000 --app ' + app.id).toString('utf8');
  182. if (logs.indexOf('Executed the cron event \'wp_version_check\'') !== -1) return done();
  183. console.log(logs);
  184. done(new Error('cron jobs are possibly not running'));
  185. }, 60 * 1000);
  186. });
  187. it('can restart app', function (done) {
  188. execSync('cloudron restart --wait');
  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('move to different location', function () {
  209. browser.manage().deleteAllCookies();
  210. execSync('cloudron configure --wait --location ' + LOCATION + '2', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  211. var inspect = JSON.parse(execSync('cloudron inspect'));
  212. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  213. expect(app).to.be.an('object');
  214. mediaLink = mediaLink.replace(LOCATION, LOCATION + '2');
  215. });
  216. it('can login', login.bind(null, username, password));
  217. it('can see updated post', checkPost);
  218. it('can see media', checkMedia);
  219. it('has correct htaccess', checkHtaccess);
  220. it('can access permalink', checkPermalink);
  221. it('can login', login.bind(null, username, password));
  222. it('uninstall app', function () {
  223. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  224. });
  225. // check if the _first_ login via email succeeds
  226. it('can login via email', function (done) {
  227. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  228. var inspect = JSON.parse(execSync('cloudron inspect'));
  229. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  230. expect(app).to.be.an('object');
  231. login(email, password, function () {
  232. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  233. done();
  234. });
  235. });
  236. // No SSO
  237. it('install app (no sso)', function () {
  238. execSync('cloudron install --new --wait --no-sso --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  239. });
  240. it('can get app information', function () {
  241. var inspect = JSON.parse(execSync('cloudron inspect'));
  242. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  243. expect(app).to.be.an('object');
  244. });
  245. it('can login (no sso)', login.bind(null, 'admin', 'changeme'));
  246. it('is an admin dashboard (no sso)', function (done) {
  247. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  248. });
  249. it('uninstall app (no sso)', function () {
  250. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  251. });
  252. // test update
  253. it('can install app', function () {
  254. execSync('cloudron install --new --wait --appstore-id org.wordpress.cloudronapp --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  255. var inspect = JSON.parse(execSync('cloudron inspect'));
  256. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  257. expect(app).to.be.an('object');
  258. });
  259. it('can login', login.bind(null, username, password));
  260. it('can edit', editPost);
  261. it('can upload media', uploadMedia);
  262. it('can update', function () {
  263. execSync('cloudron install --wait --app ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  264. });
  265. it('can login', login.bind(null, username, password));
  266. it('can see updated post', checkPost);
  267. it('can see media', checkMedia);
  268. it('can access permalink', checkPermalink);
  269. it('uninstall app', function () {
  270. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  271. });
  272. });