freshrss-app/test/test.js

252 lines
9.6 KiB
JavaScript

#!/usr/bin/env node
/* jshint esversion: 8 */
/* global describe */
/* global before */
/* global after */
/* global it */
'use strict';
require('chromedriver');
var execSync = require('child_process').execSync,
expect = require('expect.js'),
path = require('path'),
superagent = require('superagent'),
{ Builder, By, Key, until } = require('selenium-webdriver'),
{ Options } = require('selenium-webdriver/chrome');
var username = 'admin',
password = 'changeme';
describe('Application life cycle test', function () {
this.timeout(0);
const LOCATION = 'test';
const TEST_TIMEOUT = 10000;
const EXEC_ARGS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' };
var browser, app;
before(function () {
const options = new Options().windowSize({ width: 1280, height: 1024 });
if (process.env.HEADLESS) options.addArguments('headless');
browser = new Builder().forBrowser('chrome').setChromeOptions(options).build();
});
after(function () {
browser.quit();
});
function getAppInfo() {
var inspect = JSON.parse(execSync('cloudron inspect'));
app = inspect.apps.filter(function (a) { return a.location.indexOf(LOCATION) === 0; })[0];
expect(app).to.be.an('object');
}
function exists(selector) {
return browser.wait(until.elementLocated(selector), TEST_TIMEOUT);
}
function visible(selector) {
return exists(selector).then(function () {
return browser.wait(until.elementIsVisible(browser.findElement(selector)), TEST_TIMEOUT);
});
}
function baseUrl() {
if (app.manifest.version === '1.4.0') return `https://${app.fqdn}/p`;
return `https://${app.fqdn}`;
}
// we do this because it perm redirects to /p/ in old versions
function clearCache() {
// https://stackoverflow.com/questions/49614217/selenium-clear-chrome-cache
// defaut clearance is 1 hour of cache
return browser.get('chrome://settings/clearBrowserData').then(function () {
return visible(By.css('* /deep/ #clearBrowsingDataConfirm'));
}).then(function () {
return browser.findElement(By.css('* /deep/ #clearBrowsingDataConfirm')).click();
}).then(function () {
return browser.sleep(5000);
});
}
function login(password, callback) {
browser.get('https://' + app.fqdn).then(function () {
return visible(By.id('loginButton'));
}).then(function () {
return browser.findElement(By.id('username')).sendKeys(username);
}).then(function () {
return browser.findElement(By.id('passwordPlain')).sendKeys(password);
}).then(function () {
return browser.findElement(By.id('loginButton')).click();
}).then(function () {
return browser.wait(until.elementLocated(By.id('dropdown-configure')), TEST_TIMEOUT);
}).then(function () {
callback();
});
}
function logout(callback) {
var logout_btn = By.xpath('//li/a[@class="signout"]');
browser.get('https://' + app.fqdn).then(function () {
return visible(By.id('stream'));
}).then(function () {
return browser.findElement(By.className('dropdown-toggle')).click();
}).then(function () {
return visible(logout_btn);
}).then(function () {
return browser.findElement(logout_btn).click();
}).then(function () {
return browser.wait(until.elementLocated(By.id('loginButton')), TEST_TIMEOUT);
}).then(function () {
callback();
});
}
function addSubscription(callback) {
var url = 'https://blog.cloudron.io/rss/';
const addUrl = app.manifest.version === '1.10.0' ? `${baseUrl()}/i/?c=subscription` : `${baseUrl()}/i/?c=subscription&a=add`;
browser.get(addUrl).then(function () {
return visible(By.xpath('//input[@name="url_rss"]'));
}).then(function () {
return browser.findElement(By.xpath('//input[@name="url_rss"]')).sendKeys(url);
}).then(function () {
return browser.findElement(By.xpath('//button[@type="submit"]/ancestor::form[@id="add_rss"]')).submit();
}).then(function () {
return visible(By.xpath('//div[@id="notification" and @class="notification good"]'));
}).then(function () {
callback();
});
}
function addUser(password, callback) {
var test_username = 'test';
browser.get(`${baseUrl()}/i/?c=user&a=manage`).then(function () {
return visible(By.id('new_user_name'));
}).then(function () {
return browser.findElement(By.id('new_user_name')).sendKeys(test_username);
}).then(function () {
return browser.findElement(By.id('new_user_passwordPlain')).sendKeys(password);
}).then(function () {
return browser.findElement(By.xpath('//button[text()="Create"]')).click();
}).then(function () {
return visible(By.xpath('//div[@id="notification" and @class="notification good"]'));
}).then(function () {
callback();
});
}
function enableApi(callback) {
browser.get(`${baseUrl()}/i/?c=auth`).then(function () {
return browser.findElement(By.id('api_enabled')).click();
}).then(function () {
return browser.findElement(By.xpath('//button[text()="Submit"]')).click();
}).then(function () {
callback();
});
}
function checkApiConfiguration(callback) {
browser.get(`${baseUrl()}/api/`).then(function () {
return exists(By.xpath('//dd[@id="greaderOutput" and contains(text(), "PASS")]'));
}).then(function () {
return exists(By.xpath('//dd[@id="feverOutput" and contains(text(), "PASS")]'));
}).then(function () {
callback();
});
}
function subscriptionExists(callback) {
browser.get(`${baseUrl()}/i/?get=c_1`).then(function () {
return visible(By.xpath('//span[text()="Cloudron"]'));
}).then(function () {
callback();
});
}
function getStaticExtensionFile(callback) {
superagent.get(`${baseUrl()}/ext.php?f=xExtension-StickyFeeds/static/script.js&t=js`)
.buffer(true)
.end(function (err, res) {
expect(err).to.be(null);
expect(res.status).to.be(200);
expect(res.text).to.contain('sticky_feeds'); // relies on the buffer flag above
callback();
});
}
xit('build app', function () { execSync('cloudron build', EXEC_ARGS); });
it('install app', function () { execSync('cloudron install --location ' + LOCATION, EXEC_ARGS); });
it('can get app information', getAppInfo);
it('can login', login.bind(null, password));
it('can subscribe', addSubscription);
it('can add users', addUser.bind(null, password));
it('can enable API', enableApi);
it('can check configuration', checkApiConfiguration);
it('subscription exists', subscriptionExists);
it('can get static extension file', getStaticExtensionFile);
it('can logout', logout);
it('backup app', function () { execSync('cloudron backup create --app ' + app.id, EXEC_ARGS); });
it('restore app', function () {
const backups = JSON.parse(execSync(`cloudron backup list --raw --app ${app.id}`));
execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS);
execSync('cloudron install --location ' + LOCATION, EXEC_ARGS);
getAppInfo();
execSync(`cloudron restore --backup ${backups[0].id} --app ${app.id}`, EXEC_ARGS);
});
it('can login', login.bind(null, password));
it('can check configuration', checkApiConfiguration);
it('subscription exists', subscriptionExists);
it('can get static extension file', getStaticExtensionFile);
it('can logout', logout);
it('move to different location', function () {
browser.manage().deleteAllCookies();
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');
});
it('can login', login.bind(null, password));
it('can check configuration', checkApiConfiguration);
it('subscription exists', subscriptionExists);
it('can get static extension file', getStaticExtensionFile);
it('can logout', logout);
it('uninstall app', function () { execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS); });
// test update
it('can install app', function () { execSync('cloudron install --appstore-id org.freshrss.cloudronapp --location ' + LOCATION, EXEC_ARGS); });
it('can get app information', getAppInfo);
it('can login', login.bind(null, password));
it('can subscribe', addSubscription);
it('can add users', addUser.bind(null, password));
it('can update', function () {
execSync('cloudron update --app ' + app.id, 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');
});
it('can login', login.bind(null, password));
it('subscription exists', subscriptionExists);
it('can get static extension file', getStaticExtensionFile);
it('uninstall app', function () { execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS); });
});