Hello friends. I’m having receiving error messages when I run automated tests for my login APIView in the console. Here is the full traceback, it shows two tests that ran here
show_sunset_warning()
System check identified no issues (0 silenced).
F.
======================================================================
FAIL: test_login_user (arborfindr.test.test_login.UserLoginTests.test_login_user)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/coreyj/Documents/ArborHub/MyProject/arborfindr/test/test_login.py", line 13, in test_login_user
self.assertTrue(self.client.login(username='username', password='Password!87'))
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: False is not true
----------------------------------------------------------------------
Ran 2 tests in 0.732s
FAILED (failures=1)
Destroying test database for alias 'default'...
Here is my login serializer class:
class LoginSerializers(serializers.ModelSerializer):
email = serializers.CharField(max_length=255)
password = serializers.CharField(
label =("Password"),
style={'input_type': 'password'},
trim_whitespace=False,
max_length=128,
write_only=True
)
def validate(self, data):
email = data.get('email')
password = data.get('password')
if email and password:
user = authenticate(request=self.context.get('request'),
email=email, password=password)
if not user:
msg = ('Invalid email/password')
raise serializers.ValidationError(msg, code = 'Authorization')
else:
msg = ('Must include valid "email" and "password".')
raise serializers.ValidationError(msg, code = 'Authorization')
data['user'] = user
return data
Here is my automated test for login APIView
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from ..models import User
from rest_framework.test import APIClient
client = APIClient()
class UserLoginTests(APITestCase):
def test_login_user(self):
user = User.objects.create_user('username', 'Password!87')
self.assertTrue(self.client.login(username='username', password='Password!87'))
response = self.client.get(reverse('login'))
self.assertEqual(response.status_code, status.HTTP_200_OK)
Finally here is my Login API endpoint in views.py
class LoginView(APIView):
def post(self, request, *args, **kwargs):
serializer = LoginSerializers(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
login(None, user)
token = Token.objects.create(user=user)
return Response({"status": status.HTTP_200_OK, "Token": token.key})
if user is not None:
# Generate token
refresh = RefreshToken.for_user(user)
access_token = str(refresh.access_token)
return Response({
'message': 'Successful login',
'access_token': access_token,
'refresh_token': str(refresh),
}, status=status.HTTP_200_OK)
return Response({'error': 'Invalid email/password'}, status=status.HTTP_401_UNAUTHORIZED)
I modified my LoginTestCase, so it doesn’t resemble the RegisterTestCase much. I’m just trying to wrap my head around why I got this error, looks different than the Traceback I got my for my Register API endpoint. I even used APIClient
as per the documentation. Please any kind of solutions, I’ll really appreciate it