# retoor from devplacepy.services.openai_gateway import auth_throttle def test_is_throttled_false_below_threshold(): auth_throttle.clear() try: for _ in range(9): auth_throttle.record_failure("1.1.1.1", 60) assert auth_throttle.is_throttled("1.1.1.1", 10, 60) is False finally: auth_throttle.clear() def test_is_throttled_true_at_threshold(): auth_throttle.clear() try: for _ in range(10): auth_throttle.record_failure("2.2.2.2", 60) assert auth_throttle.is_throttled("2.2.2.2", 10, 60) is True finally: auth_throttle.clear() def test_is_throttled_scoped_per_ip(): auth_throttle.clear() try: for _ in range(10): auth_throttle.record_failure("3.3.3.3", 60) assert auth_throttle.is_throttled("3.3.3.3", 10, 60) is True assert auth_throttle.is_throttled("4.4.4.4", 10, 60) is False finally: auth_throttle.clear() def test_record_failure_returns_running_count(): auth_throttle.clear() try: assert auth_throttle.record_failure("5.5.5.5", 60) == 1 assert auth_throttle.record_failure("5.5.5.5", 60) == 2 assert auth_throttle.record_failure("5.5.5.5", 60) == 3 finally: auth_throttle.clear() def test_throttle_resets_after_window_passes(monkeypatch): auth_throttle.clear() try: now = [1000.0] monkeypatch.setattr(auth_throttle.time, "time", lambda: now[0]) for _ in range(10): auth_throttle.record_failure("6.6.6.6", 60) assert auth_throttle.is_throttled("6.6.6.6", 10, 60) is True now[0] += 61 assert auth_throttle.is_throttled("6.6.6.6", 10, 60) is False finally: auth_throttle.clear() def test_reset_clears_one_ip(): auth_throttle.clear() try: for _ in range(10): auth_throttle.record_failure("7.7.7.7", 60) auth_throttle.reset("7.7.7.7") assert auth_throttle.is_throttled("7.7.7.7", 10, 60) is False finally: auth_throttle.clear()