From 0a3fffe21edb6b3442b017c5617485a449e2d2d4 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 3 Nov 2020 14:43:51 +0100 Subject: [PATCH] Test leave team --- apps/participation/tests.py | 46 +++++++++++++++++++++++++++++++++++++ apps/participation/views.py | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/apps/participation/tests.py b/apps/participation/tests.py index f695b67..2459276 100644 --- a/apps/participation/tests.py +++ b/apps/participation/tests.py @@ -376,6 +376,45 @@ class TestStudentParticipation(TestCase): self.assertRedirects(response, reverse("participation:team_detail", args=(self.team.pk,)), 302, 200) self.assertTrue(Team.objects.filter(trigram="BBB", participation__problem=3).exists()) + def test_leave_team(self): + """ + A user is in a team, and leaves it. + """ + # User is not in a team + response = self.client.post(reverse("participation:team_leave")) + self.assertEqual(response.status_code, 403) + + self.user.registration.team = self.team + self.user.registration.save() + + # Team is pending validation + self.team.participation.valid = False + self.team.participation.save() + response = self.client.post(reverse("participation:team_leave")) + self.assertEqual(response.status_code, 403) + + # Team is valid + self.team.participation.valid = True + self.team.participation.save() + response = self.client.post(reverse("participation:team_leave")) + self.assertEqual(response.status_code, 403) + + # Unauthenticated users are redirected to login page + self.client.logout() + response = self.client.get(reverse("participation:team_leave")) + self.assertRedirects(response, reverse("login") + "?next=" + reverse("participation:team_leave"), 302, 200) + + self.client.force_login(self.user) + + self.team.participation.valid = None + self.team.participation.save() + + response = self.client.post(reverse("participation:team_leave")) + self.assertRedirects(response, reverse("index"), 302, 200) + self.user.registration.refresh_from_db() + self.assertIsNone(self.user.registration.team) + self.assertFalse(Team.objects.filter(pk=self.team.pk).exists()) + def test_no_myparticipation_redirect_nomyparticipation(self): """ Ensure a permission denied when we search my team participation when we are in no team. @@ -500,6 +539,13 @@ class TestAdminForbidden(TestCase): )) self.assertTrue(response.status_code, 403) + def test_leave_team_forbidden(self): + """ + Ensure that an admin can't leave a team. + """ + response = self.client.get(reverse("participation:team_leave")) + self.assertTrue(response.status_code, 403) + def test_my_team_forbidden(self): """ Ensure that an admin can't access to "My team". diff --git a/apps/participation/views.py b/apps/participation/views.py index 6b98a67..4923a5d 100644 --- a/apps/participation/views.py +++ b/apps/participation/views.py @@ -316,7 +316,7 @@ class TeamLeaveView(LoginRequiredMixin, TemplateView): def dispatch(self, request, *args, **kwargs): if not request.user.is_authenticated: return self.handle_no_permission() - if not request.user.registration.team: + if not request.user.registration.participates or not request.user.registration.team: raise PermissionDenied(_("You are not in a team.")) if request.user.registration.team.participation.valid is not None: raise PermissionDenied(_("The team is already validated or the validation is pending."))