diff --git a/apps/participation/tests.py b/apps/participation/tests.py index e69de29..d7c0ca8 100644 --- a/apps/participation/tests.py +++ b/apps/participation/tests.py @@ -0,0 +1,155 @@ +from django.contrib.auth.models import User +from django.test import TestCase +from django.urls import reverse +from registration.models import StudentRegistration + +from .models import Team + + +class TestStudentParticipation(TestCase): + def setUp(self) -> None: + self.user = User.objects.create( + first_name="Toto", + last_name="Toto", + email="toto@example.com", + password="toto", + ) + StudentRegistration.objects.create( + user=self.user, + student_class=12, + school="Earth", + give_contact_to_animath=True, + email_confirmed=True, + ) + self.team = Team.objects.create( + name="Super team", + trigram="AAA", + access_code="azerty", + grant_animath_access_videos=True, + ) + self.client.force_login(self.user) + + # TODO Remove these lines + str(self.team) + str(self.team.participation) + from .models import Video + str(Video(participation=self.team.participation)) + + def test_create_team(self): + response = self.client.get(reverse("participation:create_team")) + self.assertEqual(response.status_code, 200) + + response = self.client.post(reverse("participation:create_team"), data=dict( + name="Test team", + trigram="123", + grant_animath_access_videos=False, + )) + self.assertEqual(response.status_code, 200) + + response = self.client.post(reverse("participation:create_team"), data=dict( + name="Test team", + trigram="TES", + grant_animath_access_videos=False, + )) + self.assertTrue(Team.objects.filter(trigram="TES").exists()) + team = Team.objects.get(trigram="TES") + self.assertRedirects(response, reverse("participation:team_detail", args=(team.pk,)), 302, 200) + + # Already in a team + response = self.client.post(reverse("participation:create_team"), data=dict( + name="Test team 2", + trigram="TET", + grant_animath_access_videos=False, + )) + + def test_join_team(self): + response = self.client.get(reverse("participation:join_team")) + self.assertEqual(response.status_code, 200) + + team = Team.objects.create(name="Test", trigram="TES") + + response = self.client.post(reverse("participation:join_team"), data=dict( + access_code="éééééé", + )) + self.assertEqual(response.status_code, 200) + + response = self.client.post(reverse("participation:join_team"), data=dict( + access_code=team.access_code, + )) + self.assertRedirects(response, reverse("participation:team_detail", args=(team.pk,)), 302, 200) + self.assertTrue(Team.objects.filter(trigram="TES").exists()) + + # Already joined + response = self.client.post(reverse("participation:join_team"), data=dict( + access_code=team.access_code, + )) + self.assertEqual(response.status_code, 200) + + def test_no_myteam_redirect_noteam(self): + response = self.client.get(reverse("participation:my_team_detail")) + self.assertTrue(response.status_code, 200) + + def test_team_detail(self): + self.user.registration.team = self.team + self.user.registration.save() + + response = self.client.get(reverse("participation:my_team_detail")) + self.assertRedirects(response, reverse("participation:team_detail", args=(self.team.pk,)), 302, 200) + + response = self.client.get(reverse("participation:team_detail", args=(self.team.pk,))) + self.assertEqual(response.status_code, 200) + + def test_update_team(self): + self.user.registration.team = self.team + self.user.registration.save() + + response = self.client.get(reverse("participation:update_team", args=(self.team.pk,))) + self.assertEqual(response.status_code, 200) + + # Form is invalid + response = self.client.post(reverse("participation:update_team", args=(self.team.pk,)), data=dict( + name="Updated team name", + trigram="BBB", + grant_animath_access_videos=True, + problem=42, + )) + self.assertEqual(response.status_code, 200) + + response = self.client.post(reverse("participation:update_team", args=(self.team.pk,)), data=dict( + name="Updated team name", + trigram="BBB", + grant_animath_access_videos=True, + problem=3, + )) + self.assertRedirects(response, reverse("participation:team_detail", args=(self.team.pk,)), 302, 200) + self.assertTrue(Team.objects.filter(trigram="BBB", participation__problem=3).exists()) + + +class TestAdminForbidden(TestCase): + def setUp(self) -> None: + self.user = User.objects.create_superuser( + username="admin@example.com", + email="admin@example.com", + password="admin", + ) + self.client.force_login(self.user) + + def test_create_team_forbidden(self): + response = self.client.post(reverse("participation:create_team"), data=dict( + name="Test team", + trigram="TES", + grant_animath_access_videos=False, + )) + self.assertTrue(response.status_code, 200) + + def test_join_team_forbidden(self): + team = Team.objects.create(name="Test", trigram="TES") + + response = self.client.post(reverse("participation:join_team"), data=dict( + access_code=team.access_code, + )) + self.assertTrue(response.status_code, 200) + + def test_my_team_forbidden(self): + response = self.client.get(reverse("participation:my_team_detail")) + self.assertTrue(response.status_code, 200) diff --git a/apps/participation/views.py b/apps/participation/views.py index bb06440..bebfa9c 100644 --- a/apps/participation/views.py +++ b/apps/participation/views.py @@ -32,7 +32,7 @@ class CreateTeamView(LoginRequiredMixin, CreateView): return ret def get_success_url(self): - return reverse_lazy("index") + return reverse_lazy("participation:team_detail", args=(self.object.pk,)) class JoinTeamView(LoginRequiredMixin, FormView): @@ -52,13 +52,14 @@ class JoinTeamView(LoginRequiredMixin, FormView): form.add_error(None, _("You are already in a team.")) return self.form_invalid(form) + self.object = form.instance ret = super().form_valid(form) registration.team = form.instance registration.save() return ret def get_success_url(self): - return reverse_lazy("index") + return reverse_lazy("participation:team_detail", args=(self.object.pk,)) class MyTeamDetailView(LoginRequiredMixin, RedirectView):