mirror of
				https://github.com/Tivix/django-rest-auth.git
				synced 2025-10-31 15:57:34 +03:00 
			
		
		
		
	Add a few tests for the demo app and axes
This commit is contained in:
		
							parent
							
								
									18983960a1
								
							
						
					
					
						commit
						b3d07028ca
					
				|  | @ -7,7 +7,7 @@ from rest_framework import exceptions | ||||||
| # noinspection PyAbstractClass | # noinspection PyAbstractClass | ||||||
| class RestAuthAxesLoginSerializer(LoginSerializer): | class RestAuthAxesLoginSerializer(LoginSerializer): | ||||||
| 
 | 
 | ||||||
|     def validate(self, attrs): |     def validate(self, attrs) -> dict: | ||||||
|         try: |         try: | ||||||
|             return super().validate(attrs) |             return super().validate(attrs) | ||||||
|         except exceptions.ValidationError as e: |         except exceptions.ValidationError as e: | ||||||
|  |  | ||||||
|  | @ -1,3 +0,0 @@ | ||||||
| from django.test import TestCase |  | ||||||
| 
 |  | ||||||
| # Create your tests here. |  | ||||||
							
								
								
									
										71
									
								
								demo/myapp/tests/test_serializers.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								demo/myapp/tests/test_serializers.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -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) | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user