77# See https://aboutcode.org for more information about nexB OSS projects.
88#
99
10+ import json
11+
1012from django .contrib .auth import get_user_model
1113from rest_framework .test import APIClient
1214from rest_framework .test import APITestCase
@@ -30,10 +32,10 @@ def setUp(self):
3032 self .staff_csrf_client = APIClient (enforce_csrf_checks = True )
3133 self .staff_csrf_client .credentials (HTTP_AUTHORIZATION = self .staff_auth )
3234
33- def test_api_throttling (self ):
35+ def test_packages_endpoint_throttling (self ):
3436
35- # A basic user can only access API 5 times a day
36- for i in range (0 , 5 ):
37+ # A basic user can only access /packages endpoint 10 times a day
38+ for i in range (0 , 10 ):
3739 response = self .csrf_client .get ("/api/packages" )
3840 self .assertEqual (response .status_code , 200 )
3941 response = self .staff_csrf_client .get ("/api/packages" )
@@ -46,3 +48,123 @@ def test_api_throttling(self):
4648 response = self .staff_csrf_client .get ("/api/packages" , format = "json" )
4749 # 200 - staff user can access API unlimited times
4850 self .assertEqual (response .status_code , 200 )
51+
52+ def test_cpes_endpoint_throttling (self ):
53+
54+ # A basic user can only access /cpes endpoint 4 times a day
55+ for i in range (0 , 4 ):
56+ response = self .csrf_client .get ("/api/cpes" )
57+ self .assertEqual (response .status_code , 200 )
58+ response = self .staff_csrf_client .get ("/api/cpes" )
59+ self .assertEqual (response .status_code , 200 )
60+
61+ response = self .csrf_client .get ("/api/cpes" )
62+ # 429 - too many requests for basic user
63+ self .assertEqual (response .status_code , 429 )
64+
65+ response = self .staff_csrf_client .get ("/api/cpes" , format = "json" )
66+ # 200 - staff user can access API unlimited times
67+ self .assertEqual (response .status_code , 200 )
68+
69+ def test_all_vulnerable_packages_endpoint_throttling (self ):
70+
71+ # A basic user can only access /packages/all 1 time a day
72+ for i in range (0 , 1 ):
73+ response = self .csrf_client .get ("/api/packages/all" )
74+ self .assertEqual (response .status_code , 200 )
75+ response = self .staff_csrf_client .get ("/api/packages/all" )
76+ self .assertEqual (response .status_code , 200 )
77+
78+ response = self .csrf_client .get ("/api/packages/all" )
79+ # 429 - too many requests for basic user
80+ self .assertEqual (response .status_code , 429 )
81+
82+ response = self .staff_csrf_client .get ("/api/packages/all" , format = "json" )
83+ # 200 - staff user can access API unlimited times
84+ self .assertEqual (response .status_code , 200 )
85+
86+ def test_vulnerabilities_endpoint_throttling (self ):
87+
88+ # A basic user can only access /vulnerabilities 8 times a day
89+ for i in range (0 , 8 ):
90+ response = self .csrf_client .get ("/api/vulnerabilities" )
91+ self .assertEqual (response .status_code , 200 )
92+ response = self .staff_csrf_client .get ("/api/vulnerabilities" )
93+ self .assertEqual (response .status_code , 200 )
94+
95+ response = self .csrf_client .get ("/api/vulnerabilities" )
96+ # 429 - too many requests for basic user
97+ self .assertEqual (response .status_code , 429 )
98+
99+ response = self .staff_csrf_client .get ("/api/vulnerabilities" , format = "json" )
100+ # 200 - staff user can access API unlimited times
101+ self .assertEqual (response .status_code , 200 )
102+
103+ def test_aliases_endpoint_throttling (self ):
104+
105+ # A basic user can only access /alias 2 times a day
106+ for i in range (0 , 2 ):
107+ response = self .csrf_client .get ("/api/alias" )
108+ self .assertEqual (response .status_code , 200 )
109+ response = self .staff_csrf_client .get ("/api/alias" )
110+ self .assertEqual (response .status_code , 200 )
111+
112+ response = self .csrf_client .get ("/api/alias" )
113+ # 429 - too many requests for basic user
114+ self .assertEqual (response .status_code , 429 )
115+
116+ response = self .staff_csrf_client .get ("/api/alias" , format = "json" )
117+ # 200 - staff user can access API unlimited times
118+ self .assertEqual (response .status_code , 200 )
119+
120+ def test_bulk_search_packages_endpoint_throttling (self ):
121+ data = json .dumps ({"purls" : ["pkg:foo/bar" ]})
122+
123+ # A basic user can only access /packages/bulk_search 6 times a day
124+ for i in range (0 , 6 ):
125+ response = self .csrf_client .post (
126+ "/api/packages/bulk_search" , data = data , content_type = "application/json"
127+ )
128+ self .assertEqual (response .status_code , 200 )
129+ response = self .staff_csrf_client .post (
130+ "/api/packages/bulk_search" , data = data , content_type = "application/json"
131+ )
132+ self .assertEqual (response .status_code , 200 )
133+
134+ response = self .csrf_client .post (
135+ "/api/packages/bulk_search" , data = data , content_type = "application/json"
136+ )
137+ # 429 - too many requests for basic user
138+ self .assertEqual (response .status_code , 429 )
139+
140+ response = self .staff_csrf_client .post (
141+ "/api/packages/bulk_search" , data = data , content_type = "application/json"
142+ )
143+ # 200 - staff user can access API unlimited times
144+ self .assertEqual (response .status_code , 200 )
145+
146+ def test_bulk_search_cpes_endpoint_throttling (self ):
147+ data = json .dumps ({"cpes" : ["cpe:foo/bar" ]})
148+
149+ # A basic user can only access /cpes/bulk_search 5 times a day
150+ for i in range (0 , 5 ):
151+ response = self .csrf_client .post (
152+ "/api/cpes/bulk_search" , data = data , content_type = "application/json"
153+ )
154+ self .assertEqual (response .status_code , 200 )
155+ response = self .staff_csrf_client .post (
156+ "/api/cpes/bulk_search" , data = data , content_type = "application/json"
157+ )
158+ self .assertEqual (response .status_code , 200 )
159+
160+ response = self .csrf_client .post (
161+ "/api/cpes/bulk_search" , data = data , content_type = "application/json"
162+ )
163+ # 429 - too many requests for basic user
164+ self .assertEqual (response .status_code , 429 )
165+
166+ response = self .staff_csrf_client .post (
167+ "/api/cpes/bulk_search" , data = data , content_type = "application/json"
168+ )
169+ # 200 - staff user can access API unlimited times
170+ self .assertEqual (response .status_code , 200 )
0 commit comments