1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2025-06-21 03:58:23 +02:00

Test questions, 100% coverage

This commit is contained in:
Yohann D'ANELLO
2020-11-03 17:58:05 +01:00
parent b6cefc1519
commit c35fb4e996
4 changed files with 168 additions and 78 deletions

View File

@ -2,7 +2,7 @@ import re
from bootstrap_datepicker_plus import DateTimePickerInput
from django import forms
from django.core.exceptions import ValidationError
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db.models import Q
from django.utils.translation import gettext_lazy as _
@ -128,7 +128,7 @@ class SendParticipationForm(forms.ModelForm):
super().__init__(*args, **kwargs)
try:
self.fields["sent_participation"].initial = self.instance.sent_participation
except: # No sent participation
except ObjectDoesNotExist: # No sent participation
pass
self.fields["sent_participation"].queryset = Participation.objects.filter(
~Q(pk=self.instance.pk) & Q(problem=self.instance.problem, valid=True)
@ -155,6 +155,11 @@ class QuestionForm(forms.ModelForm):
super().__init__(*args, **kwargs)
self.fields["question"].widget.attrs.update({"placeholder": _("How did you get the idea to ...?")})
def clean(self):
if Phase.current_phase().phase_number != 2:
self.add_error(None, _("You can only create or update a question during the second phase."))
return super().clean()
class Meta:
model = Question
fields = ('question',)

View File

@ -4,7 +4,6 @@ from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.core.management import call_command
from django.db.models import F
from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
@ -504,6 +503,70 @@ class TestStudentParticipation(TestCase):
data=dict(link="https://youtube.com/watch?v=73nsrixx7eI"))
self.assertEqual(response.status_code, 200)
def test_questions(self):
"""
Ensure that creating/updating/deleting a question is working.
"""
self.user.registration.team = self.team
self.user.registration.save()
self.team.participation.valid = True
self.team.participation.save()
response = self.client.get(reverse("participation:add_question", args=(self.team.participation.pk,)))
self.assertEqual(response.status_code, 200)
# We are not in second phase
response = self.client.post(reverse("participation:add_question", args=(self.team.participation.pk,)),
data=dict(question="I got censored!"))
self.assertEqual(response.status_code, 200)
# Set the second phase
for i in range(1, 5):
Phase.objects.filter(phase_number=i).update(start=timezone.now() + timedelta(days=i - 2),
end=timezone.now() + timedelta(days=i - 1))
self.assertEqual(Phase.current_phase().phase_number, 2)
# Create a question
response = self.client.post(reverse("participation:add_question", args=(self.team.participation.pk,)),
data=dict(question="I asked a question!"))
self.assertRedirects(response, reverse("participation:participation_detail",
args=(self.team.participation.pk,)), 302, 200)
qs = Question.objects.filter(participation=self.team.participation, question="I asked a question!")
self.assertTrue(qs.exists())
question = qs.get()
# Update a question
response = self.client.get(reverse("participation:update_question", args=(question.pk,)))
self.assertEqual(response.status_code, 200)
response = self.client.post(reverse("participation:update_question", args=(question.pk,)), data=dict(
question="The question changed!",
))
self.assertRedirects(response, reverse("participation:participation_detail",
args=(self.team.participation.pk,)), 302, 200)
question.refresh_from_db()
self.assertEqual(question.question, "The question changed!")
# Delete the question
response = self.client.get(reverse("participation:delete_question", args=(question.pk,)))
self.assertEqual(response.status_code, 200)
response = self.client.post(reverse("participation:delete_question", args=(question.pk,)))
self.assertRedirects(response, reverse("participation:participation_detail",
args=(self.team.participation.pk,)), 302, 200)
self.assertFalse(Question.objects.filter(pk=question.pk).exists())
# Non-authenticated users are redirected to login page
self.client.logout()
response = self.client.get(reverse("participation:add_question", args=(self.team.participation.pk,)))
self.assertRedirects(response, reverse("login") + "?next=" +
reverse("participation:add_question", args=(self.team.participation.pk,)), 302, 200)
response = self.client.get(reverse("participation:update_question", args=(self.question.pk,)))
self.assertRedirects(response, reverse("login") + "?next=" +
reverse("participation:delete_question", args=(self.question.pk,)), 302, 200)
response = self.client.get(reverse("participation:add_question", args=(self.question.pk,)))
self.assertRedirects(response, reverse("login") + "?next=" +
reverse("participation:add_question", args=(self.question.pk,)), 302, 200)
def test_current_phase(self):
"""
Ensure that the current phase is the good one.

View File

@ -418,6 +418,7 @@ class CreateQuestionView(LoginRequiredMixin, CreateView):
self.participation = Participation.objects.get(pk=kwargs["pk"])
if request.user.registration.is_admin or \
request.user.registration.participates and \
self.participation.valid and \
request.user.registration.team.pk == self.participation.team_id:
return super().dispatch(request, *args, **kwargs)
raise PermissionDenied
@ -443,6 +444,7 @@ class UpdateQuestionView(LoginRequiredMixin, UpdateView):
return self.handle_no_permission()
if request.user.registration.is_admin or \
request.user.registration.participates and \
self.object.participation.valid and \
request.user.registration.team.pk == self.object.participation.team_id:
return super().dispatch(request, *args, **kwargs)
raise PermissionDenied
@ -464,6 +466,7 @@ class DeleteQuestionView(LoginRequiredMixin, DeleteView):
return self.handle_no_permission()
if request.user.registration.is_admin or \
request.user.registration.participates and \
self.object.participation.valid and \
request.user.registration.team.pk == self.object.participation.team_id:
return super().dispatch(request, *args, **kwargs)
raise PermissionDenied