ft_transcendence/accounts/tests/edit.py

49 lines
2.6 KiB
Python
Raw Permalink Normal View History

2023-11-30 07:39:22 -05:00
from django.test import TestCase
# Create your tests here.
from django.test.client import Client
from django.http import HttpResponse
from django.contrib.auth.models import User
import uuid
2023-11-30 08:03:38 -05:00
class EditTest(TestCase):
2023-11-30 07:39:22 -05:00
def setUp(self):
self.client = Client()
2023-11-30 08:03:38 -05:00
self.url = "/api/accounts/edit"
2023-11-30 07:39:22 -05:00
self.username: str = str(uuid.uuid4())
self.password: str = str(uuid.uuid4())
self.new_password: str = str(uuid.uuid4())
User.objects.create_user(username = self.username, password = self.password)
def test_normal(self):
self.client.login(username = self.username, password = self.password)
2023-11-30 08:03:38 -05:00
response: HttpResponse = self.client.patch(self.url, {"current_password": self.password, "new_password": self.new_password, "username": "bozo"}, content_type='application/json')
2023-11-30 07:39:22 -05:00
response_text: str = response.content.decode('utf-8')
2023-11-30 08:03:38 -05:00
self.assertEqual(response_text, '"data has been alterate"')
2023-11-30 07:39:22 -05:00
2023-11-30 08:03:38 -05:00
def test_invalid_current_password(self):
self.client.login(username = self.username, password = self.password)
response: HttpResponse = self.client.patch(self.url, {"current_password": "bozo", "new_password": self.new_password, "username": "bozo"}, content_type='application/json')
errors: dict = eval(response.content)
self.assertDictEqual(errors, {"current_password":["Password is wrong."]})
def test_invalid_new_username_blank(self):
self.client.login(username = self.username, password = self.password)
response: HttpResponse = self.client.patch(self.url, {"current_password": self.password, "username": " "}, content_type='application/json')
errors: dict = eval(response.content)
self.assertDictEqual(errors, {'username': ['This field may not be blank.']})
def test_invalid_new_username_char(self):
self.client.login(username = self.username, password = self.password)
response: HttpResponse = self.client.patch(self.url, {"current_password": self.password, "username": "*&"}, content_type='application/json')
errors: dict = eval(response.content)
self.assertDictEqual(errors, {'username': ['Enter a valid username. This value may contain only letters, numbers, and @/./+/-/_ characters.']})
2023-11-30 07:39:22 -05:00
def test_nologged(self):
2023-11-30 08:03:38 -05:00
response: HttpResponse = self.client.patch(self.url, {"current_password": self.password, "new_password": self.new_password}, content_type='application/json')
2023-11-30 07:39:22 -05:00
errors: dict = eval(response.content)
self.assertDictEqual(errors, {'detail': 'Authentication credentials were not provided.'})