test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.get('https://' + app.fqdn + '/wp-login.php').then(function () {
  48. browser.sleep(2000); // there seems to be some javascript that gives auto-focus to username
  49. browser.findElement(by.id('user_login')).sendKeys(username);
  50. browser.findElement(by.id('user_pass')).sendKeys(password);
  51. browser.findElement(by.tagName('form')).submit();
  52. browser.wait(until.elementLocated(by.xpath('//h1[text()="Dashboard"]')), TIMEOUT).then(function () { done(); });
  53. });
  54. }
  55. function checkHtaccess(done) {
  56. var out = execSync('cloudron exec -- cat /app/data/htaccess');
  57. expect(out.toString('utf8').indexOf('RewriteEngine On')).to.not.be(-1); // wp generates this with permalinks in hard mode
  58. done();
  59. }
  60. function checkPermalink(done) {
  61. browser.get('https://' + app.fqdn + '/hello-world');
  62. browser.findElement(by.xpath('//h1[text()="Hello Cloudron!"]')).then(function () { done(); });
  63. }
  64. function checkMedia(done) {
  65. superagent.get(mediaLink).end(function (error, result) {
  66. expect(error).to.be(null);
  67. expect(result.statusCode).to.be(200);
  68. done();
  69. });
  70. }
  71. function checkPost(done) {
  72. browser.get('https://' + app.fqdn);
  73. browser.wait(until.elementLocated(by.xpath('//h2/a[text()="Hello Cloudron!"]')), TIMEOUT).then(function () { done(); });
  74. }
  75. xit('build app', function () {
  76. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  77. });
  78. it('can login', function (done) {
  79. var inspect = JSON.parse(execSync('cloudron inspect'));
  80. superagent.post('https://' + inspect.apiEndpoint + '/api/v1/developer/login').send({
  81. username: username,
  82. password: password
  83. }).end(function (error, result) {
  84. if (error) return done(error);
  85. if (result.statusCode !== 200) return done(new Error('Login failed with status ' + result.statusCode));
  86. token = result.body.token;
  87. superagent.get('https://' + inspect.apiEndpoint + '/api/v1/profile')
  88. .query({ access_token: token }).end(function (error, result) {
  89. if (error) return done(error);
  90. if (result.statusCode !== 200) return done(new Error('Get profile failed with status ' + result.statusCode));
  91. email = result.body.email;
  92. done();
  93. });
  94. });
  95. });
  96. it('install app', function () {
  97. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  98. });
  99. it('can get app information', function () {
  100. var inspect = JSON.parse(execSync('cloudron inspect'));
  101. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  102. expect(app).to.be.an('object');
  103. });
  104. it('can get the main page', function (done) {
  105. superagent.get('https://' + app.fqdn).end(function (error, result) {
  106. expect(error).to.be(null);
  107. expect(result.status).to.eql(200);
  108. done();
  109. });
  110. });
  111. it('can login', login.bind(null, username, password));
  112. it('is an admin dashboard', function (done) {
  113. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  114. });
  115. it('can edit', function (done) {
  116. browser.get('https://' + app.fqdn + '/wp-admin/post.php?post=1&action=edit');
  117. browser.wait(until.elementLocated(by.xpath('//input[@id="title"]')), TIMEOUT);
  118. browser.findElement(by.xpath('//input[@id="title"]')).sendKeys(Key.chord(Key.CONTROL, 'a'));
  119. browser.findElement(by.xpath('//input[@id="title"]')).sendKeys('Hello Cloudron!');
  120. browser.findElement(by.xpath('//input[@id="publish"]')).click();
  121. browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "Post updated.")]')), TIMEOUT).then(function () { done(); });
  122. });
  123. it('can upload media', function (done) {
  124. browser.get('https://' + app.fqdn + '/wp-admin/media-new.php?browser-uploader');
  125. browser.wait(until.elementLocated(by.id('async-upload')), TIMEOUT).then(function () {
  126. browser.findElement(by.xpath('//input[@id="async-upload" and @type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  127. browser.findElement(by.id('html-upload')).click();
  128. browser.wait(function () {
  129. return browser.getCurrentUrl().then(function (url) {
  130. return url === 'https://' + app.fqdn + '/wp-admin/upload.php';
  131. });
  132. }, TIMEOUT).then(function () { done(); });
  133. });
  134. });
  135. var mediaLink;
  136. it('can see media', function (done) {
  137. browser.get('https://' + app.fqdn + '/wp-admin/upload.php?item=5'); // there's got to be a better way..
  138. browser.wait(until.elementLocated(by.xpath('//*[text()="Attachment Details"]')), TIMEOUT);
  139. browser.findElement(by.xpath('//img[@class="details-image"]')).getAttribute('src').then(function (srcLink) {
  140. console.log('media is located at ', srcLink);
  141. mediaLink = srcLink;
  142. done();
  143. });
  144. });
  145. it('has correct htaccess', checkHtaccess);
  146. it('can access permalink', checkPermalink);
  147. it('can restart app', function (done) {
  148. execSync('cloudron restart');
  149. done();
  150. });
  151. it('can see updated post', checkPost);
  152. it('can see media', checkMedia);
  153. it('has correct htaccess', checkHtaccess);
  154. it('can access permalink', checkPermalink);
  155. it('backup app', function () {
  156. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  157. });
  158. it('restore app', function () {
  159. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  160. });
  161. it('can see updated post', checkPost);
  162. it('can see media', checkMedia);
  163. it('has correct htaccess', checkHtaccess);
  164. it('can access permalink', checkPermalink);
  165. it('can login', login.bind(null, username, password));
  166. it('move to different location', function () {
  167. browser.manage().deleteAllCookies();
  168. execSync('cloudron configure --wait --location ' + LOCATION + '2', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  169. var inspect = JSON.parse(execSync('cloudron inspect'));
  170. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  171. expect(app).to.be.an('object');
  172. mediaLink = mediaLink.replace(LOCATION, LOCATION + '2');
  173. });
  174. it('can see updated post', checkPost);
  175. it('can see media', checkMedia);
  176. it('has correct htaccess', checkHtaccess);
  177. it('can access permalink', checkPermalink);
  178. it('can login', login.bind(null, username, password));
  179. it('uninstall app', function () {
  180. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  181. });
  182. // check if the _first_ login via email succeeds
  183. it('can login via email', function (done) {
  184. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  185. var inspect = JSON.parse(execSync('cloudron inspect'));
  186. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  187. expect(app).to.be.an('object');
  188. login(email, password, function () {
  189. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  190. done();
  191. });
  192. });
  193. // No SSO
  194. it('install app', function () {
  195. execSync('cloudron install --new --wait --no-sso --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  196. });
  197. it('can get app information', function () {
  198. var inspect = JSON.parse(execSync('cloudron inspect'));
  199. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  200. expect(app).to.be.an('object');
  201. });
  202. it('can login', login.bind(null, 'admin', 'changeme'));
  203. it('is an admin dashboard', function (done) {
  204. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  205. });
  206. it('uninstall app', function () {
  207. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  208. });
  209. });