99
1010import json
1111
12+ from django .contrib .auth .models import Group
1213from django .core .cache import cache
1314from rest_framework .test import APIClient
1415from rest_framework .test import APITestCase
@@ -23,12 +24,20 @@ def setUp(self):
2324 # See https://www.django-rest-framework.org/api-guide/throttling/#setting-up-the-cache
2425 cache .clear ()
2526
26- # create a basic user
27+ # create a basic user (silver)
2728 self .user = ApiUser .objects .create_api_user (username = "e@mail.com" )
2829 self .auth = f"Token { self .user .auth_token .key } "
2930 self .csrf_client = APIClient (enforce_csrf_checks = True )
3031 self .csrf_client .credentials (HTTP_AUTHORIZATION = self .auth )
3132
33+ # create user (gold)
34+ self .gold_user = ApiUser .objects .create_api_user (username = "g@mail.com" )
35+ gold , _ = Group .objects .get_or_create (name = "gold" )
36+ self .gold_user .groups .add (gold )
37+ self .gold_auth = f"Token { self .gold_user .auth_token .key } "
38+ self .gold_csrf_client = APIClient (enforce_csrf_checks = True )
39+ self .gold_csrf_client .credentials (HTTP_AUTHORIZATION = self .gold_auth )
40+
3241 # create a staff user
3342 self .staff_user = ApiUser .objects .create_api_user (username = "staff@mail.com" , is_staff = True )
3443 self .staff_auth = f"Token { self .staff_user .auth_token .key } "
@@ -45,6 +54,12 @@ def test_package_endpoint_throttling(self):
4554 response = self .staff_csrf_client .get ("/api/packages" )
4655 self .assertEqual (response .status_code , 200 )
4756
57+ for i in range (0 , 25 ):
58+ response = self .gold_csrf_client .get ("/api/packages" )
59+ self .assertEqual (response .status_code , 200 )
60+ response = self .csrf_client .get ("/api/packages" )
61+ self .assertEqual (response .status_code , 200 )
62+
4863 response = self .csrf_client .get ("/api/packages" )
4964 # 429 - too many requests for basic user
5065 self .assertEqual (response .status_code , 429 )
0 commit comments