test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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().then(function () {
  54. return browser.get('https://' + app.fqdn + '/user/login');
  55. }).then(function () {
  56. return browser.findElement(by.id('user_name')).sendKeys(username);
  57. }).then(function () {
  58. return browser.findElement(by.id('password')).sendKeys(password);
  59. }).then(function () {
  60. return browser.findElement(by.tagName('form')).submit();
  61. }).then(function () {
  62. return browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT);
  63. }).then(function () {
  64. done();
  65. });
  66. }
  67. function waitForUrl(url, done) {
  68. browser.wait(function () {
  69. return browser.getCurrentUrl().then(function (currentUrl) {
  70. return currentUrl === url;
  71. });
  72. }, TIMEOUT).then(function () { done(); });
  73. }
  74. function setAvatar(done) {
  75. browser.get('https://' + app.fqdn + '/user/settings/avatar').then(function () {
  76. return browser.findElement(by.xpath('//input[@type="file" and @name="avatar"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  77. }).then(function () {
  78. return browser.findElement(by.xpath('//button[contains(text(), "Update Avatar Setting")]')).click();
  79. }).then(function () {
  80. return browser.wait(until.elementLocated(by.xpath('//p[contains(text(),"updated successfully")]')), TIMEOUT);
  81. }).then(function () {
  82. done();
  83. });
  84. }
  85. function checkAvatar(done) {
  86. superagent.get('https://' + app.fqdn + '/avatars/1').end(function (error, result) {
  87. expect(error).to.be(null);
  88. expect(result.statusCode).to.be(200);
  89. done();
  90. });
  91. }
  92. function createRepo(done) {
  93. browser.get('https://' + app.fqdn).then(function () {
  94. return browser.findElement(by.linkText('New Repository')).click();
  95. }).then(function () {
  96. return browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "New Repository")]')), TIMEOUT);
  97. }).then(function () {
  98. return browser.findElement(by.id('repo_name')).sendKeys(reponame);
  99. }).then(function () {
  100. return browser.findElement(by.id('auto-init')).click();
  101. }).then(function () {
  102. return browser.findElement(by.xpath('//button[contains(text(), "Create Repository")]')).click();
  103. }).then(function () {
  104. return browser.wait(function () {
  105. return browser.getCurrentUrl().then(function (url) {
  106. return url === 'https://' + app.fqdn + '/' + username + '/' + reponame;
  107. });
  108. }, TIMEOUT);
  109. }).then(function () {
  110. done();
  111. });
  112. }
  113. function cloneRepo() {
  114. rimraf.sync(repodir);
  115. var env = Object.create(process.env);
  116. env.GIT_SSH_COMMAND = __dirname + '/git_ssh_wrapper.sh';
  117. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  118. }
  119. function pushFile() {
  120. var env = Object.create(process.env);
  121. env.GIT_SSH_COMMAND = __dirname + '/git_ssh_wrapper.sh';
  122. execSync('touch newfile && git add newfile && git commit -a -mx && git push ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + ' master', { env: env, cwd: repodir });
  123. }
  124. function editFile(done) {
  125. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame + '/_edit/master/newfile').then(function () {
  126. var cm = browser.findElement(by.xpath('//div[contains(@class,"CodeMirror")]'));
  127. var text = 'yo';
  128. return browser.executeScript('arguments[0].CodeMirror.setValue("' + text + '");', cm);
  129. }).then(function () {
  130. return browser.findElement(by.xpath('//input[@name="commit_summary"]')).sendKeys('Dummy edit');
  131. })..then(function () {
  132. return browser.findElement(by.xpath('//button[contains(text(), "Commit Changes")]')).click();
  133. }).then(function () {
  134. waitForUrl('https://' + app.fqdn + '/' + username + '/' + reponame + '/src/master/newfile', done);
  135. });
  136. }
  137. function fileExists() {
  138. expect(fs.existsSync(repodir + '/newfile')).to.be(true);
  139. }
  140. function checkCloneUrl(done) {
  141. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame).then(function () {
  142. return browser.findElement(by.id('repo-clone-ssh')).click();
  143. }).then(function () {
  144. return browser.findElement(by.id('repo-clone-url')).getAttribute('value');
  145. }).then(function (cloneUrl) {
  146. expect(cloneUrl).to.be('ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git');
  147. done();
  148. });
  149. }
  150. function addPublicKey(done) {
  151. browser.get('https://' + app.fqdn + '/user/settings/ssh').then(function () {
  152. var publicKey = fs.readFileSync(__dirname + '/id_rsa.pub', 'utf8');
  153. return browser.findElement(by.xpath('//div[text()="Add Key"]')).click();
  154. }).then(function () {
  155. return browser.findElement(by.id('title')).sendKeys('testkey');
  156. }).then(function () {
  157. return browser.findElement(by.id('content')).sendKeys(publicKey.trim()); // #3480
  158. }).then(function () {
  159. return browser.findElement(by.xpath('//button[contains(text(), "Add Key")]')).click();
  160. }).then(function () {
  161. return browser.wait(until.elementLocated(by.xpath('//p[contains(text(), "added successfully!")]')), TIMEOUT);
  162. }).then(function () {
  163. done();
  164. });
  165. }
  166. xit('build app', function () {
  167. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  168. });
  169. it('install app', function () {
  170. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  171. });
  172. it('can get app information', getAppInfo);
  173. it('can login', login);
  174. it('can set avatar', setAvatar);
  175. it('can get avatar', checkAvatar);
  176. it('can add public key', addPublicKey);
  177. it('can create repo', createRepo);
  178. it('displays correct clone url', checkCloneUrl);
  179. it('can clone the url', cloneRepo);
  180. it('can add and push a file', pushFile);
  181. it('can edit file', editFile);
  182. it('can restart app', function () {
  183. execSync('cloudron restart --wait --app ' + app.id);
  184. });
  185. it('can clone the url', cloneRepo);
  186. it('file exists in cloned repo', fileExists);
  187. it('backup app', function () {
  188. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  189. });
  190. it('restore app', function () {
  191. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  192. });
  193. it('can get avatar', checkAvatar);
  194. it('can clone the url', cloneRepo);
  195. it('file exists in cloned repo', fileExists);
  196. it('move to different location', function () {
  197. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  198. });
  199. it('can get app information', getAppInfo);
  200. it('can login', login);
  201. it('can get avatar', checkAvatar);
  202. it('displays correct clone url', checkCloneUrl);
  203. it('can clone the url', cloneRepo);
  204. it('file exists in cloned repo', fileExists);
  205. it('uninstall app', function () {
  206. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  207. });
  208. // check if the _first_ login via email succeeds
  209. it('can install app', function () {
  210. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  211. });
  212. it('can get app information', getAppInfo);
  213. it('can login via email', function (done) {
  214. browser.get('https://' + app.fqdn + '/user/login').then(function () {
  215. return browser.findElement(by.id('user_name')).sendKeys(email);
  216. }).then(function () {
  217. return browser.findElement(by.id('password')).sendKeys(password);
  218. }).then(function () {
  219. return browser.findElement(by.tagName('form')).submit();
  220. }).then(function () {
  221. return browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT);
  222. }).then(function () {
  223. done();
  224. });
  225. });
  226. it('uninstall app', function () {
  227. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  228. });
  229. // test update
  230. it('can install app', function () {
  231. execSync('cloudron install --new --wait --appstore-id ' + app.manifest.id + ' --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  232. });
  233. it('can get app information', getAppInfo);
  234. it('can login', login);
  235. it('can set avatar', setAvatar);
  236. it('can get avatar', checkAvatar);
  237. it('can add public key', addPublicKey);
  238. it('can create repo', createRepo);
  239. it('can clone the url', cloneRepo);
  240. it('can add and push a file', pushFile);
  241. it('can update', function () {
  242. execSync('cloudron install --wait --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  243. });
  244. it('can get avatar', checkAvatar);
  245. it('can clone the url', cloneRepo);
  246. it('file exists in cloned repo', fileExists);
  247. it('uninstall app', function () {
  248. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  249. });
  250. });