Skip to content

Commit

Permalink
Merge pull request jazzband#294 from Rotzbua/remove_legacy_py2
Browse files Browse the repository at this point in the history
Remove legacy code from Python 2
  • Loading branch information
giovannicimolin authored Oct 10, 2023
2 parents 678d20a + 5cb9fe6 commit 4d519b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
7 changes: 1 addition & 6 deletions knox/auth.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
try:
from hmac import compare_digest
except ImportError:
def compare_digest(a, b):
return a == b

import binascii
from hmac import compare_digest

from django.utils import timezone
from django.utils.translation import gettext_lazy as _
Expand Down
50 changes: 25 additions & 25 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
from datetime import datetime, timedelta
from importlib import reload

from django.contrib.auth import get_user_model
from django.test import override_settings
Expand All @@ -8,7 +9,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 @@ -94,15 +94,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)
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)
(views)
self.assertEqual(response.status_code, 200)
self.assertIn('token', response.data)
username_field = self.user.USERNAME_FIELD
Expand All @@ -112,7 +112,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)
reload(views)
self.assertEqual(AuthToken.objects.count(), 0)
url = reverse('knox_login')
self.client.credentials(
Expand All @@ -123,7 +123,7 @@ def test_login_returns_configured_expiry_datetime_format(self):
expiry_datetime_format_knox["EXPIRY_DATETIME_FORMAT"],
EXPIRY_DATETIME_FORMAT
)
reload_module(views)
reload(views)
self.assertEqual(response.status_code, 200)
self.assertIn('token', response.data)
self.assertNotIn('user', response.data)
Expand Down Expand Up @@ -258,10 +258,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
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)
reload(auth)
self.assertEqual(response.status_code, 200)

# original expiry date was extended:
Expand Down Expand Up @@ -310,10 +310,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
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
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 @@ -338,23 +338,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)
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)
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)
reload(views)
for _ in range(9):
AuthToken.objects.create(user=self.user)
AuthToken.objects.create(user=self.user, expiry=timedelta(seconds=-1))
Expand All @@ -365,7 +365,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)
reload(views)
self.assertEqual(response.status_code, 200)
self.assertIn('token', response.data)
self.assertEqual(failed_response.status_code, 403)
Expand All @@ -375,7 +375,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)
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 @@ -385,13 +385,13 @@ def test_invalid_prefix_return_401(self):
)
)
response = self.client.get(root_url)
reload_module(auth)
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)
reload(views)
self.assertEqual(AuthToken.objects.count(), 0)
url = reverse('knox_login')
self.client.credentials(
Expand All @@ -410,7 +410,7 @@ def test_expiry_present_also_when_none(self):
response.data['expiry'],
None
)
reload_module(views)
reload(views)

def test_expiry_is_present(self):
self.assertEqual(AuthToken.objects.count(), 0)
Expand All @@ -433,8 +433,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)
reload(views)
reload(crypto)
self.assertEqual(AuthToken.objects.count(), 0)
url = reverse('knox_login')
self.client.credentials(
Expand All @@ -447,12 +447,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)
reload(views)
reload(crypto)

def test_token_with_prefix_returns_200(self):
with override_settings(REST_KNOX=token_prefix_knox):
reload_module(views)
reload(views)
self.assertEqual(AuthToken.objects.count(), 0)
url = reverse('knox_login')
self.client.credentials(
Expand All @@ -470,7 +470,7 @@ def test_token_with_prefix_returns_200(self):
)
response = self.client.get(root_url, {}, format='json')
self.assertEqual(response.status_code, 200)
reload_module(views)
reload(views)

def test_prefix_set_longer_than_max_length_raises_valueerror(self):
with self.assertRaises(ValueError):
Expand All @@ -489,10 +489,10 @@ 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)
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)
reload(views)

0 comments on commit 4d519b9

Please sign in to comment.