diff --git a/src/auto_archiver/modules/telethon_extractor/telethon_extractor.py b/src/auto_archiver/modules/telethon_extractor/telethon_extractor.py index 9acec30..73fb4e8 100644 --- a/src/auto_archiver/modules/telethon_extractor/telethon_extractor.py +++ b/src/auto_archiver/modules/telethon_extractor/telethon_extractor.py @@ -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( diff --git a/tests/extractors/test_telethon_extractor.py b/tests/extractors/test_telethon_extractor.py index 23ba338..0a2d5c8 100644 --- a/tests/extractors/test_telethon_extractor.py +++ b/tests/extractors/test_telethon_extractor.py @@ -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")