wrolpi/modules/torrents/conftest.py
lrnselfreliance d6a8363903
Torrents: refuse to download into the media directory itself (#1038)
* Torrents: refuse to download into the media directory itself

A torrent's files are chosen by whoever made it. Saved in the media
directory itself, a torrent holding a config directory would put its
files in WROLPi's config directory. Downloads now need a directory in
the media directory. Share still seeds a top-level directory from the
media directory: its files are the shared directory's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Torrents: refuse a WebUI save path WROLPi would refuse

A torrent added in qBittorrent's WebUI to the media directory itself (or
outside the library) is removed from qBittorrent, its files kept, and a
"Torrent Refused" event says why. A torrent moved there in the WebUI is
moved back to its config's save path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Torrents: keep an untagged WROLPi torrent; refuse a WebUI download path

A WROLPi torrent whose tag was removed in the WebUI was removed from
qBittorrent when its save path was refused, and forgotten (config and
.torrent deleted) on the next pass. It is now tagged again and kept as
the config says, as adopt() did before.

qBittorrent keeps a downloading torrent's incomplete files in its
download path, which the WebUI can set per torrent. A WebUI torrent with
a refused download path is removed (files kept); a WROLPi torrent's is
set back to its save path. A finished torrent's download path is unused,
and left alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 15:05:52 -07:00

138 lines
5.1 KiB
Python

import copy
import pytest
from modules.torrents import lib
@pytest.fixture
def test_torrents_config(test_directory):
"""An isolated torrents config in the test directory."""
from wrolpi.api_utils import api_app
from wrolpi.contexts import attach_shared_contexts
if not hasattr(api_app.shared_ctx, 'switches_lock'):
attach_shared_contexts(api_app)
(test_directory / 'config' / 'torrents').mkdir(parents=True, exist_ok=True)
lib.set_test_torrents_config(True)
config = lib.get_torrents_config()
config.initialize()
try:
yield config
finally:
lib.set_test_torrents_config(False)
class FakeQBittorrent:
"""qBittorrent's WebUI API, in memory."""
def __init__(self, torrents=None, tags=None, preferences=None, metadata=True, created=None,
creator_statuses=None):
self._torrents = {t['hash']: dict(t) for t in torrents or []}
self._tags = list(tags or [])
self._preferences = dict(preferences or {'upnp': True, 'lsd': True, 'listen_port': 57968,
'current_network_interface': 'vpn0'})
self.metadata = metadata
self.calls = []
# The torrent creator: the .torrent it makes, and the statuses it reports in turn.
self.created = created
self.creator_statuses = list(creator_statuses or [{'status': 'Finished', 'progress': None}])
async def version(self):
return 'v5.1.0'
async def preferences(self):
return dict(self._preferences)
async def set_preferences(self, preferences):
self.calls.append(('set_preferences', preferences))
self._preferences.update(preferences)
async def transfer_info(self):
return {'connection_status': 'connected', 'up_info_speed': 100, 'dl_info_speed': 0}
async def torrents(self):
return [copy.deepcopy(t) for t in self._torrents.values()]
async def tags(self):
return list(self._tags)
async def create_tags(self, *tags):
self._tags.extend(t for t in tags if t not in self._tags)
def _tag_set(self, hash_):
return {t for t in self._torrents[hash_].get('tags', '').split(',') if t}
async def add_tags(self, hashes, *tags):
for h in hashes:
self._torrents[h]['tags'] = ','.join(sorted(self._tag_set(h) | set(tags)))
async def remove_tags(self, hashes, *tags):
for h in hashes:
self._torrents[h]['tags'] = ','.join(sorted(self._tag_set(h) - set(tags)))
async def add(self, save_path, torrent=None, url=None, tags=None, stopped=False, skip_checking=False):
self.calls.append(('add', save_path, torrent, url, tags, stopped))
if skip_checking:
self.calls.append(('skip_checking', save_path))
if torrent and torrent.startswith(b'd'):
hash_, name = lib.torrent_identity(torrent)
elif torrent:
# A stand-in .torrent written by a test: b'torrent <hash>'.
hash_ = name = torrent.decode()[-40:]
else:
hash_, name = lib.magnet_identity(url)
self._torrents[hash_] = dict(hash=hash_, name=name, save_path=save_path,
tags=','.join(tags or []), state='stoppedDL' if stopped else 'downloading',
progress=0)
async def delete(self, hashes, delete_files=False):
self.calls.append(('delete', hashes, delete_files))
for h in hashes:
self._torrents.pop(h, None)
async def set_location(self, hashes, location):
self.calls.append(('set_location', hashes, location))
for h in hashes:
self._torrents[h]['save_path'] = location
async def set_download_path(self, hashes, path):
self.calls.append(('set_download_path', hashes, path))
for h in hashes:
self._torrents[h]['download_path'] = path
async def export(self, hash_):
return f'torrent {hash_}'.encode() if self.metadata else None
async def set_share_limits(self, hashes, ratio_limit, seeding_time_limit):
self.calls.append(('set_share_limits', hashes, ratio_limit, seeding_time_limit))
for h in hashes:
self._torrents[h].update(ratio_limit=ratio_limit, seeding_time_limit=seeding_time_limit)
async def create_torrent(self, source_path, trackers, url_seeds):
self.calls.append(('create_torrent', source_path, trackers, url_seeds))
return 'task-1'
async def torrent_creator_status(self, task_id):
return self.creator_statuses.pop(0) if len(self.creator_statuses) > 1 else self.creator_statuses[0]
async def torrent_creator_file(self, task_id):
return self.created
async def delete_torrent_creator_task(self, task_id):
self.calls.append(('delete_torrent_creator_task', task_id))
async def start(self, hashes):
self.calls.append(('start', hashes))
for h in hashes:
self._torrents[h]['state'] = 'downloading'
async def stop(self, hashes):
self.calls.append(('stop', hashes))
for h in hashes:
self._torrents[h]['state'] = 'stoppedDL'
@pytest.fixture
def fake_qbittorrent():
return FakeQBittorrent