Some checks failed
CI / lint-and-test (push) Failing after 11s
- Upgraded `pytest-asyncio` to version 1.0 to ensure compatibility with the latest features and improvements. - Increased the coverage threshold in pytest configuration to 80%, enhancing the quality assurance process. - Added a new `conftest.py` file to manage shared fixtures and improve test organization. - Introduced multiple new test files to cover various components, ensuring comprehensive test coverage across the application. - Updated the `.coverage` file to reflect the latest coverage metrics.
100 lines
3.9 KiB
Python
100 lines
3.9 KiB
Python
"""Tests for duty_teller.api.dependencies (lang, auth error, date validation, private client)."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
import duty_teller.api.dependencies as deps
|
|
import duty_teller.config as config
|
|
|
|
|
|
class TestLangFromAcceptLanguage:
|
|
"""Tests for _lang_from_accept_language."""
|
|
|
|
def test_none_returns_default(self):
|
|
assert deps._lang_from_accept_language(None) == config.DEFAULT_LANGUAGE
|
|
|
|
def test_empty_string_returns_default(self):
|
|
assert deps._lang_from_accept_language("") == config.DEFAULT_LANGUAGE
|
|
assert deps._lang_from_accept_language(" ") == config.DEFAULT_LANGUAGE
|
|
|
|
def test_ru_ru_returns_ru(self):
|
|
assert deps._lang_from_accept_language("ru-RU,ru;q=0.9") == "ru"
|
|
|
|
def test_en_us_returns_en(self):
|
|
assert deps._lang_from_accept_language("en-US") == "en"
|
|
|
|
def test_invalid_fallback_to_en(self):
|
|
assert deps._lang_from_accept_language("zz") == "en"
|
|
assert deps._lang_from_accept_language("x") == "en"
|
|
|
|
|
|
class TestAuthErrorDetail:
|
|
"""Tests for _auth_error_detail."""
|
|
|
|
def test_hash_mismatch_uses_bad_signature_key(self):
|
|
with patch("duty_teller.api.dependencies.t") as mock_t:
|
|
mock_t.return_value = "Bad signature"
|
|
result = deps._auth_error_detail("hash_mismatch", "en")
|
|
assert result == "Bad signature"
|
|
mock_t.assert_called_once_with("en", "api.auth_bad_signature")
|
|
|
|
def test_other_reason_uses_auth_invalid_key(self):
|
|
with patch("duty_teller.api.dependencies.t") as mock_t:
|
|
mock_t.return_value = "Invalid auth"
|
|
result = deps._auth_error_detail("expired", "ru")
|
|
assert result == "Invalid auth"
|
|
mock_t.assert_called_once_with("ru", "api.auth_invalid")
|
|
|
|
|
|
class TestValidateDutyDates:
|
|
"""Tests for _validate_duty_dates."""
|
|
|
|
def test_valid_range_no_exception(self):
|
|
deps._validate_duty_dates("2025-01-01", "2025-01-31", "en")
|
|
|
|
def test_bad_format_raises_400_with_i18n(self):
|
|
with patch("duty_teller.api.dependencies.t") as mock_t:
|
|
mock_t.return_value = "Bad format message"
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
deps._validate_duty_dates("01-01-2025", "2025-01-31", "en")
|
|
assert exc_info.value.status_code == 400
|
|
assert exc_info.value.detail == "Bad format message"
|
|
mock_t.assert_called_with("en", "dates.bad_format")
|
|
|
|
def test_from_after_to_raises_400_with_i18n(self):
|
|
with patch("duty_teller.api.dependencies.t") as mock_t:
|
|
mock_t.return_value = "From after to message"
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
deps._validate_duty_dates("2025-02-01", "2025-01-01", "ru")
|
|
assert exc_info.value.status_code == 400
|
|
assert exc_info.value.detail == "From after to message"
|
|
mock_t.assert_called_with("ru", "dates.from_after_to")
|
|
|
|
|
|
class TestIsPrivateClient:
|
|
"""Tests for _is_private_client."""
|
|
|
|
def test_loopback_true(self):
|
|
assert deps._is_private_client("127.0.0.1") is True
|
|
assert deps._is_private_client("::1") is True
|
|
|
|
def test_rfc1918_private_true(self):
|
|
assert deps._is_private_client("10.0.0.1") is True
|
|
assert deps._is_private_client("192.168.1.1") is True
|
|
assert deps._is_private_client("172.16.0.1") is True
|
|
assert deps._is_private_client("172.31.255.255") is True
|
|
|
|
def test_public_ip_false(self):
|
|
assert deps._is_private_client("8.8.8.8") is False
|
|
|
|
def test_non_ip_false(self):
|
|
assert deps._is_private_client("example.com") is False
|
|
assert deps._is_private_client("") is False
|
|
assert deps._is_private_client(None) is False
|
|
|
|
def test_172_non_private_octet_false(self):
|
|
assert deps._is_private_client("172.15.0.1") is False
|
|
assert deps._is_private_client("172.32.0.1") is False
|