Skip to content

Commit a5262f3

Browse files
authored
Merge pull request #151 from anx-bhagmann/django4-deprecation-fixes
Fix deprecation warnings for Django 4.0 #hacktoberfest
2 parents 039f2c7 + b72f59d commit a5262f3

File tree

6 files changed

+57
-52
lines changed

6 files changed

+57
-52
lines changed
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import django.dispatch
1+
from django.dispatch import Signal
22

33
__all__ = [
44
'reset_password_token_created',
55
'pre_password_reset',
66
'post_password_reset',
77
]
88

9-
reset_password_token_created = django.dispatch.Signal(
10-
providing_args=["instance", "reset_password_token"],
11-
)
9+
"""
10+
Signal arguments: instance, reset_password_token
11+
"""
12+
reset_password_token_created = Signal()
1213

13-
pre_password_reset = django.dispatch.Signal(providing_args=["user"])
14+
"""
15+
Signal arguments: user
16+
"""
17+
pre_password_reset = Signal()
1418

15-
post_password_reset = django.dispatch.Signal(providing_args=["user"])
19+
"""
20+
Signal arguments: user
21+
"""
22+
post_password_reset = Signal()

django_rest_passwordreset/urls.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
""" URL Configuration for core auth """
2-
from django.conf.urls import url
2+
from django.urls import path
33

4-
from django_rest_passwordreset.views import ResetPasswordValidateTokenViewSet, ResetPasswordConfirmViewSet, \
5-
ResetPasswordRequestTokenViewSet, reset_password_validate_token, reset_password_confirm, \
6-
reset_password_request_token
4+
from django_rest_passwordreset.views import ResetPasswordConfirmViewSet, ResetPasswordRequestTokenViewSet, \
5+
ResetPasswordValidateTokenViewSet, reset_password_confirm, reset_password_request_token, \
6+
reset_password_validate_token
77

88
app_name = 'password_reset'
99

1010

1111
def add_reset_password_urls_to_router(router, base_path=''):
1212
router.register(
13-
base_path + r'/validate_token',
13+
base_path + "/validate_token",
1414
ResetPasswordValidateTokenViewSet,
1515
basename='reset-password-validate'
1616
)
1717
router.register(
18-
base_path + r'/confirm',
18+
base_path + "/confirm",
1919
ResetPasswordConfirmViewSet,
2020
basename='reset-password-confirm'
2121
)
2222
router.register(
23-
base_path + r'',
23+
base_path,
2424
ResetPasswordRequestTokenViewSet,
2525
basename='reset-password-request'
2626
)
2727

2828

2929
urlpatterns = [
30-
url(r'^validate_token/', reset_password_validate_token, name="reset-password-validate"),
31-
url(r'^confirm/', reset_password_confirm, name="reset-password-confirm"),
32-
url(r'^', reset_password_request_token, name="reset-password-request"),
30+
path("validate_token/", reset_password_validate_token, name="reset-password-validate"),
31+
path("confirm/", reset_password_confirm, name="reset-password-confirm"),
32+
path("", reset_password_request_token, name="reset-password-request"),
3333
]

tests/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
django>=2.2,<3.3
2+
djangorestframework>=3.12.4,<3.13

tests/test/test_auth_test_case.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import json
2-
from django.test import override_settings
2+
33
from django.contrib.auth.models import User
4-
from django.db.models import Q
4+
from django.test import override_settings
55
from rest_framework import status
66
from rest_framework.test import APITestCase
7+
78
from django_rest_passwordreset.models import ResetPasswordToken
89
from tests.test.helpers import HelperMixin, patch
910

@@ -12,6 +13,7 @@ class AuthTestCase(APITestCase, HelperMixin):
1213
"""
1314
Several Test Cases for the Multi Auth Token Django App
1415
"""
16+
1517
def setUp(self):
1618
self.setUpUrls()
1719
self.user1 = User.objects.create_user("user1", "[email protected]", "secret1")
@@ -72,7 +74,7 @@ def test_validate_bad_token(self):
7274
# try to validate an invalid token
7375
response = self.rest_do_validate_token("not_a_valid_token")
7476
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
75-
77+
7678
# there should be zero tokens
7779
self.assertEqual(ResetPasswordToken.objects.all().count(), 0)
7880

@@ -224,21 +226,20 @@ def test_reset_password_multiple_users(self, mock_reset_password_token_created):
224226
# connect signal
225227
# we need to check whether the signal is getting called
226228

227-
228229
# create a token for user 1
229230
response = self.rest_do_request_reset_token(email="[email protected]")
230231
self.assertEqual(response.status_code, status.HTTP_200_OK)
231232
self.assertEqual(ResetPasswordToken.objects.all().count(), 1)
232233
self.assertTrue(mock_reset_password_token_created.called)
233-
self.assertEquals(mock_reset_password_token_created.call_count, 1)
234+
self.assertEqual(mock_reset_password_token_created.call_count, 1)
234235
token1 = mock_reset_password_token_created.call_args[1]['reset_password_token']
235236

236237
# create another token for user 2
237238
response = self.rest_do_request_reset_token(email="[email protected]")
238239
self.assertEqual(response.status_code, status.HTTP_200_OK)
239240
tokens = ResetPasswordToken.objects.all()
240241
self.assertEqual(tokens.count(), 2)
241-
self.assertEquals(mock_reset_password_token_created.call_count, 2)
242+
self.assertEqual(mock_reset_password_token_created.call_count, 2)
242243
token2 = mock_reset_password_token_created.call_args[1]['reset_password_token']
243244

244245
# validate that those two tokens are different
@@ -269,7 +270,7 @@ def test_reset_password_multiple_users(self, mock_reset_password_token_created):
269270

270271
# try to reset again with token2 (should not work)
271272
response = self.rest_do_reset_password_with_token(token2.key, "secret2_fake_new")
272-
self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND)
273+
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
273274

274275
# user 2 should still be able to login with "secret2_new" now
275276
self.assertTrue(
@@ -296,10 +297,11 @@ def test_signals(self,
296297

297298
# verify that the reset_password_token_created signal was fired
298299
self.assertTrue(mock_reset_password_token_created.called)
299-
self.assertEquals(mock_reset_password_token_created.call_count, 1)
300+
self.assertEqual(mock_reset_password_token_created.call_count, 1)
300301

301302
token1 = mock_reset_password_token_created.call_args[1]['reset_password_token']
302-
self.assertNotEqual(token1.key, "", msg="Verify that the reset_password_token of the reset_password_Token_created signal is not empty")
303+
self.assertNotEqual(token1.key, "",
304+
msg="Verify that the reset_password_token of the reset_password_Token_created signal is not empty")
303305

304306
# verify that the other two signals have not yet been called
305307
self.assertFalse(mock_post_password_reset.called)
@@ -315,12 +317,13 @@ def test_signals(self,
315317

316318
@override_settings(DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE=True)
317319
def test_try_reset_password_email_does_not_exist_no_leakage_enabled(self):
318-
"""
320+
"""
319321
Tests requesting a token for an email that does not exist when
320322
DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE == True
321-
"""
323+
"""
322324
response = self.rest_do_request_reset_token(email="[email protected]")
323325
self.assertEqual(response.status_code, status.HTTP_200_OK)
326+
324327
def test_user_without_password(self):
325328
""" Tests requesting a token for an email without a password doesn't work"""
326329
response = self.rest_do_request_reset_token(email="[email protected]")
@@ -334,7 +337,6 @@ def test_user_without_password(self):
334337
def test_user_without_password_where_not_required(self, mock_reset_password_token_created):
335338
""" Tests requesting a token for an email without a password works when not required"""
336339
response = self.rest_do_request_reset_token(email="[email protected]")
337-
decoded_response = json.loads(response.content.decode())
338340
self.assertEqual(response.status_code, status.HTTP_200_OK)
339341
# check that the signal was sent once
340342
self.assertTrue(mock_reset_password_token_created.called)
@@ -372,4 +374,3 @@ def test_user_without_password_where_not_required(self, mock_reset_password_toke
372374
self.django_check_login("user4", "new_secret"),
373375
msg="User 4 should be able to login with the modified credentials"
374376
)
375-

tests/test/test_token_generators.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
from django.test import TestCase
21
from django.conf import settings
2+
from django.test import TestCase
33

4-
from django_rest_passwordreset.tokens import RandomStringTokenGenerator, RandomNumberTokenGenerator, get_token_generator
4+
from django_rest_passwordreset.tokens import RandomNumberTokenGenerator, RandomStringTokenGenerator, get_token_generator
55

66

77
class TokenGeneratorTestCase(TestCase):
88
"""
99
Tests that the token generators work as expected
1010
"""
11-
def setUp(self):
12-
pass
1311

1412
def test_string_token_generator(self):
1513
token_generator = RandomStringTokenGenerator(min_length=10, max_length=15)
1614

1715
tokens = []
1816

1917
# generate 100 tokens
20-
for i in range(0, 100):
18+
for _ in range(0, 100):
2119
tokens.append(token_generator.generate_token())
2220

2321
# validate that those 100 tokens are unique
2422
unique_tokens = list(set(tokens))
2523

26-
self.assertEquals(
24+
self.assertEqual(
2725
len(tokens), len(unique_tokens), msg="StringTokenGenerator must create unique tokens"
2826
)
2927
################################################################################################################
@@ -46,13 +44,13 @@ def test_number_token_generator(self):
4644
tokens = []
4745

4846
# generate 100 tokens
49-
for i in range(0, 100):
47+
for _ in range(0, 100):
5048
tokens.append(token_generator.generate_token())
5149

5250
# validate that those 100 tokens are unique
5351
unique_tokens = list(set(tokens))
5452

55-
self.assertEquals(
53+
self.assertEqual(
5654
len(tokens), len(unique_tokens), msg="RandomNumberTokenGenerator must create unique tokens"
5755
)
5856
################################################################################################################
@@ -62,18 +60,15 @@ def test_number_token_generator(self):
6260

6361
# validate that each token is a number between 100000 and 999999
6462
for token in tokens:
65-
is_number = False
6663
try:
6764
num = int(token)
68-
is_number = True
6965
except:
70-
is_number = False
71-
72-
self.assertEquals(is_number, True, msg="RandomNumberTokenGenerator must return a number, but returned "
73-
+ token)
66+
self.fail("RandomNumberTokenGenerator must return a number, but returned " + token)
7467

75-
self.assertGreaterEqual(num, 1000000000, msg="RandomNumberTokenGenerator must return a number greater or equal to 1000000000")
76-
self.assertLess(num, 9999999999, msg="RandomNumberTokenGenerator must return a number less or equal to 9999999999")
68+
self.assertGreaterEqual(num, 1000000000,
69+
msg="RandomNumberTokenGenerator must return a number greater or equal to 1000000000")
70+
self.assertLess(num, 9999999999,
71+
msg="RandomNumberTokenGenerator must return a number less or equal to 9999999999")
7772

7873
def test_generate_token_generator_from_empty_settings(self):
7974
"""
@@ -86,7 +81,7 @@ def test_generate_token_generator_from_empty_settings(self):
8681

8782
token_generator = get_token_generator()
8883

89-
self.assertEquals(
84+
self.assertEqual(
9085
token_generator.__class__, RandomStringTokenGenerator,
9186
msg="If no class is set in DJANGO_REST_PASSWORDRESET_TOKEN_CONFIG, a RandomStringTokenGenerator should"
9287
"be created"
@@ -104,7 +99,7 @@ def test_generate_token_generator_from_settings_string_token_generator(self):
10499

105100
token_generator = get_token_generator()
106101

107-
self.assertEquals(
102+
self.assertEqual(
108103
token_generator.__class__, RandomStringTokenGenerator,
109104
msg="get_token_generator() should return an instance of RandomStringTokenGenerator "
110105
"if configured in settings"
@@ -122,7 +117,7 @@ def test_generate_token_generator_from_settings_number_token_generator(self):
122117

123118
token_generator = get_token_generator()
124119

125-
self.assertEquals(
120+
self.assertEqual(
126121
token_generator.__class__, RandomNumberTokenGenerator,
127122
msg="get_token_generator() should return an instance of RandomNumberTokenGenerator "
128123
"if configured in settings"

tests/urls.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
""" Tests App URL Config """
2-
from django.conf.urls import url, include
2+
from django.conf.urls import include
33
from django.contrib import admin
4-
4+
from django.urls import path
55

66
urlpatterns = [
7-
url(r'^api/password_reset/', include('django_rest_passwordreset.urls', namespace='password_reset')),
8-
url(r'^admin/', admin.site.urls),
7+
path("api/password_reset/", include('django_rest_passwordreset.urls', namespace='password_reset')),
8+
path("admin/", admin.site.urls),
99
]

0 commit comments

Comments
 (0)