test.js 10 KB

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