diff --git a/demo/myapp/serializers.py b/demo/myapp/serializers.py index 051f9c6..cfd9d8b 100644 --- a/demo/myapp/serializers.py +++ b/demo/myapp/serializers.py @@ -7,7 +7,7 @@ from rest_framework import exceptions # noinspection PyAbstractClass class RestAuthAxesLoginSerializer(LoginSerializer): - def validate(self, attrs): + def validate(self, attrs) -> dict: try: return super().validate(attrs) except exceptions.ValidationError as e: diff --git a/demo/myapp/tests.py b/demo/myapp/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/demo/myapp/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/demo/myapp/tests/test_serializers.py b/demo/myapp/tests/test_serializers.py new file mode 100644 index 0000000..2402688 --- /dev/null +++ b/demo/myapp/tests/test_serializers.py @@ -0,0 +1,71 @@ +from django.conf import settings +from django.contrib.auth.models import User +from django.http import HttpRequest +from django.test import TestCase +from rest_framework.exceptions import ValidationError + +from myapp import serializers + + +class TestRestAuthAxesLoginSerializer(TestCase): + + def setUp(self) -> None: + self.request = HttpRequest() + + def test_validate_wrong_user(self) -> None: + serializer = serializers.RestAuthAxesLoginSerializer( + context=dict(request=self.request) + ) + with self.assertRaisesMessage(ValidationError, 'Unable to log in with provided credentials.'): + serializer.validate({ + 'username': 'test', + 'email': 'test@example.com', + 'password': 'test' + }) + + def test_validate_good_user(self) -> None: + User.objects.create_user( + username='test', + email='test@example.com', + password='test' + ) + serializer = serializers.RestAuthAxesLoginSerializer( + context=dict(request=self.request) + ) + attrs = serializer.validate({ + 'username': 'test', + 'email': 'test@example.com', + 'password': 'test' + }) + + self.assertIsNotNone(attrs) + + def test_validate_axes_locked_out(self) -> None: + good_password_creds = { + 'username': 'test', + 'email': 'test@example.com', + 'password': 'good_password' + } + + bad_password_creds = { + 'username': 'test', + 'email': 'test@example.com', + 'password': 'bad_password' + } + + User.objects.create_user(**good_password_creds) + serializer = serializers.RestAuthAxesLoginSerializer( + context=dict(request=self.request) + ) + + for i in range(settings.AXES_FAILURE_LIMIT - 1): + with self.assertRaisesMessage(ValidationError, 'Unable to log in with provided credentials.'): + serializer.validate(bad_password_creds) + + account_locked_message = 'Account locked: too many login attempts. Contact an admin to unlock your account.' + + with self.assertRaisesMessage(ValidationError, account_locked_message): + serializer.validate(bad_password_creds) + + with self.assertRaisesMessage(ValidationError, account_locked_message): + serializer.validate(good_password_creds)