mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 05:09:58 -05:00
Fixing bug where configs could be overwritten with empty configs on first refresh. Fixing bug where configs were displayed as JSON in admin settings.
This commit is contained in:
parent
83ad253d6f
commit
755b133fa4
7 changed files with 91 additions and 5 deletions
|
|
@ -175,7 +175,11 @@ class DomainsConfig(ConfigFile):
|
|||
# Use to_config to export each collection
|
||||
collections_data = [collection.to_config() for collection in collections]
|
||||
|
||||
self._config['collections'] = collections_data
|
||||
if not collections_data and self._config.get('collections'):
|
||||
logger.warning('Refusing to save empty domains config because domains exist in the config file!')
|
||||
return
|
||||
|
||||
self._config['collections'] = collections_data
|
||||
|
||||
logger.info(f'Dumping {len(collections_data)} domain collections to config')
|
||||
self.save(file, send_events, overwrite)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
"""Tests for domain collection tagging functionality."""
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from wrolpi.collections import Collection
|
||||
|
||||
|
|
@ -316,3 +317,43 @@ async def test_untag_domain_without_directory_saves_config(test_session, test_di
|
|||
domain_entry = next((c for c in data.get('collections', []) if c['name'] == 'example.com'), None)
|
||||
assert domain_entry is not None, "Domain collection not found in config"
|
||||
assert domain_entry.get('tag_name') is None, "Tag should be removed from config"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_dump_domains_config_does_not_wipe_config(test_session, test_directory, async_client):
|
||||
"""Dumping domains config when DB is empty must not overwrite an existing config file that has domains.
|
||||
|
||||
Regression test: a fresh DB triggering dump_config would write collections: []
|
||||
to the config file, permanently wiping the domain definitions.
|
||||
"""
|
||||
from modules.archive.lib import DomainsConfig
|
||||
from wrolpi.common import get_media_directory
|
||||
|
||||
# Create domain collection and dump config.
|
||||
domain_dir = get_media_directory() / 'archives' / 'example.com'
|
||||
domain_dir.mkdir(parents=True, exist_ok=True)
|
||||
collection = Collection(name='example.com', kind='domain', directory=domain_dir)
|
||||
test_session.add(collection)
|
||||
test_session.commit()
|
||||
|
||||
config_file = test_directory / 'config' / 'domains.yaml'
|
||||
config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
domains_config = DomainsConfig()
|
||||
domains_config.dump_config(config_file, overwrite=True)
|
||||
|
||||
data = yaml.safe_load(config_file.read_text())
|
||||
assert len(data['collections']) == 1, 'Config should have a domain'
|
||||
|
||||
# Simulate a fresh DB by deleting all domain collections.
|
||||
test_session.query(Collection).filter_by(kind='domain').delete()
|
||||
test_session.commit()
|
||||
|
||||
# Re-initialize the config from the file so in-memory has the collections.
|
||||
domains_config.initialize()
|
||||
file_data = yaml.safe_load(config_file.read_text())
|
||||
assert len(file_data['collections']) == 1, 'File should still have domains before dump'
|
||||
|
||||
# Dumping with an empty DB should NOT wipe the config file.
|
||||
domains_config.dump_config(config_file)
|
||||
data = yaml.safe_load(config_file.read_text())
|
||||
assert len(data['collections']) == 1, 'Config was wiped by dumping with empty DB!'
|
||||
|
|
|
|||
|
|
@ -799,6 +799,10 @@ def save_channels_config():
|
|||
logger.warning('Refusing to save channels config because test did not initialize a test config!')
|
||||
return
|
||||
|
||||
if not config.get('channels') and channels_config.channels:
|
||||
logger.warning('Refusing to save empty channels config because channels exist in the config file!')
|
||||
return
|
||||
|
||||
channels_config.update(config)
|
||||
logger.info('save_channels_config completed')
|
||||
|
||||
|
|
|
|||
|
|
@ -450,6 +450,36 @@ async def test_import_channel_download_comments(await_switches, test_session, ch
|
|||
assert channel.download_missing_data is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_save_channels_config_does_not_wipe_config(await_switches, test_session, channel_factory,
|
||||
test_channels_config):
|
||||
"""Saving channels config when DB is empty must not overwrite an existing config file that has channels.
|
||||
|
||||
Regression test: a fresh DB (e.g. after repair) triggering save_channels_config would write channels: []
|
||||
to the config file, permanently wiping the channel definitions.
|
||||
"""
|
||||
# Create a channel and save it to the config file.
|
||||
channel = channel_factory()
|
||||
test_session.commit()
|
||||
save_channels_config()
|
||||
await await_switches()
|
||||
assert test_channels_config.is_file()
|
||||
config_text = test_channels_config.read_text()
|
||||
assert 'channels: []' not in config_text, 'Config should have a channel'
|
||||
|
||||
# Simulate a fresh DB by deleting all channels.
|
||||
test_session.query(Channel).delete()
|
||||
test_session.query(Collection).filter_by(kind='channel').delete()
|
||||
test_session.commit()
|
||||
assert test_session.query(Channel).count() == 0
|
||||
|
||||
# Saving with an empty DB should NOT wipe the config file.
|
||||
save_channels_config()
|
||||
await await_switches()
|
||||
config_text = test_channels_config.read_text()
|
||||
assert 'channels: []' not in config_text, 'Config was wiped by saving with empty DB!'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ffprobe_json(async_client, video_file, corrupted_video_file):
|
||||
content = await common.ffprobe_json(video_file)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue