@@ -29,10 +29,11 @@ def setUp(self):
2929 self .staff_csrf_client = APIClient (enforce_csrf_checks = True )
3030 self .staff_csrf_client .credentials (HTTP_AUTHORIZATION = self .staff_auth )
3131
32- def test_packages_endpoint_throttling (self ):
32+ self .csrf_client_anon = APIClient (enforce_csrf_checks = True )
33+ self .csrf_client_anon_1 = APIClient (enforce_csrf_checks = True )
3334
34- # A basic user can only access /packages endpoint 10 times a day
35- for i in range (0 , 10 ):
35+ def test_package_endpoint_throttling ( self ):
36+ for i in range (0 , 20 ):
3637 response = self .csrf_client .get ("/api/packages" )
3738 self .assertEqual (response .status_code , 200 )
3839 response = self .staff_csrf_client .get ("/api/packages" )
@@ -46,122 +47,36 @@ def test_packages_endpoint_throttling(self):
4647 # 200 - staff user can access API unlimited times
4748 self .assertEqual (response .status_code , 200 )
4849
49- def test_cpes_endpoint_throttling (self ):
50-
51- # A basic user can only access /cpes endpoint 4 times a day
52- for i in range (0 , 4 ):
53- response = self .csrf_client .get ("/api/cpes" )
54- self .assertEqual (response .status_code , 200 )
55- response = self .staff_csrf_client .get ("/api/cpes" )
56- self .assertEqual (response .status_code , 200 )
57-
58- response = self .csrf_client .get ("/api/cpes" )
59- # 429 - too many requests for basic user
60- self .assertEqual (response .status_code , 429 )
61-
62- response = self .staff_csrf_client .get ("/api/cpes" , format = "json" )
63- # 200 - staff user can access API unlimited times
64- self .assertEqual (response .status_code , 200 )
65-
66- def test_all_vulnerable_packages_endpoint_throttling (self ):
67-
68- # A basic user can only access /packages/all 1 time a day
69- for i in range (0 , 1 ):
70- response = self .csrf_client .get ("/api/packages/all" )
71- self .assertEqual (response .status_code , 200 )
72- response = self .staff_csrf_client .get ("/api/packages/all" )
73- self .assertEqual (response .status_code , 200 )
74-
75- response = self .csrf_client .get ("/api/packages/all" )
76- # 429 - too many requests for basic user
77- self .assertEqual (response .status_code , 429 )
78-
79- response = self .staff_csrf_client .get ("/api/packages/all" , format = "json" )
80- # 200 - staff user can access API unlimited times
81- self .assertEqual (response .status_code , 200 )
82-
83- def test_vulnerabilities_endpoint_throttling (self ):
84-
85- # A basic user can only access /vulnerabilities 8 times a day
86- for i in range (0 , 8 ):
87- response = self .csrf_client .get ("/api/vulnerabilities" )
88- self .assertEqual (response .status_code , 200 )
89- response = self .staff_csrf_client .get ("/api/vulnerabilities" )
50+ # A anonymous user can only access /packages endpoint 10 times a day
51+ for i in range (0 , 10 ):
52+ print (i )
53+ response = self .csrf_client_anon .get ("/api/packages" )
9054 self .assertEqual (response .status_code , 200 )
9155
92- response = self .csrf_client .get ("/api/vulnerabilities " )
93- # 429 - too many requests for basic user
56+ response = self .csrf_client_anon .get ("/api/packages " )
57+ # 429 - too many requests for anon user
9458 self .assertEqual (response .status_code , 429 )
59+ self .assertEqual (
60+ response .data .get ("message" ),
61+ "Your request has been throttled. Please contact [email protected] " ,
62+ )
9563
96- response = self .staff_csrf_client .get ("/api/vulnerabilities" , format = "json" )
97- # 200 - staff user can access API unlimited times
98- self .assertEqual (response .status_code , 200 )
99-
100- def test_aliases_endpoint_throttling (self ):
101-
102- # A basic user can only access /alias 2 times a day
103- for i in range (0 , 2 ):
104- response = self .csrf_client .get ("/api/aliases" )
105- self .assertEqual (response .status_code , 200 )
106- response = self .staff_csrf_client .get ("/api/aliases" )
107- self .assertEqual (response .status_code , 200 )
108-
109- response = self .csrf_client .get ("/api/aliases" )
110- # 429 - too many requests for basic user
64+ response = self .csrf_client_anon .get ("/api/vulnerabilities" )
65+ # 429 - too many requests for anon user
11166 self .assertEqual (response .status_code , 429 )
67+ self .assertEqual (
68+ response .data .get ("message" ),
69+ "Your request has been throttled. Please contact [email protected] " ,
70+ )
11271
113- response = self .staff_csrf_client .get ("/api/aliases" , format = "json" )
114- # 200 - staff user can access API unlimited times
115- self .assertEqual (response .status_code , 200 )
116-
117- def test_bulk_search_packages_endpoint_throttling (self ):
11872 data = json .dumps ({"purls" : ["pkg:foo/bar" ]})
11973
120- # A basic user can only access /packages/bulk_search 6 times a day
121- for i in range (0 , 6 ):
122- response = self .csrf_client .post (
123- "/api/packages/bulk_search" , data = data , content_type = "application/json"
124- )
125- self .assertEqual (response .status_code , 200 )
126- response = self .staff_csrf_client .post (
127- "/api/packages/bulk_search" , data = data , content_type = "application/json"
128- )
129- self .assertEqual (response .status_code , 200 )
130-
131- response = self .csrf_client .post (
74+ response = self .csrf_client_anon .post (
13275 "/api/packages/bulk_search" , data = data , content_type = "application/json"
13376 )
134- # 429 - too many requests for basic user
77+ # 429 - too many requests for anon user
13578 self .assertEqual (response .status_code , 429 )
136-
137- response = self . staff_csrf_client . post (
138- "/api/packages/bulk_search" , data = data , content_type = "application/json"
79+ self . assertEqual (
80+ response . data . get ( "message" ),
81+ "Your request has been throttled. Please contact [email protected] " , 13982 )
140- # 200 - staff user can access API unlimited times
141- self .assertEqual (response .status_code , 200 )
142-
143- def test_bulk_search_cpes_endpoint_throttling (self ):
144- data = json .dumps ({"cpes" : ["cpe:foo/bar" ]})
145-
146- # A basic user can only access /cpes/bulk_search 5 times a day
147- for i in range (0 , 5 ):
148- response = self .csrf_client .post (
149- "/api/cpes/bulk_search" , data = data , content_type = "application/json"
150- )
151- self .assertEqual (response .status_code , 200 )
152- response = self .staff_csrf_client .post (
153- "/api/cpes/bulk_search" , data = data , content_type = "application/json"
154- )
155- self .assertEqual (response .status_code , 200 )
156-
157- response = self .csrf_client .post (
158- "/api/cpes/bulk_search" , data = data , content_type = "application/json"
159- )
160- # 429 - too many requests for basic user
161- self .assertEqual (response .status_code , 429 )
162-
163- response = self .staff_csrf_client .post (
164- "/api/cpes/bulk_search" , data = data , content_type = "application/json"
165- )
166- # 200 - staff user can access API unlimited times
167- self .assertEqual (response .status_code , 200 )
0 commit comments