forgejo-app/test/test.js

252 lines
10 KiB
JavaScript
Raw Normal View History

2015-11-24 21:59:52 +00:00
#!/usr/bin/env node
2015-12-01 02:07:21 +00:00
/* jslint node:true */
/* global it:false */
/* global xit:false */
/* global describe:false */
/* global before:false */
/* global after:false */
2015-11-24 21:59:52 +00:00
'use strict';
var execSync = require('child_process').execSync,
ejs = require('ejs'),
expect = require('expect.js'),
fs = require('fs'),
mkdirp = require('mkdirp'),
path = require('path'),
rimraf = require('rimraf'),
2015-11-25 03:47:03 +00:00
superagent = require('superagent'),
webdriver = require('selenium-webdriver');
2015-11-24 21:59:52 +00:00
2015-11-25 08:28:22 +00:00
var by = require('selenium-webdriver').By,
2017-04-05 03:55:07 +00:00
until = require('selenium-webdriver').until,
Builder = require('selenium-webdriver').Builder;
2015-11-25 08:28:22 +00:00
2015-11-24 21:59:52 +00:00
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
describe('Application life cycle test', function () {
this.timeout(0);
2017-04-05 03:55:07 +00:00
var server, browser = new Builder().forBrowser('chrome').build();
2017-04-05 03:17:35 +00:00
var LOCATION = 'test';
2015-11-25 09:36:29 +00:00
var repodir = '/tmp/testrepo';
var app, reponame = 'testrepo';
var username = process.env.USERNAME;
var password = process.env.PASSWORD;
2016-08-18 17:14:13 +00:00
var TIMEOUT = process.env.TIMEOUT || 5000;
2016-04-13 04:01:52 +00:00
var email, token;
2015-11-24 21:59:52 +00:00
2015-11-25 08:34:37 +00:00
before(function (done) {
if (!process.env.USERNAME) return done(new Error('USERNAME env var not set'));
if (!process.env.PASSWORD) return done(new Error('PASSWORD env var not set'));
2015-11-25 03:47:03 +00:00
var seleniumJar= require('selenium-server-standalone-jar');
var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
2015-11-25 08:28:22 +00:00
server = new SeleniumServer(seleniumJar.path, { port: 4444 });
2015-11-25 03:47:03 +00:00
server.start();
2015-11-25 08:56:04 +00:00
done();
2015-11-25 03:47:03 +00:00
});
2015-11-25 08:56:04 +00:00
after(function (done) {
2015-11-25 08:28:22 +00:00
browser.quit();
server.stop();
2015-11-25 09:36:29 +00:00
rimraf.sync(repodir);
2015-11-25 08:56:04 +00:00
done();
2015-11-25 08:28:22 +00:00
});
2015-11-25 09:36:29 +00:00
xit('build app', function () {
2015-11-24 21:59:52 +00:00
execSync('cloudron build', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
});
2016-04-13 04:01:52 +00:00
it('can login', function (done) {
var inspect = JSON.parse(execSync('cloudron inspect'));
superagent.post('https://' + inspect.apiEndpoint + '/api/v1/developer/login').send({
username: username,
password: password
}).end(function (error, result) {
if (error) return done(error);
if (result.statusCode !== 200) return done(new Error('Login failed with status ' + result.statusCode));
token = result.body.token;
superagent.get('https://' + inspect.apiEndpoint + '/api/v1/profile')
.query({ access_token: token }).end(function (error, result) {
if (error) return done(error);
if (result.statusCode !== 200) return done(new Error('Get profile failed with status ' + result.statusCode));
email = result.body.email;
done();
});
});
});
2015-11-24 21:59:52 +00:00
it('install app', function () {
2015-11-29 07:50:28 +00:00
execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
2015-11-24 21:59:52 +00:00
});
it('can get app information', function () {
var inspect = JSON.parse(execSync('cloudron inspect'));
app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
expect(app).to.be.an('object');
});
it('can get the main page', function (done) {
superagent.get('https://' + app.fqdn).end(function (error, result) {
expect(error).to.be(null);
expect(result.status).to.eql(200);
done();
});
});
2015-11-25 08:28:22 +00:00
it('can login', function (done) {
browser.get('https://' + app.fqdn + '/user/login');
browser.findElement(by.id('user_name')).sendKeys(username);
browser.findElement(by.id('password')).sendKeys(password);
browser.findElement(by.tagName('form')).submit();
2016-08-18 17:14:13 +00:00
browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT).then(function () { done(); });
2015-11-25 08:28:22 +00:00
});
it('can add public key', function (done) {
browser.get('https://' + app.fqdn + '/user/settings/ssh');
var publicKey = fs.readFileSync(__dirname + '/id_rsa.pub', 'utf8');
browser.findElement(by.xpath('//div[text()="Add Key"]')).click();
browser.findElement(by.id('title')).sendKeys('testkey');
2016-08-20 08:12:11 +00:00
browser.findElement(by.id('content')).sendKeys(publicKey.trim()); // #3480
2015-11-25 08:28:22 +00:00
browser.findElement(by.xpath('//button[contains(text(), "Add Key")]')).click();
2016-08-18 17:14:13 +00:00
browser.wait(until.elementLocated(by.xpath('//p[contains(text(), "added successfully!")]')), TIMEOUT).then(function () { done(); });
2015-11-25 08:28:22 +00:00
});
it('can create repo', function (done) {
browser.get('https://' + app.fqdn);
browser.findElement(by.linkText('New Repository')).click();
2016-08-18 17:14:13 +00:00
browser.wait(until.elementLocated(by.xpath('//*[contains(text(), "New Repository")]')), TIMEOUT);
2015-11-25 08:28:22 +00:00
browser.findElement(by.id('repo_name')).sendKeys(reponame);
browser.findElement(by.id('auto-init')).click();
browser.findElement(by.xpath('//button[contains(text(), "Create Repository")]')).click();
browser.wait(function () {
return browser.getCurrentUrl().then(function (url) {
return url === 'https://' + app.fqdn + '/' + username + '/' + reponame;
});
2016-08-18 17:14:13 +00:00
}, TIMEOUT).then(function () { done(); });
2015-11-25 08:28:22 +00:00
});
it('displays correct clone url', function (done) {
browser.get('https://' + app.fqdn + '/' + username + '/' + reponame);
browser.findElement(by.id('repo-clone-ssh')).click();
2015-11-25 08:28:22 +00:00
browser.findElement(by.id('repo-clone-url')).getAttribute('value').then(function (cloneUrl) {
2016-02-22 02:52:08 +00:00
expect(cloneUrl).to.be('ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git');
2015-11-25 08:28:22 +00:00
done();
});
});
it('can clone the url', function (done) {
2015-11-25 09:36:29 +00:00
var env = Object.create(process.env);
env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
2016-02-22 02:52:08 +00:00
execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
2015-11-25 09:36:29 +00:00
done();
});
it('can add and push a file', function (done) {
var env = Object.create(process.env);
env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
2016-02-22 02:52:08 +00:00
execSync('touch newfile && git add newfile && git commit -a -mx && git push ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + ' master',
2015-11-25 09:36:29 +00:00
{ env: env, cwd: repodir });
2015-11-25 08:28:22 +00:00
rimraf.sync('/tmp/testrepo');
done();
});
2015-12-01 02:09:13 +00:00
it('can restart app', function (done) {
execSync('cloudron restart');
done();
});
it('can clone the url', function (done) {
var env = Object.create(process.env);
env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
2016-02-22 02:52:08 +00:00
execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
2015-12-01 02:09:13 +00:00
expect(fs.existsSync(repodir + '/newfile')).to.be(true);
rimraf.sync(repodir);
done();
});
2015-11-24 21:59:52 +00:00
it('backup app', function () {
execSync('cloudron backup create --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
2015-11-24 21:59:52 +00:00
});
it('restore app', function () {
execSync('cloudron restore --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
});
2015-11-25 08:28:22 +00:00
it('can clone the url', function (done) {
2015-11-25 09:36:29 +00:00
var env = Object.create(process.env);
env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
2016-02-22 02:52:08 +00:00
execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
2015-11-25 09:36:29 +00:00
expect(fs.existsSync(repodir + '/newfile')).to.be(true);
rimraf.sync(repodir);
2015-11-25 08:28:22 +00:00
done();
});
2015-12-01 02:13:47 +00:00
it('move to different location', function () {
browser.manage().deleteAllCookies();
2017-02-10 22:21:17 +00:00
execSync('cloudron configure --location ' + LOCATION + '2', { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
2015-12-01 02:13:47 +00:00
var inspect = JSON.parse(execSync('cloudron inspect'));
app = inspect.apps.filter(function (a) { return a.location === LOCATION + '2'; })[0];
expect(app).to.be.an('object');
});
it('can login', function (done) {
browser.get('https://' + app.fqdn + '/user/login');
browser.findElement(by.id('user_name')).sendKeys(username);
browser.findElement(by.id('password')).sendKeys(password);
browser.findElement(by.tagName('form')).submit();
2016-08-18 17:14:13 +00:00
browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT).then(function () { done(); });
2015-12-01 02:13:47 +00:00
});
it('displays correct clone url', function (done) {
browser.get('https://' + app.fqdn + '/' + username + '/' + reponame);
browser.findElement(by.id('repo-clone-ssh')).click();
2015-12-01 02:13:47 +00:00
browser.findElement(by.id('repo-clone-url')).getAttribute('value').then(function (cloneUrl) {
2016-02-22 02:52:08 +00:00
expect(cloneUrl).to.be('ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git');
2015-12-01 02:13:47 +00:00
done();
});
});
it('can clone the url', function (done) {
var env = Object.create(process.env);
env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
2016-02-22 02:52:08 +00:00
execSync('git clone ssh://git@' + app.fqdn + ':29418/' + username + '/' + reponame + '.git ' + repodir, { env: env });
2015-12-01 02:13:47 +00:00
expect(fs.existsSync(repodir + '/newfile')).to.be(true);
rimraf.sync(repodir);
done();
});
2015-11-24 21:59:52 +00:00
it('uninstall app', function () {
execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
});
2016-04-11 21:58:06 +00:00
// check if the _first_ login via email succeeds
2016-07-09 21:35:46 +00:00
it('can login via email', function (done) {
2016-04-11 21:58:06 +00:00
execSync('cloudron install --new --wait --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
var inspect = JSON.parse(execSync('cloudron inspect'));
app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
expect(app).to.be.an('object');
browser.get('https://' + app.fqdn + '/user/login');
browser.findElement(by.id('user_name')).sendKeys(email);
browser.findElement(by.id('password')).sendKeys(password);
browser.findElement(by.tagName('form')).submit();
2016-08-18 17:14:13 +00:00
browser.wait(until.elementLocated(by.linkText('Dashboard')), TIMEOUT).then(function () {
2016-04-13 05:00:21 +00:00
execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' });
done();
});
2016-04-11 21:58:06 +00:00
});
2015-11-24 21:59:52 +00:00
});