gitea-app/test/test.js

449 lines
17 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';
2020-03-09 01:59:04 +00:00
require('chromedriver');
2015-11-24 21:59:52 +00:00
var execSync = require('child_process').execSync,
expect = require('expect.js'),
fs = require('fs'),
path = require('path'),
rimraf = require('rimraf'),
2019-11-14 15:45:34 +00:00
superagent = require('superagent'),
{ Builder, By, until } = require('selenium-webdriver'),
{ Options } = require('selenium-webdriver/chrome');
2015-11-25 08:28:22 +00:00
2019-11-14 15:45:34 +00:00
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);
}
2015-11-24 21:59:52 +00:00
describe('Application life cycle test', function () {
this.timeout(0);
2019-11-14 15:45:34 +00:00
var TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 5000;
var EXEC_ARGS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' };
2017-04-05 03:17:35 +00:00
var LOCATION = 'test';
var SSH_PORT = 29420;
2019-11-14 15:45:34 +00:00
var app;
var token;
2020-03-09 01:59:04 +00:00
var server, browser = new Builder().forBrowser('chrome').build();
2015-11-25 09:36:29 +00:00
var repodir = '/tmp/testrepo';
2019-11-14 15:45:34 +00:00
var reponame = 'testrepo';
2015-11-25 09:36:29 +00:00
var username = process.env.USERNAME;
var password = process.env.PASSWORD;
2019-11-14 15:45:34 +00:00
var email = process.env.EMAIL;
2015-11-25 08:34:37 +00:00
2019-11-14 15:45:34 +00:00
before(function () {
2020-03-09 01:59:04 +00:00
var seleniumJar= require('selenium-server-standalone-jar');
var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer;
server = new SeleniumServer(seleniumJar.path, { port: 4444 });
server.start();
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();
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
});
2019-06-05 00:23:35 +00:00
function waitForUrl(url) {
return browser.wait(function () {
return browser.getCurrentUrl().then(function (currentUrl) {
return currentUrl === url;
});
2019-06-05 00:23:35 +00:00
}, TIMEOUT);
}
2017-08-07 18:16:29 +00:00
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');
}
2018-08-12 17:36:17 +00:00
function setAvatarOld(done) {
2017-06-02 21:25:09 +00:00
browser.get('https://' + app.fqdn + '/user/settings/avatar').then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.xpath('//input[@type="file" and @name="avatar"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
2017-06-02 21:25:09 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.xpath('//button[contains(text(), "Update Avatar Setting")]')).click();
2017-06-02 21:25:09 +00:00
}).then(function () {
2017-10-14 18:37:34 +00:00
if (app.manifest.version === '1.0.3') {
2019-11-14 15:45:34 +00:00
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(),"updated successfully")]')), TIMEOUT);
2017-10-14 18:37:34 +00:00
} else {
2019-11-14 15:45:34 +00:00
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(),"Your avatar setting has been updated.")]')), TIMEOUT);
2017-10-14 18:37:34 +00:00
}
}).then(function () {
done();
});
}
2018-08-12 17:36:17 +00:00
function setAvatar(done) {
if (app.manifest.version === '1.5.4') return setAvatarOld(done);
browser.get('https://' + app.fqdn + '/user/settings').then(function () {
2019-11-14 15:45:34 +00:00
var button = browser.findElement(By.xpath('//label[contains(text(), "Use Custom Avatar")]'));
2018-08-12 17:36:17 +00:00
return browser.executeScript('arguments[0].scrollIntoView(false)', button);
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.xpath('//label[contains(text(), "Use Custom Avatar")]')).click();
2018-08-12 17:36:17 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.xpath('//input[@type="file" and @name="avatar"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
2018-08-12 17:36:17 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.xpath('//button[contains(text(), "Update Avatar")]')).click();
2018-08-12 17:36:17 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(),"Your avatar has been updated.")]')), TIMEOUT);
2018-08-12 17:36:17 +00:00
}).then(function () {
done();
});
}
function checkAvatar(done) {
2017-10-17 17:46:38 +00:00
return done();
2017-06-02 09:20:27 +00:00
superagent.get('https://' + app.fqdn + '/avatars/a3e6f3316fc1738e29d621e6a26e93d3').end(function (error, result) {
expect(error).to.be(null);
expect(result.statusCode).to.be(200);
done();
});
}
2019-06-05 00:23:35 +00:00
function login(username, password, done) {
2017-06-02 21:25:09 +00:00
browser.get('https://' + app.fqdn + '/user/login').then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.id('user_name')).sendKeys(username);
2017-06-02 21:25:09 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.id('password')).sendKeys(password);
2017-06-02 21:25:09 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.tagName('form')).submit();
2017-06-02 21:25:09 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.wait(until.elementLocated(By.linkText('Dashboard')), TIMEOUT);
2017-06-02 21:25:09 +00:00
}).then(function () {
done();
});
}
2019-06-05 00:23:35 +00:00
function adminLogin(done) {
login('root', 'changeme', done);
}
function logout(done) {
2020-03-09 01:59:04 +00:00
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);
2019-06-05 00:23:35 +00:00
}).then(function () {
done();
});
}
2017-06-02 21:25:09 +00:00
function addPublicKey(done) {
var publicKey = fs.readFileSync(__dirname + '/id_rsa.pub', 'utf8');
2018-08-12 17:36:17 +00:00
const sshPage = 'https://' + app.fqdn + '/user/settings/keys';
2017-10-14 18:37:34 +00:00
browser.get(sshPage).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.xpath('//div[text()="Add Key"]')).click();
2017-06-02 21:25:09 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.id('ssh-key-title')).sendKeys('testkey');
2017-06-02 21:25:09 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.id('ssh-key-content')).sendKeys(publicKey.trim()); // #3480
2017-10-14 18:37:34 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
var button = browser.findElement(By.xpath('//button[contains(text(), "Add Key")]'));
2017-10-14 18:37:34 +00:00
return browser.executeScript('arguments[0].scrollIntoView(false)', button);
2017-06-02 21:25:09 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.xpath('//button[contains(text(), "Add Key")]')).click();
2017-06-02 21:25:09 +00:00
}).then(function () {
2017-10-14 18:37:34 +00:00
if (app.manifest.version === '1.0.3') {
2019-11-14 15:45:34 +00:00
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(), "added successfully!")]')), TIMEOUT);
2017-10-14 18:37:34 +00:00
} else {
2019-11-14 15:45:34 +00:00
return browser.wait(until.elementLocated(By.xpath('//p[contains(text(), "has been added.")]')), TIMEOUT);
2017-10-14 18:37:34 +00:00
}
2017-06-02 21:25:09 +00:00
}).then(function () {
done();
});
}
function createRepo(done) {
2017-10-14 18:37:34 +00:00
var getRepoPage;
if (app.manifest.version === '1.0.3') {
getRepoPage = browser.get('https://' + app.fqdn).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.linkText('New Repository')).click();
2017-10-14 18:37:34 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.wait(until.elementLocated(By.xpath('//*[contains(text(), "New Repository")]')), TIMEOUT);
2017-10-14 18:37:34 +00:00
});
} else {
getRepoPage = browser.get('https://' + app.fqdn + '/repo/create');
}
getRepoPage.then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.id('repo_name')).sendKeys(reponame);
2017-10-14 18:37:34 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
var button = browser.findElement(By.xpath('//button[contains(text(), "Create Repository")]'));
2017-10-14 18:37:34 +00:00
return browser.executeScript('arguments[0].scrollIntoView(true)', button);
2017-06-02 21:25:09 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.id('auto-init')).click();
2017-06-02 21:25:09 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.xpath('//button[contains(text(), "Create Repository")]')).click();
2017-06-02 21:25:09 +00:00
}).then(function () {
browser.wait(function () {
return browser.getCurrentUrl().then(function (url) {
return url === 'https://' + app.fqdn + '/' + username + '/' + reponame;
});
}, TIMEOUT);
}).then(function () {
done();
});
}
2017-06-02 21:25:09 +00:00
function checkCloneUrl(done) {
browser.get('https://' + app.fqdn + '/' + username + '/' + reponame).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.id('repo-clone-ssh')).click();
2017-06-02 21:25:09 +00:00
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.id('repo-clone-url')).getAttribute('value');
2017-06-02 21:25:09 +00:00
}).then(function (cloneUrl) {
expect(cloneUrl).to.be(`ssh://git@${app.fqdn}:${SSH_PORT}/${username}/${reponame}.git`);
2017-06-02 21:25:09 +00:00
done();
});
}
2017-08-07 18:16:29 +00:00
function cloneRepo(done) {
2017-06-02 21:25:09 +00:00
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 });
2017-06-02 21:25:09 +00:00
done();
}
2017-08-07 18:16:29 +00:00
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`,
2017-08-07 18:16:29 +00:00
{ env: env, cwd: repodir });
rimraf.sync('/tmp/testrepo');
done();
}
2018-08-02 17:15:05 +00:00
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`);
2018-08-02 17:15:05 +00:00
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();
});
}
2017-08-07 18:16:29 +00:00
function fileExists() {
expect(fs.existsSync(repodir + '/newfile')).to.be(true);
}
function sendMail(done) {
browser.get(`https://${app.fqdn}/admin/config`).then(function () {
2019-11-14 15:45:34 +00:00
var button = browser.findElement(By.xpath('//button[@id="test-mail-btn"]'));
return browser.executeScript('arguments[0].scrollIntoView(true)', button);
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.xpath('//input[@name="email"]')).sendKeys('test@cloudron.io');
}).then(function () {
2019-11-14 15:45:34 +00:00
return browser.findElement(By.xpath('//button[@id="test-mail-btn"]')).click();
}).then(function () {
2019-11-14 15:45:34 +00:00
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); });
2015-11-24 21:59:52 +00:00
// it('can login', function (done) {
// var inspect = JSON.parse(execSync('cloudron inspect'));
2016-04-13 04:01:52 +00:00
// 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));
2016-04-13 04:01:52 +00:00
// token = result.body.accessToken;
2016-04-13 04:01:52 +00:00
// 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));
2016-04-13 04:01:52 +00:00
// email = result.body.email;
// done();
// });
// });
// });
2016-04-13 04:01:52 +00:00
it('install app', function () { execSync(`cloudron install --location ${LOCATION} -p SSH_PORT=${SSH_PORT}`, EXEC_ARGS); });
2015-11-24 21:59:52 +00:00
2017-08-07 18:16:29 +00:00
it('can get app information', getAppInfo);
2015-11-24 21:59:52 +00:00
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();
});
});
2019-06-05 00:23:35 +00:00
it('can admin login', adminLogin);
it('can send mail', sendMail);
2019-06-05 00:23:35 +00:00
it('can logout', logout);
it('can login', login.bind(null, username, password));
it('can set avatar', setAvatar);
it('can get avatar', checkAvatar);
2017-06-02 21:25:09 +00:00
it('can add public key', addPublicKey);
2015-11-25 08:28:22 +00:00
2017-06-02 21:25:09 +00:00
it('can create repo', createRepo);
2015-11-25 08:28:22 +00:00
2017-06-02 21:25:09 +00:00
it('displays correct clone url', checkCloneUrl);
2015-11-25 08:28:22 +00:00
2017-08-07 18:16:29 +00:00
it('can clone the url', cloneRepo);
2015-11-25 08:28:22 +00:00
2017-08-07 18:16:29 +00:00
it('can add and push a file', pushFile);
2018-08-02 17:15:05 +00:00
it('can add custom file', addCustomFile);
it('can check custom file', checkCustomFile);
2019-12-06 12:07:42 +00:00
it('can restart app', function () { execSync('cloudron restart --app ' + app.id); });
2015-12-01 02:09:13 +00:00
xit('can login', login.bind(null, username, password)); // no need to relogin since session persists
2017-10-14 18:37:34 +00:00
it('displays correct clone url', checkCloneUrl);
2017-08-07 18:16:29 +00:00
it('can clone the url', cloneRepo);
it('file exists in repo', fileExists);
2015-12-01 02:09:13 +00:00
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); });
2015-11-24 21:59:52 +00:00
2019-06-05 00:23:35 +00:00
it('can login', login.bind(null, username, password));
it('can get avatar', checkAvatar);
2017-08-07 18:16:29 +00:00
it('can clone the url', cloneRepo);
2017-06-02 21:25:09 +00:00
it('file exists in repo', function () { expect(fs.existsSync(repodir + '/newfile')).to.be(true); });
2015-11-25 08:28:22 +00:00
it('move to different location', function (done) {
2017-11-29 20:21:25 +00:00
//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 () {
2019-12-06 12:07:42 +00:00
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();
});
2015-12-01 02:13:47 +00:00
});
2019-06-05 00:23:35 +00:00
it('can login', login.bind(null, username, password));
it('can get avatar', checkAvatar);
2017-06-02 21:25:09 +00:00
it('displays correct clone url', checkCloneUrl);
2017-08-07 18:16:29 +00:00
it('can clone the url', cloneRepo);
2017-06-02 21:25:09 +00:00
it('file exists in repo', function () { expect(fs.existsSync(repodir + '/newfile')).to.be(true); });
2015-12-01 02:13:47 +00:00
it('uninstall app', function (done) {
// ensure we don't hit NXDOMAIN in the mean time
browser.get('about:blank').then(function () {
2019-11-14 15:45:34 +00:00
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
done();
});
2015-11-24 21:59:52 +00:00
});
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) {
2019-12-06 12:07:42 +00:00
execSync(`cloudron install --location ${LOCATION} -p SSH_PORT=${SSH_PORT}`, EXEC_ARGS);
2016-04-11 21:58:06 +00:00
var inspect = JSON.parse(execSync('cloudron inspect'));
app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0];
expect(app).to.be.an('object');
2019-06-05 00:23:35 +00:00
login(email, password, function (error) {
2017-06-02 21:25:09 +00:00
if (error) return done(error);
// ensure we don't hit NXDOMAIN in the mean time
browser.get('about:blank').then(function () {
2019-11-14 15:45:34 +00:00
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
done();
});
2016-04-13 05:00:21 +00:00
});
2016-04-11 21:58:06 +00:00
});
2017-08-07 18:16:29 +00:00
2019-06-05 00:23:35 +00:00
// No SSO
it('install app (no sso)', function () {
2019-12-06 12:07:42 +00:00
execSync(`cloudron install --no-sso --location ${LOCATION} -p SSH_PORT=${SSH_PORT}`, EXEC_ARGS);
2019-06-05 00:23:35 +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 admin login (no sso)', adminLogin);
it('can logout', logout);
it('uninstall app (no sso)', function () {
2019-11-14 15:45:34 +00:00
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
2019-06-05 00:23:35 +00:00
});
2017-08-07 18:16:29 +00:00
// test update
it('can install app', function () {
2019-12-06 12:07:42 +00:00
execSync(`cloudron install --appstore-id ${app.manifest.id} --location ${LOCATION} -p SSH_PORT=${SSH_PORT}`, EXEC_ARGS);
2017-08-07 18:16:29 +00:00
});
it('can get app information', getAppInfo);
2019-06-05 00:23:35 +00:00
it('can login', login.bind(null, username, password));
2017-08-07 18:16:29 +00:00
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 () {
2019-12-06 12:07:42 +00:00
execSync('cloudron update --app ' + app.id, EXEC_ARGS);
2017-08-07 18:16:29 +00:00
});
2019-06-05 00:23:35 +00:00
xit('can admin login', adminLogin);
xit('can send mail', sendMail);
xit('can logout', logout);
it('can login', login.bind(null, username, password));
2017-08-07 18:16:29 +00:00
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 () {
2019-11-14 15:45:34 +00:00
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
done();
});
2017-08-07 18:16:29 +00:00
});
2015-11-24 21:59:52 +00:00
});