|
1 | | -from unittest.mock import AsyncMock |
| 1 | +from unittest.mock import AsyncMock, MagicMock, patch |
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
| 5 | +from app.core.rate_limiter.rate_limiting_algorithm.registry import get_rate_limiter |
5 | 6 | from app.core.rate_limiter.rate_limiting_algorithm.sliding_window import ( |
6 | 7 | SlidingWindowRateLimiter, |
7 | 8 | ) |
8 | 9 |
|
| 10 | +@pytest.fixture |
| 11 | +def mock_redis_from_url(): |
| 12 | + with patch("redis.asyncio.from_url") as mocker: |
| 13 | + mock_instance = MagicMock() |
| 14 | + mocker.return_value = mock_instance |
| 15 | + yield mocker |
9 | 16 |
|
10 | 17 | @pytest.mark.asyncio |
11 | 18 | async def test_allow_first_request(): |
@@ -40,3 +47,50 @@ async def test_fail_open_allows_requests(): |
40 | 47 |
|
41 | 48 | assert allowed is True |
42 | 49 | assert retry_after is None |
| 50 | + |
| 51 | +def test_get_rate_limiter_none_strategy(mock_redis_from_url): |
| 52 | + rl = get_rate_limiter(strategy="none", redis_url="redis://localhost:6379/0", fail_open=True) |
| 53 | + assert rl is None |
| 54 | + |
| 55 | + |
| 56 | +def test_get_rate_limiter_empty_strategy(mock_redis_from_url): |
| 57 | + rl = get_rate_limiter(strategy="", redis_url="redis://localhost:6379/0", fail_open=True) |
| 58 | + assert rl is None |
| 59 | + |
| 60 | + |
| 61 | +def test_get_rate_limiter_null_strategy(mock_redis_from_url): |
| 62 | + rl = get_rate_limiter(strategy="null", redis_url="redis://localhost:6379/0", fail_open=True) |
| 63 | + assert rl is None |
| 64 | + |
| 65 | + |
| 66 | +def test_get_rate_limiter_no_strategy(mock_redis_from_url): |
| 67 | + rl = get_rate_limiter(strategy=None, redis_url="redis://localhost:6379/0", fail_open=True) |
| 68 | + assert rl is None |
| 69 | + |
| 70 | + |
| 71 | +def test_get_rate_limiter_no_redis_url(mock_redis_from_url): |
| 72 | + rl = get_rate_limiter(strategy="sliding_window", redis_url="", fail_open=True) |
| 73 | + assert rl is None |
| 74 | + |
| 75 | + |
| 76 | +def test_get_rate_limiter_sliding_window(mock_redis_from_url): |
| 77 | + rl = get_rate_limiter(strategy="sliding_window", redis_url="redis://x", fail_open=True) |
| 78 | + assert isinstance(rl, SlidingWindowRateLimiter) |
| 79 | + |
| 80 | + |
| 81 | +def test_get_rate_limiter_sliding_window_dash(mock_redis_from_url): |
| 82 | + rl = get_rate_limiter(strategy="sliding-window", redis_url="redis://x", fail_open=False) |
| 83 | + assert isinstance(rl, SlidingWindowRateLimiter) |
| 84 | + assert rl.get_fail_open() is False |
| 85 | + |
| 86 | + |
| 87 | +def test_get_rate_limiter_fail_open_coercion(mock_redis_from_url): |
| 88 | + rl = get_rate_limiter(strategy="sliding_window", redis_url="redis://x", fail_open=None) |
| 89 | + assert isinstance(rl, SlidingWindowRateLimiter) |
| 90 | + assert rl.get_fail_open() is False # default fallback |
| 91 | + |
| 92 | + |
| 93 | +def test_rate_limiter_unknown_strategy(mock_redis_from_url): |
| 94 | + with pytest.raises(ValueError) as e: |
| 95 | + get_rate_limiter(strategy="weird_strategy", redis_url="redis://x", fail_open=True) |
| 96 | + assert "Unknown rate limiter strategy" in str(e.value) |
0 commit comments