"""Tests for the inbound rate-limit policy.""" from limits import parse_many from limits.strategies import STRATEGIES from core.hardening.rate_policy import ( RATE_CATEGORIES, RATE_DEFAULT, RATE_EXEMPT, RATE_STRATEGY, RateCategory, ) def test_rate_categories_cover_every_enum(): """Every RateCategory has a configured limit set.""" assert set(RATE_CATEGORIES) == set(RateCategory) def test_every_category_is_a_nonempty_list_of_parseable_limits(): """Each category maps to a list of limit strings the limits backend can parse.""" for category, limits in RATE_CATEGORIES.items(): assert isinstance(limits, list) and limits, category assert parse_many(';'.join(limits)) def test_default_limit_is_parseable(): """RATE_DEFAULT (sourced from Config) parses too -- closes the review's validation gap.""" assert parse_many(RATE_DEFAULT) def test_read_category_combines_a_second_burst_with_a_minute_sustained_cap(): """READ carries both a per-second burst limit and a per-minute sustained limit.""" read = RATE_CATEGORIES[RateCategory.READ] assert any(limit.endswith('/second') for limit in read) assert any(limit.endswith('/minute') for limit in read) def test_rate_strategy_is_supported_by_the_backend(): """RATE_STRATEGY names a strategy the limits library actually ships.""" assert RATE_STRATEGY in STRATEGIES def test_rate_exempt_are_endpoint_names(): """RATE_EXEMPT holds non-empty 'blueprint.endpoint' route names.""" assert RATE_EXEMPT and all('.' in name and name.strip() for name in RATE_EXEMPT)