gitea-app/test/test.js

310 lines
13 KiB
JavaScript
Raw Normal View History

2015-11-24 21:59:52 +00:00
#!/usr/bin/env node
2021-08-23 11:10:31 +00:00
/* jshint esversion: 8 */
2024-10-09 07:08:28 +00:00
/* global it, xit, describe, before, after, afterEach */
2015-12-01 02:07:21 +00:00
2015-11-24 21:59:52 +00:00
'use strict';
2020-03-09 01:59:04 +00:00
require('chromedriver');
2022-02-25 03:42:35 +00:00
const execSync = require('child_process').execSync,
2015-11-24 21:59:52 +00:00
expect = require('expect.js'),
fs = require('fs'),
path = require('path'),
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
2023-07-29 14:38:07 +00:00
const TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 5000;
const EXEC_ARGS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' };
2024-10-09 07:08:28 +00:00
const LOCATION = process.env.LOCATION || 'test';
2023-07-29 14:38:07 +00:00
const SSH_PORT = 29420;
2019-11-14 15:45:34 +00:00
2023-07-29 14:38:07 +00:00
let app, browser;
2019-11-14 15:45:34 +00:00
2023-07-29 14:38:07 +00:00
const repodir = '/tmp/testrepo';
const reponame = 'testrepo';
2019-11-14 15:45:34 +00:00
2023-07-29 14:38:07 +00:00
const username = process.env.USERNAME;
const password = process.env.PASSWORD;
2015-11-25 08:34:37 +00:00
2019-11-14 15:45:34 +00:00
before(function () {
2024-10-09 07:08:28 +00:00
const chromeOptions = new Options().windowSize({ width: 1280, height: 1024 });
if (process.env.CI) chromeOptions.addArguments('no-sandbox', 'disable-dev-shm-usage', 'headless');
browser = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();
if (!fs.existsSync('./screenshots')) fs.mkdirSync('./screenshots');
2015-11-25 03:47:03 +00:00
});
2020-10-02 15:45:03 +00:00
after(function () {
2015-11-25 08:28:22 +00:00
browser.quit();
2022-02-25 03:42:35 +00:00
fs.rmSync(repodir, { recursive: true, force: true });
2015-11-25 08:28:22 +00:00
});
2024-10-09 07:08:28 +00:00
afterEach(async function () {
if (!process.env.CI || !app) return;
const currentUrl = await browser.getCurrentUrl();
if (!currentUrl.includes(app.domain)) return;
expect(this.currentTest.title).to.be.a('string');
const screenshotData = await browser.takeScreenshot();
fs.writeFileSync(`./screenshots/${new Date().getTime()}-${this.currentTest.title.replaceAll(' ', '_')}.png`, screenshotData, 'base64');
});
2017-08-07 18:16:29 +00:00
function getAppInfo() {
2024-10-09 07:08:28 +00:00
const inspect = JSON.parse(execSync('cloudron inspect'));
2021-08-23 11:10:31 +00:00
app = inspect.apps.filter(function (a) { return a.location.indexOf(LOCATION) === 0; })[0];
2017-08-07 18:16:29 +00:00
expect(app).to.be.an('object');
}
async function waitForElement(elem) {
await browser.wait(until.elementLocated(elem), TIMEOUT);
await browser.wait(until.elementIsVisible(browser.findElement(elem)), TIMEOUT);
}
2021-08-23 11:10:31 +00:00
function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
2021-08-23 11:10:31 +00:00
async function setAvatar() {
await browser.get('https://' + app.fqdn + '/user/settings');
var button = await browser.findElement(By.xpath('//label[contains(text(), "Use Custom Avatar")]'));
await browser.executeScript('arguments[0].scrollIntoView(false)', button);
await browser.findElement(By.xpath('//label[contains(text(), "Use Custom Avatar")]')).click();
await browser.findElement(By.xpath('//input[@type="file" and @name="avatar"]')).sendKeys(path.resolve(__dirname, '../logo.png'));
await browser.findElement(By.xpath('//button[contains(text(), "Update Avatar")]')).click();
await browser.wait(until.elementLocated(By.xpath('//p[contains(text(),"Your avatar has been updated.")]')), TIMEOUT);
2018-08-12 17:36:17 +00:00
}
2021-08-23 11:10:31 +00:00
async function checkAvatar() {
await browser.get(`https://${app.fqdn}/${username}`);
2022-08-19 14:18:45 +00:00
const avatarSrc = await browser.findElement(By.xpath('//div[@id="profile-avatar"]/a/img')).getAttribute('src');
2022-07-31 06:12:39 +00:00
const response = await superagent.get(avatarSrc);
expect(response.statusCode).to.equal(200);
}
2021-08-23 11:10:31 +00:00
async function login(username, password) {
await browser.get('https://' + app.fqdn + '/user/login');
await browser.findElement(By.id('user_name')).sendKeys(username);
await browser.findElement(By.id('password')).sendKeys(password);
2021-10-22 06:27:40 +00:00
await browser.findElement(By.xpath('//form[@action="/user/login"]//button')).click();
2022-01-31 13:08:14 +00:00
await browser.wait(until.elementLocated(By.xpath('//img[contains(@class, "avatar")]')), TIMEOUT);
2017-06-02 21:25:09 +00:00
}
2021-08-23 11:10:31 +00:00
async function adminLogin() {
await login('root', 'changeme');
2019-06-05 00:23:35 +00:00
}
2024-12-09 08:57:55 +00:00
async function loginOIDC(username, password, alreadyAuthenticated = true) {
browser.manage().deleteAllCookies();
await browser.get(`https://${app.fqdn}/user/login`);
await browser.sleep(2000);
2024-12-09 08:57:55 +00:00
await browser.findElement(By.xpath('//a[contains(@href, "/user/oauth2/Cloudron")]')).click();
await browser.sleep(2000);
2024-12-09 08:57:55 +00:00
if (!alreadyAuthenticated) {
await waitForElement(By.xpath('//input[@name="username"]'));
await browser.findElement(By.xpath('//input[@name="username"]')).sendKeys(username);
await browser.findElement(By.xpath('//input[@name="password"]')).sendKeys(password);
await browser.sleep(2000);
2024-02-01 13:27:07 +00:00
await browser.findElement(By.id('loginSubmitButton')).click();
await browser.sleep(2000);
}
await waitForElement(By.xpath('//img[contains(@class, "avatar")]'));
}
2021-08-23 11:10:31 +00:00
async function logout() {
await browser.get('https://' + app.fqdn);
await browser.findElement(By.xpath('//img[contains(@class, "avatar")]')).click();
await sleep(2000);
await browser.findElement(By.xpath('//a[@data-url="/user/logout"]')).click();
await sleep(2000);
2019-06-05 00:23:35 +00:00
}
2021-08-23 11:10:31 +00:00
async function addPublicKey() {
2024-10-09 07:36:48 +00:00
const publicKey = fs.readFileSync(__dirname + '/id_ed25519.pub', 'utf8');
execSync(`chmod g-rw,o-rw ${__dirname}/id_ed25519`); // ssh will complain about perms later
2017-06-02 21:25:09 +00:00
2023-11-15 11:50:25 +00:00
await browser.get('https://' + app.fqdn + '/user/settings/keys');
2017-10-14 18:37:34 +00:00
2023-11-15 11:50:25 +00:00
await browser.wait(until.elementLocated(By.id('add-ssh-button')), TIMEOUT);
await browser.findElement(By.id('add-ssh-button')).click();
await browser.findElement(By.id('ssh-key-title')).sendKeys('testkey');
await browser.findElement(By.id('ssh-key-content')).sendKeys(publicKey.trim()); // #3480
var button = browser.findElement(By.xpath('//button[contains(text(), "Add Key")]'));
await browser.executeScript('arguments[0].scrollIntoView(false)', button);
await browser.findElement(By.xpath('//form//button[contains(text(),"Add Key")]')).click();
await browser.wait(until.elementLocated(By.xpath('//p[contains(text(), "has been added.")]')), TIMEOUT);
}
2021-08-23 11:10:31 +00:00
async function createRepo() {
2024-10-09 07:28:39 +00:00
await browser.get(`https://${app.fqdn}/repo/create`);
await browser.wait(until.elementLocated(By.id('repo_name')));
2021-08-23 11:10:31 +00:00
await browser.findElement(By.id('repo_name')).sendKeys(reponame);
var button = browser.findElement(By.xpath('//button[contains(text(), "Create Repository")]'));
await browser.executeScript('arguments[0].scrollIntoView(true)', button);
await browser.findElement(By.id('auto-init')).click();
await browser.findElement(By.xpath('//button[contains(text(), "Create Repository")]')).click();
await browser.wait(function () {
return browser.getCurrentUrl().then(function (url) {
return url === 'https://' + app.fqdn + '/' + username + '/' + reponame;
2017-10-14 18:37:34 +00:00
});
2021-08-23 11:10:31 +00:00
}, TIMEOUT);
}
2021-08-23 11:10:31 +00:00
async function checkCloneUrl() {
await browser.get('https://' + app.fqdn + '/' + username + '/' + reponame);
await browser.findElement(By.id('repo-clone-ssh')).click();
var cloneUrl = await browser.findElement(By.id('repo-clone-url')).getAttribute('value');
expect(cloneUrl).to.be(`ssh://git@${app.fqdn}:${SSH_PORT}/${username}/${reponame}.git`);
2017-06-02 21:25:09 +00:00
}
2021-08-23 11:10:31 +00:00
function cloneRepo() {
2022-02-25 03:42:35 +00:00
fs.rmSync(repodir, { recursive: true, force: true });
2017-06-02 21:25:09 +00:00
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
}
2021-08-23 11:10:31 +00:00
function pushFile() {
2022-07-31 06:12:39 +00:00
const env = Object.create(process.env);
2017-08-07 18:16:29 +00:00
env.GIT_SSH = __dirname + '/git_ssh_wrapper.sh';
2022-08-19 14:31:54 +00:00
execSync(`touch newfile && git add newfile && git commit -a -mx && git push ssh://git@${app.fqdn}:${SSH_PORT}/${username}/${reponame} main`,
2017-08-07 18:16:29 +00:00
{ env: env, cwd: repodir });
2022-02-25 03:42:35 +00:00
fs.rmSync(repodir, { recursive: true, force: true });
2018-08-02 17:15:05 +00:00
}
2017-08-07 18:16:29 +00:00
function fileExists() {
expect(fs.existsSync(repodir + '/newfile')).to.be(true);
}
2021-08-23 11:10:31 +00:00
async function sendMail() {
await browser.get(`https://${app.fqdn}/admin/config`);
2023-07-17 04:36:18 +00:00
var button = await browser.findElement(By.xpath('//button[contains(text(), "Send")]'));
2021-08-23 11:10:31 +00:00
await browser.executeScript('arguments[0].scrollIntoView(true)', button);
await browser.findElement(By.xpath('//input[@name="email"]')).sendKeys('test@cloudron.io');
2023-07-17 04:36:18 +00:00
await browser.findElement(By.xpath('//button[contains(text(), "Send")]')).click();
await browser.wait(until.elementLocated(By.xpath('//p[contains(text(), "A testing email has been sent")]')), TIMEOUT);
}
xit('build app', function () { execSync('cloudron build', EXEC_ARGS); });
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
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);
2024-12-09 08:57:55 +00:00
it('can login', loginOIDC.bind(null, username, password, false));
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);
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', loginOIDC.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
it('can login', loginOIDC.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
2021-08-23 11:10:31 +00:00
it('move to different location', async function () {
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
2021-08-23 11:10:31 +00:00
await browser.get('about:blank');
execSync('cloudron configure --location ' + LOCATION + '2 --app ' + app.id, EXEC_ARGS);
2015-12-01 02:13:47 +00:00
});
2021-08-23 11:10:31 +00:00
it('can get app information', getAppInfo);
2015-12-01 02:13:47 +00:00
it('can login', loginOIDC.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
2021-08-23 11:10:31 +00:00
it('uninstall app', async function () {
// ensure we don't hit NXDOMAIN in the mean time
2021-08-23 11:10:31 +00:00
await browser.get('about:blank');
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
2015-11-24 21:59:52 +00:00
});
2016-04-11 21:58:06 +00:00
2019-06-05 00:23:35 +00:00
// No SSO
2021-08-23 11:10:31 +00:00
it('install app (no sso)', function () { execSync(`cloudron install --no-sso --location ${LOCATION} -p SSH_PORT=${SSH_PORT}`, EXEC_ARGS); });
2019-06-05 00:23:35 +00:00
2021-08-23 11:10:31 +00:00
it('can get app information', getAppInfo);
2019-06-05 00:23:35 +00:00
it('can admin login (no sso)', adminLogin);
it('can logout', logout);
2021-08-23 11:10:31 +00:00
it('uninstall app (no sso)', async function () {
await browser.get('about:blank');
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
2021-08-23 11:10:31 +00:00
it('can install app', function () { 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);
2023-10-03 15:23:13 +00:00
it('can login', loginOIDC.bind(null, username, password));
2017-08-07 18:16:29 +00:00
it('can set avatar', setAvatar);
it('can get avatar', checkAvatar);
2022-02-25 03:42:35 +00:00
it('can add public key', addPublicKey);
2017-08-07 18:16:29 +00:00
it('can create repo', createRepo);
it('can clone the url', cloneRepo);
it('can add and push a file', pushFile);
2021-08-23 11:10:31 +00:00
it('can update', function () { execSync('cloudron update --app ' + app.id, EXEC_ARGS); });
2022-07-31 06:12:39 +00:00
it('can get app information', getAppInfo);
2017-08-07 18:16:29 +00:00
2022-07-31 06:21:18 +00:00
it('can admin login', adminLogin);
it('can send mail', sendMail);
it('can logout', logout);
2019-06-05 00:23:35 +00:00
it('can login', loginOIDC.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);
2021-08-23 11:10:31 +00:00
it('uninstall app', async function () {
// ensure we don't hit NXDOMAIN in the mean time
2021-08-23 11:10:31 +00:00
await browser.get('about:blank');
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
2017-08-07 18:16:29 +00:00
});
2015-11-24 21:59:52 +00:00
});