Skip to content

Commit

Permalink
Remove legacy compat lib six
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotzbua committed Jan 13, 2023
1 parent d2fc245 commit 169e31a
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.serializers import DateTimeField
from rest_framework.test import APIRequestFactory, APITestCase as TestCase
from six.moves import reload_module

from knox import auth, crypto, views
from knox.auth import TokenAuthentication
Expand Down Expand Up @@ -93,15 +92,15 @@ def test_login_returns_serialized_token(self):
def test_login_returns_serialized_token_and_username_field(self):

with override_settings(REST_KNOX=user_serializer_knox):
reload_module(views)
importlib.reload(views)
self.assertEqual(AuthToken.objects.count(), 0)
url = reverse('knox_login')
self.client.credentials(
HTTP_AUTHORIZATION=get_basic_auth_header(self.username, self.password)
)
response = self.client.post(url, {}, format='json')
self.assertEqual(user_serializer_knox["USER_SERIALIZER"], UserSerializer)
reload_module(views)
importlib.reload(views)
self.assertEqual(response.status_code, 200)
self.assertIn('token', response.data)
username_field = self.user.USERNAME_FIELD
Expand All @@ -111,7 +110,7 @@ def test_login_returns_serialized_token_and_username_field(self):
def test_login_returns_configured_expiry_datetime_format(self):

with override_settings(REST_KNOX=expiry_datetime_format_knox):
reload_module(views)
importlib.reload(views)
self.assertEqual(AuthToken.objects.count(), 0)
url = reverse('knox_login')
self.client.credentials(
Expand All @@ -122,7 +121,7 @@ def test_login_returns_configured_expiry_datetime_format(self):
expiry_datetime_format_knox["EXPIRY_DATETIME_FORMAT"],
EXPIRY_DATETIME_FORMAT
)
reload_module(views)
importlib.reload(views)
self.assertEqual(response.status_code, 200)
self.assertIn('token', response.data)
self.assertNotIn('user', response.data)
Expand Down Expand Up @@ -257,10 +256,10 @@ def test_token_expiry_is_extended_with_auto_refresh_activated(self):
self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % token))
five_hours_later = original_time + timedelta(hours=5)
with override_settings(REST_KNOX=auto_refresh_knox):
reload_module(auth) # necessary to reload settings in core code
importlib.reload(auth) # necessary to reload settings in core code
with freeze_time(five_hours_later):
response = self.client.get(root_url, {}, format='json')
reload_module(auth)
importlib.reload(auth)
self.assertEqual(response.status_code, 200)

# original expiry date was extended:
Expand Down Expand Up @@ -309,10 +308,10 @@ def test_token_expiry_is_not_extended_within_MIN_REFRESH_INTERVAL(self):
self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % token))
in_min_interval = now + timedelta(seconds=knox_settings.MIN_REFRESH_INTERVAL - 10)
with override_settings(REST_KNOX=auto_refresh_knox):
reload_module(auth) # necessary to reload settings in core code
importlib.reload(auth) # necessary to reload settings in core code
with freeze_time(in_min_interval):
response = self.client.get(root_url, {}, format='json')
reload_module(auth) # necessary to reload settings in core code
importlib.reload(auth) # necessary to reload settings in core code

self.assertEqual(response.status_code, 200)
self.assertEqual(original_expiry, AuthToken.objects.get().expiry)
Expand All @@ -334,23 +333,23 @@ def handler(sender, username, **kwargs):
def test_exceed_token_amount_per_user(self):

with override_settings(REST_KNOX=token_user_limit_knox):
reload_module(views)
importlib.reload(views)
for _ in range(10):
AuthToken.objects.create(user=self.user)
url = reverse('knox_login')
self.client.credentials(
HTTP_AUTHORIZATION=get_basic_auth_header(self.username, self.password)
)
response = self.client.post(url, {}, format='json')
reload_module(views)
importlib.reload(views)
self.assertEqual(response.status_code, 403)
self.assertEqual(response.data,
{"error": "Maximum amount of tokens allowed per user exceeded."})

def test_does_not_exceed_on_expired_keys(self):

with override_settings(REST_KNOX=token_user_limit_knox):
reload_module(views)
importlib.reload(views)
for _ in range(9):
AuthToken.objects.create(user=self.user)
AuthToken.objects.create(user=self.user, expiry=timedelta(seconds=-1))
Expand All @@ -361,7 +360,7 @@ def test_does_not_exceed_on_expired_keys(self):
)
response = self.client.post(url, {}, format='json')
failed_response = self.client.post(url, {}, format='json')
reload_module(views)
importlib.reload(views)
self.assertEqual(response.status_code, 200)
self.assertIn('token', response.data)
self.assertEqual(failed_response.status_code, 403)
Expand All @@ -371,7 +370,7 @@ def test_does_not_exceed_on_expired_keys(self):
def test_invalid_prefix_return_401(self):

with override_settings(REST_KNOX=auth_header_prefix_knox):
reload_module(auth)
importlib.reload(auth)
instance, token = AuthToken.objects.create(user=self.user)
self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % token))
failed_response = self.client.get(root_url)
Expand All @@ -381,13 +380,13 @@ def test_invalid_prefix_return_401(self):
)
)
response = self.client.get(root_url)
reload_module(auth)
importlib.reload(auth)
self.assertEqual(failed_response.status_code, 401)
self.assertEqual(response.status_code, 200)

def test_expiry_present_also_when_none(self):
with override_settings(REST_KNOX=token_no_expiration_knox):
reload_module(views)
importlib.reload(views)
self.assertEqual(AuthToken.objects.count(), 0)
url = reverse('knox_login')
self.client.credentials(
Expand All @@ -406,7 +405,7 @@ def test_expiry_present_also_when_none(self):
response.data['expiry'],
None
)
reload_module(views)
importlib.reload(views)

def test_expiry_is_present(self):
self.assertEqual(AuthToken.objects.count(), 0)
Expand All @@ -429,8 +428,8 @@ def test_expiry_is_present(self):

def test_login_returns_serialized_token_with_prefix_when_prefix_set(self):
with override_settings(REST_KNOX=token_prefix_knox):
reload_module(views)
reload_module(crypto)
importlib.reload(views)
importlib.reload(crypto)
self.assertEqual(AuthToken.objects.count(), 0)
url = reverse('knox_login')
self.client.credentials(
Expand All @@ -443,12 +442,12 @@ def test_login_returns_serialized_token_with_prefix_when_prefix_set(self):
)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.data['token'].startswith(token_prefix))
reload_module(views)
reload_module(crypto)
importlib.reload(views)
importlib.reload(crypto)

def test_token_with_prefix_returns_200(self):
with override_settings(REST_KNOX=token_prefix_knox):
reload_module(views)
importlib.reload(views)
self.assertEqual(AuthToken.objects.count(), 0)
url = reverse('knox_login')
self.client.credentials(
Expand All @@ -464,7 +463,7 @@ def test_token_with_prefix_returns_200(self):
self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % response.data['token']))
response = self.client.get(root_url, {}, format='json')
self.assertEqual(response.status_code, 200)
reload_module(views)
importlib.reload(views)

def test_prefix_set_longer_than_max_length_raises_valueerror(self):
with self.assertRaises(ValueError):
Expand All @@ -483,8 +482,8 @@ def test_tokens_created_before_prefix_still_work(self):
)
self.assertFalse(response.data['token'].startswith(token_prefix))
with override_settings(REST_KNOX=token_prefix_knox):
reload_module(views)
importlib.reload(views)
self.client.credentials(HTTP_AUTHORIZATION=('Token %s' % response.data['token']))
response = self.client.get(root_url, {}, format='json')
self.assertEqual(response.status_code, 200)
reload_module(views)
importlib.reload(views)

0 comments on commit 169e31a

Please sign in to comment.