Showing
12 changed files
with
188 additions
and
6 deletions
capacitacion/settings_testing.py
0 → 100644
core/tests/__init__.py
0 → 100644
core/tests/fixtures_usuario.py
0 → 100644
1 | +from django.contrib.auth.models import User, Permission | ||
2 | +from django.contrib.contenttypes.models import ContentType | ||
3 | + | ||
4 | + | ||
5 | +def create_user(username, first_name='Admin', last_name='Root', email=None, is_superuser=False, is_staff=False): | ||
6 | + user, created = User.objects.get_or_create( | ||
7 | + username=username, | ||
8 | + email='{}@root.com'.format(username) if email is None else email, | ||
9 | + defaults=dict( | ||
10 | + first_name=first_name, | ||
11 | + last_name=last_name, | ||
12 | + is_superuser=is_superuser, | ||
13 | + is_staff=is_staff, | ||
14 | + ) | ||
15 | + ) | ||
16 | + | ||
17 | + user.set_password('password') | ||
18 | + user.save() | ||
19 | + | ||
20 | + return user | ||
21 | + | ||
22 | + | ||
23 | +def crear_permiso_usuario(codename, name, app_name, model_name): | ||
24 | + model_ct = ContentType.objects.get(app_label=app_name, model=model_name) | ||
25 | + permission, _ = Permission.objects.get_or_create(codename=codename, | ||
26 | + content_type=model_ct, | ||
27 | + defaults={'name': name}) | ||
28 | + return permission |
@@ -3,17 +3,27 @@ | @@ -3,17 +3,27 @@ | ||
3 | Lista de Personas | 3 | Lista de Personas |
4 | {% endblock %} | 4 | {% endblock %} |
5 | 5 | ||
6 | +{% block extra_head %} | ||
7 | + <style> | ||
8 | + table, th, td { | ||
9 | + border: 1px solid black; | ||
10 | + } | ||
11 | + </style> | ||
12 | +{% endblock %} | ||
13 | + | ||
6 | {% block contenido %} | 14 | {% block contenido %} |
7 | <h1>Lista de Personas</h1> | 15 | <h1>Lista de Personas</h1> |
8 | <table> | 16 | <table> |
9 | <tr> | 17 | <tr> |
10 | <th>Nombre</th> | 18 | <th>Nombre</th> |
11 | <th>Documento</th> | 19 | <th>Documento</th> |
20 | + <th>Edad</th> | ||
12 | </tr> | 21 | </tr> |
13 | {% for persona in personas %} | 22 | {% for persona in personas %} |
14 | <tr> | 23 | <tr> |
15 | <td>{{persona}}</td> | 24 | <td>{{persona}}</td> |
16 | <td>{{persona.documento_identidad}}</td> | 25 | <td>{{persona.documento_identidad}}</td> |
26 | + <td>{{persona.edad}}</td> | ||
17 | </tr> | 27 | </tr> |
18 | {% endfor %} | 28 | {% endfor %} |
19 | </table> | 29 | </table> |
persona/tests/__init__.py
0 → 100644
persona/tests/fixtures.py
0 → 100644
1 | +import pytest | ||
2 | + | ||
3 | +from persona.models import Persona | ||
4 | + | ||
5 | + | ||
6 | +@pytest.fixture | ||
7 | +def crear_personas(): | ||
8 | + cecilia, _ = Persona.objects.get_or_create( | ||
9 | + documento_identidad='27351172', | ||
10 | + defaults={ | ||
11 | + 'nombre': 'Cecilia', | ||
12 | + 'apellido': 'Gallardo', | ||
13 | + 'fecha_nacimiento': '1979-06-13', | ||
14 | + | ||
15 | + } | ||
16 | + ) | ||
17 | + | ||
18 | + david, _ = Persona.objects.get_or_create( | ||
19 | + documento_identidad='39935311', | ||
20 | + defaults={ | ||
21 | + 'nombre': 'David', | ||
22 | + 'apellido': 'Sanchez', | ||
23 | + 'fecha_nacimiento': '1996-09-30', | ||
24 | + | ||
25 | + } | ||
26 | + ) | ||
27 | + | ||
28 | + santiago, _ = Persona.objects.get_or_create( | ||
29 | + documento_identidad='56586746', | ||
30 | + defaults={ | ||
31 | + 'nombre': 'Santiago', | ||
32 | + 'apellido': 'Quinteros', | ||
33 | + 'fecha_nacimiento': '2017-10-18', | ||
34 | + | ||
35 | + } | ||
36 | + ) | ||
37 | + return cecilia, david, santiago |
persona/tests/test_personas.py
0 → 100644
1 | +import pytest | ||
2 | +from rest_framework.test import APIClient | ||
3 | + | ||
4 | +from core.tests.fixtures_usuario import create_user, crear_permiso_usuario | ||
5 | + | ||
6 | +from persona.models import Persona | ||
7 | +from persona.tests.fixtures import crear_personas | ||
8 | + | ||
9 | + | ||
10 | +@pytest.mark.django_db | ||
11 | +def test_vista_lista_personas(crear_personas): | ||
12 | + client = APIClient() | ||
13 | + response = client.get('/persona/lista/') | ||
14 | + | ||
15 | + assert response.status_code == 200 | ||
16 | + | ||
17 | + queryset_personas = response.context['personas'] | ||
18 | + assert queryset_personas.count() == 3 | ||
19 | + assert queryset_personas[0].nombre == 'Cecilia' | ||
20 | + assert queryset_personas[1].nombre == 'David' | ||
21 | + assert queryset_personas[2].nombre == 'Santiago' | ||
22 | + | ||
23 | + | ||
24 | +@pytest.mark.django_db | ||
25 | +def test_vista_lista_personas_por_edad(crear_personas): | ||
26 | + client = APIClient() | ||
27 | + response = client.get(f'/persona/lista/?edad=25') | ||
28 | + | ||
29 | + assert response.status_code == 200 | ||
30 | + | ||
31 | + queryset_personas = response.context['personas'] | ||
32 | + assert queryset_personas.count() == 1 | ||
33 | + assert queryset_personas[0].nombre == 'David' | ||
34 | + | ||
35 | + | ||
36 | +@pytest.mark.django_db | ||
37 | +def test_vista_creacion_persona_autorizado(): | ||
38 | + usuario = create_user(username='Cecilia', is_staff=True) | ||
39 | + | ||
40 | + permiso_crear_personas = crear_permiso_usuario( | ||
41 | + codename='add_persona', | ||
42 | + name='Puede agregar una persona', | ||
43 | + app_name='persona', | ||
44 | + model_name='persona') | ||
45 | + | ||
46 | + usuario.user_permissions.add(permiso_crear_personas,) | ||
47 | + | ||
48 | + client = APIClient() | ||
49 | + client.login(username='Cecilia', password='password') | ||
50 | + | ||
51 | + data = { | ||
52 | + 'nombre': 'Ruben', | ||
53 | + 'apellido': 'Lopez', | ||
54 | + 'documento_identidad': '23789475', | ||
55 | + } | ||
56 | + response = client.post(f'/persona/creacion/', data=data) | ||
57 | + | ||
58 | + assert response.status_code == 302 | ||
59 | + assert Persona.objects.filter(documento_identidad='23789475').exists() | ||
60 | + | ||
61 | + | ||
62 | +@pytest.mark.django_db | ||
63 | +def test_vista_creacion_persona_no_autorizado(): | ||
64 | + usuario = create_user(username='Cecilia', is_staff=True) | ||
65 | + | ||
66 | + client = APIClient() | ||
67 | + client.login(username='Cecilia', password='password') | ||
68 | + | ||
69 | + data = { | ||
70 | + 'nombre': 'Ruben', | ||
71 | + 'apellido': 'Lopez', | ||
72 | + 'documento_identidad': '23789475', | ||
73 | + } | ||
74 | + response = client.post(f'/persona/creacion/', data=data) | ||
75 | + | ||
76 | + assert response.status_code == 403 | ||
77 | + assert not Persona.objects.filter(documento_identidad='23789475').exists() |
@@ -3,7 +3,7 @@ from django.urls import path | @@ -3,7 +3,7 @@ from django.urls import path | ||
3 | from persona.views import persona_lista, persona_detalle, PersonaCreateView | 3 | from persona.views import persona_lista, persona_detalle, PersonaCreateView |
4 | 4 | ||
5 | urlpatterns = [ | 5 | urlpatterns = [ |
6 | - path('lista', persona_lista, name='persona-lista'), | ||
7 | - path('detalle/<int:pk>', persona_detalle, name='persona-detalle'), | ||
8 | - path('creacion', PersonaCreateView.as_view(), name='persona-creacion') | 6 | + path('lista/', persona_lista, name='persona-lista'), |
7 | + path('detalle/<int:pk>/', persona_detalle, name='persona-detalle'), | ||
8 | + path('creacion/', PersonaCreateView.as_view(), name='persona-creacion') | ||
9 | ] | 9 | ] |
1 | -from django.contrib.auth.mixins import LoginRequiredMixin | 1 | +from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin |
2 | +from django.db.models.expressions import RawSQL | ||
2 | from django.shortcuts import render, get_object_or_404 | 3 | from django.shortcuts import render, get_object_or_404 |
3 | 4 | ||
4 | # Create your views here. | 5 | # Create your views here. |
@@ -10,7 +11,14 @@ from persona.models import Persona | @@ -10,7 +11,14 @@ from persona.models import Persona | ||
10 | 11 | ||
11 | 12 | ||
12 | def persona_lista(request): | 13 | def persona_lista(request): |
13 | - personas = Persona.objects.all() | 14 | + personas = Persona.objects.annotate( |
15 | + edad=RawSQL("date('now')-fecha_nacimiento", params="") | ||
16 | + ).order_by('documento_identidad') | ||
17 | + | ||
18 | + edad = request.GET.get('edad', None) | ||
19 | + if edad: | ||
20 | + personas = personas.filter(edad=int(edad)) | ||
21 | + | ||
14 | return render(request, 'persona/lista_personas.html', {'personas': personas}) | 22 | return render(request, 'persona/lista_personas.html', {'personas': personas}) |
15 | 23 | ||
16 | 24 | ||
@@ -19,9 +27,10 @@ def persona_detalle(request, pk): | @@ -19,9 +27,10 @@ def persona_detalle(request, pk): | ||
19 | return render(request, 'persona/detalle_persona.html', {'persona': persona}) | 27 | return render(request, 'persona/detalle_persona.html', {'persona': persona}) |
20 | 28 | ||
21 | 29 | ||
22 | -class PersonaCreateView(LoginRequiredMixin, CreateView): | 30 | +class PersonaCreateView(PermissionRequiredMixin, CreateView): |
23 | model = Persona | 31 | model = Persona |
24 | form_class = PersonaForm | 32 | form_class = PersonaForm |
25 | template_name = 'persona/crear.html' | 33 | template_name = 'persona/crear.html' |
26 | success_url = reverse_lazy('persona-lista') | 34 | success_url = reverse_lazy('persona-lista') |
35 | + permission_required = ('persona.add_persona',) | ||
27 | 36 |
pytest.ini
0 → 100644
requirements/testing.txt
0 → 100644
@@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
2 | <html> | 2 | <html> |
3 | <head> | 3 | <head> |
4 | <title>{% block titulo %}{% endblock %}</title> | 4 | <title>{% block titulo %}{% endblock %}</title> |
5 | + {% block extra_head %}{% endblock %} | ||
5 | </head> | 6 | </head> |
6 | <body> | 7 | <body> |
7 | {% block contenido %}{% endblock %} | 8 | {% block contenido %}{% endblock %} |
-
Please register or login to post a comment