@@ -30,11 +30,10 @@ def setUp(self):
3030 self .staff_csrf_client .credentials (HTTP_AUTHORIZATION = self .staff_auth )
3131
3232 self .csrf_client_anon = APIClient (enforce_csrf_checks = True )
33+ self .csrf_client_anon_1 = APIClient (enforce_csrf_checks = True )
3334
34- def test_packages_endpoint_throttling (self ):
35-
36- # A basic user can only access /packages endpoint 10 times a day
37- for i in range (0 , 10 ):
35+ def test_package_endpoint_throttling (self ):
36+ for i in range (0 , 20 ):
3837 response = self .csrf_client .get ("/api/packages" )
3938 self .assertEqual (response .status_code , 200 )
4039 response = self .staff_csrf_client .get ("/api/packages" )
@@ -50,86 +49,48 @@ def test_packages_endpoint_throttling(self):
5049
5150 # A anonymous user can only access /packages endpoint 10 times a day
5251 for i in range (0 , 10 ):
52+ print (i )
5353 response = self .csrf_client_anon .get ("/api/packages" )
5454 self .assertEqual (response .status_code , 200 )
5555
5656 response = self .csrf_client_anon .get ("/api/packages" )
5757 # 429 - too many requests for anon user
5858 self .assertEqual (response .status_code , 429 )
59+ self .assertEqual (
60+ response .data .get ("message" ),
61+ "Your request has been throttled. Please contact support@nexb.com" ,
62+ )
5963
60- def test_cpes_endpoint_throttling (self ):
61-
62- # A basic user can only access /cpes endpoint 4 times a day
63- for i in range (0 , 4 ):
64- response = self .csrf_client .get ("/api/cpes" )
65- self .assertEqual (response .status_code , 200 )
66- response = self .staff_csrf_client .get ("/api/cpes" )
67- self .assertEqual (response .status_code , 200 )
68-
69- response = self .csrf_client .get ("/api/cpes" )
70- # 429 - too many requests for basic user
71- self .assertEqual (response .status_code , 429 )
72-
73- response = self .staff_csrf_client .get ("/api/cpes" , format = "json" )
74- # 200 - staff user can access API unlimited times
75- self .assertEqual (response .status_code , 200 )
76-
77- def test_all_vulnerable_packages_endpoint_throttling (self ):
78-
79- # A basic user can only access /packages/all 1 time a day
80- for i in range (0 , 1 ):
81- response = self .csrf_client .get ("/api/packages/all" )
82- self .assertEqual (response .status_code , 200 )
83- response = self .staff_csrf_client .get ("/api/packages/all" )
84- self .assertEqual (response .status_code , 200 )
85-
86- response = self .csrf_client .get ("/api/packages/all" )
87- # 429 - too many requests for basic user
88- self .assertEqual (response .status_code , 429 )
89-
90- response = self .staff_csrf_client .get ("/api/packages/all" , format = "json" )
91- # 200 - staff user can access API unlimited times
92- self .assertEqual (response .status_code , 200 )
93-
94- def test_vulnerabilities_endpoint_throttling (self ):
95-
96- # A basic user can only access /vulnerabilities 8 times a day
97- for i in range (0 , 8 ):
98- response = self .csrf_client .get ("/api/vulnerabilities" )
99- self .assertEqual (response .status_code , 200 )
100- response = self .staff_csrf_client .get ("/api/vulnerabilities" )
101- self .assertEqual (response .status_code , 200 )
102-
103- response = self .csrf_client .get ("/api/vulnerabilities" )
104- # 429 - too many requests for basic user
64+ response = self .csrf_client_anon .get ("/api/vulnerabilties" )
65+ # 429 - too many requests for anon user
10566 self .assertEqual (response .status_code , 429 )
67+ self .assertEqual (
68+ response .data .get ("message" ),
69+ "Your request has been throttled. Please contact support@nexb.com" ,
70+ )
10671
107- response = self .staff_csrf_client .get ("/api/vulnerabilities" , format = "json" )
108- # 200 - staff user can access API unlimited times
109- self .assertEqual (response .status_code , 200 )
110-
111- def test_aliases_endpoint_throttling (self ):
11272
113- # A basic user can only access /alias 2 times a day
114- for i in range (0 , 2 ):
115- response = self .csrf_client .get ("/api/aliases" )
116- self .assertEqual (response .status_code , 200 )
117- response = self .staff_csrf_client .get ("/api/aliases" )
118- self .assertEqual (response .status_code , 200 )
73+ class ThrottleApiTestsForPostRequest (APITestCase ):
74+ def setUp (self ):
75+ # create a basic user
76+ self .user = ApiUser .objects .create_api_user (username = "e@mail.com" )
77+ self .auth = f"Token { self .user .auth_token .key } "
78+ self .csrf_client = APIClient (enforce_csrf_checks = True )
79+ self .csrf_client .credentials (HTTP_AUTHORIZATION = self .auth )
11980
120- response = self .csrf_client .get ("/api/aliases" )
121- # 429 - too many requests for basic user
122- self .assertEqual (response .status_code , 429 )
81+ # create a staff user
82+ self .staff_user = ApiUser .objects .create_api_user (username = "staff@mail.com" , is_staff = True )
83+ self .staff_auth = f"Token { self .staff_user .auth_token .key } "
84+ self .staff_csrf_client = APIClient (enforce_csrf_checks = True )
85+ self .staff_csrf_client .credentials (HTTP_AUTHORIZATION = self .staff_auth )
12386
124- response = self .staff_csrf_client .get ("/api/aliases" , format = "json" )
125- # 200 - staff user can access API unlimited times
126- self .assertEqual (response .status_code , 200 )
87+ self .csrf_client_anon = APIClient (enforce_csrf_checks = True )
12788
12889 def test_bulk_search_packages_endpoint_throttling (self ):
12990 data = json .dumps ({"purls" : ["pkg:foo/bar" ]})
13091
131- # A basic user can only access /packages/bulk_search 6 times a day
132- for i in range (0 , 6 ):
92+ # A basic user can only access /packages/bulk_search 20 times a day
93+ for i in range (0 , 20 ):
13394 response = self .csrf_client .post (
13495 "/api/packages/bulk_search" , data = data , content_type = "application/json"
13596 )
@@ -151,28 +112,19 @@ def test_bulk_search_packages_endpoint_throttling(self):
151112 # 200 - staff user can access API unlimited times
152113 self .assertEqual (response .status_code , 200 )
153114
154- def test_bulk_search_cpes_endpoint_throttling (self ):
155- data = json .dumps ({"cpes" : ["cpe:foo/bar" ]})
156-
157- # A basic user can only access /cpes/bulk_search 5 times a day
158- for i in range (0 , 5 ):
159- response = self .csrf_client .post (
160- "/api/cpes/bulk_search" , data = data , content_type = "application/json"
161- )
162- self .assertEqual (response .status_code , 200 )
163- response = self .staff_csrf_client .post (
164- "/api/cpes/bulk_search" , data = data , content_type = "application/json"
115+ # A anonymous user can only access /packages endpoint 10 times a day
116+ for i in range (0 , 10 ):
117+ response = self .csrf_client_anon .post (
118+ "/api/packages/bulk_search" , data = data , content_type = "application/json"
165119 )
166120 self .assertEqual (response .status_code , 200 )
167121
168- response = self .csrf_client .post (
169- "/api/cpes /bulk_search" , data = data , content_type = "application/json"
122+ response = self .csrf_client_anon .post (
123+ "/api/packages /bulk_search" , data = data , content_type = "application/json"
170124 )
171- # 429 - too many requests for basic user
125+ # 429 - too many requests for anon user
172126 self .assertEqual (response .status_code , 429 )
173-
174- response = self . staff_csrf_client . post (
175- "/api/cpes/bulk_search" , data = data , content_type = "application/json"
127+ self . assertEqual (
128+ response . data . get ( "message" ),
129+ "Your request has been throttled. Please contact support@nexb.com" ,
176130 )
177- # 200 - staff user can access API unlimited times
178- self .assertEqual (response .status_code , 200 )
0 commit comments