new reddit tests with .env.test

This commit is contained in:
msramalho
2025-06-11 11:22:23 +01:00
parent 79f42c3c41
commit 1039e9631f
6 changed files with 57 additions and 5 deletions

6
tests/.env.test.example Normal file
View File

@@ -0,0 +1,6 @@
# reddit test credentials
REDDIT_TEST_USERNAME=""
REDDIT_TEST_PASSWORD=""
# twitter test credentials
TWITTER_BEARER_TOKEN="TEST_KEY"

View File

@@ -9,6 +9,7 @@ from tempfile import TemporaryDirectory
from typing import Dict, Tuple
import hashlib
from loguru import logger
import pytest
from auto_archiver.core.metadata import Metadata, Media
from auto_archiver.core.module import ModuleFactory
@@ -20,6 +21,24 @@ from auto_archiver.core.module import ModuleFactory
TESTS_TO_RUN_LAST = ["test_generic_archiver", "test_twitter_api_archiver"]
def pytest_configure():
# load environment variables from .env.test file.
env_path = os.path.join(os.path.dirname(__file__), ".env.test")
if os.path.exists(env_path):
with open(env_path) as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" in line:
key, value = line.split("=", 1)
os.environ[key.strip()] = value.strip().lstrip('"').rstrip('"')
else:
logger.warning(
f"Environment file {env_path} not found. Skipping loading environment variables, some tests may fail."
)
# don't check for ytdlp updates in tests
@pytest.fixture(autouse=True)
def skip_check_for_update(mocker):

View File

@@ -1,3 +1,4 @@
import os
import pytest
from auto_archiver.modules.antibot_extractor_enricher.antibot_extractor_enricher import AntibotExtractorEnricher
@@ -34,7 +35,14 @@ class TestAntibotExtractorEnricher(TestExtractorBase):
"save_to_pdf": False,
"max_download_images": 0,
"max_download_videos": 0,
"user_data_dir": "./tests/tmp/user_data",
"proxy": None,
"authentication": {
"reddit.com": {
"username": os.environ.get("REDDIT_TEST_USERNAME"),
"password": os.environ.get("REDDIT_TEST_PASSWORD"),
}
},
}
@pytest.mark.download
@@ -76,16 +84,23 @@ class TestAntibotExtractorEnricher(TestExtractorBase):
5,
0,
),
(
"https://www.reddit.com/r/BeAmazed/comments/1l6b1n4/duy_tran_is_the_owner_and_prime_wood_work_artist/",
"Duy tran is the owner and prime wood work artist",
" Created Jan 26, 2015",
4,
0,
),
],
)
def test_download_pages_with_media(self, setup_module, make_item, url, in_title, in_text, image_count, video_count):
"""
Test downloading pages with media.
"""
self.extractor = setup_module(
self.extractor_module,
{
self.config
| {
"save_to_pdf": True,
"max_download_images": 5,
"max_download_videos": "inf",