Skip to content

Commit

Permalink
test(#16): login tests case
Browse files Browse the repository at this point in the history
  • Loading branch information
pfc15 committed Jan 15, 2025
1 parent 219715d commit f9abf83
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
8 changes: 8 additions & 0 deletions portalGalt/cadastro/tests/test_setup.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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 protected]",
"username": "email",
Expand Down
21 changes: 18 additions & 3 deletions portalGalt/cadastro/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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'
Expand All @@ -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)






Expand Down
2 changes: 1 addition & 1 deletion portalGalt/cadastro/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit f9abf83

Please sign in to comment.