test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. var button = browser.findElement(by.xpath('//button[contains(text(), "Create Repository")]'));
  101. return browser.executeScript('arguments[0].scrollIntoView(true)', button);
  102. }).then(function () {
  103. return browser.findElement(by.id('auto-init')).click();
  104. }).then(function () {
  105. return browser.findElement(by.xpath('//button[contains(text(), "Create Repository")]')).click();
  106. }).then(function () {
  107. return browser.wait(function () {
  108. return browser.getCurrentUrl().then(function (url) {
  109. return url === 'https://' + app.fqdn + '/' + username + '/' + reponame;
  110. });
  111. }, TIMEOUT);
  112. }).then(function () {
  113. done();
  114. });
  115. }
  116. function cloneRepo() {
  117. rimraf.sync(repodir);
  118. var env = Object.create(process.env);
  119. env.GIT_SSH_COMMAND = __dirname + '/git_ssh_wrapper.sh';
  120. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  121. }
  122. function pushFile() {
  123. var env = Object.create(process.env);
  124. env.GIT_SSH_COMMAND = __dirname + '/git_ssh_wrapper.sh';
  125. execSync('touch newfile && git add newfile && git commit -a -mx && git push ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + ' master', { env: env, cwd: repodir });
  126. }
  127. function editFile(done) {
  128. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame + '/_edit/master/newfile').then(function () {
  129. var cm = browser.findElement(by.xpath('//div[contains(@class,"CodeMirror")]'));
  130. var text = 'yo';
  131. return browser.executeScript('arguments[0].CodeMirror.setValue("' + text + '");', cm);
  132. }).then(function () {
  133. return browser.findElement(by.xpath('//input[@name="commit_summary"]')).sendKeys('Dummy edit');
  134. }).then(function () {
  135. return browser.findElement(by.xpath('//button[contains(text(), "Commit Changes")]')).click();
  136. }).then(function () {
  137. waitForUrl('https://' + app.fqdn + '/' + username + '/' + reponame + '/src/master/newfile', done);
  138. });
  139. }
  140. function fileExists() {
  141. expect(fs.existsSync(repodir + '/newfile')).to.be(true);
  142. }
  143. function checkCloneUrl(done) {
  144. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame).then(function () {
  145. return browser.findElement(by.id('repo-clone-ssh')).click();
  146. }).then(function () {
  147. return browser.findElement(by.id('repo-clone-url')).getAttribute('value');
  148. }).then(function (cloneUrl) {
  149. expect(cloneUrl).to.be('ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git');
  150. done();
  151. });
  152. }
  153. function addPublicKey(done) {
  154. var publicKey = fs.readFileSync(__dirname + '/id_rsa.pub', 'utf8');
  155. browser.get('https://' + app.fqdn + '/user/settings/ssh').then(function () {
  156. return browser.findElement(by.xpath('//div[text()="Add Key"]')).click();
  157. }).then(function () {
  158. return browser.findElement(by.id('title')).sendKeys('testkey');
  159. }).then(function () {
  160. return browser.findElement(by.id('content')).sendKeys(publicKey.trim()); // #3480
  161. }).then(function () {
  162. var button = browser.findElement(by.xpath('//button[contains(text(), "Add Key")]'));
  163. return browser.executeScript('arguments[0].scrollIntoView(false)', button);
  164. }).then(function () {
  165. return browser.findElement(by.xpath('//button[contains(text(), "Add Key")]')).click();
  166. }).then(function () {
  167. return browser.wait(until.elementLocated(by.xpath('//p[contains(text(), "added successfully!")]')), TIMEOUT);
  168. }).then(function () {
  169. done();
  170. });
  171. }
  172. function sendMail(done) {
  173. browser.get('https://' + app.fqdn + '/admin/config').then(function () {
  174. var button = browser.findElement(by.xpath('//button[@id="test-mail-btn"]'));
  175. return browser.executeScript('arguments[0].scrollIntoView(true)', button);
  176. }).then(function () {
  177. return browser.findElement(by.xpath('//input[@name="email"]')).sendKeys('test@cloudron.io');
  178. }).then(function () {
  179. return browser.findElement(by.xpath('//button[@id="test-mail-btn"]')).click();
  180. }).then(function () {
  181. return browser.wait(until.elementLocated(by.xpath('//p[contains(text(),"Test email has been sent to \'test@cloudron.io\'")]')), TIMEOUT);
  182. }).then(function () {
  183. done();
  184. });
  185. }
  186. xit('build app', function () {
  187. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  188. });
  189. it('install app', function () {
  190. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  191. });
  192. it('can get app information', getAppInfo);
  193. it('can login', login);
  194. it('can send mail', sendMail);
  195. it('can set avatar', setAvatar);
  196. it('can get avatar', checkAvatar);
  197. it('can add public key', addPublicKey);
  198. it('can create repo', createRepo);
  199. it('displays correct clone url', checkCloneUrl);
  200. it('can clone the url', cloneRepo);
  201. it('can add and push a file', pushFile);
  202. it('can edit file', editFile);
  203. it('can restart app', function () {
  204. execSync('cloudron restart --wait --app ' + app.id);
  205. });
  206. it('can clone the url', cloneRepo);
  207. it('file exists in cloned repo', fileExists);
  208. it('backup app', function () {
  209. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  210. });
  211. it('restore app', function () {
  212. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  213. });
  214. it('can get avatar', checkAvatar);
  215. it('can clone the url', cloneRepo);
  216. it('file exists in cloned repo', fileExists);
  217. it('move to different location', function () {
  218. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  219. });
  220. it('can get app information', getAppInfo);
  221. it('can login', login);
  222. it('can get avatar', checkAvatar);
  223. it('displays correct clone url', checkCloneUrl);
  224. it('can clone the url', cloneRepo);
  225. it('file exists in cloned repo', fileExists);
  226. it('uninstall app', function () {
  227. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  228. });
  229. // check if the _first_ login via email succeeds
  230. it('can install app', function () {
  231. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  232. });
  233. it('can get app information', getAppInfo);
  234. it('can login via email', function (done) {
  235. browser.get('https://' + app.fqdn + '/user/login').then(function () {
  236. return browser.findElement(by.id('user_name')).sendKeys(email);
  237. }).then(function () {
  238. return browser.findElement(by.id('password')).sendKeys(password);
  239. }).then(function () {
  240. return browser.findElement(by.tagName('form')).submit();
  241. }).then(function () {
  242. return browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT);
  243. }).then(function () {
  244. done();
  245. });
  246. });
  247. it('uninstall app', function () {
  248. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  249. });
  250. // test update
  251. it('can install app', function () {
  252. execSync('cloudron install --new --wait --appstore-id ' + app.manifest.id + ' --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  253. });
  254. it('can get app information', getAppInfo);
  255. it('can login', login);
  256. it('can set avatar', setAvatar);
  257. it('can get avatar', checkAvatar);
  258. it('can add public key', addPublicKey);
  259. it('can create repo', createRepo);
  260. it('can clone the url', cloneRepo);
  261. it('can add and push a file', pushFile);
  262. it('can update', function () {
  263. execSync('cloudron install --wait --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  264. });
  265. it('can login', login);
  266. it('can send mail', sendMail);
  267. it('can get avatar', checkAvatar);
  268. it('can clone the url', cloneRepo);
  269. it('file exists in cloned repo', fileExists);
  270. it('uninstall app', function () {
  271. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  272. });
  273. });