Skip to content

Commit 0db4727

Browse files
committed
Reset throttling to properly test api rate limits
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent 5f2eb94 commit 0db4727

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

vulnerabilities/tests/test_api_v2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from unittest.mock import patch
1111

1212
from django.contrib.auth.models import User
13+
from django.core.cache import cache
1314
from django.db.models import Prefetch
1415
from django.urls import reverse
1516
from rest_framework import status
@@ -670,6 +671,12 @@ def test_lookup_with_invalid_purl_format(self):
670671

671672
class PipelineScheduleV2ViewSetTest(APITestCase):
672673
def setUp(self):
674+
# Reset the api throttling for anon user to properly test the
675+
# access on schedule endpoint.
676+
# DRF stores throttling state in cache, clear cache to reset throttling.
677+
# See https://www.django-rest-framework.org/api-guide/throttling/#setting-up-the-cache
678+
cache.clear()
679+
673680
patcher = patch.object(PipelineSchedule, "create_new_job")
674681
self.mock_create_new_job = patcher.start()
675682
self.addCleanup(patcher.stop)

vulnerabilities/tests/test_throttling.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import json
1111

12+
from django.core.cache import cache
1213
from rest_framework.test import APIClient
1314
from rest_framework.test import APITestCase
1415

@@ -17,6 +18,11 @@
1718

1819
class ThrottleApiTests(APITestCase):
1920
def setUp(self):
21+
# Reset the api throttling to properly test the rate limit on anon users.
22+
# DRF stores throttling state in cache, clear cache to reset throttling.
23+
# See https://www.django-rest-framework.org/api-guide/throttling/#setting-up-the-cache
24+
cache.clear()
25+
2026
# create a basic user
2127
self.user = ApiUser.objects.create_api_user(username="[email protected]")
2228
self.auth = f"Token {self.user.auth_token.key}"

0 commit comments

Comments
 (0)