diff --git a/app/migrations/0015_merge_20240604_2040.py b/app/migrations/0015_merge_20240604_2040.py new file mode 100644 index 00000000..ad8714b9 --- /dev/null +++ b/app/migrations/0015_merge_20240604_2040.py @@ -0,0 +1,14 @@ +# Generated by Django 5.0.4 on 2024-06-04 23:40 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0012_alter_product_price'), + ('app', '0014_merge_20240603_0914'), + ] + + operations = [ + ] diff --git a/app/migrations/0020_merge_20240605_1018.py b/app/migrations/0020_merge_20240605_1018.py new file mode 100644 index 00000000..836a0515 --- /dev/null +++ b/app/migrations/0020_merge_20240605_1018.py @@ -0,0 +1,14 @@ +# Generated by Django 5.0.4 on 2024-06-05 13:18 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0015_merge_20240604_2040'), + ('app', '0019_merge_20240605_0942'), + ] + + operations = [ + ] diff --git a/app/models.py b/app/models.py index 8e0e9384..7f46de5f 100644 --- a/app/models.py +++ b/app/models.py @@ -1,3 +1,4 @@ +import re # Importa el módulo de expresiones regulares from datetime import datetime from django.core.exceptions import ValidationError @@ -24,6 +25,8 @@ def validate_client(data): if name == "": errors["name"] = "Por favor ingrese un nombre" + elif not re.match(r'^[a-zA-ZáéíóúÁÉÍÓÚñÑ\s]+$', name): + errors["name"] = "El nombre debe contener solo letras y espacios" if phone == "": errors["phone"] = "Por favor ingrese un teléfono" @@ -93,6 +96,7 @@ def update_client(self, client_data): if len(errors) > 0: return False, errors + self.name = client_data.get("name", "") or self.name self.email = client_data.get("email", "") or self.email self.phone = client_data.get("phone", "") or self.phone diff --git a/app/templates/clients/form.html b/app/templates/clients/form.html index 726056c6..39168f10 100644 --- a/app/templates/clients/form.html +++ b/app/templates/clients/form.html @@ -25,6 +25,7 @@

Nuevo Cliente

diff --git a/app/tests_integration.py b/app/tests_integration.py index e3543989..53f2c653 100644 --- a/app/tests_integration.py +++ b/app/tests_integration.py @@ -120,7 +120,6 @@ def test_edit_user_with_valid_data_test(self): "phone": "221456789", "email": "brujita71@vetsoft.com", "city": "Berisso", - }, ) @@ -161,6 +160,70 @@ def test_edit_user_with_invalid_data_test_city(self): editedClient = Client.objects.get(pk=client.id) self.assertEqual(editedClient.city, "La Plata") + def test_validation_invalid_name_client(self): + """ + Prueba si se muestra un error de validación cuando se ingresa un nombre inválido. + """ + response = self.client.post( + reverse("clients_form"), + data={ + "name": "Juan Sebastian Veron 11", + "phone": "221555232", + "address": "13 y 44", + "email": "brujita75@hotmail.com", + }, + ) + + self.assertContains(response, "El nombre debe contener solo letras y espacios") + + def test_user_cant_edit_client_with_empty_name(self): + """ + Prueba que un usuario no pueda editar un cliente con un nombre vacío. + """ + client=Client.objects.create( + name="Juan Sebastian Veron", + phone="221555232", + city="La Plata", + email="brujita75@hotmail.com", + ) + + response = self.client.post( + reverse("clients_form"), + data={ + "id":client.id, + "name":"", + "phone":client.phone, + "city":client.city, + "email":client.email, + }, + ) + + self.assertContains(response, "Por favor ingrese un nombre") + + def test_user_cant_edit_client_with_incorrect_name(self): + """ + Prueba que un usuario no pueda editar un cliente con un nombre incorrecto. + """ + client=Client.objects.create( + name="Juan Sebastian Veron", + phone="221555232", + city="La Plata", + email="brujita75@hotmail.com", + ) + + response = self.client.post( + reverse("clients_form"), + data={ + "id":client.id, + "name":"Juan Sebastian Veron 11", + "phone":client.phone, + "city":client.city, + "email":client.email, + }, + ) + + self.assertContains(response, "El nombre debe contener solo letras y espacios") + class MedicineIntegrationTest(TestCase): def test_can_create_medicine(self): """ diff --git a/app/tests_unit.py b/app/tests_unit.py index 8de10055..a7d14ce8 100644 --- a/app/tests_unit.py +++ b/app/tests_unit.py @@ -1,7 +1,15 @@ from django.test import TestCase from django.utils import timezone -from app.models import Client, Medicine, Pet, Product, Provider, validate_pet +from app.models import ( + Client, + Medicine, + Pet, + Product, + Provider, + validate_client, + validate_pet, +) class ClientModelTest(TestCase): @@ -41,6 +49,8 @@ def test_can_update_client(self): ) client = Client.objects.get(pk=1) + self.assertEqual(client.phone, "221555232") + client.update_client( { "name": "Juan Sebastian Veron", "phone": "221555232", @@ -48,6 +58,7 @@ def test_can_update_client(self): "city": "Berisso", },) + client_updated = Client.objects.get(pk=1) self.assertEqual(client_updated.phone, "221555232") @@ -77,6 +88,39 @@ def test_update_client_with_error(self): self.assertEqual(client_updated.phone, "221555232") + + def test_validate_client_incorrect_name(self): + """ + Prueba que verifica que si un nombre es ingresado con algún caracter que no sean letras minúsculas, mayúsculas o espacios devuelva el error + """ + + data = { + "name": "Juan Sebastian Veron 11", + "phone": "221555232", + "city": "La Plata", + "email": "brujita75@hotmail.com", + } + + result = validate_client(data) + + self.assertIn("El nombre debe contener solo letras y espacios", result.values()) + + def test_validate_client_with_empty_name(self): + """ + Prueba que verifica que no se pueda crear un cliente con el campo nombre vacío + """ + data = { + "name": "", + "phone": "221555232", + "city": "La Plata", + "email": "brujita75@hotmail.com", + } + + errors = validate_client(data) + + self.assertIn("Por favor ingrese un nombre", errors.values()) + + def test_update_client_with_email_null(self): #nuevo test verificando que no pueda hacer update con email nulo """ Esta funcion testea el cliente acutalizado con un email nulo @@ -98,6 +142,53 @@ def test_update_client_with_email_null(self): #nuevo test verificando que no pue client_updated = Client.objects.get(pk=1) self.assertEqual(client_updated.email, "brujita75@vetsoft.com") + + def test_update_client_with_empty_name(self): + """ + Prueba que verifica si se produce un error al intentar actualizar un cliente con un campo de nombre vacío. + """ + Client.save_client( + { + "name": "Juan Sebastian Veron", + "phone": "221555232", + "city": "La Plata", + "email": "brujita75@vetsoft.com", + }, + ) + client = Client.objects.get(pk=1) + + self.assertEqual(client.name, "Juan Sebastian Veron") + + client.update_client({"name": ""}) + client_updated = Client.objects.get(pk=1) + + self.assertEqual(client_updated.name, "Juan Sebastian Veron") + + def test_update_client_with_incorrect_name(self): + """ + Prueba que verifica si se produce un error al intentar actualizar un cliente con un campo de nombre incorrecto. + """ + Client.save_client( + { + "name": "Juan Sebastian Veron", + "phone": "221555232", + "city": "La Plata", + "email": "brujita75@vetsoft.com", + }, + ) + client = Client.objects.get(pk=1) + + self.assertEqual(client.name, "Juan Sebastian Veron") + + client.update_client({ + "name": "Juan Sebastian Veron 11", + "phone": "221555232", + "city": "La Plata", + "email": "brujita75@vetsoft.com",}) + client_updated = Client.objects.get(pk=1) + + self.assertEqual(client_updated.name, "Juan Sebastian Veron") + class MedicineModelTest(TestCase): def test_can_create_medicine_with_valid_dose(self): diff --git a/functional_tests/tests.py b/functional_tests/tests.py index a146c057..f0460cb4 100644 --- a/functional_tests/tests.py +++ b/functional_tests/tests.py @@ -115,6 +115,7 @@ def test_should_show_clients_data(self): expect(self.page.get_by_text("Juan Sebastián Veron")).to_be_visible() expect(self.page.get_by_text("La Plata")).to_be_visible() + expect(self.page.get_by_text("221555232")).to_be_visible() expect(self.page.get_by_text("brujita75@vetsoft.com")).to_be_visible() @@ -140,7 +141,7 @@ def test_should_show_client_edit_action(self): """ client = Client.objects.create( name="Juan Sebastián Veron", - city="13 y 44", + city="La Plata", phone="221555232", email="brujita75@vetsoft.com", ) @@ -158,7 +159,7 @@ def test_should_show_client_delete_action(self): """ client = Client.objects.create( name="Juan Sebastián Veron", - city="13 y 44", + city="La Plata", phone="221555232", email="brujita75@vetsoft.com", ) @@ -181,15 +182,15 @@ def test_should_can_be_able_to_delete_a_client(self): Esta función verifica que un cliente pueda ser eliminado correctamente a través de una solicitud POST al servidor. """ Client.objects.create( - name="Juan Sebastián Veron", - city="13 y 44", + name="Juan Sebastian Veron", + city="La Plata", phone="221555232", email="brujita75@vetsoft.com", ) self.page.goto(f"{self.live_server_url}{reverse('clients_repo')}") - expect(self.page.get_by_text("Juan Sebastián Veron")).to_be_visible() + expect(self.page.get_by_text("Juan Sebastian Veron")).to_be_visible() def is_delete_response(response): """ @@ -204,7 +205,7 @@ def is_delete_response(response): response = response_info.value self.assertTrue(response.status < 400) - expect(self.page.get_by_text("Juan Sebastián Veron")).not_to_be_visible() + expect(self.page.get_by_text("Juan Sebastian Veron")).not_to_be_visible() class ClientCreateEditTestCase(PlaywrightTestCase): @@ -216,7 +217,7 @@ def test_should_be_able_to_create_a_new_client(self): expect(self.page.get_by_role("form")).to_be_visible() - self.page.get_by_label("Nombre").fill("Juan Sebastián Veron") + self.page.get_by_label("Nombre").fill("Juan Sebastian Veron") self.page.get_by_label("Teléfono").fill("221555232") self.page.get_by_label("Email").fill("brujita75@vetsoft.com") self.page.get_by_label("Ciudad").select_option("La Plata") @@ -224,7 +225,7 @@ def test_should_be_able_to_create_a_new_client(self): self.page.get_by_role("button", name="Guardar").click() - expect(self.page.get_by_text("Juan Sebastián Veron")).to_be_visible() + expect(self.page.get_by_text("Juan Sebastian Veron")).to_be_visible() expect(self.page.get_by_text("221555232")).to_be_visible() expect(self.page.get_by_text("brujita75@vetsoft.com")).to_be_visible() expect(self.page.get_by_text("La Plata")).to_be_visible() @@ -245,7 +246,7 @@ def test_should_view_errors_if_form_is_invalid(self): expect(self.page.get_by_text("Por favor ingrese un teléfono")).to_be_visible() expect(self.page.get_by_text("Por favor ingrese un email")).to_be_visible() - self.page.get_by_label("Nombre").fill("Juan Sebastián Veron") + self.page.get_by_label("Nombre").fill("Juan Sebastian Veron") self.page.get_by_label("Teléfono").fill("221555232") self.page.get_by_label("Email").fill("brujita75") self.page.get_by_label("Ciudad").select_option("La Plata")