feat: Enhanced anime add flow with sanitized folders and targeted scan

- Add sanitize_folder_name utility for filesystem-safe folder names
- Add sanitized_folder property to Serie entity
- Update SerieList.add() to use sanitized display names for folders
- Add scan_single_series() method for targeted episode scanning
- Enhance add_series endpoint: DB save -> folder create -> targeted scan
- Update response to include missing_episodes and total_missing
- Add comprehensive unit tests for new functionality
- Update API tests with proper mock support
This commit is contained in:
2025-12-26 12:49:23 +01:00
parent f28dc756c5
commit 1b7ca7b4da
11 changed files with 1370 additions and 146 deletions

View File

@@ -0,0 +1,295 @@
"""
Unit tests for filesystem utilities.
Tests the sanitize_folder_name function and related filesystem utilities.
"""
import os
import tempfile
import pytest
from src.server.utils.filesystem import (
MAX_FOLDER_NAME_LENGTH,
create_safe_folder,
is_safe_path,
sanitize_folder_name,
)
class TestSanitizeFolderName:
"""Test sanitize_folder_name function."""
def test_simple_name(self):
"""Test sanitizing a simple name with no special characters."""
assert sanitize_folder_name("Attack on Titan") == "Attack on Titan"
def test_name_with_colon(self):
"""Test sanitizing name with colon."""
result = sanitize_folder_name("Attack on Titan: Final Season")
assert ":" not in result
assert result == "Attack on Titan Final Season"
def test_name_with_question_mark(self):
"""Test sanitizing name with question mark."""
result = sanitize_folder_name("What If...?")
assert "?" not in result
# Trailing dots are stripped
assert result == "What If"
def test_name_with_multiple_special_chars(self):
"""Test sanitizing name with multiple special characters."""
result = sanitize_folder_name('Test: "Episode" <1> | Part?')
# All invalid chars should be removed
assert ":" not in result
assert '"' not in result
assert "<" not in result
assert ">" not in result
assert "|" not in result
assert "?" not in result
def test_name_with_forward_slash(self):
"""Test sanitizing name with forward slash."""
result = sanitize_folder_name("Attack/Titan")
assert "/" not in result
def test_name_with_backslash(self):
"""Test sanitizing name with backslash."""
result = sanitize_folder_name("Attack\\Titan")
assert "\\" not in result
def test_unicode_characters_preserved(self):
"""Test that Unicode characters are preserved."""
# Japanese title
result = sanitize_folder_name("進撃の巨人")
assert result == "進撃の巨人"
def test_mixed_unicode_and_special(self):
"""Test mixed Unicode and special characters."""
result = sanitize_folder_name("Re:ゼロ")
assert ":" not in result
assert "ゼロ" in result
def test_leading_dots_removed(self):
"""Test that leading dots are removed."""
result = sanitize_folder_name("...Hidden Folder")
assert not result.startswith(".")
def test_trailing_dots_removed(self):
"""Test that trailing dots are removed."""
result = sanitize_folder_name("Folder Name...")
assert not result.endswith(".")
def test_leading_spaces_removed(self):
"""Test that leading spaces are removed."""
result = sanitize_folder_name(" Attack on Titan")
assert result == "Attack on Titan"
def test_trailing_spaces_removed(self):
"""Test that trailing spaces are removed."""
result = sanitize_folder_name("Attack on Titan ")
assert result == "Attack on Titan"
def test_multiple_spaces_collapsed(self):
"""Test that multiple consecutive spaces are collapsed."""
result = sanitize_folder_name("Attack on Titan")
assert result == "Attack on Titan"
def test_null_byte_removed(self):
"""Test that null byte is removed."""
result = sanitize_folder_name("Attack\x00Titan")
assert "\x00" not in result
def test_newline_removed(self):
"""Test that newline is removed."""
result = sanitize_folder_name("Attack\nTitan")
assert "\n" not in result
def test_tab_removed(self):
"""Test that tab is removed."""
result = sanitize_folder_name("Attack\tTitan")
assert "\t" not in result
def test_none_raises_error(self):
"""Test that None raises ValueError."""
with pytest.raises(ValueError, match="cannot be None"):
sanitize_folder_name(None)
def test_empty_string_raises_error(self):
"""Test that empty string raises ValueError."""
with pytest.raises(ValueError, match="cannot be empty"):
sanitize_folder_name("")
def test_whitespace_only_raises_error(self):
"""Test that whitespace-only string raises ValueError."""
with pytest.raises(ValueError, match="cannot be empty"):
sanitize_folder_name(" ")
def test_only_invalid_chars_raises_error(self):
"""Test that string with only invalid characters raises ValueError."""
with pytest.raises(ValueError, match="only invalid characters"):
sanitize_folder_name("???:::***")
def test_max_length_truncation(self):
"""Test that long names are truncated."""
long_name = "A" * 300
result = sanitize_folder_name(long_name)
assert len(result) <= MAX_FOLDER_NAME_LENGTH
def test_max_length_custom(self):
"""Test custom max length."""
result = sanitize_folder_name("Attack on Titan", max_length=10)
assert len(result) <= 10
def test_truncation_at_word_boundary(self):
"""Test that truncation happens at word boundary when possible."""
result = sanitize_folder_name(
"The Very Long Anime Title That Needs Truncation",
max_length=25
)
# Should truncate at word boundary
assert len(result) <= 25
assert not result.endswith(" ")
def test_custom_replacement_character(self):
"""Test custom replacement character."""
result = sanitize_folder_name("Test:Name", replacement="_")
assert ":" not in result
assert "Test_Name" == result
def test_asterisk_removed(self):
"""Test that asterisk is removed."""
result = sanitize_folder_name("Attack*Titan")
assert "*" not in result
def test_pipe_removed(self):
"""Test that pipe is removed."""
result = sanitize_folder_name("Attack|Titan")
assert "|" not in result
def test_real_anime_titles(self):
"""Test real anime titles with special characters."""
# Test that invalid filesystem characters are removed
# Note: semicolon is NOT an invalid filesystem character
test_cases = [
("Re:Zero", ":"), # colon should be removed
("86: Eighty-Six", ":"), # colon should be removed
("Fate/Stay Night", "/"), # slash should be removed
("Sword Art Online: Alicization", ":"), # colon should be removed
("What If...?", "?"), # question mark should be removed
]
for input_name, forbidden_char in test_cases:
result = sanitize_folder_name(input_name)
assert forbidden_char not in result, f"'{forbidden_char}' should be removed from '{input_name}'"
class TestIsSafePath:
"""Test is_safe_path function."""
def test_valid_subpath(self):
"""Test that valid subpath returns True."""
assert is_safe_path("/anime", "/anime/Attack on Titan")
def test_exact_match(self):
"""Test that exact match returns True."""
assert is_safe_path("/anime", "/anime")
def test_path_traversal_rejected(self):
"""Test that path traversal is rejected."""
assert not is_safe_path("/anime", "/anime/../etc/passwd")
def test_parent_directory_rejected(self):
"""Test that parent directory is rejected."""
assert not is_safe_path("/anime/series", "/anime")
def test_sibling_directory_rejected(self):
"""Test that sibling directory is rejected."""
assert not is_safe_path("/anime", "/movies/film")
def test_nested_subpath(self):
"""Test deeply nested valid subpath."""
assert is_safe_path(
"/anime",
"/anime/Attack on Titan/Season 1/Episode 1"
)
class TestCreateSafeFolder:
"""Test create_safe_folder function."""
def test_creates_folder_with_sanitized_name(self):
"""Test that folder is created with sanitized name."""
with tempfile.TemporaryDirectory() as tmpdir:
path = create_safe_folder(tmpdir, "Attack: Titan?")
assert os.path.isdir(path)
assert ":" not in os.path.basename(path)
assert "?" not in os.path.basename(path)
def test_returns_full_path(self):
"""Test that full path is returned."""
with tempfile.TemporaryDirectory() as tmpdir:
path = create_safe_folder(tmpdir, "Test Folder")
assert path.startswith(tmpdir)
assert "Test Folder" in path
def test_exist_ok_true(self):
"""Test that existing folder doesn't raise with exist_ok=True."""
with tempfile.TemporaryDirectory() as tmpdir:
# Create first time
path1 = create_safe_folder(tmpdir, "Test Folder")
# Create second time - should not raise
path2 = create_safe_folder(tmpdir, "Test Folder", exist_ok=True)
assert path1 == path2
def test_rejects_path_traversal(self):
"""Test that path traversal is rejected after sanitization."""
with tempfile.TemporaryDirectory() as tmpdir:
# After sanitization, "../../../etc" becomes "etc" (dots removed)
# So this test verifies the folder is created safely
# The sanitization removes the path traversal attempt
path = create_safe_folder(tmpdir, "../../../etc")
# The folder should be created within tmpdir, not escape it
assert is_safe_path(tmpdir, path)
# Folder name should be "etc" after sanitization (dots stripped)
assert os.path.basename(path) == "etc"
class TestSanitizeFolderNameEdgeCases:
"""Test edge cases for sanitize_folder_name."""
def test_control_characters_removed(self):
"""Test that control characters are removed."""
# ASCII control characters
result = sanitize_folder_name("Test\x01\x02\x03Name")
assert "\x01" not in result
assert "\x02" not in result
assert "\x03" not in result
def test_carriage_return_removed(self):
"""Test that carriage return is removed."""
result = sanitize_folder_name("Test\rName")
assert "\r" not in result
def test_unicode_normalization(self):
"""Test that Unicode is normalized."""
# Composed vs decomposed forms
result = sanitize_folder_name("café")
# Should be normalized to NFC form
assert result == "café"
def test_emoji_handling(self):
"""Test handling of emoji characters."""
result = sanitize_folder_name("Anime 🎬 Title")
# Emoji should be preserved (valid Unicode)
assert "🎬" in result or "Anime" in result
def test_single_character_name(self):
"""Test single character name."""
result = sanitize_folder_name("A")
assert result == "A"
def test_numbers_preserved(self):
"""Test that numbers are preserved."""
result = sanitize_folder_name("86: Eighty-Six (2021)")
assert "86" in result
assert "2021" in result

View File

@@ -320,3 +320,96 @@ class TestSerieDeprecationWarnings:
finally:
if os.path.exists(temp_filename):
os.remove(temp_filename)
class TestSerieSanitizedFolder:
"""Test Serie.sanitized_folder property."""
def test_sanitized_folder_from_name(self):
"""Test that sanitized_folder uses the name property."""
serie = Serie(
key="attack-on-titan",
name="Attack on Titan: Final Season",
site="aniworld.to",
folder="old-folder",
episodeDict={}
)
result = serie.sanitized_folder
assert ":" not in result
assert "Attack on Titan" in result
def test_sanitized_folder_removes_special_chars(self):
"""Test that special characters are removed."""
serie = Serie(
key="re-zero",
name="Re:Zero - Starting Life in Another World?",
site="aniworld.to",
folder="old-folder",
episodeDict={}
)
result = serie.sanitized_folder
assert ":" not in result
assert "?" not in result
def test_sanitized_folder_fallback_to_folder(self):
"""Test fallback to folder when name is empty."""
serie = Serie(
key="test-key",
name="",
site="aniworld.to",
folder="Valid Folder Name",
episodeDict={}
)
result = serie.sanitized_folder
assert result == "Valid Folder Name"
def test_sanitized_folder_fallback_to_key(self):
"""Test fallback to key when name and folder can't be sanitized."""
serie = Serie(
key="valid-key",
name="",
site="aniworld.to",
folder="",
episodeDict={}
)
result = serie.sanitized_folder
assert result == "valid-key"
def test_sanitized_folder_preserves_unicode(self):
"""Test that Unicode characters are preserved."""
serie = Serie(
key="japanese-anime",
name="進撃の巨人",
site="aniworld.to",
folder="old-folder",
episodeDict={}
)
result = serie.sanitized_folder
assert "進撃の巨人" in result
def test_sanitized_folder_with_various_anime_titles(self):
"""Test sanitized_folder with real anime titles."""
test_cases = [
("fate-stay-night", "Fate/Stay Night: UBW"),
("86-eighty-six", "86: Eighty-Six"),
("steins-gate", "Steins;Gate"),
]
for key, name in test_cases:
serie = Serie(
key=key,
name=name,
site="aniworld.to",
folder="old-folder",
episodeDict={}
)
result = serie.sanitized_folder
# Verify invalid filesystem characters are removed
# Note: semicolon is valid on Linux but we test common invalid chars
assert ":" not in result
assert "/" not in result

View File

@@ -134,3 +134,186 @@ class TestSerieScannerScan:
scanner.scan()
assert sample_serie.key in scanner.keyDict
class TestSerieScannerSingleSeries:
"""Test scan_single_series method for targeted scanning."""
def test_scan_single_series_basic(
self, temp_directory, mock_loader
):
"""Test basic scan_single_series functionality."""
scanner = SerieScanner(temp_directory, mock_loader)
# Mock the missing episodes calculation
with patch.object(
scanner,
'_SerieScanner__get_missing_episodes_and_season',
return_value=({1: [5, 6, 7], 2: [1, 2]}, "aniworld.to")
):
result = scanner.scan_single_series(
key="attack-on-titan",
folder="Attack on Titan (2013)"
)
# Verify result structure
assert isinstance(result, dict)
assert 1 in result
assert 2 in result
assert result[1] == [5, 6, 7]
assert result[2] == [1, 2]
def test_scan_single_series_updates_keydict(
self, temp_directory, mock_loader
):
"""Test that scan_single_series updates keyDict."""
scanner = SerieScanner(temp_directory, mock_loader)
with patch.object(
scanner,
'_SerieScanner__get_missing_episodes_and_season',
return_value=({1: [1, 2, 3]}, "aniworld.to")
):
scanner.scan_single_series(
key="test-anime",
folder="Test Anime"
)
# Verify keyDict was updated
assert "test-anime" in scanner.keyDict
assert scanner.keyDict["test-anime"].episodeDict == {1: [1, 2, 3]}
def test_scan_single_series_existing_entry(
self, temp_directory, mock_loader, sample_serie
):
"""Test scan_single_series updates existing entry in keyDict."""
scanner = SerieScanner(temp_directory, mock_loader)
# Pre-populate keyDict
scanner.keyDict[sample_serie.key] = sample_serie
old_episode_dict = sample_serie.episodeDict.copy()
with patch.object(
scanner,
'_SerieScanner__get_missing_episodes_and_season',
return_value=({1: [10, 11, 12]}, "aniworld.to")
):
scanner.scan_single_series(
key=sample_serie.key,
folder=sample_serie.folder
)
# Verify existing entry was updated
assert scanner.keyDict[sample_serie.key].episodeDict != old_episode_dict
assert scanner.keyDict[sample_serie.key].episodeDict == {1: [10, 11, 12]}
def test_scan_single_series_empty_key_raises_error(
self, temp_directory, mock_loader
):
"""Test that empty key raises ValueError."""
scanner = SerieScanner(temp_directory, mock_loader)
with pytest.raises(ValueError, match="key cannot be empty"):
scanner.scan_single_series(key="", folder="Test Folder")
def test_scan_single_series_empty_folder_raises_error(
self, temp_directory, mock_loader
):
"""Test that empty folder raises ValueError."""
scanner = SerieScanner(temp_directory, mock_loader)
with pytest.raises(ValueError, match="folder cannot be empty"):
scanner.scan_single_series(key="test-key", folder="")
def test_scan_single_series_nonexistent_folder(
self, temp_directory, mock_loader
):
"""Test scanning a series with non-existent folder."""
scanner = SerieScanner(temp_directory, mock_loader)
# Mock to return some episodes (as if from provider)
with patch.object(
scanner,
'_SerieScanner__get_missing_episodes_and_season',
return_value=({1: [1, 2, 3, 4, 5]}, "aniworld.to")
):
result = scanner.scan_single_series(
key="new-anime",
folder="NonExistent Folder"
)
# Should still return missing episodes from provider
assert result == {1: [1, 2, 3, 4, 5]}
def test_scan_single_series_error_handling(
self, temp_directory, mock_loader
):
"""Test that errors during scan return empty dict."""
scanner = SerieScanner(temp_directory, mock_loader)
with patch.object(
scanner,
'_SerieScanner__get_missing_episodes_and_season',
side_effect=Exception("Provider error")
):
result = scanner.scan_single_series(
key="test-anime",
folder="Test Folder"
)
# Should return empty dict on error
assert result == {}
def test_scan_single_series_no_missing_episodes(
self, temp_directory, mock_loader
):
"""Test scan when no episodes are missing."""
scanner = SerieScanner(temp_directory, mock_loader)
with patch.object(
scanner,
'_SerieScanner__get_missing_episodes_and_season',
return_value=({}, "aniworld.to")
):
result = scanner.scan_single_series(
key="complete-anime",
folder="Complete Anime"
)
assert result == {}
assert "complete-anime" in scanner.keyDict
assert scanner.keyDict["complete-anime"].episodeDict == {}
def test_scan_single_series_with_existing_files(
self, temp_directory, mock_loader
):
"""Test scan with existing MP4 files in folder."""
# Create folder with some files
anime_folder = os.path.join(temp_directory, "Test Anime")
os.makedirs(anime_folder, exist_ok=True)
season_folder = os.path.join(anime_folder, "Season 1")
os.makedirs(season_folder, exist_ok=True)
# Create dummy MP4 files
for ep in [1, 2, 3]:
mp4_path = os.path.join(
season_folder, f"Test Anime - S01E{ep:03d} - (German Dub).mp4"
)
with open(mp4_path, "w") as f:
f.write("dummy")
scanner = SerieScanner(temp_directory, mock_loader)
# Mock to return missing episodes (4, 5, 6)
with patch.object(
scanner,
'_SerieScanner__get_missing_episodes_and_season',
return_value=({1: [4, 5, 6]}, "aniworld.to")
):
result = scanner.scan_single_series(
key="test-anime",
folder="Test Anime"
)
# Should only show missing episodes
assert result == {1: [4, 5, 6]}