gitea-app/test/test.js

436 lines
17 KiB
JavaScript
Executable File

#!/usr/bin/env node
/* jslint node:true */
/* global it:false */
/* global xit:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
require('chromedriver');
var execSync = require('child_process').execSync,
expect = require('expect.js'),
fs = require('fs'),
path = require('path'),
rimraf = require('rimraf'),
superagent = require('superagent'),
{ Builder, By, until } = require('selenium-webdriver'),
{ Options } = require('selenium-webdriver/chrome');
if (!process.env.USERNAME || !process.env.PASSWORD || !process.env.EMAIL) {
console.log('USERNAME, PASSWORD and EMAIL env vars need to be set');
process.exit(1);
}
describe('Application life cycle test', function () {
this.timeout(0);
var TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 5000;
var EXEC_ARGS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' };
var LOCATION = 'test';
var SSH_PORT = 29420;
var app;
var browser;
var repodir = '/tmp/testrepo';
var reponame = 'testrepo';
var username = process.env.USERNAME;
var password = process.env.PASSWORD;
var email = process.env.EMAIL;
before(function () {
browser = new Builder().forBrowser('chrome').setChromeOptions(new Options().windowSize({ width: 1280, height: 1024 })).build();
});
after(function () {
browser.quit();
rimraf.sync(repodir);
});
function getAppInfo() {
var inspect = JSON.parse(execSync('cloudron inspect'));
app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
expect(app).to.be.an('object');
}
function setAvatarOld(done) {
browser.get('https://' + app.fqdn + '/user/settings/avatar').then(function () {
return browser.findElement(By.xpath('//input[@type="file" and @name="avatar"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
}).then(function () {
return browser.findElement(By.xpath('//button[contains(text(), "Update Avatar Setting")]')).click();
}).then(function () {
if (app.manifest.version === '1.0.3') {
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(),"updated successfully")]')), TIMEOUT);
} else {
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(),"Your avatar setting has been updated.")]')), TIMEOUT);
}
}).then(function () {
done();
});
}
function setAvatar(done) {
if (app.manifest.version === '1.5.4') return setAvatarOld(done);
browser.get('https://' + app.fqdn + '/user/settings').then(function () {
var button = browser.findElement(By.xpath('//label[contains(text(), "Use Custom Avatar")]'));
return browser.executeScript('arguments[0].scrollIntoView(false)', button);
}).then(function () {
return browser.findElement(By.xpath('//label[contains(text(), "Use Custom Avatar")]')).click();
}).then(function () {
return browser.findElement(By.xpath('//input[@type="file" and @name="avatar"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
}).then(function () {
return browser.findElement(By.xpath('//button[contains(text(), "Update Avatar")]')).click();
}).then(function () {
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(),"Your avatar has been updated.")]')), TIMEOUT);
}).then(function () {
done();
});
}
function checkAvatar(done) {
return done();
superagent.get('https://' + app.fqdn + '/avatars/a3e6f3316fc1738e29d621e6a26e93d3').end(function (error, result) {
expect(error).to.be(null);
expect(result.statusCode).to.be(200);
done();
});
}
function login(username, password, done) {
browser.get('https://' + app.fqdn + '/user/login').then(function () {
return browser.findElement(By.id('user_name')).sendKeys(username);
}).then(function () {
return browser.findElement(By.id('password')).sendKeys(password);
}).then(function () {
return browser.findElement(By.tagName('form')).submit();
}).then(function () {
return browser.wait(until.elementLocated(By.linkText('Dashboard')), TIMEOUT);
}).then(function () {
done();
});
}
function adminLogin(done) {
login('root', 'changeme', done);
}
function logout(done) {
browser.get('https://' + app.fqdn).then(function () {
return browser.findElement(By.xpath('//img[contains(@class, "avatar")]')).click();
}).then(function () {
browser.sleep(2000);
}).then(function () {
return browser.findElement(By.xpath('//a[@data-url="/user/logout"]')).click();
}).then(function () {
browser.sleep(2000);
}).then(function () {
done();
});
}
function addPublicKey(done) {
var publicKey = fs.readFileSync(__dirname + '/id_rsa.pub', 'utf8');
const sshPage = 'https://' + app.fqdn + '/user/settings/keys';
browser.get(sshPage).then(function () {
return browser.findElement(By.xpath('//div[text()="Add Key"]')).click();
}).then(function () {
return browser.findElement(By.id('ssh-key-title')).sendKeys('testkey');
}).then(function () {
return browser.findElement(By.id('ssh-key-content')).sendKeys(publicKey.trim()); // #3480
}).then(function () {
var button = browser.findElement(By.xpath('//button[contains(text(), "Add Key")]'));
return browser.executeScript('arguments[0].scrollIntoView(false)', button);
}).then(function () {
return browser.findElement(By.xpath('//button[contains(text(), "Add Key")]')).click();
}).then(function () {
if (app.manifest.version === '1.0.3') {
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(), "added successfully!")]')), TIMEOUT);
} else {
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(), "has been added.")]')), TIMEOUT);
}
}).then(function () {
done();
});
}
function createRepo(done) {
var getRepoPage;
if (app.manifest.version === '1.0.3') {
getRepoPage = browser.get('https://' + app.fqdn).then(function () {
return browser.findElement(By.linkText('New Repository')).click();
}).then(function () {
return browser.wait(until.elementLocated(By.xpath('//*[contains(text(), "New Repository")]')), TIMEOUT);
});
} else {
getRepoPage = browser.get('https://' + app.fqdn + '/repo/create');
}
getRepoPage.then(function () {
return browser.findElement(By.id('repo_name')).sendKeys(reponame);
}).then(function () {
var button = browser.findElement(By.xpath('//button[contains(text(), "Create Repository")]'));
return browser.executeScript('arguments[0].scrollIntoView(true)', button);
}).then(function () {
return browser.findElement(By.id('auto-init')).click();
}).then(function () {
return browser.findElement(By.xpath('//button[contains(text(), "Create Repository")]')).click();
}).then(function () {
browser.wait(function () {
return browser.getCurrentUrl().then(function (url) {
return url === 'https://' + app.fqdn + '/' + username + '/' + reponame;
});
}, TIMEOUT);
}).then(function () {
done();
});
}
function checkCloneUrl(done) {
browser.get('https://' + app.fqdn + '/' + username + '/' + reponame).then(function () {
return browser.findElement(By.id('repo-clone-ssh')).click();
}).then(function () {
return browser.findElement(By.id('repo-clone-url')).getAttribute('value');
}).then(function (cloneUrl) {
expect(cloneUrl).to.be(`ssh://git@${app.fqdn}:${SSH_PORT}/${username}/${reponame}.git`);
done();
});
}
function cloneRepo(done) {
rimraf.sync(repodir);
var env = Object.create(process.env);
env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
execSync(`git clone ssh://git@${app.fqdn}:${SSH_PORT}/${username}/${reponame}.git ${repodir}`, { env: env });
done();
}
function pushFile(done) {
var env = Object.create(process.env);
env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
execSync(`touch newfile && git add newfile && git commit -a -mx && git push ssh://git@${app.fqdn}:${SSH_PORT}/${username}/${reponame} master`,
{ env: env, cwd: repodir });
rimraf.sync('/tmp/testrepo');
done();
}
function addCustomFile(done) {
fs.writeFileSync('/tmp/customfile.txt', 'GOGS TEST', 'utf8');
execSync(`cloudron exec --app ${app.id} -- mkdir -p /app/data/custom/public`);
execSync(`cloudron push --app ${app.id} /tmp/customfile.txt /app/data/custom/public/customfile.txt`);
fs.unlinkSync('/tmp/customfile.txt');
done();
}
function checkCustomFile(done) {
superagent.get('https://' + app.fqdn + '/customfile.txt').end(function (error, result) {
if (error) return done(error);
expect(result.text).to.contain('GOGS TEST');
done();
});
}
function fileExists() {
expect(fs.existsSync(repodir + '/newfile')).to.be(true);
}
function sendMail(done) {
browser.get(`https://${app.fqdn}/admin/config`).then(function () {
var button = browser.findElement(By.xpath('//button[@id="test-mail-btn"]'));
return browser.executeScript('arguments[0].scrollIntoView(true)', button);
}).then(function () {
return browser.findElement(By.xpath('//input[@name="email"]')).sendKeys('test@cloudron.io');
}).then(function () {
return browser.findElement(By.xpath('//button[@id="test-mail-btn"]')).click();
}).then(function () {
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(),"A testing email has been sent to \'test@cloudron.io\'")]')), TIMEOUT);
}).then(function () {
done();
});
}
xit('build app', function () { execSync('cloudron build', EXEC_ARGS); });
// 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.accessToken;
// 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();
// });
// });
// });
it('install app', function () { execSync(`cloudron install --location ${LOCATION} -p SSH_PORT=${SSH_PORT}`, EXEC_ARGS); });
it('can get app information', getAppInfo);
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();
});
});
it('can admin login', adminLogin);
it('can send mail', sendMail);
it('can logout', logout);
it('can login', login.bind(null, username, password));
it('can set avatar', setAvatar);
it('can get avatar', checkAvatar);
it('can add public key', addPublicKey);
it('can create repo', createRepo);
it('displays correct clone url', checkCloneUrl);
it('can clone the url', cloneRepo);
it('can add and push a file', pushFile);
it('can add custom file', addCustomFile);
it('can check custom file', checkCustomFile);
it('can restart app', function () { execSync('cloudron restart --app ' + app.id); });
xit('can login', login.bind(null, username, password)); // no need to relogin since session persists
it('displays correct clone url', checkCloneUrl);
it('can clone the url', cloneRepo);
it('file exists in repo', fileExists);
it('backup app', function () { execSync('cloudron backup create --app ' + app.id, EXEC_ARGS); });
it('restore app', function () { execSync('cloudron restore --app ' + app.id, EXEC_ARGS); });
it('can login', login.bind(null, username, password));
it('can get avatar', checkAvatar);
it('can clone the url', cloneRepo);
it('file exists in repo', function () { expect(fs.existsSync(repodir + '/newfile')).to.be(true); });
it('move to different location', function (done) {
//browser.manage().deleteAllCookies(); // commented because of error "'Network.deleteCookie' wasn't found"
// ensure we don't hit NXDOMAIN in the mean time
browser.get('about:blank').then(function () {
execSync('cloudron configure --location ' + LOCATION + '2 --app ' + app.id, EXEC_ARGS);
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');
done();
});
});
it('can login', login.bind(null, username, password));
it('can get avatar', checkAvatar);
it('displays correct clone url', checkCloneUrl);
it('can clone the url', cloneRepo);
it('file exists in repo', function () { expect(fs.existsSync(repodir + '/newfile')).to.be(true); });
it('uninstall app', function (done) {
// ensure we don't hit NXDOMAIN in the mean time
browser.get('about:blank').then(function () {
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
done();
});
});
// check if the _first_ login via email succeeds
it('can login via email', function (done) {
execSync(`cloudron install --location ${LOCATION} -p SSH_PORT=${SSH_PORT}`, EXEC_ARGS);
var inspect = JSON.parse(execSync('cloudron inspect'));
app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
expect(app).to.be.an('object');
login(email, password, function (error) {
if (error) return done(error);
// ensure we don't hit NXDOMAIN in the mean time
browser.get('about:blank').then(function () {
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
done();
});
});
});
// No SSO
it('install app (no sso)', function () {
execSync(`cloudron install --no-sso --location ${LOCATION} -p SSH_PORT=${SSH_PORT}`, EXEC_ARGS);
});
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 admin login (no sso)', adminLogin);
it('can logout', logout);
it('uninstall app (no sso)', function () {
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
});
// test update
it('can install app', function () {
execSync(`cloudron install --appstore-id ${app.manifest.id} --location ${LOCATION} -p SSH_PORT=${SSH_PORT}`, EXEC_ARGS);
});
it('can get app information', getAppInfo);
it('can login', login.bind(null, username, password));
it('can set avatar', setAvatar);
it('can get avatar', checkAvatar);
it('can add public key', addPublicKey);
it('can create repo', createRepo);
it('can clone the url', cloneRepo);
it('can add and push a file', pushFile);
it('can update', function () {
execSync('cloudron update --app ' + app.id, EXEC_ARGS);
});
xit('can admin login', adminLogin);
xit('can send mail', sendMail);
xit('can logout', logout);
it('can login', login.bind(null, username, password));
it('can get avatar', checkAvatar);
it('can clone the url', cloneRepo);
it('file exists in cloned repo', fileExists);
it('uninstall app', function (done) {
// ensure we don't hit NXDOMAIN in the mean time
browser.get('about:blank').then(function () {
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
done();
});
});
});