@@ -41,10 +41,18 @@ def setUp(self):
4141 # See https://www.django-rest-framework.org/api-guide/throttling/#setting-up-the-cache
4242 cache .clear ()
4343
44+ permission_3600 = Permission .objects .get (codename = "throttle_3600_hour" )
4445 permission_14400 = Permission .objects .get (codename = "throttle_14400_hour" )
4546 permission_18000 = Permission .objects .get (codename = "throttle_18000_hour" )
4647 permission_unrestricted = Permission .objects .get (codename = "throttle_unrestricted" )
4748
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 )
55+
4856 # basic user without any special throttling perm
4957 self .basic_user = ApiUser .objects .create_api_user (username = "a@mail.com" )
5058 self .basic_user_auth = f"Token { self .basic_user .auth_token .key } "
@@ -77,6 +85,20 @@ def setUp(self):
7785 self .csrf_client_anon = APIClient (enforce_csrf_checks = True )
7886 self .csrf_client_anon_1 = APIClient (enforce_csrf_checks = True )
7987
88+ def test_user_with_3600_perm_throttling (self ):
89+ simulate_throttle_usage (
90+ url = "/api/packages" ,
91+ client = self .th_3600_user_csrf_client ,
92+ mock_use_count = 3599 ,
93+ )
94+
95+ response = self .th_3600_user_csrf_client .get ("/api/packages" )
96+ self .assertEqual (response .status_code , status .HTTP_200_OK )
97+
98+ # exhausted 3600/hr allowed requests.
99+ response = self .th_3600_user_csrf_client .get ("/api/packages" )
100+ self .assertEqual (response .status_code , status .HTTP_429_TOO_MANY_REQUESTS )
101+
80102 def test_basic_user_throttling (self ):
81103 simulate_throttle_usage (
82104 url = "/api/packages" ,
@@ -87,7 +109,7 @@ def test_basic_user_throttling(self):
87109 response = self .basic_user_csrf_client .get ("/api/packages" )
88110 self .assertEqual (response .status_code , status .HTTP_200_OK )
89111
90- # exhausted 10800/hr allowed requests for basic user .
112+ # exhausted 10800/hr allowed requests.
91113 response = self .basic_user_csrf_client .get ("/api/packages" )
92114 self .assertEqual (response .status_code , status .HTTP_429_TOO_MANY_REQUESTS )
93115
0 commit comments