ft_transcendence/accounts/tests/login.py

54 lines
2.3 KiB
Python
Raw Normal View History

2023-10-25 10:17:55 -04:00
from django.test import TestCase
# Create your tests here.
from django.test.client import Client
from django.contrib.auth.models import User
2023-10-25 10:17:55 -04:00
from django.http import HttpResponse
import uuid
class LoginTest(TestCase):
def setUp(self):
self.client = Client()
2023-11-30 07:39:22 -05:00
self.url = "/api/accounts/login"
2023-10-25 10:17:55 -04:00
self.username: str = str(uuid.uuid4())
self.password: str = str(uuid.uuid4())
User.objects.create_user(username=self.username, password=self.password)
2023-10-25 10:17:55 -04:00
def test_normal_login(self):
response: HttpResponse = self.client.post(self.url, {'username': self.username, 'password': self.password})
response_text = response.content.decode('utf-8')
2023-11-11 13:50:14 -05:00
#self.assertEqual(response_text, 'user connected')
2023-10-25 10:17:55 -04:00
def test_invalid_username(self):
response: HttpResponse = self.client.post(self.url, {"username": self.password, "password": self.password})
errors: dict = eval(response.content)
2023-11-11 13:50:14 -05:00
errors_expected: dict = {'user': ['Username or password wrong.']}
self.assertEqual(errors, errors_expected)
2023-10-25 10:17:55 -04:00
def test_invalid_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.username})
errors: dict = eval(response.content)
2023-11-11 13:50:14 -05:00
errors_expected: dict = {'user': ['Username or password wrong.']}
self.assertEqual(errors, errors_expected)
2023-10-25 10:17:55 -04:00
def test_invalid_no_username(self):
response: HttpResponse = self.client.post(self.url, {"password": self.password})
errors: dict = eval(response.content)
2023-11-11 13:50:14 -05:00
errors_expected: dict = {'username': ['This field is required.']}
self.assertEqual(errors, errors_expected)
2023-10-25 10:17:55 -04:00
def test_invalid_no_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username})
errors: dict = eval(response.content)
2023-11-11 13:50:14 -05:00
errors_expected: dict = {'password': ['This field is required.']}
self.assertEqual(errors, errors_expected)
2023-10-25 10:17:55 -04:00
def test_invalid_no_password_no_username(self):
response: HttpResponse = self.client.post(self.url, {})
errors: dict = eval(response.content)
2023-11-11 13:50:14 -05:00
errors_expected: dict = {'username': ['This field is required.'], 'password': ['This field is required.']}
self.assertEqual(errors, errors_expected)