Skip to content

Commit

Permalink
Merge pull request #27 from egc-sierrezuela1/test/booth-21
Browse files Browse the repository at this point in the history
INTEGRAR RAMA test/booth-21 EN DEVELOP
  • Loading branch information
Servandofg12 authored Dec 26, 2021
2 parents 2624612 + cb3d8f4 commit cdb2e28
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 6 deletions.
5 changes: 3 additions & 2 deletions decide/booth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

9 changes: 8 additions & 1 deletion decide/booth/templates/booth/sugerenciaform.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<h2>Formulario sugerencia de voto</h2>
</div>


<form action="{% url 'suggesting-send' %}" method="POST" aria-describedby="formHelp">
<div id="formHelp" style="display: none">Formulario para enviar a los administradores del sistema una sugerencia de votación. Debes introducir un título para la votación, la fecha que propones y tus argumentos y razones para incluirla</div>
{% csrf_token %}
Expand All @@ -36,6 +37,11 @@ <h2>Formulario sugerencia de voto</h2>
{% if suggesting %} value="{{suggesting.title}}" readonly {% endif %}
{% if post_data %} value="{{post_data.title}}" {% endif %}>
</div>
{% if error %}
<p style="color: red;">
Por favor, seleccione una fecha posterior al día de hoy.
</p>
{% endif %}
<div class="form-group">
<label for="suggestingDate">Fecha propuesta</label>
<input name="suggesting-date" type="date" class="form-control" id="suggestingDate" required
Expand All @@ -60,4 +66,5 @@ <h2>Formulario sugerencia de voto</h2>
</form>

</div>
{% endblock %}
{% endblock %}

123 changes: 123 additions & 0 deletions decide/booth/tests.py
Original file line number Diff line number Diff line change
@@ -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):

Expand Down Expand Up @@ -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="[email protected]")
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)
2 changes: 1 addition & 1 deletion decide/booth/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
path('login/', ingresar),
path('logout/', logout_view, name="logout"),
path('<int:voting_id>/', 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")
]
5 changes: 3 additions & 2 deletions decide/booth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cdb2e28

Please sign in to comment.