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

Bugfix/validar email cliente #43

Merged
merged 9 commits into from
Jun 25, 2024
7 changes: 4 additions & 3 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ def validate_client(data):

if email == "":
errors["email"] = "Por favor ingrese un email"
elif "@vetsoft.com" not in email:
errors["email"] = "El email debe ser de la forma @vetsoft.com"

elif not re.match(r"^[a-zA-Z0-9._%+-]+@vetsoft\.com$", email):
errors["email"] = "El email debe tener un formato válido y ser de la forma [email protected]"


if city == "" or city is None:
errors["city"] = "Por favor ingrese una ciudad"
elif city not in dict(CityEnum.choices):
Expand Down
2 changes: 1 addition & 1 deletion app/templates/clients/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h1>Nuevo Cliente</h1>
<input type="email"
id="email"
name="email"
pattern="\w[email protected]"
pattern="^[\w\.\-]+@vetsoft\.com$"
class="form-control"
value="{{client.email}}"
required/>
Expand Down
22 changes: 19 additions & 3 deletions app/tests_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,37 @@ def test_should_response_with_404_status_if_client_doesnt_exists(self):
response = self.client.get(reverse("clients_edit", kwargs={"id": 100}))
self.assertEqual(response.status_code, 404)

def test_validation_invalid_email(self):
#Test pra comprobar que tenga texto delante de @vetsoft.com
def test_validation_invalid_email_whit_vetsoft(self):
"""
Esta función testea la validación de emails invalidos.
agregando la validacion de que deba tener texto delante del @vetsoft.com
"""
response = self.client.post(
reverse("clients_form"),
data={
"name": "Juan Sebastian Veron",
"phone": 54221555232,
"city": "La Plata",
"email": "brujita75",
"email": "@vetsoft.com",
},
)
self.assertContains(response, "El email debe ser de la forma @vetsoft.com")
self.assertContains(response, "El email debe tener un formato válido y ser de la forma nombre@vesoft.com")

def test_validation_email_null(self):
"""
Esta función testea la validación de emails nulos.
"""
response = self.client.post(
reverse("clients_form"),
data={
"name": "Juan Sebastian Veron",
"phone": 54221555232,
"city": "La Plata",
"email": "",
},
)
self.assertContains(response, "Por favor ingrese un email")


def test_validation_invalid_phone(self):
Expand Down
31 changes: 31 additions & 0 deletions app/tests_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,37 @@ def test_update_client_with_incorrect_name(self):

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

def test_invalid_email_no_text_before_at(self):
"""
Test agregado del email del cliente
Verifica que deba haber texto delante del @vetsoft
"""
data = {
"name": "Gonza",
"phone": "54221555232",
"city": "La Plata",
"email": "@vetsoft.com",
}
errors = validate_client(data)

self.assertIn("El email debe tener un formato válido y ser de la forma [email protected]", errors.values())

def test_invalid_email_not_ending_with_com(self):

"""
Verifica que el correo termine en .com, por mas de que ingrese un email valido cotidianamente.
"""
data = {
"name": "Gonza",
"phone": "54221555232",
"city": "La Plata",
"email": "[email protected]",
}
errors = validate_client(data)

self.assertIn("El email debe tener un formato válido y ser de la forma [email protected]", errors.values())


class MedicineModelTest(TestCase):
"""
Pruebas para el modelo Medicina.
Expand Down
10 changes: 6 additions & 4 deletions app/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.shortcuts import get_object_or_404, redirect, render, reverse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse

from .models import CityEnum, Client, Medicine, Pet, Product, Provider, Veterinary

Expand Down Expand Up @@ -38,10 +39,11 @@ def clients_form(request, id=None):
saved = True

if saved:
"""print("Saved successfully! Redirecting to clients_repo...")"""
return redirect(reverse("clients_repo"))

return render(
request, "clients/form.html", {"errors": errors, "client": request.POST, "ciudades": ciudades},)
else:
"""print("Error saving client! Errors:", errors"""
return render(request, "clients/form.html", {"errors": errors, "client": request.POST, "ciudades": ciudades})

client = None
if id is not None:
Expand Down
62 changes: 40 additions & 22 deletions functional_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def test_should_view_errors_if_form_is_invalid(self):
).not_to_be_visible()

expect(
self.page.get_by_text("El email debe ser de la forma @vetsoft.com"),
self.page.get_by_text("El email debe tener un formato válido y ser de la forma nombre@vesoft.com"),
).to_be_visible()

def test_should_be_able_to_edit_a_client(self):
Expand Down Expand Up @@ -316,43 +316,61 @@ def test_should_be_able_to_edit_a_client(self):
"href", reverse("clients_edit", kwargs={"id": client.id}),
)


def test_should_view_errors_if_name_is_invalid(self):
#Tests agregados para validacion de email.
def test_should_view_errors_if_form_is_invalid_email(self):
"""
Esta función verifica que muestre un mesnaje de error para un nombre invalido.
Esta función verifica que se muestren los errores de validación en el email del
formulario si se envían datos inválidos al intentar crear un nuevo cliente.
"""
self.page.goto(f"{self.live_server_url}{reverse('clients_form')}")

expect(self.page.get_by_role("form")).to_be_visible()

self.page.get_by_label("Nombre").fill("manu22")
#Ingreso datos vacios
self.page.get_by_role("button", name="Guardar").click()

expect(self.page.get_by_text("Por favor ingrese un nombre")).to_be_visible()
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()
expect(self.page.get_by_text("Por favor ingrese una ciudad")).to_be_visible()

#Ingreso un email vacio
self.page.get_by_label("Nombre").fill("Gonzalo")
self.page.get_by_label("Teléfono").fill("54221555232")
self.page.get_by_label("Email").fill("[email protected]")
self.page.get_by_label("Email").fill("")
self.page.get_by_label("Ciudad").select_option("La Plata")

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

expect(self.page.get_by_text("El nombre debe contener solo letras y espacios")).to_be_visible()


def test_should_show_message_if_city_is_not_selected(self):
"""
Esta función verifica que se muestre un mensaje de error cuando se intenta
crear un cliente sin seleccionar ciudad
"""
self.page.goto(f"{self.live_server_url}{reverse('clients_form')}")
expect(self.page.get_by_text("Por favor ingrese un nombre")).not_to_be_visible()
expect(self.page.get_by_text("Por favor ingrese un teléfono")).not_to_be_visible()
expect(self.page.get_by_text("Por favor ingrese una ciudad")).not_to_be_visible()
expect(self.page.get_by_text("Por favor ingrese un email")).to_be_visible()

expect(self.page.get_by_role("form")).to_be_visible()
# Ingresar un email erróneo
self.page.get_by_label("Nombre").fill("Gonzalo")
self.page.get_by_label("Teléfono").fill("54221555232")
self.page.get_by_label("Email").fill("@vetsoft.net")
self.page.get_by_label("Ciudad").select_option("La Plata")

self.page.get_by_label("Nombre").fill("Angel")
self.page.get_by_label("Teléfono").fill("542226836789")
self.page.get_by_label("Email").fill("[email protected]")
# no selecciono una ciudad
self.page.get_by_role("button", name="Guardar").click()

# Verifica si se muestra el mensaje de error esperado
expect(self.page.get_by_text("Por favor ingrese una ciudad")).to_be_visible()
expect(self.page.get_by_text("Por favor ingrese un email")).not_to_be_visible()
expect(self.page.get_by_text("El email debe tener un formato válido y ser de la forma [email protected]")).to_be_visible()

# Ingreso un email válido
self.page.get_by_label("Nombre").fill("Gonzalo")
self.page.get_by_label("Teléfono").fill("54221555232")
self.page.get_by_label("Email").fill("[email protected]")
self.page.get_by_label("Ciudad").select_option("La Plata")

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

expect(self.page.get_by_text("Gonzalo")).to_be_visible()
expect(self.page.get_by_text("54221555232")).to_be_visible()
expect(self.page.get_by_text("[email protected]")).to_be_visible()
expect(self.page.get_by_text("La Plata")).to_be_visible()


class MedicineCreateEditTestCase(PlaywrightTestCase):
"""
Expand Down