296 lines
12 KiB
Python
296 lines
12 KiB
Python
from django.contrib.auth.models import User
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.contrib.sites.models import Site
|
|
from django.test import TestCase
|
|
from django.urls import reverse
|
|
from registration.models import StudentRegistration
|
|
|
|
from .models import Participation, Question, Team, Video
|
|
|
|
|
|
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.question = Question.objects.create(participation=self.team.participation,
|
|
question="Pourquoi l'existence précède l'essence ?")
|
|
self.client.force_login(self.user)
|
|
|
|
def test_admin_pages(self):
|
|
"""
|
|
Load Django-admin pages.
|
|
"""
|
|
superuser = User.objects.create_superuser(
|
|
username="admin",
|
|
email="admin@example.com",
|
|
password="toto1234",
|
|
)
|
|
self.client.force_login(superuser)
|
|
|
|
# Test team pages
|
|
response = self.client.get(reverse("admin:index") + "participation/team/")
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
response = self.client.get(reverse("admin:index")
|
|
+ f"participation/team/{self.team.pk}/change/")
|
|
self.assertEqual(response.status_code, 200)
|
|
response = self.client.get(reverse("admin:index") +
|
|
f"r/{ContentType.objects.get_for_model(Team).id}/"
|
|
f"{self.team.pk}/")
|
|
self.assertRedirects(response, "http://" + Site.objects.get().domain +
|
|
str(self.team.get_absolute_url()), 302, 200)
|
|
|
|
# Test participation pages
|
|
self.team.participation.valid = True
|
|
self.team.participation.save()
|
|
response = self.client.get(reverse("admin:index") + "participation/participation/")
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
response = self.client.get(reverse("admin:index")
|
|
+ f"participation/participation/{self.team.participation.pk}/change/")
|
|
self.assertEqual(response.status_code, 200)
|
|
response = self.client.get(reverse("admin:index") +
|
|
f"r/{ContentType.objects.get_for_model(Participation).id}/"
|
|
f"{self.team.participation.pk}/")
|
|
self.assertRedirects(response, "http://" + Site.objects.get().domain +
|
|
str(self.team.participation.get_absolute_url()), 302, 200)
|
|
|
|
# Test video pages
|
|
response = self.client.get(reverse("admin:index") + "participation/video/")
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
response = self.client.get(reverse("admin:index")
|
|
+ f"participation/video/{self.team.participation.solution.pk}/change/")
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
# Test question pages
|
|
response = self.client.get(reverse("admin:index") + "participation/question/")
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
response = self.client.get(reverse("admin:index")
|
|
+ f"participation/question/{self.question.pk}/change/")
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
# Test phase pages
|
|
response = self.client.get(reverse("admin:index") + "participation/phase/")
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
response = self.client.get(reverse("admin:index")
|
|
+ f"participation/phase/1/change/")
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
def test_create_team(self):
|
|
"""
|
|
Try to create a team.
|
|
"""
|
|
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,
|
|
))
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
def test_join_team(self):
|
|
"""
|
|
Try to join an existing team.
|
|
"""
|
|
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, 403)
|
|
|
|
def test_no_myteam_redirect_noteam(self):
|
|
"""
|
|
Test redirection.
|
|
"""
|
|
response = self.client.get(reverse("participation:my_team_detail"))
|
|
self.assertTrue(response.status_code, 200)
|
|
|
|
def test_team_detail(self):
|
|
"""
|
|
Try to display the information of a team.
|
|
"""
|
|
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):
|
|
"""
|
|
Try to update team information.
|
|
"""
|
|
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())
|
|
|
|
def test_no_myparticipation_redirect_nomyparticipation(self):
|
|
"""
|
|
Ensure a permission denied when we search my team participation when we are in no team.
|
|
"""
|
|
response = self.client.get(reverse("participation:my_participation_detail"))
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
def test_participation_detail(self):
|
|
"""
|
|
Try to display the detail of a team participation.
|
|
"""
|
|
self.user.registration.team = self.team
|
|
self.user.registration.save()
|
|
|
|
response = self.client.get(reverse("participation:my_participation_detail"))
|
|
self.assertRedirects(response,
|
|
reverse("participation:participation_detail", args=(self.team.participation.pk,)),
|
|
302, 403)
|
|
|
|
self.team.participation.valid = True
|
|
self.team.participation.save()
|
|
response = self.client.get(reverse("participation:my_participation_detail"))
|
|
self.assertRedirects(response,
|
|
reverse("participation:participation_detail", args=(self.team.participation.pk,)),
|
|
302, 200)
|
|
|
|
response = self.client.get(reverse("participation:participation_detail", args=(self.team.participation.pk,)))
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
def test_upload_video(self):
|
|
"""
|
|
Try to send a solution video link.
|
|
"""
|
|
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:upload_video", args=(self.team.participation.solution.pk,)))
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
response = self.client.post(reverse("participation:upload_video", args=(self.team.participation.solution.pk,)),
|
|
data=dict(link="https://youtube.com/watch?v=73nsrixx7eI"))
|
|
self.assertRedirects(response,
|
|
reverse("participation:participation_detail", args=(self.team.participation.id,)),
|
|
302, 200)
|
|
self.team.participation.refresh_from_db()
|
|
self.assertEqual(self.team.participation.solution.platform, "youtube")
|
|
self.assertEqual(self.team.participation.solution.youtube_code, "73nsrixx7eI")
|
|
|
|
response = self.client.get(reverse("participation:participation_detail", args=(self.team.participation.pk,)))
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
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):
|
|
"""
|
|
Ensure that an admin can't create a team.
|
|
"""
|
|
response = self.client.post(reverse("participation:create_team"), data=dict(
|
|
name="Test team",
|
|
trigram="TES",
|
|
grant_animath_access_videos=False,
|
|
))
|
|
self.assertEqual(response.status_code, 403)
|
|
|
|
def test_join_team_forbidden(self):
|
|
"""
|
|
Ensure that an admin can't join a team.
|
|
"""
|
|
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, 403)
|
|
|
|
def test_my_team_forbidden(self):
|
|
"""
|
|
Ensure that an admin can't access to "My team".
|
|
"""
|
|
response = self.client.get(reverse("participation:my_team_detail"))
|
|
self.assertEqual(response.status_code, 403)
|