From f9abf83c7d6c58f26f5076f01a1a032857a23c81 Mon Sep 17 00:00:00 2001 From: pedro cruz Date: Wed, 15 Jan 2025 14:47:59 -0300 Subject: [PATCH] test(#16): login tests case --- portalGalt/cadastro/tests/test_setup.py | 8 ++++++++ portalGalt/cadastro/tests/tests_views.py | 21 ++++++++++++++++++--- portalGalt/cadastro/views.py | 2 +- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/portalGalt/cadastro/tests/test_setup.py b/portalGalt/cadastro/tests/test_setup.py index 3acd3ba..5d0e3bd 100644 --- a/portalGalt/cadastro/tests/test_setup.py +++ b/portalGalt/cadastro/tests/test_setup.py @@ -1,6 +1,7 @@ from rest_framework.test import APITestCase from django.urls import reverse from django.contrib.auth.models import Group, User +from rest_framework.authtoken.models import Token class TestSetUp(APITestCase): @@ -19,6 +20,13 @@ def setUp(self): self.student.set_password("password") self.student.save() + self.token_admin = Token.objects.create(user=self.admin) + self.token_student = Token.objects.create(user=self.student) + + self.header = { + 'CONTENT_TYPE': 'application/json' + } + self.user_data_not_exist = { "email": "email@email.com", "username": "email", diff --git a/portalGalt/cadastro/tests/tests_views.py b/portalGalt/cadastro/tests/tests_views.py index 9ce2780..bad8446 100644 --- a/portalGalt/cadastro/tests/tests_views.py +++ b/portalGalt/cadastro/tests/tests_views.py @@ -4,6 +4,9 @@ from django.test import TestCase import json +# to run only one test linux/bash +# python3 manage.py test cadastro.tests.tests_views.TestViews.TESTMETHOD + class TestViews(TestSetUp): @@ -12,7 +15,7 @@ def test_user_cannot_register_with_not_data(self): self.assertEqual(res.status_code, 400) - def test_login_sucefull(self): + def test_login_sucessfull(self): header = { 'CONTENT_TYPE': 'application/json' @@ -24,8 +27,20 @@ def test_login_sucefull(self): self.assertEqual(res.data["user"]["username"], self.user_data_admin["username"]) self.assertEqual(res.data["user"]["email"], self.user_data_admin["email"]) - - # Call the function that queries the User model + + def test_login_User_Not_exists(self): + + res = self.client.post(self.login_url, self.user_data_not_exist, **self.header) + self.assertEqual(res.status_code, 404) + + def test_login_worng_password(self): + data = self.user_data_admin + data["password"] = "not his password" + res = self.client.post(self.login_url, data, **self.header) + self.assertEqual(res.status_code, 401) + + + diff --git a/portalGalt/cadastro/views.py b/portalGalt/cadastro/views.py index 993c152..b7dfe89 100644 --- a/portalGalt/cadastro/views.py +++ b/portalGalt/cadastro/views.py @@ -26,7 +26,7 @@ def login(request): user = get_object_or_404(User, username=request.data["username"]) if not user.check_password(request.data['password']): - return Response({"detail": "Not found"}, status=status.HTTP_400_BAD_REQUEST) + return Response({"detail": "wrong username or password"}, status=status.HTTP_401_UNAUTHORIZED) token, created = Token.objects.get_or_create(user=user) serializer = UserSerializer(instance=user) return Response({"token":token.key, "user":serializer.data})