-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
553c555
commit cab097d
Showing
6 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ build/ | |
.tox/ | ||
db.sqlite3 | ||
dist/ | ||
|
||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.core.exceptions import ImproperlyConfigured | ||
from rest_framework.throttling import UserRateThrottle | ||
|
||
|
||
__all__ = ("ResetPasswordRequestTokenThrottle",) | ||
|
||
|
||
class ResetPasswordRequestTokenThrottle(UserRateThrottle): | ||
scope = "django-rest-passwordreset-request-token" | ||
default_rate = "3/day" | ||
|
||
def get_rate(self): | ||
try: | ||
return super().get_rate() | ||
except ImproperlyConfigured: | ||
return self.default_rate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from django.urls import reverse | ||
from rest_framework.test import APIClient, APITestCase | ||
from django.contrib.auth.models import User | ||
from django.contrib.auth import get_user_model | ||
from django_rest_passwordreset.models import ResetPasswordToken | ||
from throttling import ResetPasswordRequestTokenThrottle | ||
|
||
|
||
User = get_user_model() | ||
|
||
|
||
class TestThrottle(APITestCase): | ||
def setUp(self): | ||
self.client = APIClient() | ||
|
||
self.user_1 = User.objects.create_user(username='user_1', password='password', email='[email protected]') | ||
self.user_admin = User.objects.create_superuser(username='admin', password='password', email='[email protected]') | ||
|
||
def _reset_password_logged_in(self): | ||
|
||
# generate token | ||
url = reverse('password_reset:reset-password-request') | ||
data = {'email': self.user_1.email} | ||
response = self.client.post(url, data).json() | ||
|
||
self.assertIn('status', response) | ||
self.assertEqual(response['status'], 'OK') | ||
|
||
# test validity of the token | ||
token = ResetPasswordToken.objects.filter(user=self.user_1).first() | ||
url = reverse('password_reset:reset-password-validate') | ||
data = {'token': token.key} | ||
response = self.client.post(url, data).json() | ||
|
||
self.assertIn('status', response) | ||
self.assertEqual(response['status'], 'OK') | ||
|
||
# reset password | ||
url = reverse('password_reset:reset-password-confirm') | ||
data = {'token': token.key, 'password': 'new_password'} | ||
response = self.client.post(url, data).json() | ||
|
||
# check if new password was set | ||
self.assertTrue(token.user.check_password('new_password')) | ||
self.assertFalse(token.user.check_password('password')) | ||
|
||
def test_throttle(self,): | ||
# first run on _reset_password_logged_in | ||
# x number of runs (adjust number of calls to the throttle settings) | ||
for i in range(2): | ||
self._reset_password_logged_in() | ||
# last run should raise an exception | ||
self.assertRaises(Exception, self._reset_password_logged_in) | ||
|
||
def test_default_rate(self): | ||
class ThrottleWithAnotherScope(ResetPasswordRequestTokenThrottle): | ||
scope = "throttle-with-another-scope" | ||
|
||
self.assertEqual(ThrottleWithAnotherScope().rate, "3/day") |