'How to add custom headers in django test cases?
I have implented a custom authentication class in django rest framework which requires client id and client secret on user registration in headers. I am writing test cases for user registration like this:-
User = get_user_model()
client = Client()
class TestUserRegister(TestCase):
def setUp(self):
# pass
self.test_users = {
'test_user': {
'email': '[email protected]',
'password': 'Test@1234',
'username': 'test',
'company': 'test',
'provider': 'email'
}
}
response = client.post(
reverse('user_register'),
headers={
"CLIENTID": <client id>,
"CLIENTSECRET": <client secret>
},
data={
'email': self.test_users['test_user']['email'],
'username': self.test_users['test_user']['username'],
'password': self.test_users['test_user']['password'],
'company': self.test_users['test_user']['company'],
'provider': self.test_users['test_user']['provider'],
},
content_type='application/json',
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_register(self):
response = client.post(
reverse('user_register'),
headers={
"CLIENTID": <client id>,
"CLIENTSECRET": <client secret>
},
data={
"first_name": "john",
"last_name": "williams",
"email": "[email protected]",
"password": "John@1234",
"username": "john",
"company": "john and co.",
"provider": "email",
},
content_type="application/json"
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Here is my custom authentication class:-
from oauth2_provider.models import Application
from rest_framework import authentication
from rest_framework.exceptions import APIException
class ClientAuthentication(authentication.BaseAuthentication):
@staticmethod
def get_client_credentials(request):
try:
client_id = request.headers.get('CLIENTID')
client_secret = request.headers.get('CLIENTSECRET')
except:
raise APIException(detail="Missing Client Credentials", code=400)
return {
'client_id': client_id,
'client_secret': client_secret
}
def authenticate(self, request):
credentials = self.get_client_credentials(request)
client_instance = Application.objects.filter(
client_id=credentials['client_id'],
client_secret=credentials['client_secret'],
).first()
if not client_instance:
raise APIException("Invalid client credentials", code=401)
return client_instance, None
But it is still giving me 500 internal server error everything is working fine if I dont add the custom authentication to my register view. How can i fix this?
Solution 1:[1]
To add custom headers you need to prefix them with HTTP_:
client.post(url, ..., HTTP_CLIENTID=<client id>, HTTP_CLIENTSECRET=<client secret>)
or using a dict:
headers = {"HTTP_CLIENTID": "Some id", "HTTP_CLIENTSECRET": "some secret"}
client.post(url, **headers)
Solution 2:[2]
According to the docs the way to pass headers is as additional keyword arguments
client.post(url, ..., CLIENTID=<client id>, CLIENTSECRET=<client secret>)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Iain Shelvington |