diff --git a/tests/tests.py b/tests/tests.py index 574305c2..409a498a 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -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 @@ -93,7 +92,7 @@ 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( @@ -101,7 +100,7 @@ def test_login_returns_serialized_token_and_username_field(self): ) 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 @@ -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( @@ -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) @@ -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: @@ -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) @@ -334,7 +333,7 @@ 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') @@ -342,7 +341,7 @@ def test_exceed_token_amount_per_user(self): 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."}) @@ -350,7 +349,7 @@ def test_exceed_token_amount_per_user(self): 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)) @@ -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) @@ -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) @@ -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( @@ -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) @@ -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( @@ -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( @@ -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): @@ -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)