test.js 8.8 KB

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