diff --git a/decide/booth/models.py b/decide/booth/models.py index d26d7df8d2..e872f1777c 100644 --- a/decide/booth/models.py +++ b/decide/booth/models.py @@ -26,9 +26,9 @@ def __str__(self): class Sugerencia(models.Model): user_id = models.IntegerField() title = models.CharField(max_length=200, blank=False) - suggesting_date = models.DateField(default=datetime.date.today) + suggesting_date = models.DateField(blank=False) content = models.TextField(blank=False, max_length=4000) - send_date = models.DateField(blank=False) + send_date = models.DateField(default=datetime.date.today) is_approved = models.NullBooleanField() def __str__(self): @@ -39,3 +39,4 @@ def was_published_recently(self): now = timezone.now().date() limit_date = now - datetime.timedelta(weeks=4) return limit_date <= self.send_date + diff --git a/decide/booth/templates/booth/sugerenciaform.html b/decide/booth/templates/booth/sugerenciaform.html index b2d31d2e25..cee9158722 100644 --- a/decide/booth/templates/booth/sugerenciaform.html +++ b/decide/booth/templates/booth/sugerenciaform.html @@ -27,6 +27,7 @@

Formulario sugerencia de voto

+
{% csrf_token %} @@ -36,6 +37,11 @@

Formulario sugerencia de voto

{% if suggesting %} value="{{suggesting.title}}" readonly {% endif %} {% if post_data %} value="{{post_data.title}}" {% endif %}> + {% if error %} +

+ Por favor, seleccione una fecha posterior al día de hoy. +

+ {% endif %}
Formulario sugerencia de voto
-{% endblock %} \ No newline at end of file +{% endblock %} + diff --git a/decide/booth/tests.py b/decide/booth/tests.py index 5c7c13c609..6a61ee36ea 100644 --- a/decide/booth/tests.py +++ b/decide/booth/tests.py @@ -1,14 +1,28 @@ +import datetime +from django.contrib.auth import authenticate +from django.http import request + from django.test import TestCase +from django.test.client import Client, RequestFactory +from .forms import SugerenciaForm +from .models import Sugerencia +from .views import check_unresolved_post_data, is_future_date, send_suggesting_form from rest_framework.test import APIClient from django.contrib.auth.models import User from django.urls import reverse from django.conf import settings +from django.utils import timezone from mixnet.models import Auth from base import mods +NOW_DATE = timezone.now().date() +S_DATE = NOW_DATE + datetime.timedelta(weeks=1) +M_DATE = NOW_DATE - datetime.timedelta(days=31) +E_DATE = NOW_DATE - datetime.timedelta(weeks=1) + #--------------------------------------TEST LOGIN--------------------------------------------- class LoginTest(TestCase): @@ -38,3 +52,112 @@ def test_get_logout(self): self.assertEqual('username' in session, False) +#--------------------------------------TEST FORMULARIO SUGERENCIA--------------------------------------------- +class FormSugerenciaTest(TestCase): + + def setUp(self): + self.client = APIClient() + self.request_factory = RequestFactory() + self.user = User.objects.create(username="User", password="password", email="ejemplo@gmail.com") + self.client.force_authenticate(user=self.user) + + + def tearDown(self): + super().tearDown() + + + def test_was_published_recently_more_than_month(self): + now = timezone.now().date() + past_date = now - datetime.timedelta(weeks=4, days=1) + past_suggesting_form = Sugerencia(send_date=past_date, suggesting_date=now) + self.assertIs(past_suggesting_form.was_published_recently(), False) + + + def test_was_published_recently_last_week(self): + now = timezone.now().date() + past_date = now - datetime.timedelta(weeks=1) + past_suggesting_form = Sugerencia(send_date=past_date, suggesting_date=now) + self.assertIs(past_suggesting_form.was_published_recently(), True) + + + def test_send_suggesting_form_success(self): + + + future_date = timezone.now().date() + datetime.timedelta(weeks=1) + date = future_date.strftime("%Y-%m-%d") + + data = {'suggesting-title': 'Suggesting', 'suggesting-date': date, 'suggesting-content': 'Full suggesting content...'} + + request = self.request_factory.post('booth/sugerenciaformulario/send', data) + request.user = self.user #le meto el usuario a mano + + initital_suggesting_counter = Sugerencia.objects.all().count() + + response = send_suggesting_form(request) + + afterpost_suggesting_counter = Sugerencia.objects.all().count() + + + self.assertEqual(afterpost_suggesting_counter, initital_suggesting_counter + 1) + #self.assertEqual(response.status_code, 200) #-> no devuelve un 200 porque le metemos el controlador a mano, pero hace el post que es lo que queriamos probar + + + def test_send_suggesting_form_with_error(self): + data = {'suggesting-title': 'Suggestsing', 'suggesting-date': '2020-12-01', 'suggesting-content': 'Full suggesting content...'} + initital_suggesting_counter = Sugerencia.objects.all().count() + + response = self.client.post('/booth/sugerenciaformulario/send/', data, follow=True) + + afterpost_suggesting_counter = Sugerencia.objects.all().count() + + self.assertEqual(response.status_code, 200) + self.assertEqual(afterpost_suggesting_counter, initital_suggesting_counter) + + + def test_send_suggesting_form_not_post_method(self): + response = self.client.get('/booth/sugerenciaformulario/send/') + print("AQUIII") + print(response.status_code) + self.assertEqual(response.status_code, 302) + + + def test_check_unresolved_post_data(self): + context = {} + session = self.client.session + session['title'] = "Suggesting title" + session['suggesting_date'] = "2020-12-01" + session['content'] = "Suggesting content..." + session['errors'] = "Suggesting error msg!" + session.save() + + context['post_data'] = check_unresolved_post_data(session) + + self.assertEqual(context['post_data']['title'], "Suggesting title") + self.assertEqual('title' in session, False) + self.assertEqual('suggesting_date' in session, False) + self.assertEqual('content' in session, False) + self.assertEqual('errors' in session, False) + + + def test_check_unresolved_post_data_with_empty_session(self): + context = {} + session = self.client.session + + context['post_data'] = check_unresolved_post_data(session) + + self.assertEqual(not context['post_data'], True) + + + def test_is_future_date_with_past_date(self): + date = timezone.now().date() - datetime.timedelta(weeks=1) + self.assertEqual(is_future_date(date), False) + + + def test_is_future_date_with_now_date(self): + date = timezone.now().date() + self.assertEqual(is_future_date(date), False) + + + def test_is_future_date_with_future_date(self): + date = timezone.now().date() + datetime.timedelta(weeks=1) + self.assertEqual(is_future_date(date), True) \ No newline at end of file diff --git a/decide/booth/urls.py b/decide/booth/urls.py index bf7e6c903a..e6dd6b6ff5 100644 --- a/decide/booth/urls.py +++ b/decide/booth/urls.py @@ -12,6 +12,6 @@ path('login/', ingresar), path('logout/', logout_view, name="logout"), path('/', BoothView.as_view(), name="votacion"), - path('sugerenciaformulario/', SugerenciaVista.sugerencia_de_voto), + path('sugerenciaformulario/', SugerenciaVista.sugerencia_de_voto, name="formulario_suggest"), path('sugerenciaformulario/send/', send_suggesting_form, name="suggesting-send") ] diff --git a/decide/booth/views.py b/decide/booth/views.py index 13ef8ed0c7..a5abf7bb6d 100644 --- a/decide/booth/views.py +++ b/decide/booth/views.py @@ -156,18 +156,19 @@ def send_suggesting_form(request): s_date = datetime.datetime.strptime(str_s_date, '%Y-%m-%d').date() - if s_date > timezone.now().date(): + if is_future_date(s_date): s = Sugerencia(user_id=user_id, title=title, suggesting_date=s_date, content=content, send_date=send_date) s.save() return HttpResponseRedirect(reverse('pagina-inicio')) else: + #no muestra el error en la vista request.session['title'] = title request.session['suggesting_date'] = str_s_date request.session['content'] = content request.session['errors'] = "La fecha seleccionada ya ha pasado. Debe seleccionar una posterior al día de hoy." return HttpResponseRedirect(reverse('formulario_suggest')) else: - return HttpResponseRedirect(reverse('booth/inicio.html')) + return HttpResponseRedirect(reverse('pagina-inicio')) def is_future_date(date): return date > timezone.now().date()