test.js 9.7 KB

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