mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-15 02:53:18 +03:00
converted authentication_test asserts to pytest
This commit is contained in:
parent
792b50feee
commit
85807e1958
|
@ -106,7 +106,7 @@ class BasicAuthTests(TestCase):
|
||||||
{'example': 'example'},
|
{'example': 'example'},
|
||||||
HTTP_AUTHORIZATION=auth
|
HTTP_AUTHORIZATION=auth
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
|
||||||
def test_post_json_passing_basic_auth(self):
|
def test_post_json_passing_basic_auth(self):
|
||||||
"""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"""
|
||||||
|
@ -121,7 +121,7 @@ class BasicAuthTests(TestCase):
|
||||||
format='json',
|
format='json',
|
||||||
HTTP_AUTHORIZATION=auth
|
HTTP_AUTHORIZATION=auth
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
|
||||||
def test_regression_handle_bad_base64_basic_auth_header(self):
|
def test_regression_handle_bad_base64_basic_auth_header(self):
|
||||||
"""Ensure POSTing JSON over basic auth with incorrectly padded Base64 string is handled correctly"""
|
"""Ensure POSTing JSON over basic auth with incorrectly padded Base64 string is handled correctly"""
|
||||||
|
@ -134,12 +134,12 @@ class BasicAuthTests(TestCase):
|
||||||
format='json',
|
format='json',
|
||||||
HTTP_AUTHORIZATION=auth
|
HTTP_AUTHORIZATION=auth
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||||
|
|
||||||
def test_post_form_failing_basic_auth(self):
|
def test_post_form_failing_basic_auth(self):
|
||||||
"""Ensure POSTing form over basic auth without correct credentials fails"""
|
"""Ensure POSTing form over basic auth without correct credentials fails"""
|
||||||
response = self.csrf_client.post('/basic/', {'example': 'example'})
|
response = self.csrf_client.post('/basic/', {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||||
|
|
||||||
def test_post_json_failing_basic_auth(self):
|
def test_post_json_failing_basic_auth(self):
|
||||||
"""Ensure POSTing json over basic auth without correct credentials fails"""
|
"""Ensure POSTing json over basic auth without correct credentials fails"""
|
||||||
|
@ -148,8 +148,8 @@ class BasicAuthTests(TestCase):
|
||||||
{'example': 'example'},
|
{'example': 'example'},
|
||||||
format='json'
|
format='json'
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||||
self.assertEqual(response['WWW-Authenticate'], 'Basic realm="api"')
|
assert response['WWW-Authenticate'] == 'Basic realm="api"'
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_authentication')
|
@override_settings(ROOT_URLCONF='tests.test_authentication')
|
||||||
|
@ -175,9 +175,7 @@ class SessionAuthTests(TestCase):
|
||||||
cf. [#1810](https://github.com/tomchristie/django-rest-framework/pull/1810)
|
cf. [#1810](https://github.com/tomchristie/django-rest-framework/pull/1810)
|
||||||
"""
|
"""
|
||||||
response = self.csrf_client.get('/auth/login/')
|
response = self.csrf_client.get('/auth/login/')
|
||||||
self.assertContains(
|
assert '<label for="id_username">Username:</label>' in response
|
||||||
response, '<label for="id_username">Username:</label>'
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_post_form_session_auth_failing_csrf(self):
|
def test_post_form_session_auth_failing_csrf(self):
|
||||||
"""
|
"""
|
||||||
|
@ -185,7 +183,7 @@ class SessionAuthTests(TestCase):
|
||||||
"""
|
"""
|
||||||
self.csrf_client.login(username=self.username, password=self.password)
|
self.csrf_client.login(username=self.username, password=self.password)
|
||||||
response = self.csrf_client.post('/session/', {'example': 'example'})
|
response = self.csrf_client.post('/session/', {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||||
|
|
||||||
def test_post_form_session_auth_passing(self):
|
def test_post_form_session_auth_passing(self):
|
||||||
"""
|
"""
|
||||||
|
@ -198,7 +196,7 @@ class SessionAuthTests(TestCase):
|
||||||
response = self.non_csrf_client.post(
|
response = self.non_csrf_client.post(
|
||||||
'/session/', {'example': 'example'}
|
'/session/', {'example': 'example'}
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
|
||||||
def test_put_form_session_auth_passing(self):
|
def test_put_form_session_auth_passing(self):
|
||||||
"""
|
"""
|
||||||
|
@ -211,14 +209,14 @@ class SessionAuthTests(TestCase):
|
||||||
response = self.non_csrf_client.put(
|
response = self.non_csrf_client.put(
|
||||||
'/session/', {'example': 'example'}
|
'/session/', {'example': 'example'}
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
|
||||||
def test_post_form_session_auth_failing(self):
|
def test_post_form_session_auth_failing(self):
|
||||||
"""
|
"""
|
||||||
Ensure POSTing form over session authentication without logged in user fails.
|
Ensure POSTing form over session authentication without logged in user fails.
|
||||||
"""
|
"""
|
||||||
response = self.csrf_client.post('/session/', {'example': 'example'})
|
response = self.csrf_client.post('/session/', {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||||
|
|
||||||
|
|
||||||
class BaseTokenAuthTests(object):
|
class BaseTokenAuthTests(object):
|
||||||
|
@ -248,7 +246,7 @@ class BaseTokenAuthTests(object):
|
||||||
response = self.csrf_client.post(
|
response = self.csrf_client.post(
|
||||||
self.path, {'example': 'example'}, HTTP_AUTHORIZATION=auth
|
self.path, {'example': 'example'}, HTTP_AUTHORIZATION=auth
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
|
||||||
def test_fail_post_form_passing_nonexistent_token_auth(self):
|
def test_fail_post_form_passing_nonexistent_token_auth(self):
|
||||||
# use a nonexistent token key
|
# use a nonexistent token key
|
||||||
|
@ -256,7 +254,7 @@ class BaseTokenAuthTests(object):
|
||||||
response = self.csrf_client.post(
|
response = self.csrf_client.post(
|
||||||
self.path, {'example': 'example'}, HTTP_AUTHORIZATION=auth
|
self.path, {'example': 'example'}, HTTP_AUTHORIZATION=auth
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||||
|
|
||||||
def test_fail_post_form_passing_invalid_token_auth(self):
|
def test_fail_post_form_passing_invalid_token_auth(self):
|
||||||
# add an 'invalid' unicode character
|
# add an 'invalid' unicode character
|
||||||
|
@ -264,7 +262,7 @@ class BaseTokenAuthTests(object):
|
||||||
response = self.csrf_client.post(
|
response = self.csrf_client.post(
|
||||||
self.path, {'example': 'example'}, HTTP_AUTHORIZATION=auth
|
self.path, {'example': 'example'}, HTTP_AUTHORIZATION=auth
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||||
|
|
||||||
def test_post_json_passing_token_auth(self):
|
def test_post_json_passing_token_auth(self):
|
||||||
"""
|
"""
|
||||||
|
@ -276,7 +274,7 @@ class BaseTokenAuthTests(object):
|
||||||
self.path, {'example': 'example'},
|
self.path, {'example': 'example'},
|
||||||
format='json', HTTP_AUTHORIZATION=auth
|
format='json', HTTP_AUTHORIZATION=auth
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
|
||||||
def test_post_json_makes_one_db_query(self):
|
def test_post_json_makes_one_db_query(self):
|
||||||
"""
|
"""
|
||||||
|
@ -298,7 +296,7 @@ class BaseTokenAuthTests(object):
|
||||||
Ensure POSTing form over token auth without correct credentials fails
|
Ensure POSTing form over token auth without correct credentials fails
|
||||||
"""
|
"""
|
||||||
response = self.csrf_client.post(self.path, {'example': 'example'})
|
response = self.csrf_client.post(self.path, {'example': 'example'})
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||||
|
|
||||||
def test_post_json_failing_token_auth(self):
|
def test_post_json_failing_token_auth(self):
|
||||||
"""
|
"""
|
||||||
|
@ -307,7 +305,7 @@ class BaseTokenAuthTests(object):
|
||||||
response = self.csrf_client.post(
|
response = self.csrf_client.post(
|
||||||
self.path, {'example': 'example'}, format='json'
|
self.path, {'example': 'example'}, format='json'
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_authentication')
|
@override_settings(ROOT_URLCONF='tests.test_authentication')
|
||||||
|
@ -319,13 +317,13 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase):
|
||||||
"""Ensure creating a token with no key will auto-assign a key"""
|
"""Ensure creating a token with no key will auto-assign a key"""
|
||||||
self.token.delete()
|
self.token.delete()
|
||||||
token = self.model.objects.create(user=self.user)
|
token = self.model.objects.create(user=self.user)
|
||||||
self.assertTrue(bool(token.key))
|
assert bool(token.key)
|
||||||
|
|
||||||
def test_generate_key_returns_string(self):
|
def test_generate_key_returns_string(self):
|
||||||
"""Ensure generate_key returns a string"""
|
"""Ensure generate_key returns a string"""
|
||||||
token = self.model()
|
token = self.model()
|
||||||
key = token.generate_key()
|
key = token.generate_key()
|
||||||
self.assertTrue(isinstance(key, six.string_types))
|
assert isinstance(key, six.string_types)
|
||||||
|
|
||||||
def test_token_login_json(self):
|
def test_token_login_json(self):
|
||||||
"""Ensure token login view using JSON POST works."""
|
"""Ensure token login view using JSON POST works."""
|
||||||
|
@ -335,8 +333,8 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase):
|
||||||
{'username': self.username, 'password': self.password},
|
{'username': self.username, 'password': self.password},
|
||||||
format='json'
|
format='json'
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
assert response.status_code == status.HTTP_200_OK
|
||||||
self.assertEqual(response.data['token'], self.key)
|
assert response.data['token'] == self.key
|
||||||
|
|
||||||
def test_token_login_json_bad_creds(self):
|
def test_token_login_json_bad_creds(self):
|
||||||
"""
|
"""
|
||||||
|
@ -349,22 +347,24 @@ class TokenAuthTests(BaseTokenAuthTests, TestCase):
|
||||||
{'username': self.username, 'password': "badpass"},
|
{'username': self.username, 'password': "badpass"},
|
||||||
format='json'
|
format='json'
|
||||||
)
|
)
|
||||||
self.assertEqual(response.status_code, 400)
|
assert response.status_code == 400
|
||||||
|
|
||||||
def test_token_login_json_missing_fields(self):
|
def test_token_login_json_missing_fields(self):
|
||||||
"""Ensure token login view using JSON POST fails if missing fields."""
|
"""Ensure token login view using JSON POST fails if missing fields."""
|
||||||
client = APIClient(enforce_csrf_checks=True)
|
client = APIClient(enforce_csrf_checks=True)
|
||||||
response = client.post('/auth-token/',
|
response = client.post('/auth-token/',
|
||||||
{'username': self.username}, format='json')
|
{'username': self.username}, format='json')
|
||||||
self.assertEqual(response.status_code, 400)
|
assert response.status_code == 400
|
||||||
|
|
||||||
def test_token_login_form(self):
|
def test_token_login_form(self):
|
||||||
"""Ensure token login view using form POST works."""
|
"""Ensure token login view using form POST works."""
|
||||||
client = APIClient(enforce_csrf_checks=True)
|
client = APIClient(enforce_csrf_checks=True)
|
||||||
response = client.post('/auth-token/',
|
response = client.post(
|
||||||
{'username': self.username, 'password': self.password})
|
'/auth-token/',
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
{'username': self.username, 'password': self.password}
|
||||||
self.assertEqual(response.data['token'], self.key)
|
)
|
||||||
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
assert response.data['token'] == self.key
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_authentication')
|
@override_settings(ROOT_URLCONF='tests.test_authentication')
|
||||||
|
@ -397,8 +397,8 @@ class IncorrectCredentialsTests(TestCase):
|
||||||
permission_classes=()
|
permission_classes=()
|
||||||
)
|
)
|
||||||
response = view(request)
|
response = view(request)
|
||||||
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
|
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||||
self.assertEqual(response.data, {'detail': 'Bad credentials'})
|
assert response.data == {'detail': 'Bad credentials'}
|
||||||
|
|
||||||
|
|
||||||
class FailingAuthAccessedInRenderer(TestCase):
|
class FailingAuthAccessedInRenderer(TestCase):
|
||||||
|
@ -435,7 +435,7 @@ class FailingAuthAccessedInRenderer(TestCase):
|
||||||
request = factory.get('/')
|
request = factory.get('/')
|
||||||
response = self.view(request)
|
response = self.view(request)
|
||||||
content = response.render().content
|
content = response.render().content
|
||||||
self.assertEqual(content, b'not authenticated')
|
assert content == b'not authenticated'
|
||||||
|
|
||||||
|
|
||||||
class NoAuthenticationClassesTests(TestCase):
|
class NoAuthenticationClassesTests(TestCase):
|
||||||
|
@ -458,6 +458,5 @@ class NoAuthenticationClassesTests(TestCase):
|
||||||
permission_classes=(DummyPermission,),
|
permission_classes=(DummyPermission,),
|
||||||
)
|
)
|
||||||
response = view(request)
|
response = view(request)
|
||||||
self.assertEqual(response.status_code,
|
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||||
status.HTTP_403_FORBIDDEN)
|
assert response.data == {'detail': 'Dummy permission message'}
|
||||||
self.assertEqual(response.data, {'detail': 'Dummy permission message'})
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user