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 shutil
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
from pathlib import Path
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
from telethon.sync import TelegramClient
|
from telethon.sync import TelegramClient
|
||||||
|
@ -42,8 +43,7 @@ class TelethonExtractor(Extractor):
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"SETUP - Session file {base_session_filepath} does not exist for {self.name}, creating an empty one."
|
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:
|
Path(base_session_filepath).touch()
|
||||||
f.write("")
|
|
||||||
|
|
||||||
# make a copy of the session that is used exclusively with this archiver instance
|
# make a copy of the session that is used exclusively with this archiver instance
|
||||||
self.session_file = os.path.join(
|
self.session_file = os.path.join(
|
||||||
|
|
|
@ -3,36 +3,24 @@ from datetime import date
|
||||||
|
|
||||||
import pytest
|
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):
|
def test_setup_fails_clear_session_file(get_lazy_module, tmp_path, mocker):
|
||||||
extractor_module = "telethon_extractor"
|
start = mocker.patch("telethon.client.auth.AuthMethods.start")
|
||||||
extractor: TelethonExtractor
|
start.side_effect = Exception("Test exception")
|
||||||
config = {
|
|
||||||
"api_id": 123,
|
|
||||||
"api_hash": "ABC",
|
|
||||||
}
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
# make sure the default setup file is created
|
||||||
def mock_client_setup(self, mocker):
|
session_file = tmp_path / "test.session"
|
||||||
mocker.patch("telethon.client.auth.AuthMethods.start")
|
|
||||||
|
|
||||||
def test_setup_fails_clear_session_file(self, get_lazy_module, tmp_path, mocker):
|
lazy_module = get_lazy_module("telethon_extractor")
|
||||||
start = mocker.patch("telethon.client.auth.AuthMethods.start")
|
|
||||||
start.side_effect = Exception("Test exception")
|
|
||||||
|
|
||||||
# make sure the default setup file is created
|
with pytest.raises(Exception):
|
||||||
session_file = tmp_path / "test.session"
|
lazy_module.load({"telethon_extractor": {"session_file": str(session_file), "api_id": 123, "api_hash": "ABC"}})
|
||||||
|
|
||||||
lazy_module = get_lazy_module("telethon_extractor")
|
assert session_file.exists()
|
||||||
|
assert f"telethon-{date.today().strftime('%Y-%m-%d')}" in lazy_module._instance.session_file
|
||||||
with pytest.raises(Exception):
|
assert os.path.exists(lazy_module._instance.session_file + ".session")
|
||||||
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")
|
|
||||||
|
|
Ładowanie…
Reference in New Issue