87 lines
3 KiB
Python
87 lines
3 KiB
Python
"""Tests for main collector logic."""
|
|
|
|
from src.main import is_domain_blacklisted, should_download_media, should_download_post
|
|
from src.reddit_client import Post
|
|
|
|
|
|
def _make_reddit_post(**kwargs):
|
|
"""Helper to create a Post with defaults."""
|
|
defaults = {
|
|
"id": "test123",
|
|
"subreddit": "pics",
|
|
"author": "testuser",
|
|
"title": "Test Post",
|
|
"url": "https://i.redd.it/test.jpg",
|
|
"score": 100,
|
|
"created_utc": 1700000000.0,
|
|
"over_18": False,
|
|
"is_gallery": False,
|
|
"preview": None,
|
|
"media_metadata": None,
|
|
"permalink": "/r/pics/test123",
|
|
"flair": None,
|
|
}
|
|
defaults.update(kwargs)
|
|
return Post(**defaults)
|
|
|
|
|
|
class TestShouldDownloadPost:
|
|
def test_normal_post_allowed(self, sample_config):
|
|
post = _make_reddit_post()
|
|
allowed, reason = should_download_post(post, sample_config)
|
|
assert allowed is True
|
|
assert reason == ""
|
|
|
|
def test_nsfw_skipped(self, sample_config):
|
|
post = _make_reddit_post(over_18=True)
|
|
allowed, reason = should_download_post(post, sample_config)
|
|
assert allowed is False
|
|
assert reason == "nsfw"
|
|
|
|
def test_low_score_skipped(self, sample_config):
|
|
post = _make_reddit_post(score=1)
|
|
allowed, reason = should_download_post(post, sample_config)
|
|
assert allowed is False
|
|
assert reason == "score"
|
|
|
|
def test_blacklisted_author(self, sample_config):
|
|
post = _make_reddit_post(author="spammer")
|
|
allowed, reason = should_download_post(post, sample_config)
|
|
assert allowed is False
|
|
assert reason == "blacklist_author"
|
|
|
|
def test_blacklisted_subreddit(self, sample_config):
|
|
post = _make_reddit_post(subreddit="spam_sub")
|
|
allowed, reason = should_download_post(post, sample_config)
|
|
assert allowed is False
|
|
assert reason == "blacklist_subreddit"
|
|
|
|
def test_blacklisted_keyword(self, sample_config):
|
|
post = _make_reddit_post(title="Amazing! Buy now for cheap!")
|
|
allowed, reason = should_download_post(post, sample_config)
|
|
assert allowed is False
|
|
assert reason == "blacklist_keyword"
|
|
|
|
|
|
class TestIsDomainBlacklisted:
|
|
def test_blacklisted_domain(self):
|
|
assert is_domain_blacklisted("https://malware.com/image.jpg", ["malware.com"]) is True
|
|
|
|
def test_clean_domain(self):
|
|
assert is_domain_blacklisted("https://i.redd.it/image.jpg", ["malware.com"]) is False
|
|
|
|
def test_empty_blacklist(self):
|
|
assert is_domain_blacklisted("https://example.com", []) is False
|
|
|
|
def test_empty_url(self):
|
|
assert is_domain_blacklisted("", ["malware.com"]) is False
|
|
|
|
|
|
class TestShouldDownloadMedia:
|
|
def test_allowed_type(self, sample_config):
|
|
assert should_download_media("image", sample_config) is True
|
|
assert should_download_media("video", sample_config) is True
|
|
|
|
def test_disallowed_type(self, sample_config):
|
|
sample_config.download.media_types = ["image"]
|
|
assert should_download_media("video", sample_config) is False
|