0

I am working on a project and i am using django restframework throttle it works well when using the server either on local or production but when i tries testing the endpoint using testcase it returns an error

This is my configurations

the default rest framework setting

REST_FRAMEWORK = {

'DEFAULT_THROTTLE_CLASSES': [
    'rest_framework.throttling.AnonRateThrottle',
    'rest_framework.throttling.UserRateThrottle',
    'rest_framework.throttling.ScopedRateThrottle',
],
'DEFAULT_THROTTLE_RATES': {
   
    'anon': '100/min',
    'user': '200/min',
    # the throttle is the amount of time a user can access a route in a minute
    'authentication': '3/min',
    # used on route where the user is not logged in like requesting otp
    'monitor': '3/min',
},
'DEFAULT_AUTHENTICATION_CLASSES': (
    # currently setting the default authentication for django rest framework to jwt
    'rest_framework_simplejwt.authentication.JWTAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (

    'rest_framework.permissions.IsAuthenticatedOrReadOnly',
),
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',

}

The test case

def test_verify_and_check_if_profile_created(self):
    """
    This test if the user profile was created,
    and also it verify the user first
    :return:
    """
    #  checks if the user profile is not none
    self.assertIsNotNone(self.user.user_profile)
    #  request otp for the set-up user
    client = APIClient()
    client.credentials(HTTP_INSTASAW_SK_HEADER=config('INSTASAW_SK_HEADER'))
    request_otp_response = client.post('/api/v1/auth/verify_account/', {
        "email": self.user_email,
    })
    print(request_otp_response.json())

The error

django.core.exceptions.ImproperlyConfigured: No default throttle rate set for 'anon' scope
Codertjay
  • 588
  • 8
  • 13
  • can you provide the test case code? – Maxim Danilov Aug 31 '22 at 12:42
  • What is the error you are getting? 429 response? That is is being throttled? Current situation vs expected would be nice to share. "An error" is ambiguous – The Pjot Aug 31 '22 at 12:50
  • 1
    You have a docstring in your dictionary. I wonder how that even works at all. – Klaus D. Aug 31 '22 at 13:00
  • 1
    You're correct @KlausD. that is indeed the problem. Tried it locally in my ipython, the key for `anon` is added to that docstring. Try using comments `#` instead. – The Pjot Aug 31 '22 at 13:09
  • I have removed the string but it just doesnt work and i noticed the problem was coming from this client.credentials(HTTP_INSTASAW_SK_HEADER=config('INSTASAW_SK_HEADER')) because it seems after adding this it remove the headers needed for throttle for something elese – Codertjay Aug 31 '22 at 13:10
  • No, that was not *the* problem. Your first problem was the multi-line string causing the ImproperlyConfigured. The 2nd problem might indeed be in the test itself. Can you update the question? – The Pjot Aug 31 '22 at 13:13
  • When i tried printing the headers it provided this the credentials {'HTTP_INSTASAW_SK_HEADER': 'the heder secret '} so it erase the content type and other headers – Codertjay Aug 31 '22 at 13:13
  • I just found out that on testserver in django using testcase is doenst returns all headers to i just bypass in the server is https://testserver/my/path/ – Codertjay Sep 17 '22 at 13:37

0 Answers0