Skip to content

Commit b604103

Browse files
committed
Add tests for API throttling
Signed-off-by: Tushar Goel <[email protected]>
1 parent ed8a2c1 commit b604103

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
from django.contrib.auth import get_user_model
11+
from rest_framework.test import APIClient
12+
from rest_framework.test import APITestCase
13+
14+
User = get_user_model()
15+
16+
17+
class ThrottleApiTests(APITestCase):
18+
def setUp(self):
19+
# create a basic user
20+
self.user = User.objects.create_user("username", "[email protected]", "secret")
21+
self.auth = f"Token {self.user.auth_token.key}"
22+
self.csrf_client = APIClient(enforce_csrf_checks=True)
23+
self.csrf_client.credentials(HTTP_AUTHORIZATION=self.auth)
24+
25+
# create a staff user
26+
self.staff_user = User.objects.create_user(
27+
"staff", "[email protected]", "secret", is_staff=True
28+
)
29+
self.staff_auth = f"Token {self.staff_user.auth_token.key}"
30+
self.staff_csrf_client = APIClient(enforce_csrf_checks=True)
31+
self.staff_csrf_client.credentials(HTTP_AUTHORIZATION=self.staff_auth)
32+
33+
def test_api_throttling(self):
34+
35+
# A basic user can only access API 5 times a day
36+
for i in range(0, 5):
37+
response = self.csrf_client.get("/api/packages")
38+
self.assertEqual(response.status_code, 200)
39+
response = self.staff_csrf_client.get("/api/packages")
40+
self.assertEqual(response.status_code, 200)
41+
42+
response = self.csrf_client.get("/api/packages")
43+
# 429 - too many requests for basic user
44+
self.assertEqual(response.status_code, 429)
45+
46+
response = self.staff_csrf_client.get("/api/packages", format="json")
47+
# 200 - staff user can access API unlimited times
48+
self.assertEqual(response.status_code, 200)

vulnerablecode/settings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,12 @@
150150

151151
LOGIN_REDIRECT_URL = "/"
152152
LOGOUT_REDIRECT_URL = "/"
153+
THROTTLING_RATE = env.str("THROTTLING_RATE", default="1000/day")
153154

154155
if IS_TESTS:
155156
VULNERABLECODEIO_REQUIRE_AUTHENTICATION = True
157+
THROTTLING_RATE = "5/day"
158+
156159

157160
USE_L10N = True
158161

@@ -187,7 +190,7 @@
187190
"DEFAULT_THROTTLE_CLASSES": [
188191
"vulnerabilities.throttling.ExceptionalUserRateThrottle",
189192
],
190-
"DEFAULT_THROTTLE_RATES": {"user": "1000/hour"},
193+
"DEFAULT_THROTTLE_RATES": {"user": THROTTLING_RATE},
191194
"DEFAULT_PAGINATION_CLASS": "vulnerabilities.pagination.SmallResultSetPagination",
192195
# Limit the load on the Database returning a small number of records by default. https://github.com/nexB/vulnerablecode/issues/819
193196
"PAGE_SIZE": 10,

0 commit comments

Comments
 (0)