test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. require('chromedriver');
  10. var execSync = require('child_process').execSync,
  11. ejs = require('ejs'),
  12. expect = require('expect.js'),
  13. fs = require('fs'),
  14. mkdirp = require('mkdirp'),
  15. path = require('path'),
  16. rimraf = require('rimraf'),
  17. superagent = require('superagent'),
  18. webdriver = require('selenium-webdriver');
  19. var by = require('selenium-webdriver').By,
  20. until = require('selenium-webdriver').until,
  21. Builder = require('selenium-webdriver').Builder;
  22. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  23. describe('Application life cycle test', function () {
  24. this.timeout(0);
  25. var server, browser = new Builder().forBrowser('chrome').build();
  26. var LOCATION = 'test';
  27. var repodir = '/tmp/testrepo';
  28. var app, reponame = 'testrepo';
  29. var username = process.env.USERNAME;
  30. var password = process.env.PASSWORD;
  31. var email = process.env.EMAIL;
  32. var TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 5000;
  33. before(function (done) {
  34. if (!process.env.USERNAME) return done(new Error('USERNAME env var not set'));
  35. if (!process.env.PASSWORD) return done(new Error('PASSWORD env var not set'));
  36. var seleniumJar= require('selenium-server-standalone-jar');
  37. var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
  38. server = new SeleniumServer(seleniumJar.path, { port: 4444 });
  39. server.start();
  40. done();
  41. });
  42. after(function (done) {
  43. browser.quit();
  44. server.stop();
  45. rimraf.sync(repodir);
  46. done();
  47. });
  48. function getAppInfo() {
  49. var inspect = JSON.parse(execSync('cloudron inspect'));
  50. app = inspect.apps.filter(function (a) { return a.location === LOCATION || a.location === LOCATION + '2'; })[0];
  51. expect(app).to.be.an('object');
  52. }
  53. function login(done) {
  54. browser.manage().deleteAllCookies().then(function () {
  55. return browser.get('https://' + app.fqdn + '/user/login');
  56. }).then(function () {
  57. return browser.findElement(by.id('user_name')).sendKeys(username);
  58. }).then(function () {
  59. return browser.findElement(by.id('password')).sendKeys(password);
  60. }).then(function () {
  61. return browser.findElement(by.tagName('form')).submit();
  62. }).then(function () {
  63. return browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT);
  64. }).then(function () {
  65. done();
  66. });
  67. }
  68. function waitForUrl(url, done) {
  69. browser.wait(function () {
  70. return browser.getCurrentUrl().then(function (currentUrl) {
  71. return currentUrl === url;
  72. });
  73. }, TIMEOUT).then(function () { done(); });
  74. }
  75. function setAvatar(done) {
  76. browser.get('https://' + app.fqdn + '/user/settings/avatar').then(function () {
  77. return browser.findElement(by.xpath('//input[@type="file" and @name="avatar"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
  78. }).then(function () {
  79. return browser.findElement(by.xpath('//button[contains(text(), "Update Avatar Setting")]')).click();
  80. }).then(function () {
  81. return browser.wait(until.elementLocated(by.xpath('//p[contains(text(),"updated successfully")]')), TIMEOUT);
  82. }).then(function () {
  83. done();
  84. });
  85. }
  86. function checkAvatar(done) {
  87. superagent.get('https://' + app.fqdn + '/avatars/1').end(function (error, result) {
  88. expect(error).to.be(null);
  89. expect(result.statusCode).to.be(200);
  90. done();
  91. });
  92. }
  93. function createRepo(done) {
  94. browser.get('https://' + app.fqdn).then(function () {
  95. return browser.findElement(by.linkText('New Repository')).click();
  96. }).then(function () {
  97. return browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "New Repository")]')), TIMEOUT);
  98. }).then(function () {
  99. return browser.findElement(by.id('repo_name')).sendKeys(reponame);
  100. }).then(function () {
  101. var button = browser.findElement(by.xpath('//button[contains(text(), "Create Repository")]'));
  102. return browser.executeScript('arguments[0].scrollIntoView(true)', button);
  103. }).then(function () {
  104. return browser.findElement(by.id('auto-init')).click();
  105. }).then(function () {
  106. return browser.findElement(by.xpath('//button[contains(text(), "Create Repository")]')).click();
  107. }).then(function () {
  108. return browser.wait(function () {
  109. return browser.getCurrentUrl().then(function (url) {
  110. return url === 'https://' + app.fqdn + '/' + username + '/' + reponame;
  111. });
  112. }, TIMEOUT);
  113. }).then(function () {
  114. done();
  115. });
  116. }
  117. function cloneRepo() {
  118. rimraf.sync(repodir);
  119. var env = Object.create(process.env);
  120. env.GIT_SSH_COMMAND = __dirname + '/git_ssh_wrapper.sh';
  121. execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
  122. }
  123. function pushFile() {
  124. var env = Object.create(process.env);
  125. env.GIT_SSH_COMMAND = __dirname + '/git_ssh_wrapper.sh';
  126. execSync('touch newfile && git add newfile && git commit -a -mx && git push ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + ' master', { env: env, cwd: repodir });
  127. }
  128. function editFile(done) {
  129. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame + '/_edit/master/newfile').then(function () {
  130. var cm = browser.findElement(by.xpath('//div[contains(@class,"CodeMirror")]'));
  131. var text = 'yo';
  132. return browser.executeScript('arguments[0].CodeMirror.setValue("' + text + '");', cm);
  133. }).then(function () {
  134. return browser.findElement(by.xpath('//input[@name="commit_summary"]')).sendKeys('Dummy edit');
  135. }).then(function () {
  136. return browser.findElement(by.xpath('//button[contains(text(), "Commit Changes")]')).click();
  137. }).then(function () {
  138. waitForUrl('https://' + app.fqdn + '/' + username + '/' + reponame + '/src/master/newfile', done);
  139. });
  140. }
  141. function fileExists() {
  142. expect(fs.existsSync(repodir + '/newfile')).to.be(true);
  143. }
  144. function checkCloneUrl(done) {
  145. browser.get('https://' + app.fqdn + '/' + username + '/' + reponame).then(function () {
  146. return browser.findElement(by.id('repo-clone-ssh')).click();
  147. }).then(function () {
  148. return browser.findElement(by.id('repo-clone-url')).getAttribute('value');
  149. }).then(function (cloneUrl) {
  150. expect(cloneUrl).to.be('ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git');
  151. done();
  152. });
  153. }
  154. function addPublicKey(done) {
  155. var publicKey = fs.readFileSync(__dirname + '/id_rsa.pub', 'utf8');
  156. browser.get('https://' + app.fqdn + '/user/settings/ssh').then(function () {
  157. return browser.findElement(by.xpath('//div[text()="Add Key"]')).click();
  158. }).then(function () {
  159. return browser.findElement(by.id('title')).sendKeys('testkey');
  160. }).then(function () {
  161. return browser.findElement(by.id('content')).sendKeys(publicKey.trim()); // #3480
  162. }).then(function () {
  163. var button = browser.findElement(by.xpath('//button[contains(text(), "Add Key")]'));
  164. return browser.executeScript('arguments[0].scrollIntoView(false)', button);
  165. }).then(function () {
  166. return browser.findElement(by.xpath('//button[contains(text(), "Add Key")]')).click();
  167. }).then(function () {
  168. return browser.wait(until.elementLocated(by.xpath('//p[contains(text(), "added successfully!")]')), TIMEOUT);
  169. }).then(function () {
  170. done();
  171. });
  172. }
  173. function sendMail(done) {
  174. browser.get('https://' + app.fqdn + '/admin/config').then(function () {
  175. var button = browser.findElement(by.xpath('//button[@id="test-mail-btn"]'));
  176. return browser.executeScript('arguments[0].scrollIntoView(true)', button);
  177. }).then(function () {
  178. return browser.findElement(by.xpath('//input[@name="email"]')).sendKeys('test@cloudron.io');
  179. }).then(function () {
  180. return browser.findElement(by.xpath('//button[@id="test-mail-btn"]')).click();
  181. }).then(function () {
  182. return browser.wait(until.elementLocated(by.xpath('//p[contains(text(),"Test email has been sent to \'test@cloudron.io\'")]')), TIMEOUT);
  183. }).then(function () {
  184. done();
  185. });
  186. }
  187. xit('build app', function () {
  188. execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  189. });
  190. it('install app', function () {
  191. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  192. });
  193. it('can get app information', getAppInfo);
  194. it('can login', login);
  195. it('can send mail', sendMail);
  196. it('can set avatar', setAvatar);
  197. it('can get avatar', checkAvatar);
  198. it('can add public key', addPublicKey);
  199. it('can create repo', createRepo);
  200. it('displays correct clone url', checkCloneUrl);
  201. it('can clone the url', cloneRepo);
  202. it('can add and push a file', pushFile);
  203. it('can edit file', editFile);
  204. it('can restart app', function () {
  205. execSync('cloudron restart --wait --app ' + app.id);
  206. });
  207. it('can clone the url', cloneRepo);
  208. it('file exists in cloned repo', fileExists);
  209. it('backup app', function () {
  210. execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  211. });
  212. it('restore app', function () {
  213. execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  214. });
  215. it('can get avatar', checkAvatar);
  216. it('can clone the url', cloneRepo);
  217. it('file exists in cloned repo', fileExists);
  218. it('move to different location', function () {
  219. execSync('cloudron configure --wait --location ' + LOCATION + '2 --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  220. });
  221. it('can get app information', getAppInfo);
  222. it('can login', login);
  223. it('can get avatar', checkAvatar);
  224. it('displays correct clone url', checkCloneUrl);
  225. it('can clone the url', cloneRepo);
  226. it('file exists in cloned repo', fileExists);
  227. it('uninstall app', function () {
  228. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  229. });
  230. // check if the _first_ login via email succeeds
  231. it('can install app', function () {
  232. execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  233. });
  234. it('can get app information', getAppInfo);
  235. it('can login via email', function (done) {
  236. browser.get('https://' + app.fqdn + '/user/login').then(function () {
  237. return browser.findElement(by.id('user_name')).sendKeys(email);
  238. }).then(function () {
  239. return browser.findElement(by.id('password')).sendKeys(password);
  240. }).then(function () {
  241. return browser.findElement(by.tagName('form')).submit();
  242. }).then(function () {
  243. return browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT);
  244. }).then(function () {
  245. done();
  246. });
  247. });
  248. it('uninstall app', function () {
  249. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  250. });
  251. // test update
  252. it('can install app', function () {
  253. execSync('cloudron install --new --wait --appstore-id ' + app.manifest.id + ' --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  254. });
  255. it('can get app information', getAppInfo);
  256. it('can login', login);
  257. it('can set avatar', setAvatar);
  258. it('can get avatar', checkAvatar);
  259. it('can add public key', addPublicKey);
  260. it('can create repo', createRepo);
  261. it('can clone the url', cloneRepo);
  262. it('can add and push a file', pushFile);
  263. it('can update', function () {
  264. execSync('cloudron install --wait --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  265. });
  266. it('can login', login);
  267. it('can send mail', sendMail);
  268. it('can get avatar', checkAvatar);
  269. it('can clone the url', cloneRepo);
  270. it('file exists in cloned repo', fileExists);
  271. it('uninstall app', function () {
  272. execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
  273. });
  274. });