test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. xit('build app', function () {
  90. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  91. });
  92. it('can login', function (done) {
  93. var inspect = JSON.parse(execSync('cloudron inspect'));
  94. superagent.post('https://' + inspect.apiEndpoint + '/api/v1/developer/login').send({
  95. username: username,
  96. password: password
  97. }).end(function (error, result) {
  98. if (error) return done(error);
  99. if (result.statusCode !== 200) return done(new Error('Login failed with status ' + result.statusCode));
  100. token = result.body.token;
  101. superagent.get('https://' + inspect.apiEndpoint + '/api/v1/profile')
  102. .query({ access_token: token }).end(function (error, result) {
  103. if (error) return done(error);
  104. if (result.statusCode !== 200) return done(new Error('Get profile failed with status ' + result.statusCode));
  105. email = result.body.email;
  106. done();
  107. });
  108. });
  109. });
  110. it('install app', function () {
  111. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  112. });
  113. it('can get app information', function () {
  114. var inspect = JSON.parse(execSync('cloudron inspect'));
  115. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  116. expect(app).to.be.an('object');
  117. });
  118. it('can get the main page', function (done) {
  119. superagent.get('https://' + app.fqdn).end(function (error, result) {
  120. expect(error).to.be(null);
  121. expect(result.status).to.eql(200);
  122. done();
  123. });
  124. });
  125. it('can login', login.bind(null, username, password));
  126. it('is an admin dashboard', function (done) {
  127. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  128. });
  129. it('can edit', function (done) {
  130. browser.get('https://' + app.fqdn + '/wp-admin/post.php?post=1&action=edit').then(function () {
  131. return browser.wait(until.elementLocated(by.xpath('//input[@id="title"]')), TIMEOUT);
  132. }).then(function () {
  133. return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys(Key.chord(Key.CONTROL, 'a'));
  134. }).then(function () {
  135. return browser.findElement(by.xpath('//input[@id="title"]')).sendKeys('Hello Cloudron!');
  136. }).then(function () {
  137. return browser.findElement(by.xpath('//input[@id="publish"]')).click();
  138. }).then(function () {
  139. return browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "Post updated.")]')), TIMEOUT);
  140. }).then(function () {
  141. done();
  142. });
  143. });
  144. it('can upload media', function (done) {
  145. browser.get('https://' + app.fqdn + '/wp-admin/media-new.php?browser-uploader').then(function () {
  146. return browser.wait(until.elementLocated(by.id('async-upload')), TIMEOUT);
  147. }).then(function () {
  148. return browser.findElement(by.xpath('//input[@id="async-upload" and @type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  149. }).then(function () {
  150. return browser.findElement(by.id('html-upload')).click();
  151. }).then(function () {
  152. return browser.wait(function () {
  153. return browser.getCurrentUrl().then(function (url) {
  154. return url === 'https://' + app.fqdn + '/wp-admin/upload.php';
  155. });
  156. }, TIMEOUT);
  157. }).then(function () {
  158. done();
  159. });
  160. });
  161. var mediaLink;
  162. it('can see media', function (done) {
  163. browser.get('https://' + app.fqdn + '/wp-admin/upload.php?item=5').then(function () { // there's got to be a better way..
  164. return browser.wait(until.elementLocated(by.xpath('//*[text()="Attachment Details"]')), TIMEOUT);
  165. }).then(function () {
  166. return browser.findElement(by.xpath('//img[@class="details-image"]')).getAttribute('src');
  167. }).then(function (srcLink) {
  168. console.log('media is located at ', srcLink);
  169. mediaLink = srcLink;
  170. done();
  171. });
  172. });
  173. it('has correct htaccess', checkHtaccess);
  174. it('can access permalink', checkPermalink);
  175. it('can restart app', function (done) {
  176. execSync('cloudron restart');
  177. done();
  178. });
  179. it('can see updated post', checkPost);
  180. it('can see media', checkMedia);
  181. it('has correct htaccess', checkHtaccess);
  182. it('can access permalink', checkPermalink);
  183. it('backup app', function () {
  184. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  185. });
  186. it('restore app', function () {
  187. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  188. });
  189. it('can see updated post', checkPost);
  190. it('can see media', checkMedia);
  191. it('has correct htaccess', checkHtaccess);
  192. it('can access permalink', checkPermalink);
  193. it('can login', login.bind(null, username, password));
  194. it('move to different location', function () {
  195. browser.manage().deleteAllCookies();
  196. execSync('cloudron configure --wait --location ' + LOCATION + '2', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  197. var inspect = JSON.parse(execSync('cloudron inspect'));
  198. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  199. expect(app).to.be.an('object');
  200. mediaLink = mediaLink.replace(LOCATION, LOCATION + '2');
  201. });
  202. it('can see updated post', checkPost);
  203. it('can see media', checkMedia);
  204. it('has correct htaccess', checkHtaccess);
  205. it('can access permalink', checkPermalink);
  206. it('can login', login.bind(null, username, password));
  207. it('uninstall app', function () {
  208. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  209. });
  210. // check if the _first_ login via email succeeds
  211. it('can login via email', function (done) {
  212. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  213. var inspect = JSON.parse(execSync('cloudron inspect'));
  214. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  215. expect(app).to.be.an('object');
  216. login(email, password, function () {
  217. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  218. done();
  219. });
  220. });
  221. // No SSO
  222. it('install app (no sso)', function () {
  223. execSync('cloudron install --new --wait --no-sso --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  224. });
  225. it('can get app information', function () {
  226. var inspect = JSON.parse(execSync('cloudron inspect'));
  227. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  228. expect(app).to.be.an('object');
  229. });
  230. it('can login (no sso)', login.bind(null, 'admin', 'changeme'));
  231. it('is an admin dashboard (no sso)', function (done) {
  232. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  233. });
  234. it('uninstall app (no sso)', function () {
  235. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  236. });
  237. });