feat: add tests for admin checks and error handling
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.
This commit is contained in:
2026-02-21 00:50:29 +03:00
parent bf8ac87a73
commit 35946a5812
6 changed files with 99 additions and 0 deletions

View File

@@ -52,3 +52,26 @@ async def test_error_handler_update_none_does_not_crash():
context = MagicMock()
context.error = Exception("test")
await error_handler(None, context)
@pytest.mark.asyncio
async def test_error_handler_reply_text_raises_does_not_propagate():
"""error_handler: when reply_text raises, exception is caught and logged; does not propagate."""
class FakeUpdate:
pass
update = FakeUpdate()
update.effective_message = MagicMock()
update.effective_message.reply_text = AsyncMock(
side_effect=Exception("reply failed")
)
update.effective_user = MagicMock()
context = MagicMock()
context.error = Exception("test error")
with patch("duty_teller.handlers.errors.Update", FakeUpdate):
with patch("duty_teller.handlers.errors.get_lang", return_value="en"):
with patch("duty_teller.handlers.errors.t", return_value="An error occurred."):
await error_handler(update, context)
# Handler must not raise; reply_text was attempted
update.effective_message.reply_text.assert_called_once()