1414from rest_framework import status
1515from rest_framework .test import APIClient
1616from rest_framework .test import APITestCase
17- from rest_framework .throttling import AnonRateThrottle
1817
1918from vulnerabilities .api import PermissionBasedUserRateThrottle
2019from vulnerabilities .models import ApiUser
2120
2221
23- def simulate_throttle_usage (
24- url ,
25- client ,
26- mock_use_count ,
27- throttle_cls = PermissionBasedUserRateThrottle ,
28- ):
29- throttle = throttle_cls ()
22+ def simulate_throttle_usage (url , client , mock_use_count ):
23+ throttle = PermissionBasedUserRateThrottle ()
3024 request = client .get (url ).wsgi_request
3125
3226 if cache_key := throttle .get_cache_key (request , view = None ):
27+ print (cache_key )
3328 now = throttle .timer ()
3429 cache .set (cache_key , [now ] * mock_use_count )
3530
@@ -41,37 +36,37 @@ def setUp(self):
4136 # See https://www.django-rest-framework.org/api-guide/throttling/#setting-up-the-cache
4237 cache .clear ()
4338
44- permission_3600 = Permission .objects .get (codename = "throttle_3600_hour " )
45- permission_14400 = Permission .objects .get (codename = "throttle_14400_hour " )
46- permission_18000 = Permission .objects .get (codename = "throttle_18000_hour " )
47- permission_unrestricted = Permission .objects .get (codename = "throttle_unrestricted " )
39+ permission_low = Permission .objects .get (codename = "throttle_0_low " )
40+ permission_medium = Permission .objects .get (codename = "throttle_1_medium " )
41+ permission_high = Permission .objects .get (codename = "throttle_2_high " )
42+ permission_unrestricted = Permission .objects .get (codename = "throttle_3_unrestricted " )
4843
49- # user with 3600/hour permission
50- self .th_3600_user = ApiUser .objects .create_api_user (username = "z@mail.com" )
51- self .th_3600_user .user_permissions .add (permission_3600 )
52- self .th_3600_user_auth = f"Token { self .th_3600_user .auth_token .key } "
53- self .th_3600_user_csrf_client = APIClient (enforce_csrf_checks = True )
54- self .th_3600_user_csrf_client .credentials (HTTP_AUTHORIZATION = self .th_3600_user_auth )
44+ # user with low permission
45+ self .th_low_user = ApiUser .objects .create_api_user (username = "z@mail.com" )
46+ self .th_low_user .user_permissions .add (permission_low )
47+ self .th_low_user_auth = f"Token { self .th_low_user .auth_token .key } "
48+ self .th_low_user_csrf_client = APIClient (enforce_csrf_checks = True )
49+ self .th_low_user_csrf_client .credentials (HTTP_AUTHORIZATION = self .th_low_user_auth )
5550
5651 # basic user without any special throttling perm
5752 self .basic_user = ApiUser .objects .create_api_user (username = "a@mail.com" )
5853 self .basic_user_auth = f"Token { self .basic_user .auth_token .key } "
5954 self .basic_user_csrf_client = APIClient (enforce_csrf_checks = True )
6055 self .basic_user_csrf_client .credentials (HTTP_AUTHORIZATION = self .basic_user_auth )
6156
62- # 14400/hour permission
63- self .th_14400_user = ApiUser .objects .create_api_user (username = "b@mail.com" )
64- self .th_14400_user .user_permissions .add (permission_14400 )
65- self .th_14400_user_auth = f"Token { self .th_14400_user .auth_token .key } "
66- self .th_14400_user_csrf_client = APIClient (enforce_csrf_checks = True )
67- self .th_14400_user_csrf_client .credentials (HTTP_AUTHORIZATION = self .th_14400_user_auth )
57+ # medium permission
58+ self .th_medium_user = ApiUser .objects .create_api_user (username = "b@mail.com" )
59+ self .th_medium_user .user_permissions .add (permission_medium )
60+ self .th_medium_user_auth = f"Token { self .th_medium_user .auth_token .key } "
61+ self .th_medium_user_csrf_client = APIClient (enforce_csrf_checks = True )
62+ self .th_medium_user_csrf_client .credentials (HTTP_AUTHORIZATION = self .th_medium_user_auth )
6863
69- # 18000/hour permission
70- self .th_18000_user = ApiUser .objects .create_api_user (username = "c@mail.com" )
71- self .th_18000_user .user_permissions .add (permission_18000 )
72- self .th_18000_user_auth = f"Token { self .th_18000_user .auth_token .key } "
73- self .th_18000_user_csrf_client = APIClient (enforce_csrf_checks = True )
74- self .th_18000_user_csrf_client .credentials (HTTP_AUTHORIZATION = self .th_18000_user_auth )
64+ # high permission
65+ self .th_high_user = ApiUser .objects .create_api_user (username = "c@mail.com" )
66+ self .th_high_user .user_permissions .add (permission_high )
67+ self .th_high_user_auth = f"Token { self .th_high_user .auth_token .key } "
68+ self .th_high_user_csrf_client = APIClient (enforce_csrf_checks = True )
69+ self .th_high_user_csrf_client .credentials (HTTP_AUTHORIZATION = self .th_high_user_auth )
7570
7671 # unrestricted throttling perm
7772 self .th_unrestricted_user = ApiUser .objects .create_api_user (username = "d@mail.com" )
@@ -85,60 +80,60 @@ def setUp(self):
8580 self .csrf_client_anon = APIClient (enforce_csrf_checks = True )
8681 self .csrf_client_anon_1 = APIClient (enforce_csrf_checks = True )
8782
88- def test_user_with_3600_perm_throttling (self ):
83+ def test_user_with_low_perm_throttling (self ):
8984 simulate_throttle_usage (
9085 url = "/api/packages" ,
91- client = self .th_3600_user_csrf_client ,
92- mock_use_count = 3599 ,
86+ client = self .th_low_user_csrf_client ,
87+ mock_use_count = 10799 ,
9388 )
9489
95- response = self .th_3600_user_csrf_client .get ("/api/packages" )
90+ response = self .th_low_user_csrf_client .get ("/api/packages" )
9691 self .assertEqual (response .status_code , status .HTTP_200_OK )
9792
98- # exhausted 3600 /hr allowed requests.
99- response = self .th_3600_user_csrf_client .get ("/api/packages" )
93+ # exhausted 10800 /hr allowed requests.
94+ response = self .th_low_user_csrf_client .get ("/api/packages" )
10095 self .assertEqual (response .status_code , status .HTTP_429_TOO_MANY_REQUESTS )
10196
10297 def test_basic_user_throttling (self ):
10398 simulate_throttle_usage (
10499 url = "/api/packages" ,
105100 client = self .basic_user_csrf_client ,
106- mock_use_count = 10799 ,
101+ mock_use_count = 14399 ,
107102 )
108103
109104 response = self .basic_user_csrf_client .get ("/api/packages" )
110105 self .assertEqual (response .status_code , status .HTTP_200_OK )
111106
112- # exhausted 10800 /hr allowed requests.
107+ # exhausted 14400 /hr allowed requests.
113108 response = self .basic_user_csrf_client .get ("/api/packages" )
114109 self .assertEqual (response .status_code , status .HTTP_429_TOO_MANY_REQUESTS )
115110
116- def test_user_with_14400_perm_throttling (self ):
111+ def test_user_with_medium_perm_throttling (self ):
117112 simulate_throttle_usage (
118113 url = "/api/packages" ,
119- client = self .th_14400_user_csrf_client ,
114+ client = self .th_medium_user_csrf_client ,
120115 mock_use_count = 14399 ,
121116 )
122117
123- response = self .th_14400_user_csrf_client .get ("/api/packages" )
118+ response = self .th_medium_user_csrf_client .get ("/api/packages" )
124119 self .assertEqual (response .status_code , status .HTTP_200_OK )
125120
126121 # exhausted 14400/hr allowed requests for user with 14400 perm.
127- response = self .th_14400_user_csrf_client .get ("/api/packages" )
122+ response = self .th_medium_user_csrf_client .get ("/api/packages" )
128123 self .assertEqual (response .status_code , status .HTTP_429_TOO_MANY_REQUESTS )
129124
130- def test_user_with_18000_perm_throttling (self ):
125+ def test_user_with_high_perm_throttling (self ):
131126 simulate_throttle_usage (
132127 url = "/api/packages" ,
133- client = self .th_18000_user_csrf_client ,
128+ client = self .th_high_user_csrf_client ,
134129 mock_use_count = 17999 ,
135130 )
136131
137- response = self .th_18000_user_csrf_client .get ("/api/packages" )
132+ response = self .th_high_user_csrf_client .get ("/api/packages" )
138133 self .assertEqual (response .status_code , status .HTTP_200_OK )
139134
140135 # exhausted 18000/hr allowed requests for user with 18000 perm.
141- response = self .th_18000_user_csrf_client .get ("/api/packages" )
136+ response = self .th_high_user_csrf_client .get ("/api/packages" )
142137 self .assertEqual (response .status_code , status .HTTP_429_TOO_MANY_REQUESTS )
143138
144139 def test_user_with_unrestricted_perm_throttling (self ):
@@ -154,7 +149,6 @@ def test_user_with_unrestricted_perm_throttling(self):
154149
155150 def test_anon_throttling (self ):
156151 simulate_throttle_usage (
157- throttle_cls = AnonRateThrottle ,
158152 url = "/api/packages" ,
159153 client = self .csrf_client_anon ,
160154 mock_use_count = 3599 ,
0 commit comments