wrolpi/wrolpi/test/test_common.py
lrnselfreliance 87ed2ffeb8
Config: wrolpi.yaml is only ever config/wrolpi.yaml (#1039)
* Config: wrolpi.yaml is only ever config/wrolpi.yaml

When config/wrolpi.yaml was missing, the API searched the media
directory three levels deep (hidden directories too) and trusted the
first wrolpi.yaml it found. A torrent, download, or git repo holding a
wrolpi.yaml (e.g. torrents/.1/wrolpi.yaml) would become the config, and
the real one was never created. The Controller and scripts already read
only the fixed path.

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

* Config: shorten the wrolpi.yaml comment to the rule

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

---------

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

1480 lines
54 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
import ctypes.wintypes
import json
import sys
import multiprocessing
import os
import pathlib
import re
import shutil
import subprocess
import tempfile
import time
from datetime import date, datetime
from decimal import Decimal
from http import HTTPStatus
from itertools import zip_longest
from time import sleep
from unittest import mock
import pytest
import pytz
import wrolpi.vars
from wrolpi import common
from wrolpi.common import cum_timer, TIMERS, print_timer, limit_concurrent, run_after, get_wrolpi_config, TRACE_LEVEL, \
is_tempfile
from wrolpi.errors import InvalidConfig
from wrolpi.test.common import build_test_directories, skip_ci, skip_macos
def test_build_video_directories(test_directory):
structure = [
'channel1/vid1.mp4',
]
with build_test_directories(structure) as tempdir:
assert (tempdir / 'channel1').is_dir()
assert (tempdir / 'channel1/vid1.mp4').is_file()
structure = [
'channel2/',
'channel2.1/channel2.2/',
]
with build_test_directories(structure) as tempdir:
assert (tempdir / 'channel2').is_dir()
assert (tempdir / 'channel2.1/channel2.2').is_dir()
structure = [
'channel3/vid1.mp4',
'channel3/vid2.mp4',
'channel4/vid1.mp4',
'channel4/vid1.en.vtt',
'channel5/',
]
with build_test_directories(structure) as tempdir:
assert (tempdir / 'channel3/vid1.mp4').is_file()
assert (tempdir / 'channel3').is_dir()
assert (tempdir / 'channel3/vid2.mp4').is_file()
assert (tempdir / 'channel4/vid1.mp4').is_file()
assert (tempdir / 'channel4/vid1.en.vtt').is_file()
assert (tempdir / 'channel5').is_dir()
structure = [
'channel6/subdirectory/vid1.mp4',
]
with build_test_directories(structure) as tempdir:
assert (tempdir / 'channel6/subdirectory').is_dir()
assert (tempdir / 'channel6/subdirectory/vid1.mp4').is_file()
def test_insert_parameter():
"""
A convenience function exists that inserts a parameter or keyword argument into the provided args/kwargs,
wherever that may be according to the function's signature.
"""
def func(foo, bar): # noqa
pass
results = common.insert_parameter(func, 'bar', 'bar', (1,), {})
assert results == ((1, 'bar'), {})
def func(foo, bar, baz): # noqa
pass
results = common.insert_parameter(func, 'bar', 'bar', (1, 2), {})
assert results == ((1, 'bar', 2), {})
def func(foo, baz, bar=None): # noqa
pass
results = common.insert_parameter(func, 'bar', 'bar', (1, 2), {})
assert results == ((1, 2, 'bar'), {})
def func(foo, baz, bar=None): # noqa
pass
results = common.insert_parameter(func, 'baz', 'baz', (1, 2), {})
assert results == ((1, 'baz', 2), {})
def func(foo, baz, qux=None, bar=None): # noqa
pass
results = common.insert_parameter(func, 'bar', 'bar', (1, 2, 3), {})
assert results == ((1, 2, 3, 'bar'), {})
# bar is not defined as a parameter!
def func(foo): # noqa
pass
pytest.raises(TypeError, common.insert_parameter, func, 'bar', 'bar', (1,), {})
def test_date_range():
# A single step results in the start.
result = common.date_range(date(1970, 1, 1), date(1970, 1, 2), 1)
assert result == [
date(1970, 1, 1),
]
# Many steps on a single day results in the same day.
result = common.date_range(date(1970, 1, 1), date(1970, 1, 1), 5)
assert result == [
date(1970, 1, 1),
date(1970, 1, 1),
date(1970, 1, 1),
date(1970, 1, 1),
date(1970, 1, 1),
]
# Many steps on a single datetime results in a range of times.
result = common.date_range(datetime(1970, 1, 1), datetime(1970, 1, 1, 23, 59, 59), 5)
assert result == [
datetime(1970, 1, 1, 0, 0),
datetime(1970, 1, 1, 4, 47, 59, 800000),
datetime(1970, 1, 1, 9, 35, 59, 600000),
datetime(1970, 1, 1, 14, 23, 59, 400000),
datetime(1970, 1, 1, 19, 11, 59, 200000),
]
# common.date_range is not inclusive, like range().
result = common.date_range(date(1970, 1, 1), date(1970, 1, 5), 4)
assert result == [
date(1970, 1, 1),
date(1970, 1, 2),
date(1970, 1, 3),
date(1970, 1, 4),
]
# Reversed dates are supported.
result = common.date_range(date(1970, 1, 5), date(1970, 1, 1), 4)
assert result == [
date(1970, 1, 5),
date(1970, 1, 4),
date(1970, 1, 3),
date(1970, 1, 2),
]
# Large date spans are supported.
result = common.date_range(date(1970, 1, 1), date(2020, 5, 1), 4)
assert result == [
date(1970, 1, 1),
date(1982, 8, 1),
date(1995, 3, 2),
date(2007, 10, 1),
]
result = common.date_range(datetime(1970, 1, 1, 0, 0, 0), datetime(1970, 1, 1, 10, 0), 8)
assert result == [
datetime(1970, 1, 1, 0, 0),
datetime(1970, 1, 1, 1, 15),
datetime(1970, 1, 1, 2, 30),
datetime(1970, 1, 1, 3, 45),
datetime(1970, 1, 1, 5, 0),
datetime(1970, 1, 1, 6, 15),
datetime(1970, 1, 1, 7, 30),
datetime(1970, 1, 1, 8, 45),
]
# More steps than days
result = common.date_range(date(1970, 1, 1), date(1970, 1, 7), 10)
assert result == [
date(1970, 1, 1),
date(1970, 1, 1),
date(1970, 1, 2),
date(1970, 1, 2),
date(1970, 1, 3),
date(1970, 1, 4),
date(1970, 1, 4),
date(1970, 1, 5),
date(1970, 1, 5),
date(1970, 1, 6),
]
@pytest.mark.parametrize(
'i,expected', [
(1, 1),
(100, 100),
(150, 100),
(-1, -1),
(-1.0, -1.0),
(1.0, 1.0),
(100.0, 100.0),
(150.0, 100.0),
('1', 1),
(None, 20),
(0, 20),
(0.0, 20),
('', 20),
]
)
def test_api_param_limiter(i, expected):
limiter = common.api_param_limiter(100) # should never return an integer greater than 100.
assert limiter(i) == expected
def test_chdir():
"""
The current working directory can be changed temporarily using the `chdir` context manager.
"""
original = os.getcwd()
home = os.environ.get('HOME')
assert home
with common.chdir():
assert os.getcwd() != original
assert is_tempfile(os.getcwd())
assert os.environ['HOME'] != os.getcwd()
# Replace $HOME
with common.chdir(with_home=True):
assert os.getcwd() != original
assert is_tempfile(os.getcwd())
assert os.environ['HOME'] == os.getcwd()
with tempfile.TemporaryDirectory() as d:
# Without replacing $HOME
d = os.path.realpath(d)
with common.chdir(pathlib.Path(d), with_home=True):
assert os.getcwd() == d
assert os.environ['HOME'] == os.getcwd()
with common.chdir(pathlib.Path(d)):
assert os.getcwd() == d
assert os.environ['HOME'] != os.getcwd()
@pytest.mark.parametrize('low,high,expected', [
(0, 10000, [0, 5000, 2500, 7500, 1250, 3750, 6250, 8750, 625, 1875, 3125, 4375, 5625, 6875, 8125, 9375]),
(0, 1000, [0, 500, 250, 750, 125, 375, 625, 875, 62, 187, 312, 437, 562, 687, 812, 937]),
(0, 100, [0, 50, 25, 75, 12, 37, 62, 87, 6, 18, 31, 43, 56, 68, 81, 93]),
# Values are unique - no duplicates after the fix.
(0, 10, [0, 5, 2, 7, 1, 3, 6, 8, 4, 9]),
(0, 5, [0, 2, 1, 3, 4]),
(0, 2, [0, 1]),
(50, 100, [50, 75, 62, 87, 56, 68, 81, 93, 53, 59, 65]),
(8, 98, [8, 53, 30, 75, 19, 41, 64]),
# Floats are supported. Output is identical to above.
(50.0, 100.0, [50.0, 75.0, 62.5, 87.5, 56.25, 68.75, 81.25, 93.75, 53.125, 59.375, 65.625]),
# Decimals are supported. Output is identical to above.
(Decimal('50.0'), Decimal('100.0'), [
Decimal('50.0'), Decimal('75.0'), Decimal('62.5'), Decimal('87.5'), Decimal('56.25'), Decimal('68.75'),
Decimal('81.25'), Decimal('93.75'), Decimal('53.125'), Decimal('59.375'), Decimal('65.625')
]),
# Datetimes are supported.
(datetime(2000, 1, 1, tzinfo=pytz.UTC), datetime(2000, 1, 8, tzinfo=pytz.UTC), [
datetime(2000, 1, 1, tzinfo=pytz.UTC), datetime(2000, 1, 4, 12, tzinfo=pytz.UTC),
datetime(2000, 1, 2, 18, tzinfo=pytz.UTC), datetime(2000, 1, 6, 6, tzinfo=pytz.UTC),
datetime(2000, 1, 1, 21, tzinfo=pytz.UTC), datetime(2000, 1, 3, 15, tzinfo=pytz.UTC),
])
])
def test_zig_zag(low, high, expected):
zagger = common.zig_zag(low, high)
for i in expected:
result = next(zagger)
assert result == i
assert low <= result < high
@pytest.mark.parametrize(
'name,expected', [
('', ''),
('foo', 'foo'),
('foo\\', 'foo'),
('foo/', 'foo⧸'),
('foo<', 'foo'),
('foo>', 'foo'),
('foo:', 'foo'),
('foo|', 'foo'),
('foo"', 'foo'),
('foo?', 'foo'),
('foo*', 'foo'),
('foo&', 'foo&'),
('foo%', 'foo'),
('foo!', 'foo'),
('fo\no', 'foo'),
('fo\ro', 'foo'),
('fo\n\no\n', 'foo'),
('fo\r\ro\r', 'foo'),
('foo ', 'foo'),
('foo ', 'foo'),
# Some whitespace characters are preserved as spaces.
('fo\to', 'fo o'),
('fo\t\to\t', 'fo o'),
('fo o', 'fo o'),
('fo o', 'fo o'),
('fo o', 'fo o'),
('foo | bar', 'foo - bar'),
]
)
def test_escape_file_name(name, expected):
result = common.escape_file_name(name)
if result != expected:
raise AssertionError(f'Escape of {repr(name)}: {repr(result)} != {repr(expected)}')
def test_truncate_object_bytes():
"""
Objects can be truncated (lists will be shortened) so they will fit in the search index.
Exact result lengths depend on the interpreter's object sizes (which changed in
Python 3.12), so assert the contract: the result fits the byte budget and is a
prefix of the input.
"""
for count in (10, 1_000, 1_000_000):
previous_length = 0
for maximum_bytes in (100, 200):
result = common.truncate_object_bytes(['foo'] * count, maximum_bytes)
assert sys.getsizeof(result) < maximum_bytes
# Small lists that already fit are returned whole.
assert 0 < len(result) <= count
assert result == ['foo'] * len(result)
# A larger budget must never retain fewer items.
assert len(result) >= previous_length
previous_length = len(result)
assert common.truncate_object_bytes([], 200) == []
assert common.truncate_object_bytes(None, 100) is None
assert common.truncate_object_bytes('', 100) == ''
previous_length = 301
for maximum_bytes in (99, 80, 55, 51, 50):
result = common.truncate_object_bytes('foo' * 100, maximum_bytes)
assert sys.getsizeof(result) < maximum_bytes
assert len(result) < 300
assert ('foo' * 100).startswith(result)
# A smaller budget must never retain more characters.
assert len(result) <= previous_length
previous_length = len(result)
assert common.truncate_object_bytes('foo' * 100, 0) == ''
def test_truncate_generator_bytes():
"""A generator can be truncated."""
def generator():
for _ in range(5):
yield 'foo'
raise ValueError('Truncate should not get here')
# Exact chunk counts depend on the interpreter's str object size (which changed
# in Python 3.12), so assert the contract: only whole chunks are yielded, all
# chunks before the last fit within the budget, and the generator is never
# consumed past the budget (the ValueError is never raised).
previous_length = 0
for maximum_bytes in (80, 200):
result = list(common.truncate_generator_bytes(generator(), maximum_bytes))
assert result == ['foo'] * len(result)
assert 0 < len(result) <= 5
assert sum(sys.getsizeof(chunk) for chunk in result[:-1]) < maximum_bytes
# A larger budget must never yield fewer chunks.
assert len(result) >= previous_length
previous_length = len(result)
def test_check_media_directory(test_directory):
"""The directory provided by the test_directory fixture is a valid media directory."""
assert common.check_media_directory() is True
def test_bad_check_media_directory():
"""/dev/full is not a valid media directory, warnings are issued and the check fails."""
with mock.patch('wrolpi.common.get_media_directory') as mock_get_media_directory:
mock_get_media_directory.return_value = pathlib.Path('/dev/full')
assert common.check_media_directory() is False
def test_chunks_by_stem(test_directory, make_files_structure):
"""`chunks_by_stem` breaks a list of paths on the name change close to the size."""
with pytest.raises(ValueError):
assert list(common.chunks_by_stem([], 0)) == [[]]
assert list(common.chunks_by_stem([], 5)) == [[]]
assert list(common.chunks_by_stem([1, 2, 3], 5)) == [[1, 2, 3]]
files = make_files_structure([
'foo.mp4', 'foo.txt', 'foo.png', 'foo.info.json',
'bar.mp4', 'bar.readability.txt', 'bar.jpeg', 'bar.info.json',
'baz.mp4', 'baz.txt', 'baz.jpg', 'baz.info.json',
'qux.mp4', 'qux.txt', 'qux.tif', 'qux.info.json',
])
def assert_chunks(size, files_, expected):
for chunk, expected_chunk in zip_longest(list(common.chunks_by_stem(files_, size)), expected):
assert chunk == [test_directory / i for i in expected_chunk]
assert_chunks(8, files, [
['bar.info.json', 'bar.jpeg', 'bar.mp4', 'bar.readability.txt',
'baz.info.json', 'baz.jpg', 'baz.mp4', 'baz.txt',
'foo.info.json', 'foo.mp4', 'foo.png', 'foo.txt'],
['qux.info.json', 'qux.mp4', 'qux.tif', 'qux.txt'],
])
assert_chunks(6, files, [
['bar.info.json', 'bar.jpeg', 'bar.mp4', 'bar.readability.txt',
'baz.info.json', 'baz.jpg', 'baz.mp4', 'baz.txt'],
['foo.info.json', 'foo.mp4', 'foo.png', 'foo.txt',
'qux.info.json', 'qux.mp4', 'qux.tif', 'qux.txt'],
])
assert_chunks(5, files, [
['bar.info.json', 'bar.jpeg', 'bar.mp4', 'bar.readability.txt',
'baz.info.json', 'baz.jpg', 'baz.mp4', 'baz.txt'],
['foo.info.json', 'foo.mp4', 'foo.png', 'foo.txt',
'qux.info.json', 'qux.mp4', 'qux.tif', 'qux.txt'],
])
assert_chunks(3, files, [
['bar.info.json', 'bar.jpeg', 'bar.mp4', 'bar.readability.txt'],
['baz.info.json', 'baz.jpg', 'baz.mp4', 'baz.txt'],
['foo.info.json', 'foo.mp4', 'foo.png', 'foo.txt'],
['qux.info.json', 'qux.mp4', 'qux.tif', 'qux.txt'],
])
assert_chunks(1, files, [
['bar.info.json', 'bar.jpeg', 'bar.mp4', 'bar.readability.txt'],
['baz.info.json', 'baz.jpg', 'baz.mp4', 'baz.txt'],
['foo.info.json', 'foo.mp4', 'foo.png', 'foo.txt'],
['qux.info.json', 'qux.mp4', 'qux.tif', 'qux.txt'],
])
files = make_files_structure(['1.mp4', '1.txt', '2.mp4', '2.txt', '2.png', '3.mp4'])
assert_chunks(1, files, [['1.mp4', '1.txt'], ['2.mp4', '2.png', '2.txt'], ['3.mp4']])
assert_chunks(3, files, [['1.mp4', '1.txt', '2.mp4', '2.png', '2.txt'], ['3.mp4']])
assert_chunks(6, files, [['1.mp4', '1.txt', '2.mp4', '2.png', '2.txt', '3.mp4']])
assert_chunks(20, files, [['1.mp4', '1.txt', '2.mp4', '2.png', '2.txt', '3.mp4']])
# A stem is never split across chunks, even when another stem sorts between its files.
files = make_files_structure(['interleaved/foo.jpg', 'interleaved/foo.tar.gz', 'interleaved/foo.txt'])
assert list(common.chunks_by_stem(files, 1)) == [
[test_directory / 'interleaved/foo.jpg', test_directory / 'interleaved/foo.txt'],
[test_directory / 'interleaved/foo.tar.gz'],
]
@pytest.mark.parametrize(
'value,expected', [
('true', True),
('t', True),
('True', True),
('1', True),
('yes', True),
('y', True),
('no', False),
('n', False),
('0', False),
('False', False),
('false', False),
('f', False),
('other', False),
('trust', False),
('', False),
(None, False),
]
)
def test_truthy_arg(value, expected):
assert wrolpi.vars.truthy_arg(value) is expected, f'{value} != {expected}'
@pytest.mark.asyncio
async def test_cum_timer():
"""`cum_timer` can be used to profile code."""
print_timer()
with cum_timer('test_cum_timer'):
await asyncio.sleep(0.1)
assert TIMERS.get('test_cum_timer')
total, calls = TIMERS['test_cum_timer']
assert total > 0
assert calls == 1
print_timer()
def test_timer():
"""`cum_timer` can be used to profile code."""
with common.timer(name='test_timer'):
time.sleep(0.1)
@pytest.mark.asyncio
@skip_macos
async def test_limit_concurrent_async():
"""`limit_concurrent` can throw an error when the limit is reached."""
@limit_concurrent(1)
async def sleeper():
await asyncio.sleep(1)
# `throw` was not defined.
await asyncio.gather(sleeper(), sleeper())
@limit_concurrent(1, throw=True)
async def sleeper():
await asyncio.sleep(1)
# One is acceptable.
await asyncio.gather(sleeper())
with pytest.raises(ValueError) as e:
# Two will throw.
await asyncio.gather(sleeper(), sleeper())
assert 'concurrent limit' in str(e)
@skip_macos
def test_limit_concurrent_sync():
"""`limit_concurrent` can throw an error when the limit is reached."""
count = multiprocessing.Value('i', 0)
assert count.value == 0
@limit_concurrent(1)
def sleeper():
sleep(1)
count.value += 1
# One is acceptable.
sleeper()
assert count.value == 1
error_value = multiprocessing.Value(ctypes.c_bool)
assert error_value.value is False
def sleeper_wrapper():
try:
sleeper()
except ValueError as e:
error_value.value = 'concurrent limit' in str(e)
def run():
count.value = 0
p1 = multiprocessing.Process(target=sleeper_wrapper)
p2 = multiprocessing.Process(target=sleeper_wrapper)
p1.start()
p2.start()
p1.join()
p2.join()
# `throw` is not defined, only one wrapper runs.
run()
assert error_value.value is False
# Only one counted.
assert count.value == 1
@limit_concurrent(1, throw=True)
def sleeper():
sleep(1)
count.value += 1
error_value.value = False
assert error_value.value is False
# Error was thrown.
run()
assert error_value.value is True
assert count.value == 1
@pytest.mark.asyncio
async def test_run_after():
"""`run_after` wrapper will run a function asynchronously after the wrapped function completes."""
count = multiprocessing.Value(ctypes.c_int, 0)
def counter():
count.value += 1
with mock.patch('wrolpi.common.RUN_AFTER', True):
@run_after(counter)
async def foo():
await asyncio.sleep(0)
return 'yup'
# Test async wrapped and after.
assert await foo() == 'yup', 'Did not get the returned value'
# Sleep so "after" will run.
await asyncio.sleep(0)
assert count.value == 1, 'Counter did not run after'
with mock.patch('wrolpi.common.RUN_AFTER', True):
@run_after(counter)
def foo():
return 'good'
# Test sync wrapped and after.
assert foo() == 'good', 'Did not get the returned value'
# Sleep so "after" will run.
await asyncio.sleep(0)
assert count.value == 2, 'Counter did not run after'
@pytest.mark.parametrize(
'max_line_length,text,expected', [
(38, '', ''),
(38, 'Lorem', 'Lorem'),
(
38,
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do',
'Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit, sed do'
),
(
10,
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do',
'Lorem\nipsum\ndolor sit\namet,\nconsectetur\nadipiscing\nelit, sed\ndo'
),
(
20,
'Lorem\nipsum\ndolor sit amet,\t\tconsectetur\n\nadipiscing elit, sed do',
'Lorem\nipsum\ndolor sit amet,\nconsectetur\n\nadipiscing elit,\nsed do'
),
(
38,
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
'Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit, sed do\neiusmod tempor incididunt ut labore et\ndolore magna aliqua.'
),
(38, 'long-word-which-should-not-be-split-up', 'long-word-which-should-not-be-split-up'),
]
)
def test_split_lines_by_length(max_line_length, text, expected):
assert common.split_lines_by_length(text, max_line_length=max_line_length) == expected
@pytest.mark.parametrize(
'obj,expected', [
([0, ], [0, ]),
(range(1), [0, ]),
({'a': 'b'}, {'a': 'b'}),
({'a': range(1, 3)}, {'a': [1, 2]}),
((1, 2, 3), (1, 2, 3)),
({3, 4, 5}, {3, 4, 5}),
]
)
def test_resolve_generators(obj, expected):
assert common.resolve_generators(obj) == expected
def test_resolve_generators2():
def gen2():
yield {'hello': 'world'}
yield {'this': 'that'}
def gen1():
yield 'hello'
yield {
'entries': gen2(),
}
obj = {
'foo': 'bar',
'entries': gen1(),
}
assert common.resolve_generators(obj) == {
'foo': 'bar',
'entries': [
'hello',
{
'entries': [
{'hello': 'world'},
{'this': 'that'},
]
}
]
}
@pytest.mark.parametrize(
'url,expected', [
('https://example.com', '/'),
('https://example.com/foo', '/foo'),
('https://example.com/foo?question=answer', '/foo?question=answer'),
('https://example.com/foo?question=answer#fragment', '/foo?question=answer#fragment'),
('https://exmaple.com/files?folders=test+%23+example%2F', '/files?folders=test+%23+example%2F'),
('https://exmaple.com/files?folders=test+%23+example%2F&folders=map%2F',
'/files?folders=test+%23+example%2F&folders=map%2F'),
]
)
def test_url_strip_host(url, expected):
assert common.url_strip_host(url) == expected
@pytest.mark.parametrize(
'search_str,expected', [
# Matches are highlighted (stemmed: jump matches jumps/jumped); non-matches return the
# start of the entry with a rank of 0.
('jump', [('The fox <b>jumps</b> over the lazy brown dog.', True),
('The fox <b>jumped</b> over the lazy dog.', True)]),
('brown', [('The fox jumps over the lazy <b>brown</b> dog.', True),
('The fox jumped over the lazy dog.', False)]),
]
)
def test_extract_headlines(test_session, search_str, expected):
entries = [
'The fox jumps over the lazy brown dog.',
'The fox jumped over the lazy dog.',
]
results = common.extract_headlines(entries, search_str)
# Compare the headline text, and whether the entry matched (exact ranks are engine noise).
assert [(headline, rank > 0) for headline, rank in results] == expected
def test_extract_html_text():
html = '''<html>
<script>
// This should be ignored.
console.log('hello');
</script>
<body>
<h1>Header</h1>
<p>Word word word word word word word word word</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<p>Another example paragraph</p>
Paragraph outside an element.
</body>
</html>'''
assert common.extract_html_text(html) == '''Header
Word word word word word word word word word
Item 1
Item 2
Another example paragraph
Paragraph outside an element.'''
def test_format_json_file():
with tempfile.TemporaryDirectory() as d:
with tempfile.NamedTemporaryFile(suffix='.json', dir=d) as fh:
file = pathlib.Path(fh.name)
file.write_text(json.dumps({
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9,
'ten': 10,
}))
# Only one file was created.
assert len(list(file.parent.iterdir())) == 1
unformatted = '{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "seven": 7, "eight": 8, "nine": 9, "ten": 10}'
assert file.read_text() == unformatted
common.format_json_file(file)
# Keys are sorted.
assert file.read_text() == '''{
"eight": 8,
"five": 5,
"four": 4,
"nine": 9,
"one": 1,
"seven": 7,
"six": 6,
"ten": 10,
"three": 3,
"two": 2
}'''
# The copy was deleted.
assert len(list(file.parent.iterdir())) == 1
@pytest.mark.parametrize(
'iterable,length,step,expected',
[
([1, 2, 3, 4], 1, None, [[1], [2], [3], [4]]),
([1, 2, 3, 4], 2, None, [[1, 2], [2, 3], [3, 4]]),
([1, 2, 3, 4], 3, None, [[1, 2, 3], [2, 3, 4]]),
# More length than items yield the original iterable.
([1, 2, 3, 4], 5, None, [[1, 2, 3, 4], ]),
# Can handle empty lists.
([], 2, None, []),
# Type inputted matches output.
((1, 2, 3, 4), 2, None, [(1, 2), (2, 3), (3, 4)]),
]
)
def test_chain(iterable, length, step, expected):
assert list(common.chain(iterable, length)) == expected
def test_config_backup(async_client, test_wrolpi_config, test_directory):
"""Configs have a backup each day."""
config = common.get_wrolpi_config()
# No backups yet.
config.dump_config()
assert (test_directory / 'config').is_dir()
assert not (test_directory / 'config/backup').is_dir()
assert (test_directory / 'config/wrolpi.yaml').is_file()
# Backups directory is created. New backup config is saved.
config.dump_config()
assert (test_directory / 'config').is_dir()
assert (test_directory / 'config/backup').is_dir()
assert (test_directory / 'config/wrolpi.yaml').is_file()
backup_file, = list((test_directory / 'config/backup').glob('*'))
assert re.match(r'wrolpi-\d{8}\.yaml', backup_file.name), \
f'{backup_file.name} is not in the expected format'
@pytest.mark.asyncio
@skip_ci
async def test_aiohttp(simple_file_server):
"""Test that aiohttp convenience functions work."""
host, port = simple_file_server.server_address
url = f'http://{host}:{port}' # noqa
async with common.aiohttp_get(url) as response:
assert response.content.read()
assert response.status == 200
async with common.aiohttp_head(url) as response:
assert response.content.read()
assert response.status == 200
def test_can_connect_to_server(simple_file_server):
host, port = simple_file_server.server_address
assert common.can_connect_to_server(host, port)
def test_can_connect_to_server_times_out(monkeypatch):
"""A server that never answers (a firewall that drops the connection) gives up after the timeout, instead of the
system's default of minutes."""
timeouts = []
def create_connection(address, timeout=None):
timeouts.append(timeout)
raise socket.timeout()
monkeypatch.setattr(common.socket, 'create_connection', create_connection)
assert common.can_connect_to_server('example.com', timeout=2) is False
assert common.can_connect_to_server('example.com') is False
assert timeouts == [2, common.INTERNET_CHECK_TIMEOUT]
@pytest.mark.asyncio
async def test_have_internet_connection(async_client, test_wrolpi_config, monkeypatch):
"""The Internet is up when wrolpi.yaml's IP address or its hostname answers. The IP address is tried first: the
hostname needs DNS, which the connection's timeout does not bound."""
answering = set()
checked = []
def can_connect_to_server(hostname, port=80, timeout=None):
checked.append(hostname)
return hostname in answering
monkeypatch.setattr(common, 'can_connect_to_server', can_connect_to_server)
config = get_wrolpi_config()
assert (config.internet_check_hostname, config.internet_check_ip) == ('one.one.one.one', '1.1.1.1')
config.internet_check_hostname = 'internet.example.com'
config.internet_check_ip = '192.0.2.1'
assert common.have_internet_connection() is False
assert checked == ['192.0.2.1', 'internet.example.com']
# A working IP address answers without a name lookup.
answering.add('192.0.2.1')
checked.clear()
assert common.have_internet_connection() is True
assert checked == ['192.0.2.1']
# Only the hostname is checked when there is no IP address.
config.internet_check_ip = None
checked.clear()
answering.add('internet.example.com')
assert common.have_internet_connection() is True
assert checked == ['internet.example.com']
@pytest.mark.asyncio
async def test_log_level(async_client, test_wrolpi_config):
"""User can change API log level."""
from wrolpi.api_utils import api_app
try:
# Default log level is INFO.
assert api_app.shared_ctx.log_level.value == 20
# Change log level to WARNING.
body = dict(log_level=30)
request, response = await async_client.patch('/api/settings', json=body)
assert response.status_code == HTTPStatus.NO_CONTENT, response.json
assert api_app.shared_ctx.log_level.value == 30
# Change log level to TRACE.
body = dict(log_level=5)
request, response = await async_client.patch('/api/settings', json=body)
assert response.status_code == HTTPStatus.NO_CONTENT, response.json
assert api_app.shared_ctx.log_level.value == 5 == TRACE_LEVEL
finally:
# Reset log level to debug.
body = dict(log_level=10)
request, response = await async_client.patch('/api/settings', json=body)
assert response.status_code == HTTPStatus.NO_CONTENT, response.json
assert api_app.shared_ctx.log_level.value == 10
@skip_macos
@skip_ci
def test_html_screenshot(singlefile_contents_factory):
assert common.html_screenshot(singlefile_contents_factory())
def test_html_screenshot_webdriver_kwargs(singlefile_contents_factory):
"""Selenium 4.10 removed the `chrome_options` kwarg; `webdriver.Chrome` must be called with `options`.
The system chromedriver must be passed explicitly because Selenium Manager downloads drivers from the
internet, and has no binary for linux/aarch64 (Raspberry Pi)."""
contents = singlefile_contents_factory()
with mock.patch('wrolpi.common.webdriver.Chrome') as mock_chrome, \
mock.patch('wrolpi.cmd.CHROMEDRIVER_BIN', pathlib.Path('/usr/bin/chromedriver')):
driver = mock_chrome.return_value.__enter__.return_value
driver.get_screenshot_as_png.return_value = b'PNG'
assert common.html_screenshot(contents) == b'PNG'
assert mock_chrome.call_args.kwargs.keys() == {'options', 'service'}
assert isinstance(mock_chrome.call_args.kwargs['options'], common.webdriver.ChromeOptions)
assert mock_chrome.call_args.kwargs['service'].path == '/usr/bin/chromedriver'
# Without a system chromedriver (development machines), fall back to Selenium Manager.
with mock.patch('wrolpi.common.webdriver.Chrome') as mock_chrome, \
mock.patch('wrolpi.cmd.CHROMEDRIVER_BIN', None):
driver = mock_chrome.return_value.__enter__.return_value
driver.get_screenshot_as_png.return_value = b'PNG'
assert common.html_screenshot(contents) == b'PNG'
assert mock_chrome.call_args.kwargs['service'] is None
@pytest.mark.parametrize('color,expected', [
('#ffffff', True),
('#FFFFFF', True),
('#fff', True),
('#FFf', True),
('#asdf', False),
('#ffff', False),
])
def test_is_valid_hex_color(color, expected):
assert common.is_valid_hex_color(color) == expected
@pytest.mark.parametrize('iterable,predicate,expected', [
((1, 2, 2, 3, 4), lambda i: i, (1, 2, 3, 4)),
([1, 1, 2, 3, 3, 3, 3, 4], None, [1, 2, 3, 4]),
([1, 1, 2, 2, 3, 4, 5, 6, 7, 8], lambda i: i % 2, [1, 2]),
(['apple', 'banana', 'pear', 'kiwi', 'plum', 'peach'], lambda i: len(i), ['apple', 'banana', 'pear']),
(
[pathlib.Path('foo.txt'), pathlib.Path('foo.mp4'), pathlib.Path('bar.txt')],
lambda i: i.stem,
[pathlib.Path('foo.txt'), pathlib.Path('bar.txt')]
),
])
def test_unique_by_predicate(iterable, predicate, expected):
assert common.unique_by_predicate(iterable, predicate) == expected
@pytest.mark.asyncio
async def test_config_lifecycle(await_switches, test_wrolpi_config):
"""Test importing, dumping, updating, saving a config."""
config = get_wrolpi_config()
# Config is not imported, default values are used.
assert config.successful_import is False
assert config.wrol_mode is False
assert config.is_valid() is None
assert config.version == 0
assert config.zims_destination
# Bad config cannot be imported.
test_wrolpi_config.parent.mkdir(exist_ok=True)
test_wrolpi_config.write_text('bad config\ndata')
assert config.is_valid() is False
with pytest.raises(InvalidConfig) as e:
config.import_config()
assert config.successful_import is False
assert config.version == 0
assert 'invalid' in str(e)
assert config.zims_destination
# Cannot dump config because import was not successful.
with pytest.raises(RuntimeError) as e:
config.dump_config()
assert 'imported' in str(e)
assert config.version == 0
# Incomplete config can still be imported. "destinations" cannot be empty
test_wrolpi_config.write_text('wrol_mode: true\nzims_destination: null')
assert config.is_valid() is True
config.import_config()
assert config.successful_import is True
assert config.wrol_mode is True
assert config.is_valid() is True
assert config.version == 0
# Default destination was used instead.
assert config.zims_destination
# Items from the default config are written to the file.
config.dump_config()
assert 'version: ' in test_wrolpi_config.read_text()
assert config.wrol_mode is True
assert config.is_valid() is True
assert config.version == 1
# Other data is ignored.
test_wrolpi_config.write_text('wrol_mode: false\nother_data: false')
assert config.is_valid() is True
config.import_config()
assert 'other_data' not in config._config, 'Extra data in config should be ignored'
assert config.is_valid() is True
assert config.wrol_mode is False
# Changing value changes both file and config data.
config.wrol_mode = True
assert config._config['wrol_mode'] is True
await await_switches() # `wrol_mode = True` calls background switch
assert 'wrol_mode: true' in test_wrolpi_config.read_text()
assert 'other_data' not in test_wrolpi_config.read_text(), 'Other data should be removed on save.'
assert config.is_valid() is True
assert config.version == 2
# Cannot overwrite newer config.
config_data = config.read_config_file()
config_data['version'] = 5
config.write_config_data(config_data, test_wrolpi_config)
with pytest.raises(RuntimeError) as e:
config.dump_config()
assert 'newer config' in str(e)
# Can import, then config can be saved.
config.import_config()
config.dump_config()
assert config.is_valid() is True
@pytest.mark.asyncio
async def test_config_valid(async_client, test_wrolpi_config):
# Config file does not yet exist.
config = get_wrolpi_config()
assert config.is_valid() is None
config.dump_config()
assert config.is_valid() is True
test_wrolpi_config.write_text('bad config')
assert config.is_valid() is False
# Does not require all items.
test_wrolpi_config.write_text('wrol_mode: true')
assert config.is_valid() is True
@pytest.mark.asyncio
async def test_write_config_data_failure_preserves_existing_file(async_client, test_wrolpi_config):
"""A failed config write must leave the existing config file untouched, and no write may leave
a temporary file behind."""
def temporary_leftovers() -> list:
return [i for i in test_wrolpi_config.parent.iterdir()
if i.name.startswith(test_wrolpi_config.name) and i.name != test_wrolpi_config.name]
test_wrolpi_config.write_text('version: 5\nwrol_mode: true\n')
# `object()` cannot be serialized; the key sorts last so YAML would emit partial output first.
with pytest.raises(ValueError):
common.write_config_data({'version': 6, 'zzz': object()}, test_wrolpi_config)
# The original file survives the failed write, and no temporary file is left behind.
assert common.read_config_data(test_wrolpi_config) == {'version': 5, 'wrol_mode': True}
assert not temporary_leftovers()
# A successful write replaces the contents and leaves no temporary file behind.
common.write_config_data({'version': 6}, test_wrolpi_config)
assert common.read_config_data(test_wrolpi_config) == {'version': 6}
assert not temporary_leftovers()
@pytest.mark.asyncio
async def test_save_version_check_runs_under_config_save_lock(async_client, test_wrolpi_config):
"""`ConfigFile.save` must read the config file for its version check only while holding
`config_save_lock`, otherwise it can read a config another process is writing."""
from wrolpi.api_utils import api_app
config = get_wrolpi_config()
config.dump_config()
class RecordingLock:
def __init__(self, lock):
self.lock = lock
self.held = False
def __enter__(self):
self.lock.__enter__()
self.held = True
def __exit__(self, *args):
self.held = False
return self.lock.__exit__(*args)
recording_lock = RecordingLock(api_app.shared_ctx.config_save_lock)
lock_held_during_read = list()
real_read_config_file = config.read_config_file
def recording_read_config_file(*args, **kwargs):
lock_held_during_read.append(recording_lock.held)
return real_read_config_file(*args, **kwargs)
with mock.patch.object(api_app.shared_ctx, 'config_save_lock', recording_lock), \
mock.patch.object(config, 'read_config_file', recording_read_config_file):
config.dump_config()
assert lock_held_during_read, 'The version check never read the config file'
assert all(lock_held_during_read), 'The config file was read without holding config_save_lock'
@pytest.mark.parametrize('name,expected_name', [
('foo', 'foo'),
(pathlib.Path('foo'), pathlib.Path('foo')),
# Short filenames with parents (no trimming needed) should preserve parent directories.
('some/parent/short.txt', 'some/parent/short.txt'),
('/absolute/parent/short.txt', '/absolute/parent/short.txt'),
(pathlib.Path('/some/parent/short.txt'), pathlib.Path('/some/parent/short.txt')),
# Long ASCII filenames are trimmed to 200 BYTES (not characters)
(
'this name is toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo long.txt',
'this name is toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.txt'
),
(
'some/parent/is/not/trimmed/this name is toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo long.info.json',
'some/parent/is/not/trimmed/this name is toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.info.json'
),
(
'/absolute/parent/is/not/trimmed/this name is toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo long.info.json',
'/absolute/parent/is/not/trimmed/this name is toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.info.json'
),
(
pathlib.Path(
'/some/parent/is/not/trimmed/this name is toooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo long.readability.json'),
pathlib.Path(
'/some/parent/is/not/trimmed/this name is tooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo.readability.json')
),
])
def test_trim_file_name(name, expected_name):
assert common.trim_file_name(name) == expected_name
def test_trim_file_name_utf8_bytes():
"""UTF-8 multi-byte characters must be trimmed by BYTE length, not character length.
Linux ext4/xfs filesystems have a 255 BYTE limit, not a 255 character limit.
Hindi/Devanagari characters are 3 bytes each in UTF-8, so a 121-character filename
can be 263 bytes, exceeding the limit.
This test reproduces the bug from: https://decay.local reorganization failure
"""
# This filename has 133 characters but 291 bytes - exceeds 255 byte limit
hindi_filename = '2022-04-13-19-40-51_भारतीय संस्कृति और परंपराएं बहुत समृद्ध हैं हमारे देश में विविधता में एकता है यह हमारी विरासत है.readability.html'
assert len(hindi_filename) == 133 # Characters
assert len(hindi_filename.encode('utf-8')) == 291 # Bytes - exceeds 255!
result = common.trim_file_name(hindi_filename)
# Result must fit within filesystem byte limit (200 bytes leaves room for suffix variations)
max_bytes = 200
result_bytes = len(result.encode('utf-8'))
assert result_bytes <= max_bytes, \
f'Trimmed filename is {result_bytes} bytes, exceeds {max_bytes} byte limit'
# Must preserve the suffix
assert result.endswith('.readability.html'), f'Suffix not preserved: {result}'
async def test_cached_multiprocessing_result(async_client):
call_count = 0
@common.cached_multiprocessing_result
async def func(inc: int) -> int:
nonlocal call_count
call_count += 1
return inc + call_count
# First call, math is done.
assert await func(1) == 2
# Second call is cached.
assert await func(1) == 2
assert await func(1) == 2
# New args, math is done again.
assert await func(2) == 4
assert await func(2) == 4
# Kwargs are supported
assert await func(inc=1) == 4
assert await func(inc=1) == 4
@pytest.mark.asyncio
async def test_initialize_config_files(async_client, test_session, test_directory, test_wrolpi_config,
test_channels_config, test_tags_config, test_videos_downloader_config,
test_download_manager_config, simple_channel, tag_factory,
test_download_manager, test_downloader):
"""Config files should only be created if they are missing, and they will not overwrite existing entries in the DB."""
from modules.videos.lib import get_videos_downloader_config, get_channels_config
from modules.archive.lib import get_archive_downloader_config
from wrolpi.tags import get_tags_config
from wrolpi.downloader import get_download_manager_config
# Create some DB entries so the configs will not be imported.
tag1, tag2 = await tag_factory(), await tag_factory()
download = test_download_manager.create_download(test_session, 'https://example.com',
downloader_name=test_downloader.name)
test_session.commit()
# Write some config files, they should not be re-created.
get_wrolpi_config().get_file().write_text('wrolpi')
get_videos_downloader_config().get_file().write_text('video downloader')
get_archive_downloader_config().get_file().write_text('archive downloader')
assert common.create_empty_config_files() == [], 'No configs should have been created.'
# Config files were not overwritten with real configs.
assert get_wrolpi_config().get_file().read_text() == 'wrolpi'
assert get_videos_downloader_config().get_file().read_text() == 'video downloader'
assert get_archive_downloader_config().get_file().read_text() == 'archive downloader'
# WROLPi config can be created because it is now missing.
get_wrolpi_config().get_file().unlink()
assert common.create_empty_config_files() == ['wrolpi.yaml'], 'WROLPi config should have been created.'
assert get_wrolpi_config().is_valid()
# Videos Downloader config can be created because it is now missing.
get_videos_downloader_config().get_file().unlink()
assert common.create_empty_config_files() == ['videos_downloader.yaml'], \
'Videos Downloader config should have been created.'
assert get_videos_downloader_config().is_valid()
# Archive Downloader config can be created because it is now missing.
get_archive_downloader_config().get_file().unlink()
assert common.create_empty_config_files() == ['archives_downloader.yaml'], \
'Archive Downloader config should have been created.'
assert get_archive_downloader_config().is_valid()
# Channels config file does not exist, no conflicting Channels so the file is created.
simple_channel.delete_with_videos()
assert common.create_empty_config_files() == ['channels.yaml'], 'Channels config should have been created.'
assert get_channels_config().is_valid()
tag1.delete()
tag2.delete()
assert common.create_empty_config_files() == ['tags.yaml'], 'Tags config should have been created.'
assert get_tags_config().is_valid()
download.delete()
assert common.create_empty_config_files() == ['download_manager.yaml'], \
'Download Manager config should have been created.'
assert get_download_manager_config().is_valid()
# NOTE: Inventory is config-only (no DB) and is no longer created by create_empty_config_files().
def test_get_paths_in_parent_directory(test_directory, make_files_structure):
make_files_structure([
'foo/bar.txt',
'foo/bar/baz.txt',
'qux.txt',
])
paths = list(common.walk(test_directory))
assert set(common.get_paths_in_parent_directory(paths, test_directory / 'foo')) == {
test_directory / 'foo/bar.txt',
test_directory / 'foo/bar',
test_directory / 'foo/bar/baz.txt',
}
assert set(common.get_paths_in_parent_directory(paths, test_directory / 'foo/bar')) == {
test_directory / 'foo/bar/baz.txt',
}
assert set(common.get_paths_in_parent_directory(paths, test_directory)) == {
test_directory / 'qux.txt',
test_directory / 'foo',
test_directory / 'foo/bar.txt',
test_directory / 'foo/bar',
test_directory / 'foo/bar/baz.txt',
}
assert set(common.get_paths_in_media_directory(paths)) == {
test_directory / 'qux.txt',
test_directory / 'foo',
test_directory / 'foo/bar.txt',
test_directory / 'foo/bar',
test_directory / 'foo/bar/baz.txt',
}
def test_get_all_configs_includes_all_config_subclasses():
"""Every ConfigFile subclass must be included in get_all_configs so it is imported on startup.
If a ConfigFile subclass is not in get_all_configs, it will never have successful_import set to True and will fail
to save with: RuntimeError: Refusing to save config because it was never successfully imported!
"""
from wrolpi.common import ConfigFile, get_all_configs
# Ensure all ConfigFile subclasses are imported.
import modules.archive.lib # noqa
import modules.inventory.common # noqa
import modules.videos.lib # noqa
import wrolpi.collections.config # noqa
import wrolpi.downloader # noqa
import wrolpi.tags # noqa
# Temporarily disable PYTEST guard so get_channels_config() returns its global instance.
with mock.patch('modules.videos.lib.PYTEST', False):
all_configs = get_all_configs()
returned_types = {type(config) for config in all_configs.values()}
all_subclasses = set(ConfigFile.__subclasses__())
missing = all_subclasses - returned_types
assert not missing, (
f'ConfigFile subclasses not in get_all_configs(): {[c.__name__ for c in missing]}. '
f'Add their getter to get_all_configs() so they are imported on startup.'
)
@pytest.fixture
def gpg_test_key(tmp_path):
"""Generate a GPG test keypair with a short GNUPGHOME path. Yields (env, pubkey_path, tmp_path)."""
# GPG agent sockets have a ~108 char path limit; use a short temp dir.
short_tmp = pathlib.Path(tempfile.mkdtemp(prefix='gpg'))
gnupghome = short_tmp / 'gh'
gnupghome.mkdir(mode=0o700)
env = {**os.environ, 'GNUPGHOME': str(gnupghome)}
# Configure and launch gpg-agent to allow loopback pinentry.
(gnupghome / 'gpg-agent.conf').write_text('allow-loopback-pinentry\n')
subprocess.run(['gpgconf', '--homedir', str(gnupghome), '--launch', 'gpg-agent'], env=env, check=True)
subprocess.run(
['gpg', '--batch', '--quiet', '--passphrase', '', '--pinentry-mode', 'loopback',
'--quick-gen-key', 'test@test.com', 'default', 'default'],
env=env, check=True,
)
result = subprocess.run(
['gpg', '--batch', '--armor', '--export', 'test@test.com'],
env=env, capture_output=True, check=True,
)
test_pubkey = short_tmp / 'test.gpg'
test_pubkey.write_bytes(result.stdout)
yield env, test_pubkey, short_tmp
# Kill the agent and clean up.
subprocess.run(['gpgconf', '--homedir', str(gnupghome), '--kill', 'gpg-agent'], env=env)
shutil.rmtree(short_tmp, ignore_errors=True)
def _gpg_sign_file(data_path, sig_path, env):
"""Helper: create a detached GPG signature for a file."""
subprocess.run(
['gpg', '--batch', '--quiet', '--passphrase', '', '--pinentry-mode', 'loopback',
'--detach-sign', '--armor', '-o', str(sig_path), str(data_path)],
env=env, check=True,
)
@pytest.mark.asyncio
async def test_verify_gpg_signature_valid(tmp_path, gpg_test_key):
"""A file signed with the shipped GPG key should verify successfully."""
from wrolpi.common import verify_gpg_signature
env, test_pubkey, _ = gpg_test_key
data_file = tmp_path / 'data.txt'
data_file.write_text('hello world')
sig_file = tmp_path / 'data.txt.sig'
_gpg_sign_file(data_file, sig_file, env)
with mock.patch('wrolpi.common.GPG_PUBLIC_KEY', test_pubkey):
assert await verify_gpg_signature(data_file, sig_file) is True
@pytest.mark.asyncio
async def test_verify_gpg_signature_tampered(tmp_path, gpg_test_key):
"""A file whose contents don't match the signature should fail verification."""
from wrolpi.common import verify_gpg_signature
env, test_pubkey, _ = gpg_test_key
data_file = tmp_path / 'data.txt'
data_file.write_text('hello world')
sig_file = tmp_path / 'data.txt.sig'
_gpg_sign_file(data_file, sig_file, env)
# Tamper with the file after signing.
data_file.write_text('tampered content')
with mock.patch('wrolpi.common.GPG_PUBLIC_KEY', test_pubkey):
assert await verify_gpg_signature(data_file, sig_file) is False
@pytest.mark.asyncio
async def test_cancel_background_tasks_does_not_raise_for_cancelled_children():
"""Cancelling children raises CancelledError (a BaseException) from `gather` unless exceptions are collected;
that aborted the shutdown of the process calling this."""
from wrolpi.common import background_task, cancel_background_tasks, BACKGROUND_TASKS
tasks = [background_task(asyncio.sleep(3600)) for _ in range(3)]
await asyncio.sleep(0)
assert all(t in BACKGROUND_TASKS for t in tasks)
await cancel_background_tasks()
assert all(t.cancelled() for t in tasks)
assert not any(t in BACKGROUND_TASKS for t in tasks)
@pytest.mark.asyncio
async def test_wrol_mode_flag_file(test_directory, test_wrolpi_config, wrol_mode_fixture):
"""The Controller (and the VPN tunnel it runs) cannot read the API's config in memory; WROL Mode is shared with it
through config/.wrol_mode."""
flag = test_directory / 'config/.wrol_mode'
assert not flag.exists()
await wrol_mode_fixture(True)
assert flag.is_file()
await wrol_mode_fixture(False)
assert not flag.exists()
@pytest.mark.asyncio
async def test_wrol_mode_flag_file_follows_import(test_directory, test_wrolpi_config):
"""A hand-edited wrolpi.yaml turns the flag on or off when it is imported."""
flag = test_directory / 'config/.wrol_mode'
test_wrolpi_config.parent.mkdir(parents=True, exist_ok=True)
test_wrolpi_config.write_text('wrol_mode: true\n')
get_wrolpi_config().import_config()
assert flag.is_file()
test_wrolpi_config.write_text('wrol_mode: false\n')
get_wrolpi_config().import_config()
assert not flag.exists()
def test_wrolpi_config_is_never_searched_for(test_directory, test_wrolpi_config, make_files_structure):
"""wrolpi.yaml is only ever config/wrolpi.yaml. A wrolpi.yaml elsewhere in the library (from a torrent, a
download, or a git repo; even in a hidden directory) is a file like any other, never the config."""
config_directory = test_directory / 'config'
config_directory.joinpath('wrolpi.yaml').unlink(missing_ok=True)
make_files_structure({
'torrents/.1/wrolpi.yaml': 'wrol_mode: true\nallow_database_query: true\n',
'wrolpi.yaml': 'wrol_mode: true\n',
})
with mock.patch.object(common, 'PYTEST', False), mock.patch.object(common, 'CONFIG_DIR', config_directory):
assert common.get_wrolpi_config().get_file() == config_directory / 'wrolpi.yaml'