ttrss: Use privileged decorator for actions

Tests:

- Ignore setting a None domain
- Updated tests to use base class

- Functional tests work
  - Backup/restore works. Database is dumped and restored.
- Initial setup works
- Enabling/disabling works
  - API access is enabled and a valid domain is set when available
- Setting the domain works
  - Configuration is updated in update.php
  - App page show newly set domain
- Not tested: force upgrade of package

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2022-10-08 18:53:30 -04:00
1 parent 623bcefe22
commit 11a27d8efc
5 files changed
+60 -107

No files matched your search

+11 -25
View File
@@ -1,12 +1,9 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
FreedomBox app to configure Tiny Tiny RSS.
"""
"""FreedomBox app to configure Tiny Tiny RSS."""
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from plinth import actions
from plinth import app as app_module
from plinth import cfg, frontpage, menu
from plinth.daemon import Daemon
@@ -17,7 +14,7 @@ from plinth.modules.users.components import UsersAndGroups
from plinth.package import Packages, install
from plinth.utils import Version, format_lazy
from . import manifest
from . import manifest, privileged
_description = [
_('Tiny Tiny RSS is a news feed (RSS/Atom) reader and aggregator, '
@@ -97,20 +94,20 @@ class TTRSSApp(app_module.App):
def enable(self):
"""Enable components and API access."""
super().enable()
actions.superuser_run('ttrss', ['enable-api-access'])
privileged.enable_api_access()
# Try to set the domain to one of the available TLS domains
domain = get_domain()
domain = privileged.get_domain()
if not domain or domain == 'localhost':
from plinth.modules import names
domain = next(names.get_available_tls_domains(), None)
set_domain(domain)
privileged.set_domain(domain)
def setup(self, old_version):
"""Install and configure the app."""
actions.superuser_run('ttrss', ['pre-setup'])
privileged.pre_setup()
super().setup(old_version)
actions.superuser_run('ttrss', ['setup'])
privileged.setup()
self.enable()
def force_upgrade(self, packages):
@@ -124,30 +121,19 @@ class TTRSSApp(app_module.App):
return False
install(['tt-rss'], force_configuration='new')
actions.superuser_run('ttrss', ['setup'])
privileged.setup()
return True
class TTRSSBackupRestore(BackupRestore):
"""Component to backup/restore TT-RSS"""
"""Component to backup/restore TT-RSS."""
def backup_pre(self, packet):
"""Save database contents."""
super().backup_pre(packet)
actions.superuser_run('ttrss', ['dump-database'])
privileged.dump_database()
def restore_post(self, packet):
"""Restore database contents."""
super().restore_post(packet)
actions.superuser_run('ttrss', ['restore-database'])
def get_domain():
"""Read TLS domain from tt-rss config file."""
return actions.superuser_run('ttrss', ['get-domain']).strip()
def set_domain(domain):
"""Set the TLS domain in tt-rss configuration file."""
if domain:
actions.superuser_run('ttrss', ['set-domain', domain])
privileged.restore_database()
+144
View File
@@ -0,0 +1,144 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Configure Tiny Tiny RSS."""
import os
import subprocess
from typing import Optional
import augeas
from plinth import action_utils
from plinth.actions import privileged
CONFIG_FILE = '/etc/tt-rss/config.php'
DEFAULT_FILE = '/etc/default/tt-rss'
DATABASE_FILE = '/etc/tt-rss/database.php'
DB_BACKUP_FILE = '/var/lib/plinth/backups-data/ttrss-database.sql'
@privileged
def pre_setup():
"""Preseed debconf values before packages are installed."""
action_utils.debconf_set_selections(
['tt-rss tt-rss/database-type string pgsql'])
@privileged
def get_domain() -> Optional[str]:
"""Get the domain set for Tiny Tiny RSS."""
aug = load_augeas()
from urllib.parse import urlparse
for match in aug.match('/files' + CONFIG_FILE + '/define'):
if aug.get(match) == 'SELF_URL_PATH':
url = aug.get(match + '/value').strip("'")
return urlparse(url).netloc
return None
@privileged
def set_domain(domain_name: Optional[str]):
"""Set the domain to be used by Tiny Tiny RSS."""
if not domain_name:
return
url = f"'https://{domain_name}/tt-rss/'"
aug = load_augeas()
for match in aug.match('/files' + CONFIG_FILE + '/define'):
if aug.get(match) == 'SELF_URL_PATH':
aug.set(match + '/value', url)
aug.save()
@privileged
def setup():
"""Setup Tiny Tiny RSS configuration."""
aug = load_augeas()
aug.set('/files' + DEFAULT_FILE + '/DISABLED', '0')
skip_self_url_path_exists = False
for match in aug.match('/files' + CONFIG_FILE + '/define'):
if aug.get(match) == 'PLUGINS':
aug.set(match + '/value', "'auth_remote, note'")
elif aug.get(match) == '_SKIP_SELF_URL_PATH_CHECKS':
skip_self_url_path_exists = True
aug.set(match + '/value', 'true')
if not skip_self_url_path_exists:
aug.set('/files' + CONFIG_FILE + '/define[last() + 1]',
'_SKIP_SELF_URL_PATH_CHECKS')
aug.set('/files' + CONFIG_FILE + '/define[last()]/value', 'true')
aug.save()
if action_utils.service_is_enabled('tt-rss'):
action_utils.service_restart('tt-rss')
@privileged
def enable_api_access():
"""Enable API access so that tt-rss can be accessed through mobile app."""
import psycopg2 # Only available post installation
aug = load_augeas()
def get_value(variable_name):
"""Return the value of a variable from database configuration file."""
return aug.get('/files' + DATABASE_FILE + '/$' + variable_name) \
.strip("'\"")
user = get_value('dbuser')
password = get_value('dbpass')
database = get_value('dbname')
host = get_value('dbserver')
connection = psycopg2.connect(database=database, user=user,
password=password, host=host)
cursor = connection.cursor()
cursor.execute("UPDATE ttrss_prefs SET def_value=true "
"WHERE pref_name='ENABLE_API_ACCESS';")
connection.commit()
connection.close()
@privileged
def dump_database():
"""Dump database to file."""
os.makedirs(os.path.dirname(DB_BACKUP_FILE), exist_ok=True)
with open(DB_BACKUP_FILE, 'w', encoding='utf-8') as db_backup_file:
_run_as_postgres(['pg_dump', 'ttrss'], stdout=db_backup_file)
@privileged
def restore_database():
"""Restore database from file."""
_run_as_postgres(['dropdb', 'ttrss'])
_run_as_postgres(['createdb', 'ttrss'])
with open(DB_BACKUP_FILE, 'r', encoding='utf-8') as db_restore_file:
_run_as_postgres(['psql', '--dbname', 'ttrss'], stdin=db_restore_file)
def _run_as_postgres(command, stdin=None, stdout=None):
"""Run a command as postgres user."""
command = ['sudo', '--user', 'postgres'] + command
subprocess.run(command, stdin=stdin, stdout=stdout, check=True)
def load_augeas():
"""Initialize Augeas."""
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
augeas.Augeas.NO_MODL_AUTOLOAD)
aug.set('/augeas/load/Shellvars/lens', 'Shellvars.lns')
aug.set('/augeas/load/Shellvars/incl[last() + 1]', DEFAULT_FILE)
aug.set('/augeas/load/Phpvars/lens', 'Phpvars.lns')
aug.set('/augeas/load/Phpvars/incl[last() + 1]', CONFIG_FILE)
aug.set('/augeas/load/Phpvars/incl[last() + 1]', DATABASE_FILE)
aug.load()
return aug
+15 -28
View File
@@ -12,38 +12,25 @@ APP_ID = 'ttrss'
pytestmark = [pytest.mark.apps, pytest.mark.ttrss, pytest.mark.sso]
@pytest.fixture(scope='module', autouse=True)
def fixture_background(session_browser):
"""Login and install the app."""
functional.login(session_browser)
functional.install(session_browser, APP_ID)
yield
functional.app_disable(session_browser, APP_ID)
class TestTTRSSApp(functional.BaseAppTests):
"""Class to customize basic app tests for TTRSS."""
app_name = 'ttrss'
has_service = True
has_web = True
def test_enable_disable(session_browser):
"""Test enabling the app."""
functional.app_disable(session_browser, APP_ID)
@pytest.mark.backups
def test_backup_restore(self, session_browser):
"""Test backup and restore of app data."""
functional.app_enable(session_browser, APP_ID)
_subscribe(session_browser)
functional.backup_create(session_browser, APP_ID, 'test_ttrss')
functional.app_enable(session_browser, APP_ID)
assert functional.service_is_running(session_browser, APP_ID)
_unsubscribe(session_browser)
functional.backup_restore(session_browser, APP_ID, 'test_ttrss')
functional.app_disable(session_browser, APP_ID)
assert functional.service_is_not_running(session_browser, APP_ID)
@pytest.mark.backups
def test_backup_restore(session_browser):
"""Test backup and restore of app data."""
functional.app_enable(session_browser, APP_ID)
_subscribe(session_browser)
functional.backup_create(session_browser, APP_ID, 'test_ttrss')
_unsubscribe(session_browser)
functional.backup_restore(session_browser, APP_ID, 'test_ttrss')
assert functional.service_is_running(session_browser, APP_ID)
assert _is_subscribed(session_browser)
assert functional.service_is_running(session_browser, APP_ID)
assert _is_subscribed(session_browser)
def _ttrss_load_main_interface(browser):
+9 -4
View File
@@ -1,28 +1,33 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Django views for Tiny Tiny RSS app."""
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from plinth.forms import TLSDomainForm
from plinth.modules import ttrss
from plinth.views import AppView
from . import privileged
class TTRSSAppView(AppView):
"""Show TTRSS app main view."""
app_id = 'ttrss'
form_class = TLSDomainForm
def get_initial(self):
"""Return the values to fill in the form."""
initial = super().get_initial()
initial['domain'] = ttrss.get_domain()
initial['domain'] = privileged.get_domain()
return initial
def form_valid(self, form):
"""Change the domain of TT-RSS app."""
data = form.cleaned_data
if ttrss.get_domain() != data['domain']:
ttrss.set_domain(data['domain'])
old_data = form.initial
if old_data['domain'] != data['domain']:
privileged.set_domain(data['domain'])
messages.success(self.request, _('Configuration updated'))
return super().form_valid(form)