Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/cliente validar name #30

Merged
merged 14 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/migrations/0015_merge_20240604_2040.py
Original file line number Diff line number Diff line change
@@ -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 = [
]
17 changes: 15 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db import models
import re # Importa el módulo de expresiones regulares
from datetime import datetime

from django.db import IntegrityError
from django.core.exceptions import ValidationError


def validate_client(data):
Expand All @@ -12,6 +14,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"
Expand Down Expand Up @@ -50,12 +54,21 @@ def save_client(cls, client_data):
return True, None

def update_client(self, client_data):
errors = validate_client(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
self.address = client_data.get("address", "") or self.address

self.save()
try:
self.save()
return True, None
except (IntegrityError, ValidationError) as e:
return False, {"error": str(e)}

def validate_provider(data):
errors = {}
Expand Down
1 change: 1 addition & 0 deletions app/templates/clients/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ <h1>Nuevo Cliente</h1>
<input type="text"
id="name"
name="name"
pattern="^[A-Za-z\s]+$"
value="{{client.name}}"
class="form-control"
required/>
Expand Down
69 changes: 68 additions & 1 deletion app/tests_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,17 @@ def test_edit_user_with_valid_data(self):
name="Juan Sebastián Veron",
address="13 y 44",
phone="221555232",
email="brujita75@hotmail.com",
email="brujita75@vetsoft.com",
)

response = self.client.post(
reverse("clients_form"),
data={
"id": client.id,
"name": "Guido Carrillo",
"address": "13 y 44",
"phone":"221555232",
"email": "[email protected]"
},
)

Expand All @@ -98,6 +101,70 @@ def test_edit_user_with_valid_data(self):
self.assertEqual(editedClient.address, client.address)
self.assertEqual(editedClient.email, client.email)

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": "[email protected]",
},
)

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",
address="13 y 44",
email="[email protected]",
)

response = self.client.post(
reverse("clients_form"),
data={
"id":client.id,
"name":"",
"phone":client.phone,
"address":client.address,
"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",
address="13 y 44",
email="[email protected]",
)

response = self.client.post(
reverse("clients_form"),
data={
"id":client.id,
"name":"Juan Sebastian Veron 11",
"phone":client.phone,
"address":client.address,
"email":client.email,
},
)

self.assertContains(response, "El nombre debe contener solo letras y espacios")

class MedicineIntegrationTest(TestCase):
def test_can_create_medicine(self):
response = self.client.post(
Expand Down
85 changes: 83 additions & 2 deletions app/tests_unit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.test import TestCase
from app.models import Client,Pet,validate_pet,Provider, Product,Medicine
from app.models import Client,Pet,validate_pet,Provider, Product,Medicine,validate_client
from django.utils import timezone

class ClientModelTest(TestCase):
Expand Down Expand Up @@ -33,7 +33,11 @@ def test_can_update_client(self):

self.assertEqual(client.phone, "221555232")

client.update_client({"phone": "221555233"})
client.update_client({
"name": "Juan Sebastian Veron",
"phone": "221555233",
"address": "13 y 44",
"email": "[email protected]",})

client_updated = Client.objects.get(pk=1)

Expand All @@ -58,6 +62,83 @@ 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",
"address": "13 y 44",
"email": "[email protected]",
}

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",
"address": "13 y 44",
"email": "[email protected]",
}

errors = validate_client(data)

self.assertIn("Por favor ingrese un nombre", errors.values())

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",
"address": "13 y 44",
"email": "[email protected]",
},
)
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",
"address": "13 y 44",
"email": "[email protected]",
},
)
client = Client.objects.get(pk=1)

self.assertEqual(client.name, "Juan Sebastian Veron")

client.update_client({
"name": "Juan Sebastian Veron 11",
"phone": "221555232",
"address": "13 y 44",
"email": "[email protected]",})
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):
Expand Down
8 changes: 6 additions & 2 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ def clients_form(request, id=None):
if request.method == "POST":
client_id = request.POST.get("id", "")
errors = {}
saved = True
saved = False

if client_id == "":
saved, errors = Client.save_client(request.POST)
else:
client = get_object_or_404(Client, pk=client_id)
client.update_client(request.POST)
update_result = client.update_client(request.POST)
if update_result is not None:
saved, errors = update_result
else:
saved = True

if saved:
return redirect(reverse("clients_repo"))
Expand Down
24 changes: 12 additions & 12 deletions functional_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_should_show_message_if_table_is_empty(self):

def test_should_show_clients_data(self):
Client.objects.create(
name="Juan Sebastián Veron",
name="Juan Sebastian Veron",
address="13 y 44",
phone="221555232",
email="[email protected]",
Expand All @@ -90,7 +90,7 @@ def test_should_show_clients_data(self):

expect(self.page.get_by_text("No existen clientes")).not_to_be_visible()

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("13 y 44")).to_be_visible()
expect(self.page.get_by_text("221555232")).to_be_visible()
expect(self.page.get_by_text("[email protected]")).to_be_visible()
Expand All @@ -110,7 +110,7 @@ def test_should_show_add_client_action(self):

def test_should_show_client_edit_action(self):
client = Client.objects.create(
name="Juan Sebastián Veron",
name="Juan Sebastian Veron",
address="13 y 44",
phone="221555232",
email="[email protected]",
Expand All @@ -125,7 +125,7 @@ def test_should_show_client_edit_action(self):

def test_should_show_client_delete_action(self):
client = Client.objects.create(
name="Juan Sebastián Veron",
name="Juan Sebastian Veron",
address="13 y 44",
phone="221555232",
email="[email protected]",
Expand All @@ -146,15 +146,15 @@ def test_should_show_client_delete_action(self):

def test_should_can_be_able_to_delete_a_client(self):
Client.objects.create(
name="Juan Sebastián Veron",
name="Juan Sebastian Veron",
address="13 y 44",
phone="221555232",
email="[email protected]",
)

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):
return response.url.find(reverse("clients_delete"))
Expand All @@ -166,7 +166,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):
Expand All @@ -175,14 +175,14 @@ 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("[email protected]")
self.page.get_by_label("Dirección").fill("13 y 44")

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("[email protected]")).to_be_visible()
expect(self.page.get_by_text("13 y 44")).to_be_visible()
Expand All @@ -198,7 +198,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("Dirección").fill("13 y 44")
Expand All @@ -216,7 +216,7 @@ def test_should_view_errors_if_form_is_invalid(self):

def test_should_be_able_to_edit_a_client(self):
client = Client.objects.create(
name="Juan Sebastián Veron",
name="Juan Sebastian Veron",
address="13 y 44",
phone="221555232",
email="[email protected]",
Expand All @@ -232,7 +232,7 @@ def test_should_be_able_to_edit_a_client(self):

self.page.get_by_role("button", name="Guardar").click()

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()
expect(self.page.get_by_text("13 y 44")).not_to_be_visible()
expect(self.page.get_by_text("221555232")).not_to_be_visible()
expect(self.page.get_by_text("[email protected]")).not_to_be_visible()
Expand Down
2 changes: 1 addition & 1 deletion vetsoft/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['vet-soft-1-4.onrender.com','100.20.92.101','44.225.181.72','44.227.217.144']
ALLOWED_HOSTS = ['vet-soft-1-4.onrender.com','100.20.92.101','44.225.181.72','44.227.217.144','127.0.0.1']

CSRF_TRUSTED_ORIGINGS = ['https://vet-soft-1-4.onrender.com']
# Application definition
Expand Down