|
import devplacepy.main as m
|
|
from starlette.testclient import TestClient
|
|
|
|
|
|
def test_rate_limit_blocks_excess(monkeypatch):
|
|
monkeypatch.setattr(m, "RATE_LIMIT", 3)
|
|
m._rate_limit_store.clear()
|
|
client = TestClient(m.app)
|
|
codes = [client.post("/", headers={"X-Real-IP": "9.9.9.9"}).status_code for _ in range(6)]
|
|
assert 429 in codes, codes
|
|
assert codes[-1] == 429
|
|
|
|
|
|
def test_rate_limit_is_per_ip(monkeypatch):
|
|
monkeypatch.setattr(m, "RATE_LIMIT", 2)
|
|
m._rate_limit_store.clear()
|
|
client = TestClient(m.app)
|
|
for _ in range(2):
|
|
client.post("/", headers={"X-Real-IP": "1.1.1.1"})
|
|
blocked = client.post("/", headers={"X-Real-IP": "1.1.1.1"}).status_code
|
|
other_ip = client.post("/", headers={"X-Real-IP": "2.2.2.2"}).status_code
|
|
assert blocked == 429
|
|
assert other_ip != 429
|
|
|
|
|
|
def test_get_requests_not_rate_limited(monkeypatch):
|
|
monkeypatch.setattr(m, "RATE_LIMIT", 2)
|
|
m._rate_limit_store.clear()
|
|
client = TestClient(m.app)
|
|
codes = [client.get("/robots.txt", headers={"X-Real-IP": "3.3.3.3"}).status_code for _ in range(5)]
|
|
assert all(c == 200 for c in codes), codes
|