test.js 8.7 KB

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