From b6cefc1519596593e37824a053a43726026bcf01 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 3 Nov 2020 17:21:50 +0100 Subject: [PATCH] Test setting the received participation and the sent participation --- apps/participation/forms.py | 12 +++-- apps/participation/tests.py | 96 ++++++++++++++++++++++++++++++++++--- apps/participation/views.py | 4 +- 3 files changed, 99 insertions(+), 13 deletions(-) diff --git a/apps/participation/forms.py b/apps/participation/forms.py index e447998..47220a6 100644 --- a/apps/participation/forms.py +++ b/apps/participation/forms.py @@ -126,16 +126,20 @@ class SendParticipationForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.fields["sent_participation"].initial = self.instance.sent_participation + try: + self.fields["sent_participation"].initial = self.instance.sent_participation + except: # No sent participation + pass self.fields["sent_participation"].queryset = Participation.objects.filter( ~Q(pk=self.instance.pk) & Q(problem=self.instance.problem, valid=True) ) def clean(self, commit=True): cleaned_data = super().clean() - participation = cleaned_data["sent_participation"] - participation.received_participation = self.instance - self.instance = participation + if "sent_participation" in cleaned_data: + participation = cleaned_data["sent_participation"] + participation.received_participation = self.instance + self.instance = participation return cleaned_data class Meta: diff --git a/apps/participation/tests.py b/apps/participation/tests.py index 8a21981..59a20be 100644 --- a/apps/participation/tests.py +++ b/apps/participation/tests.py @@ -493,6 +493,17 @@ class TestStudentParticipation(TestCase): response = self.client.get(reverse("participation:participation_detail", args=(self.team.participation.pk,))) 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) + + # Can't update the link during the second phase + response = self.client.post(reverse("participation:upload_video", args=(self.team.participation.solution.pk,)), + data=dict(link="https://youtube.com/watch?v=73nsrixx7eI")) + self.assertEqual(response.status_code, 200) + def test_current_phase(self): """ Ensure that the current phase is the good one. @@ -618,25 +629,96 @@ class TestAdmin(TestCase): ) self.client.force_login(self.user) + self.team1 = Team.objects.create( + name="Toto", + trigram="TOT", + ) + self.team1.participation.valid = True + self.team1.participation.problem = 1 + self.team1.participation.save() + + self.team2 = Team.objects.create( + name="Bliblu", + trigram="BIU", + ) + self.team2.participation.valid = True + self.team2.participation.problem = 1 + self.team2.participation.save() + + self.team3 = Team.objects.create( + name="Zouplop", + trigram="ZPL", + ) + self.team3.participation.valid = True + self.team3.participation.problem = 1 + self.team3.participation.save() + + self.other_team = Team.objects.create( + name="I am different", + trigram="IAD", + ) + self.other_team.participation.valid = True + self.other_team.participation.problem = 2 + self.other_team.participation.save() + def test_research(self): """ Try to search some things. """ - team = Team.objects.create( - name="Best team ever", - trigram="BTE", - ) - call_command("rebuild_index", "--noinput", "--verbosity", 0) - response = self.client.get(reverse("haystack_search") + "?q=" + team.name) + response = self.client.get(reverse("haystack_search") + "?q=" + self.team1.name) self.assertEqual(response.status_code, 200) self.assertTrue(response.context["object_list"]) - response = self.client.get(reverse("haystack_search") + "?q=" + team.trigram) + response = self.client.get(reverse("haystack_search") + "?q=" + self.team2.trigram) self.assertEqual(response.status_code, 200) self.assertTrue(response.context["object_list"]) + def test_set_received_video(self): + """ + Try to define the received video of a participation. + """ + response = self.client.get(reverse("participation:participation_receive_participation", + args=(self.team1.participation.pk,))) + self.assertEqual(response.status_code, 200) + + response = self.client.post(reverse("participation:participation_receive_participation", + args=(self.team1.participation.pk,)), + data=dict(received_participation=self.team2.participation.pk)) + self.assertRedirects(response, reverse("participation:participation_detail", + args=(self.team1.participation.pk,)), 302, 200) + + response = self.client.get(reverse("participation:participation_receive_participation", + args=(self.team1.participation.pk,))) + self.assertEqual(response.status_code, 200) + + response = self.client.post(reverse("participation:participation_send_participation", + args=(self.team1.participation.pk,)), + data=dict(sent_participation=self.team3.participation.pk)) + self.assertRedirects(response, reverse("participation:participation_detail", + args=(self.team1.participation.pk,)), 302, 200) + + self.team1.participation.refresh_from_db() + self.team2.participation.refresh_from_db() + self.team3.participation.refresh_from_db() + + self.assertEqual(self.team1.participation.received_participation.pk, self.team2.participation.pk) + self.assertEqual(self.team1.participation.sent_participation.pk, self.team3.participation.pk) + self.assertEqual(self.team2.participation.sent_participation.pk, self.team1.participation.pk) + self.assertEqual(self.team3.participation.received_participation.pk, self.team1.participation.pk) + + # The other team didn't work on the same problem + response = self.client.post(reverse("participation:participation_receive_participation", + args=(self.team1.participation.pk,)), + data=dict(received_participation=self.other_team.participation.pk)) + self.assertEqual(response.status_code, 200) + + response = self.client.post(reverse("participation:participation_send_participation", + args=(self.team1.participation.pk,)), + data=dict(sent_participation=self.other_team.participation.pk)) + self.assertEqual(response.status_code, 200) + def test_create_team_forbidden(self): """ Ensure that an admin can't create a team. diff --git a/apps/participation/views.py b/apps/participation/views.py index 4923a5d..04a8455 100644 --- a/apps/participation/views.py +++ b/apps/participation/views.py @@ -388,7 +388,7 @@ class SetParticipationReceiveParticipationView(AdminMixin, UpdateView): template_name = "participation/receive_participation_form.html" def get_success_url(self): - return reverse_lazy("participation:participation_detail", args=(self.object.pk,)) + return reverse_lazy("participation:participation_detail", args=(self.kwargs["pk"],)) class SetParticipationSendParticipationView(AdminMixin, UpdateView): @@ -400,7 +400,7 @@ class SetParticipationSendParticipationView(AdminMixin, UpdateView): template_name = "participation/send_participation_form.html" def get_success_url(self): - return reverse_lazy("participation:participation_detail", args=(self.object.pk,)) + return reverse_lazy("participation:participation_detail", args=(self.kwargs["pk"],)) class CreateQuestionView(LoginRequiredMixin, CreateView):