basicauth: add dedicated test for utf8 credentials

This commit is contained in:
kennell 2020-02-17 01:11:08 +01:00
parent 25819600c9
commit 2394a50b59

View File

@ -85,7 +85,7 @@ class BasicAuthTests(TestCase):
self.csrf_client = APIClient(enforce_csrf_checks=True) self.csrf_client = APIClient(enforce_csrf_checks=True)
self.username = 'john' self.username = 'john'
self.email = 'lennon@thebeatles.com' self.email = 'lennon@thebeatles.com'
self.password = 'pässwörd' self.password = 'password'
self.user = User.objects.create_user( self.user = User.objects.create_user(
self.username, self.email, self.password self.username, self.email, self.password
) )
@ -94,7 +94,7 @@ class BasicAuthTests(TestCase):
"""Ensure POSTing json over basic auth with correct credentials passes and does not require CSRF""" """Ensure POSTing json over basic auth with correct credentials passes and does not require CSRF"""
credentials = ('%s:%s' % (self.username, self.password)) credentials = ('%s:%s' % (self.username, self.password))
base64_credentials = base64.b64encode( base64_credentials = base64.b64encode(
credentials.encode('utf-8') credentials.encode(HTTP_HEADER_ENCODING)
).decode(HTTP_HEADER_ENCODING) ).decode(HTTP_HEADER_ENCODING)
auth = 'Basic %s' % base64_credentials auth = 'Basic %s' % base64_credentials
response = self.csrf_client.post( response = self.csrf_client.post(
@ -108,7 +108,7 @@ class BasicAuthTests(TestCase):
"""Ensure POSTing form over basic auth with correct credentials passes and does not require CSRF""" """Ensure POSTing form over basic auth with correct credentials passes and does not require CSRF"""
credentials = ('%s:%s' % (self.username, self.password)) credentials = ('%s:%s' % (self.username, self.password))
base64_credentials = base64.b64encode( base64_credentials = base64.b64encode(
credentials.encode('utf-8') credentials.encode(HTTP_HEADER_ENCODING)
).decode(HTTP_HEADER_ENCODING) ).decode(HTTP_HEADER_ENCODING)
auth = 'Basic %s' % base64_credentials auth = 'Basic %s' % base64_credentials
response = self.csrf_client.post( response = self.csrf_client.post(
@ -159,6 +159,25 @@ class BasicAuthTests(TestCase):
) )
assert response.status_code == status.HTTP_401_UNAUTHORIZED assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_decoding_of_utf8_credentials(self):
username = 'walterwhité'
email = 'walterwhite@example.com'
password = 'pässwörd'
User.objects.create_user(
username, email, password
)
credentials = ('%s:%s' % (username, password))
base64_credentials = base64.b64encode(
credentials.encode('utf-8')
).decode(HTTP_HEADER_ENCODING)
auth = 'Basic %s' % base64_credentials
response = self.csrf_client.post(
'/basic/',
{'example': 'example'},
HTTP_AUTHORIZATION=auth
)
assert response.status_code == status.HTTP_200_OK
@override_settings(ROOT_URLCONF=__name__) @override_settings(ROOT_URLCONF=__name__)
class SessionAuthTests(TestCase): class SessionAuthTests(TestCase):