kopia lustrzana https://github.com/bellingcat/auto-archiver
Simplify telethon unit tests for CI (don't use TestExtractorBase - it causes loading issues)
rodzic
e0e9f93065
commit
b7949a489f
|
@ -2,6 +2,7 @@ import os
|
|||
import shutil
|
||||
import re
|
||||
import time
|
||||
from pathlib import Path
|
||||
from datetime import date
|
||||
|
||||
from telethon.sync import TelegramClient
|
||||
|
@ -42,8 +43,7 @@ class TelethonExtractor(Extractor):
|
|||
logger.warning(
|
||||
f"SETUP - Session file {base_session_filepath} does not exist for {self.name}, creating an empty one."
|
||||
)
|
||||
with open(base_session_filepath, "w") as f:
|
||||
f.write("")
|
||||
Path(base_session_filepath).touch()
|
||||
|
||||
# make a copy of the session that is used exclusively with this archiver instance
|
||||
self.session_file = os.path.join(
|
||||
|
|
|
@ -3,36 +3,24 @@ from datetime import date
|
|||
|
||||
import pytest
|
||||
|
||||
from .test_extractor_base import TestExtractorBase
|
||||
from auto_archiver.modules.telethon_extractor import TelethonExtractor
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_client_setup(mocker):
|
||||
mocker.patch("telethon.client.auth.AuthMethods.start")
|
||||
|
||||
|
||||
class TestTelethonExtractor(TestExtractorBase):
|
||||
extractor_module = "telethon_extractor"
|
||||
extractor: TelethonExtractor
|
||||
config = {
|
||||
"api_id": 123,
|
||||
"api_hash": "ABC",
|
||||
}
|
||||
def test_setup_fails_clear_session_file(get_lazy_module, tmp_path, mocker):
|
||||
start = mocker.patch("telethon.client.auth.AuthMethods.start")
|
||||
start.side_effect = Exception("Test exception")
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_client_setup(self, mocker):
|
||||
mocker.patch("telethon.client.auth.AuthMethods.start")
|
||||
# make sure the default setup file is created
|
||||
session_file = tmp_path / "test.session"
|
||||
|
||||
def test_setup_fails_clear_session_file(self, get_lazy_module, tmp_path, mocker):
|
||||
start = mocker.patch("telethon.client.auth.AuthMethods.start")
|
||||
start.side_effect = Exception("Test exception")
|
||||
lazy_module = get_lazy_module("telethon_extractor")
|
||||
|
||||
# make sure the default setup file is created
|
||||
session_file = tmp_path / "test.session"
|
||||
with pytest.raises(Exception):
|
||||
lazy_module.load({"telethon_extractor": {"session_file": str(session_file), "api_id": 123, "api_hash": "ABC"}})
|
||||
|
||||
lazy_module = get_lazy_module("telethon_extractor")
|
||||
|
||||
with pytest.raises(Exception):
|
||||
lazy_module.load(
|
||||
{"telethon_extractor": {"session_file": str(session_file), "api_id": 123, "api_hash": "ABC"}}
|
||||
)
|
||||
|
||||
assert session_file.exists()
|
||||
assert f"telethon-{date.today().strftime('%Y-%m-%d')}" in lazy_module._instance.session_file
|
||||
assert os.path.exists(lazy_module._instance.session_file + ".session")
|
||||
assert session_file.exists()
|
||||
assert f"telethon-{date.today().strftime('%Y-%m-%d')}" in lazy_module._instance.session_file
|
||||
assert os.path.exists(lazy_module._instance.session_file + ".session")
|
||||
|
|
Ładowanie…
Reference in New Issue