All checks were successful
CI / lint-and-test (push) Successful in 23s
- Introduced new tests for the `is_admin_async` function to verify correct behavior based on user roles. - Added tests for error handling in the `error_handler` to ensure exceptions are logged and do not propagate. - Enhanced test coverage for configuration settings by adding a test for parsing admin phone numbers from environment variables. - Updated the `.cursorrules` file to include the use of a virtual environment for better dependency management.
53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
"""Tests for duty_teller.handlers.common (is_admin_async)."""
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from duty_teller.handlers.common import is_admin_async
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_is_admin_async_true_when_repository_returns_true():
|
|
"""is_admin_async returns True when is_admin_for_telegram_user returns True."""
|
|
mock_session = MagicMock()
|
|
mock_scope = MagicMock()
|
|
mock_scope.return_value.__enter__.return_value = mock_session
|
|
mock_scope.return_value.__exit__.return_value = None
|
|
|
|
with patch(
|
|
"duty_teller.handlers.common.session_scope",
|
|
mock_scope,
|
|
):
|
|
with patch(
|
|
"duty_teller.handlers.common.is_admin_for_telegram_user",
|
|
return_value=True,
|
|
) as mock_is_admin:
|
|
result = await is_admin_async(12345)
|
|
assert result is True
|
|
mock_scope.assert_called_once()
|
|
mock_scope.return_value.__enter__.assert_called_once()
|
|
mock_is_admin.assert_called_once_with(mock_session, 12345)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_is_admin_async_false_when_repository_returns_false():
|
|
"""is_admin_async returns False when is_admin_for_telegram_user returns False."""
|
|
mock_session = MagicMock()
|
|
mock_scope = MagicMock()
|
|
mock_scope.return_value.__enter__.return_value = mock_session
|
|
mock_scope.return_value.__exit__.return_value = None
|
|
|
|
with patch(
|
|
"duty_teller.handlers.common.session_scope",
|
|
mock_scope,
|
|
):
|
|
with patch(
|
|
"duty_teller.handlers.common.is_admin_for_telegram_user",
|
|
return_value=False,
|
|
) as mock_is_admin:
|
|
result = await is_admin_async(99999)
|
|
assert result is False
|
|
mock_scope.assert_called_once()
|
|
mock_is_admin.assert_called_once_with(mock_session, 99999)
|