test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. Builder = require('selenium-webdriver').Builder;
  21. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  22. describe('Application life cycle test', function () {
  23. this.timeout(0);
  24. var server, browser = new Builder().forBrowser('chrome').build();
  25. var LOCATION = 'test';
  26. var repodir = '/tmp/testrepo';
  27. var app, reponame = 'testrepo';
  28. var username = process.env.USERNAME;
  29. var password = process.env.PASSWORD;
  30. var email = process.env.EMAIL;
  31. var TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 5000;
  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. rimraf.sync(repodir);
  45. done();
  46. });
  47. function getAppInfo() {
  48. var inspect = JSON.parse(execSync('cloudron inspect'));
  49. app = inspect.apps.filter(function (a) { return a.location === LOCATION || a.location === LOCATION + '2'; })[0];
  50. expect(app).to.be.an('object');
  51. }
  52. function login(done) {
  53. browser.manage().deleteAllCookies();
  54. browser.get('https://' + app.fqdn + '/user/login');
  55. browser.findElement(by.id('user_name')).sendKeys(username);
  56. browser.findElement(by.id('password')).sendKeys(password);
  57. browser.findElement(by.tagName('form')).submit();
  58. browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT).then(function () { done(); });
  59. }
  60. function waitForUrl(url, done) {
  61. browser.wait(function () {
  62. return browser.getCurrentUrl().then(function (currentUrl) {
  63. return currentUrl === url;
  64. });
  65. }, TIMEOUT).then(function () { done(); });
  66. }
  67. function setAvatar(done) {
  68. browser.get('https://' + app.fqdn + '/user/settings/avatar');
  69. browser.findElement(by.xpath('//input[@type="file" and @name="avatar"]')).sendKeys(path.resolve(__dirname, '../logo.png')).then(function () {
  70. browser.findElement(by.xpath('//button[contains(text(), "Update Avatar Setting")]')).click();
  71. browser.wait(until.elementLocated(by.xpath('//p[contains(text(),"updated successfully")]')), TIMEOUT).then(function () { done(); });
  72. });
  73. }
  74. function checkAvatar(done) {
  75. superagent.get('https://' + app.fqdn + '/avatars/1').end(function (error, result) {
  76. expect(error).to.be(null);
  77. expect(result.statusCode).to.be(200);
  78. done();
  79. });
  80. }
  81. function createRepo(done) {
  82. browser.get('https://' + app.fqdn);
  83. browser.findElement(by.linkText('New Repository')).click();
  84. browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "New Repository")]')), TIMEOUT);
  85. browser.findElement(by.id('repo_name')).sendKeys(reponame);
  86. browser.findElement(by.id('auto-init')).click();
  87. browser.findElement(by.xpath('//button[contains(text(), "Create Repository")]')).click();
  88. browser.wait(function () {
  89. return browser.getCurrentUrl().then(function (url) {
  90. return url === 'https://' + app.fqdn + '/' + username + '/' + reponame;
  91. });
  92. }, TIMEOUT).then(function () { done(); });
  93. }
  94. function cloneRepo() {
  95. rimraf.sync(repodir);
  96. var env = Object.create(process.env);
  97. env.GIT_SSH_COMMAND = __dirname + '/git_ssh_wrapper.sh';
  98. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  99. }
  100. function pushFile() {
  101. var env = Object.create(process.env);
  102. env.GIT_SSH_COMMAND = __dirname + '/git_ssh_wrapper.sh';
  103. execSync('touch newfile && git add newfile && git commit -a -mx && git push ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + ' master', { env: env, cwd: repodir });
  104. }
  105. function editFile(done) {
  106. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame + '/_edit/master/newfile');
  107. var cm = browser.findElement(by.xpath('//div[contains(@class,"CodeMirror")]'));
  108. var text = 'yo';
  109. browser.executeScript('arguments[0].CodeMirror.setValue("' + text + '");', cm).then(function () {
  110. browser.findElement(by.xpath('//input[@name="commit_summary"]')).sendKeys('Dummy edit');
  111. browser.findElement(by.xpath('//button[contains(text(), "Commit Changes")]')).click();
  112. waitForUrl('https://' + app.fqdn + '/' + username + '/' + reponame + '/src/master/newfile', done);
  113. });
  114. }
  115. function fileExists() {
  116. expect(fs.existsSync(repodir + '/newfile')).to.be(true);
  117. }
  118. function checkCloneUrl(done) {
  119. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame);
  120. browser.findElement(by.id('repo-clone-ssh')).click();
  121. browser.findElement(by.id('repo-clone-url')).getAttribute('value').then(function (cloneUrl) {
  122. expect(cloneUrl).to.be('ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git');
  123. done();
  124. });
  125. }
  126. function addPublicKey(done) {
  127. browser.get('https://' + app.fqdn + '/user/settings/ssh');
  128. var publicKey = fs.readFileSync(__dirname + '/id_rsa.pub', 'utf8');
  129. browser.findElement(by.xpath('//div[text()="Add Key"]')).click();
  130. browser.findElement(by.id('title')).sendKeys('testkey');
  131. browser.findElement(by.id('content')).sendKeys(publicKey.trim()); // #3480
  132. browser.findElement(by.xpath('//button[contains(text(), "Add Key")]')).click();
  133. browser.wait(until.elementLocated(by.xpath('//p[contains(text(), "added successfully!")]')), TIMEOUT).then(function () { done(); });
  134. }
  135. xit('build app', function () {
  136. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  137. });
  138. it('install app', function () {
  139. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  140. });
  141. it('can get app information', getAppInfo);
  142. it('can login', login);
  143. it('can set avatar', setAvatar);
  144. it('can get avatar', checkAvatar);
  145. it('can add public key', addPublicKey);
  146. it('can create repo', createRepo);
  147. it('displays correct clone url', checkCloneUrl);
  148. it('can clone the url', cloneRepo);
  149. it('can add and push a file', pushFile);
  150. it('can edit file', editFile);
  151. it('can restart app', function () {
  152. execSync('cloudron restart --wait --app ' + app.id);
  153. });
  154. it('can clone the url', cloneRepo);
  155. it('file exists in cloned repo', fileExists);
  156. it('backup app', function () {
  157. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  158. });
  159. it('restore app', function () {
  160. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  161. });
  162. it('can get avatar', checkAvatar);
  163. it('can clone the url', cloneRepo);
  164. it('file exists in cloned repo', fileExists);
  165. it('move to different location', function () {
  166. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  167. });
  168. it('can get app information', getAppInfo);
  169. it('can login', login);
  170. it('can get avatar', checkAvatar);
  171. it('displays correct clone url', checkCloneUrl);
  172. it('can clone the url', cloneRepo);
  173. it('file exists in cloned repo', fileExists);
  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 install app', function () {
  179. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  180. });
  181. it('can get app information', getAppInfo);
  182. it('can login via email', function (done) {
  183. browser.get('https://' + app.fqdn + '/user/login');
  184. browser.findElement(by.id('user_name')).sendKeys(email);
  185. browser.findElement(by.id('password')).sendKeys(password);
  186. browser.findElement(by.tagName('form')).submit();
  187. browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT).then(function () { done(); });
  188. });
  189. it('uninstall app', function () {
  190. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  191. });
  192. // test update
  193. it('can install app', function () {
  194. execSync('cloudron install --new --wait --appstore-id ' + app.manifest.id + ' --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  195. });
  196. it('can get app information', getAppInfo);
  197. it('can login', login);
  198. it('can set avatar', setAvatar);
  199. it('can get avatar', checkAvatar);
  200. it('can add public key', addPublicKey);
  201. it('can create repo', createRepo);
  202. it('can clone the url', cloneRepo);
  203. it('can add and push a file', pushFile);
  204. it('can update', function () {
  205. execSync('cloudron install --wait --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  206. });
  207. it('can get avatar', checkAvatar);
  208. it('can clone the url', cloneRepo);
  209. it('file exists in cloned repo', fileExists);
  210. it('uninstall app', function () {
  211. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  212. });
  213. });