92
Here’s how I did it:
from django.test import Client
import base64
auth_headers = {
'HTTP_AUTHORIZATION': 'Basic ' + base64.b64encode('username:password'),
}
c = Client()
response = c.get('/my-protected-url/', **auth_headers)
Note: You will also need to create a user.
34
In your Django TestCase you can update the client defaults to contain your HTTP basic auth credentials.
import base64
from django.test import TestCase
class TestMyStuff(TestCase):
def setUp(self):
credentials = base64.b64encode('username:password')
self.client.defaults['HTTP_AUTHORIZATION'] = 'Basic ' + credentials
- [Django]-Django: Adding "NULLS LAST" to query
- [Django]-How can I apply a filter to a nested resource in Django REST framework?
- [Django]-How do I integrate Ajax with Django applications?
8
For python3, you can base64-encode your username:password
string:
base64.b64encode(b'username:password')
This returns bytes, so you need to transfer it into an ASCII string with .decode('ascii')
:
Complete example:
import base64
from django.test import TestCase
class TestClass(TestCase):
def test_authorized(self):
headers = {
'HTTP_AUTHORIZATION': 'Basic ' +
base64.b64encode(b'username:password').decode("ascii")
}
response = self.client.get('/', **headers)
self.assertEqual(response.status_code, 200)
- [Django]-Django Aggregation: Summation of Multiplication of two fields
- [Django]-Django rest framework, use different serializers in the same ModelViewSet
- [Django]-How can I render a tree structure (recursive) using a django template?
2
Assuming I have a login form, I use the following technique to login through the test framework:
client = Client()
client.post('/login/', {'username': 'john.smith', 'password': 'secret'})
I then carry the client
around in my other tests since it’s already authenticated. What is your question to this post?
- [Django]-How do I display the Django '__all__' form errors in the template?
- [Django]-DRY way to add created/modified by and time
- [Django]-Filtering dropdown values in django admin
2
(python3) I’m using this in a test:
credentials_string = '%s:%s' % ('invalid', 'invalid')
credentials = base64.b64encode(credentials_string.encode())
self.client.defaults['HTTP_AUTHORIZATION'] = 'Basic ' + credentials.decode()
and the following in a view:
import base64
[...]
type, auth = request.META['HTTP_AUTHORIZATION'].split(' ', 1)
auth = base64.b64decode(auth.strip()).decode()
- [Django]-Case insensitive unique model fields in Django?
- [Django]-Aggregate() vs annotate() in Django
- [Django]-How do you set DEBUG to True when running a Django test?
-1
Another way to do it is to bypass the Django Client() and use Requests instead.
class MyTest(TestCase):
def setUp(self):
AUTH = requests.auth.HTTPBasicAuth("username", "password")
def some_test(self):
resp = requests.get(BASE_URL + 'endpoint/', auth=AUTH)
self.assertEqual(resp.status_code, 200)
- [Django]-Overriding the save method in Django ModelForm
- [Django]-Check if OneToOneField is None in Django
- [Django]-How to resize an ImageField image before saving it in python Django model
Source:stackexchange.com