test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  21. describe('Application life cycle test', function () {
  22. this.timeout(0);
  23. var firefox = require('selenium-webdriver/firefox');
  24. var server, browser = new firefox.Driver();
  25. var LOCATION = 'gogstest';
  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, 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. rimraf.sync(repodir);
  44. done();
  45. });
  46. xit('build app', function () {
  47. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  48. });
  49. it('can login', function (done) {
  50. var inspect = JSON.parse(execSync('cloudron inspect'));
  51. superagent.post('https://' + inspect.apiEndpoint + '/api/v1/developer/login').send({
  52. username: username,
  53. password: password
  54. }).end(function (error, result) {
  55. if (error) return done(error);
  56. if (result.statusCode !== 200) return done(new Error('Login failed with status ' + result.statusCode));
  57. token = result.body.token;
  58. superagent.get('https://' + inspect.apiEndpoint + '/api/v1/profile')
  59. .query({ access_token: token }).end(function (error, result) {
  60. if (error) return done(error);
  61. if (result.statusCode !== 200) return done(new Error('Get profile failed with status ' + result.statusCode));
  62. email = result.body.email;
  63. done();
  64. });
  65. });
  66. });
  67. it('install app', function () {
  68. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  69. });
  70. it('can get app information', function () {
  71. var inspect = JSON.parse(execSync('cloudron inspect'));
  72. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  73. expect(app).to.be.an('object');
  74. });
  75. it('can get the main page', function (done) {
  76. superagent.get('https://' + app.fqdn).end(function (error, result) {
  77. expect(error).to.be(null);
  78. expect(result.status).to.eql(200);
  79. done();
  80. });
  81. });
  82. it('can login', function (done) {
  83. browser.get('https://' + app.fqdn + '/user/login');
  84. browser.findElement(by.id('user_name')).sendKeys(username);
  85. browser.findElement(by.id('password')).sendKeys(password);
  86. browser.findElement(by.tagName('form')).submit();
  87. browser.wait(until.elementLocated(by.linkText('Dashboard')), 4000).then(function () { done(); });
  88. });
  89. it('can add public key', function (done) {
  90. browser.get('https://' + app.fqdn + '/user/settings/ssh');
  91. var publicKey = fs.readFileSync(__dirname + '/id_rsa.pub', 'utf8');
  92. browser.findElement(by.xpath('//div[text()="Add Key"]')).click();
  93. browser.findElement(by.id('title')).sendKeys('testkey');
  94. browser.findElement(by.id('content')).sendKeys(publicKey);
  95. browser.findElement(by.xpath('//button[contains(text(), "Add Key")]')).click();
  96. browser.wait(until.elementLocated(by.xpath('//p[contains(text(), "added successfully!")]')), 4000).then(function () { done(); });
  97. });
  98. it('can create repo', function (done) {
  99. browser.get('https://' + app.fqdn);
  100. browser.findElement(by.linkText('New Repository')).click();
  101. browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "New Repository")]')), 4000);
  102. browser.findElement(by.id('repo_name')).sendKeys(reponame);
  103. browser.findElement(by.id('auto-init')).click();
  104. browser.findElement(by.xpath('//button[contains(text(), "Create Repository")]')).click();
  105. browser.wait(function () {
  106. return browser.getCurrentUrl().then(function (url) {
  107. return url === 'https://' + app.fqdn + '/' + username + '/' + reponame;
  108. });
  109. }, 4000).then(function () { done(); });
  110. });
  111. it('displays correct clone url', function (done) {
  112. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame);
  113. browser.findElement(by.id('repo-clone-ssh')).click();
  114. browser.findElement(by.id('repo-clone-url')).getAttribute('value').then(function (cloneUrl) {
  115. expect(cloneUrl).to.be('ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git');
  116. done();
  117. });
  118. });
  119. it('can clone the url', function (done) {
  120. var env = Object.create(process.env);
  121. env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
  122. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  123. done();
  124. });
  125. it('can add and push a file', function (done) {
  126. var env = Object.create(process.env);
  127. env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
  128. execSync('touch newfile && git add newfile && git commit -a -mx && git push ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + ' master',
  129. { env: env, cwd: repodir });
  130. rimraf.sync('/tmp/testrepo');
  131. done();
  132. });
  133. it('can restart app', function (done) {
  134. execSync('cloudron restart');
  135. done();
  136. });
  137. it('can clone the url', function (done) {
  138. var env = Object.create(process.env);
  139. env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
  140. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  141. expect(fs.existsSync(repodir + '/newfile')).to.be(true);
  142. rimraf.sync(repodir);
  143. done();
  144. });
  145. it('backup app', function () {
  146. execSync('cloudron backup --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  147. });
  148. it('restore app', function () {
  149. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  150. });
  151. it('can clone the url', function (done) {
  152. var env = Object.create(process.env);
  153. env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
  154. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  155. expect(fs.existsSync(repodir + '/newfile')).to.be(true);
  156. rimraf.sync(repodir);
  157. 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. });
  166. it('can login', function (done) {
  167. browser.get('https://' + app.fqdn + '/user/login');
  168. browser.findElement(by.id('user_name')).sendKeys(username);
  169. browser.findElement(by.id('password')).sendKeys(password);
  170. browser.findElement(by.tagName('form')).submit();
  171. browser.wait(until.elementLocated(by.linkText('Dashboard')), 4000).then(function () { done(); });
  172. });
  173. it('displays correct clone url', function (done) {
  174. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame);
  175. browser.findElement(by.id('repo-clone-ssh')).click();
  176. browser.findElement(by.id('repo-clone-url')).getAttribute('value').then(function (cloneUrl) {
  177. expect(cloneUrl).to.be('ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git');
  178. done();
  179. });
  180. });
  181. it('can clone the url', function (done) {
  182. var env = Object.create(process.env);
  183. env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
  184. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  185. expect(fs.existsSync(repodir + '/newfile')).to.be(true);
  186. rimraf.sync(repodir);
  187. done();
  188. });
  189. it('uninstall app', function () {
  190. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  191. });
  192. // check if the _first_ login via email succeeds
  193. it('can login via email', function () {
  194. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  195. var inspect = JSON.parse(execSync('cloudron inspect'));
  196. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  197. expect(app).to.be.an('object');
  198. browser.get('https://' + app.fqdn + '/user/login');
  199. browser.findElement(by.id('user_name')).sendKeys(email);
  200. browser.findElement(by.id('password')).sendKeys(password);
  201. browser.findElement(by.tagName('form')).submit();
  202. browser.wait(until.elementLocated(by.linkText('Dashboard')), 4000).then(function () { done(); });
  203. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  204. });
  205. });