test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 = '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 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.trim()); // #3480
  96. browser.findElement(by.xpath('//button[contains(text(), "Add Key")]')).click();
  97. browser.wait(until.elementLocated(by.xpath('//p[contains(text(), "added successfully!")]')), TIMEOUT).then(function () { done(); });
  98. });
  99. it('can create repo', function (done) {
  100. browser.get('https://' + app.fqdn);
  101. browser.findElement(by.linkText('New Repository')).click();
  102. browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "New Repository")]')), TIMEOUT);
  103. browser.findElement(by.id('repo_name')).sendKeys(reponame);
  104. browser.findElement(by.id('auto-init')).click();
  105. browser.findElement(by.xpath('//button[contains(text(), "Create Repository")]')).click();
  106. browser.wait(function () {
  107. return browser.getCurrentUrl().then(function (url) {
  108. return url === 'https://' + app.fqdn + '/' + username + '/' + reponame;
  109. });
  110. }, TIMEOUT).then(function () { done(); });
  111. });
  112. it('displays correct clone url', function (done) {
  113. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame);
  114. browser.findElement(by.id('repo-clone-ssh')).click();
  115. browser.findElement(by.id('repo-clone-url')).getAttribute('value').then(function (cloneUrl) {
  116. expect(cloneUrl).to.be('ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git');
  117. done();
  118. });
  119. });
  120. it('can clone the url', function (done) {
  121. var env = Object.create(process.env);
  122. env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
  123. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  124. done();
  125. });
  126. it('can add and push a file', function (done) {
  127. var env = Object.create(process.env);
  128. env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
  129. execSync('touch newfile && git add newfile && git commit -a -mx && git push ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + ' master',
  130. { env: env, cwd: repodir });
  131. rimraf.sync('/tmp/testrepo');
  132. done();
  133. });
  134. it('can restart app', function (done) {
  135. execSync('cloudron restart');
  136. done();
  137. });
  138. it('can clone the url', function (done) {
  139. var env = Object.create(process.env);
  140. env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
  141. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  142. expect(fs.existsSync(repodir + '/newfile')).to.be(true);
  143. rimraf.sync(repodir);
  144. done();
  145. });
  146. it('backup app', function () {
  147. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  148. });
  149. it('restore app', function () {
  150. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  151. });
  152. it('can clone the url', function (done) {
  153. var env = Object.create(process.env);
  154. env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
  155. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  156. expect(fs.existsSync(repodir + '/newfile')).to.be(true);
  157. rimraf.sync(repodir);
  158. done();
  159. });
  160. it('move to different location', function () {
  161. browser.manage().deleteAllCookies();
  162. execSync('cloudron configure --location ' + LOCATION + '2', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  163. var inspect = JSON.parse(execSync('cloudron inspect'));
  164. app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
  165. expect(app).to.be.an('object');
  166. });
  167. it('can login', function (done) {
  168. browser.get('https://' + app.fqdn + '/user/login');
  169. browser.findElement(by.id('user_name')).sendKeys(username);
  170. browser.findElement(by.id('password')).sendKeys(password);
  171. browser.findElement(by.tagName('form')).submit();
  172. browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT).then(function () { done(); });
  173. });
  174. it('displays correct clone url', function (done) {
  175. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame);
  176. browser.findElement(by.id('repo-clone-ssh')).click();
  177. browser.findElement(by.id('repo-clone-url')).getAttribute('value').then(function (cloneUrl) {
  178. expect(cloneUrl).to.be('ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git');
  179. done();
  180. });
  181. });
  182. it('can clone the url', function (done) {
  183. var env = Object.create(process.env);
  184. env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
  185. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  186. expect(fs.existsSync(repodir + '/newfile')).to.be(true);
  187. rimraf.sync(repodir);
  188. done();
  189. });
  190. it('uninstall app', function () {
  191. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  192. });
  193. // check if the _first_ login via email succeeds
  194. it('can login via email', function (done) {
  195. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  196. var inspect = JSON.parse(execSync('cloudron inspect'));
  197. app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
  198. expect(app).to.be.an('object');
  199. browser.get('https://' + app.fqdn + '/user/login');
  200. browser.findElement(by.id('user_name')).sendKeys(email);
  201. browser.findElement(by.id('password')).sendKeys(password);
  202. browser.findElement(by.tagName('form')).submit();
  203. browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT).then(function () {
  204. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  205. done();
  206. });
  207. });
  208. });