test.js 8.9 KB

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