From e45b62815ae67dafeb1a969f9396706d8d5a1943 Mon Sep 17 00:00:00 2001 From: Gerald Iakobinyi-Pich Date: Thu, 27 Feb 2025 18:29:34 +0200 Subject: [PATCH 1/2] feat: add support for rate limits with value None in return of validate_api_key --- api/embed/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/embed/api.py b/api/embed/api.py index 058ebf610..86a153060 100644 --- a/api/embed/api.py +++ b/api/embed/api.py @@ -98,7 +98,7 @@ def handle_embed_add_stamps( class AccountAPIKeySchema(Schema): - rate_limit: str + rate_limit: str | None @api_router.get( From 8186ae68c92f0817868095f7c3fd2211cfc8a692 Mon Sep 17 00:00:00 2001 From: Gerald Iakobinyi-Pich Date: Thu, 27 Feb 2025 19:49:58 +0200 Subject: [PATCH 2/2] feat: test for validate_api_key when the api key has unlimited rate_limit --- api/embed/test/test_api_validate_api_key.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/api/embed/test/test_api_validate_api_key.py b/api/embed/test/test_api_validate_api_key.py index 2c8c9a3d2..3f4073976 100644 --- a/api/embed/test/test_api_validate_api_key.py +++ b/api/embed/test/test_api_validate_api_key.py @@ -4,7 +4,7 @@ from django.contrib.auth.models import UserManager from django.test import Client, TestCase -from account.models import Account, AccountAPIKey +from account.models import Account, AccountAPIKey, RateLimits # Avoids type issues in standard django models user_manager = cast(UserManager, get_user_model().objects) @@ -49,3 +49,21 @@ def test_rate_limit_success(self): assert rate_limit_response.status_code == 200 data = rate_limit_response.json() assert data == {"rate_limit": api_key_obj.rate_limit} + + def test_rate_limit_success_for_unlimited_rate(self): + """Test that the rate limit API when the rate limit is set to UNLIMITED""" + + (api_key_obj, api_key) = AccountAPIKey.objects.create_key( + account=self.account, + name="Token for user 1", + rate_limit=RateLimits.UNLIMITED.value, + ) + + rate_limit_response = self.client.get( + "/embed/validate-api-key", + **{"HTTP_X-API-KEY": api_key}, + ) + + assert rate_limit_response.status_code == 200 + data = rate_limit_response.json() + assert data == {"rate_limit": api_key_obj.rate_limit}