test.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  22. describe('Application life cycle test', function () {
  23. this.timeout(0);
  24. var firefox = require('selenium-webdriver/chrome');
  25. var server, browser = new firefox.Driver();
  26. var LOCATION = 'wptest';
  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(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. xit('build app', function () {
  61. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  62. });
  63. it('can login', function (done) {
  64. var inspect = JSON.parse(execSync('cloudron inspect'));
  65. superagent.post('https://' + inspect.apiEndpoint + '/api/v1/developer/login').send({
  66. username: username,
  67. password: password
  68. }).end(function (error, result) {
  69. if (error) return done(error);
  70. if (result.statusCode !== 200) return done(new Error('Login failed with status ' + result.statusCode));
  71. token = result.body.token;
  72. superagent.get('https://' + inspect.apiEndpoint + '/api/v1/profile')
  73. .query({ access_token: token }).end(function (error, result) {
  74. if (error) return done(error);
  75. if (result.statusCode !== 200) return done(new Error('Get profile failed with status ' + result.statusCode));
  76. email = result.body.email;
  77. done();
  78. });
  79. });
  80. });
  81. it('install app', function () {
  82. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  83. });
  84. it('can get app information', function () {
  85. var inspect = JSON.parse(execSync('cloudron inspect'));
  86. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  87. expect(app).to.be.an('object');
  88. });
  89. it('can get the main page', function (done) {
  90. superagent.get('https://' + app.fqdn).end(function (error, result) {
  91. expect(error).to.be(null);
  92. expect(result.status).to.eql(200);
  93. done();
  94. });
  95. });
  96. it('can login', login);
  97. it('is an admin dashboard', function (done) {
  98. browser.wait(until.elementLocated(by.xpath('//div[@class="wp-menu-name" and contains(text(), "Plugins")]')), TIMEOUT).then(function () { done(); });
  99. });
  100. it('can edit', function (done) {
  101. browser.get('https://' + app.fqdn + '/wp-admin/post.php?post=1&action=edit');
  102. browser.wait(until.elementLocated(by.xpath('//input[@id="title"]')), TIMEOUT);
  103. browser.findElement(by.xpath('//input[@id="title"]')).sendKeys(Key.chord(Key.CONTROL, 'a'));
  104. browser.findElement(by.xpath('//input[@id="title"]')).sendKeys('Hello Cloudron!');
  105. browser.findElement(by.xpath('//input[@id="publish"]')).click();
  106. browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "Post updated.")]')), TIMEOUT).then(function () { done(); });
  107. });
  108. it('can upload media', function (done) {
  109. browser.get('https://' + app.fqdn + '/wp-admin/media-new.php?browser-uploader');
  110. browser.wait(until.elementLocated(by.id('async-upload')), TIMEOUT).then(function () {
  111. browser.findElement(by.xpath('//input[@id="async-upload" and @type="file"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  112. browser.findElement(by.id('html-upload')).click();
  113. browser.wait(function () {
  114. return browser.getCurrentUrl().then(function (url) {
  115. return url === 'https://' + app.fqdn + '/wp-admin/upload.php';
  116. });
  117. }, TIMEOUT).then(function () { done(); });
  118. });
  119. });
  120. var mediaLink;
  121. it('can see media', function (done) {
  122. browser.get('https://' + app.fqdn + '/wp-admin/upload.php?item=5'); // there's got to be a better way..
  123. browser.wait(until.elementLocated(by.xpath('//*[text()="Attachment Details"]')), TIMEOUT);
  124. browser.findElement(by.xpath('//img[@class="details-image"]')).getAttribute('src').then(function (srcLink) {
  125. console.log('media is located at ', srcLink);
  126. mediaLink = srcLink;
  127. done();
  128. });
  129. });
  130. it('has correct htaccess', checkHtaccess);
  131. it('can restart app', function (done) {
  132. execSync('cloudron restart');
  133. done();
  134. });
  135. it('can see updated post', function (done) {
  136. browser.get('https://' + app.fqdn);
  137. browser.findElement(by.xpath('//*[text()="Hello Cloudron!"]')).then(function () { done(); });
  138. });
  139. it('can see media', function (done) {
  140. superagent.get(mediaLink).end(function (error, result) {
  141. expect(error).to.be(null);
  142. expect(result.statusCode).to.be(200);
  143. done();
  144. });
  145. });
  146. it('backup app', function () {
  147. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  148. });
  149. it('restore app', function () {
  150. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  151. });
  152. it('can see updated post', function (done) {
  153. browser.get('https://' + app.fqdn);
  154. browser.findElement(by.xpath('//*[text()="Hello Cloudron!"]')).then(function () { done(); });
  155. });
  156. it('can see media', function (done) {
  157. superagent.get(mediaLink).end(function (error, result) {
  158. expect(error).to.be(null);
  159. expect(result.statusCode).to.be(200);
  160. done();
  161. });
  162. });
  163. it('can login', login);
  164. it('move to different location', function () {
  165. browser.manage().deleteAllCookies();
  166. execSync('cloudron install --location ' + LOCATION + '2', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  167. var inspect = JSON.parse(execSync('cloudron inspect'));
  168. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  169. expect(app).to.be.an('object');
  170. mediaLink = mediaLink.replace(LOCATION, LOCATION + '2');
  171. });
  172. it('can see updated post', function (done) {
  173. browser.get('https://' + app.fqdn);
  174. browser.findElement(by.xpath('//*[text()="Hello Cloudron!"]')).then(function () { done(); });
  175. });
  176. it('can see media', function (done) {
  177. superagent.get(mediaLink).end(function (error, result) {
  178. expect(error).to.be(null);
  179. expect(result.statusCode).to.be(200);
  180. done();
  181. });
  182. });
  183. it('can login', login);
  184. it('uninstall app', function () {
  185. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  186. });
  187. // check if the _first_ login via email succeeds
  188. it('can login via email', function (done) {
  189. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  190. var inspect = JSON.parse(execSync('cloudron inspect'));
  191. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  192. expect(app).to.be.an('object');
  193. login(function () {
  194. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  195. done();
  196. });
  197. });
  198. });