2025-11-08 18:20:25 +01:00
|
|
|
import pytest
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
import json
|
|
|
|
|
from retoors.main import create_app
|
|
|
|
|
from retoors.services.user_service import UserService
|
|
|
|
|
from retoors.services.config_service import ConfigService
|
2025-11-08 19:39:25 +01:00
|
|
|
from pytest_mock import MockerFixture # Import MockerFixture
|
|
|
|
|
from unittest import mock # For AsyncMock
|
|
|
|
|
import aiojobs # Import aiojobs to patch it
|
2025-11-08 18:20:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2025-11-08 19:39:25 +01:00
|
|
|
async def client(aiohttp_client, mocker: MockerFixture):
|
|
|
|
|
app = create_app() # Define app here
|
|
|
|
|
|
|
|
|
|
# Directly set app["scheduler"] to a mock object
|
|
|
|
|
mock_scheduler_instance = mocker.MagicMock()
|
|
|
|
|
mock_scheduler_instance.spawn = mocker.AsyncMock()
|
|
|
|
|
mock_scheduler_instance.close = mocker.AsyncMock() # Ensure close is awaitable
|
|
|
|
|
app["scheduler"] = mock_scheduler_instance
|
2025-11-08 18:20:25 +01:00
|
|
|
|
|
|
|
|
# Create temporary data files for testing
|
|
|
|
|
base_path = Path(__file__).parent.parent
|
|
|
|
|
data_path = base_path / "data"
|
|
|
|
|
data_path.mkdir(exist_ok=True)
|
|
|
|
|
|
|
|
|
|
users_file = data_path / "users.json"
|
|
|
|
|
with open(users_file, "w") as f:
|
|
|
|
|
json.dump([], f)
|
|
|
|
|
|
|
|
|
|
config_file = data_path / "config.json"
|
|
|
|
|
with open(config_file, "w") as f:
|
|
|
|
|
json.dump({"price_per_gb": 0.0}, f)
|
|
|
|
|
|
|
|
|
|
app["user_service"] = UserService(users_file)
|
|
|
|
|
app["config_service"] = ConfigService(config_file)
|
|
|
|
|
|
2025-11-08 19:39:25 +01:00
|
|
|
yield await aiohttp_client(app)
|
2025-11-08 18:20:25 +01:00
|
|
|
|
|
|
|
|
# Clean up temporary files
|
|
|
|
|
users_file.unlink(missing_ok=True)
|
2025-11-08 19:39:25 +01:00
|
|
|
config_file.unlink(missing_ok=True) # Use missing_ok for robustness
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_send_email(mocker: MockerFixture):
|
|
|
|
|
"""
|
|
|
|
|
Fixture to mock the send_email function.
|
|
|
|
|
This fixture will return the mock that was patched globally by the client fixture.
|
|
|
|
|
"""
|
|
|
|
|
# Access the globally patched mock
|
|
|
|
|
return mocker.patch("retoors.helpers.email_sender.send_email")
|