Début des tests
This commit is contained in:
parent
52839ff987
commit
80305ae249
5 changed files with 140 additions and 3 deletions
|
@ -1,3 +0,0 @@
|
||||||
from django.test import TestCase
|
|
||||||
|
|
||||||
# Create your tests here.
|
|
0
elections/tests/__init__.py
Normal file
0
elections/tests/__init__.py
Normal file
57
elections/tests/test_models.py
Normal file
57
elections/tests/test_models.py
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
from django.conf import settings
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
from .test_utils import create_election
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
|
class UserTests(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.election_1 = create_election(1)
|
||||||
|
self.election_2 = create_election(2)
|
||||||
|
self.election_3 = create_election(3, False)
|
||||||
|
|
||||||
|
self.cas_user = User.objects.create(
|
||||||
|
username="cas__user", email="user@clipper.ens.fr", full_name="Ulric Ser"
|
||||||
|
)
|
||||||
|
|
||||||
|
self.user_1 = User.objects.create(
|
||||||
|
username="1__user",
|
||||||
|
email="user1@ens.fr",
|
||||||
|
full_name="Ulric Ser",
|
||||||
|
election=self.election_1,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.pwd_user = User.objects.create(
|
||||||
|
username="pwd__user", email="user@ens.fr", full_name="Ulric Ser"
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_special_user(self):
|
||||||
|
self.assertTrue(self.user_1.can_vote(self.client, self.election_1))
|
||||||
|
self.assertFalse(self.user_1.can_vote(self.client, self.election_2))
|
||||||
|
self.assertFalse(self.user_1.can_vote(self.client, self.election_3))
|
||||||
|
|
||||||
|
def test_cas_user(self):
|
||||||
|
# On simule la connection par CAS via authens
|
||||||
|
session = self.client.session
|
||||||
|
session["CASCONNECTED"] = True
|
||||||
|
session.save()
|
||||||
|
|
||||||
|
# On sauvegarde le cookie de session
|
||||||
|
session_cookie_name = settings.SESSION_COOKIE_NAME
|
||||||
|
self.client.cookies[session_cookie_name] = session.session_key
|
||||||
|
|
||||||
|
self.assertFalse(self.cas_user.can_vote(self.client, self.election_1))
|
||||||
|
self.assertTrue(self.cas_user.can_vote(self.client, self.election_3))
|
||||||
|
|
||||||
|
def test_pwd_user(self):
|
||||||
|
self.assertFalse(self.pwd_user.can_vote(self.client, self.election_1))
|
||||||
|
self.assertFalse(self.pwd_user.can_vote(self.client, self.election_2))
|
||||||
|
|
||||||
|
def test_connection_method(self):
|
||||||
|
self.assertEqual(self.cas_user.connection_method, _("CAS"))
|
||||||
|
self.assertEqual(self.pwd_user.connection_method, _("mot de passe"))
|
||||||
|
self.assertEqual(self.user_1.connection_method, _("identifiants spécifiques"))
|
26
elections/tests/test_utils.py
Normal file
26
elections/tests/test_utils.py
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from django.utils import timezone
|
||||||
|
|
||||||
|
from elections.models import Election
|
||||||
|
|
||||||
|
|
||||||
|
def create_election(i, restricted=True, creator=None, future=False):
|
||||||
|
now = timezone.now()
|
||||||
|
day = timedelta(days=1)
|
||||||
|
|
||||||
|
if future:
|
||||||
|
start_date = now + day
|
||||||
|
end_date = now + 2 * day
|
||||||
|
else:
|
||||||
|
start_date = now - day
|
||||||
|
end_date = now + day
|
||||||
|
|
||||||
|
return Election.objects.create(
|
||||||
|
name=f"Election {i}",
|
||||||
|
short_name=f"election_{i}",
|
||||||
|
start_date=start_date,
|
||||||
|
end_date=end_date,
|
||||||
|
restricted=restricted,
|
||||||
|
created_by=creator,
|
||||||
|
)
|
57
elections/tests/test_views.py
Normal file
57
elections/tests/test_views.py
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.contrib.auth.models import Permission
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
|
from .test_utils import create_election
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
|
class AdminViewsTest(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
perm = Permission.objects.get(codename="is_admin")
|
||||||
|
self.admin_1 = User.objects.create(
|
||||||
|
username="pwd__admin1", email="admin1@ens.fr", full_name="Alfred Dmin"
|
||||||
|
)
|
||||||
|
self.admin_2 = User.objects.create(
|
||||||
|
username="pwd__admin2", email="admin2@ens.fr", full_name="Albert Dmin"
|
||||||
|
)
|
||||||
|
|
||||||
|
self.base_user = User.objects.create(
|
||||||
|
username="cas__user", email="user@clipper.ens.fr", full_name="Ulric Ser"
|
||||||
|
)
|
||||||
|
|
||||||
|
self.admin_1.user_permissions.add(perm)
|
||||||
|
self.admin_2.user_permissions.add(perm)
|
||||||
|
|
||||||
|
self.election_1 = create_election(1, creator=self.admin_1)
|
||||||
|
self.election_2 = create_election(2, creator=self.admin_1, future=True)
|
||||||
|
self.election_3 = create_election(3, creator=self.admin_2)
|
||||||
|
self.election_4 = create_election(4)
|
||||||
|
|
||||||
|
def test_create_view(self):
|
||||||
|
url = reverse("election.create")
|
||||||
|
|
||||||
|
# Création possible pour un admin
|
||||||
|
self.client.force_login(self.admin_1)
|
||||||
|
self.assertEqual(self.client.get(url).status_code, 200)
|
||||||
|
|
||||||
|
# Création impossible pour un user lambda
|
||||||
|
self.client.force_login(self.base_user)
|
||||||
|
self.assertEqual(self.client.get(url).status_code, 403)
|
||||||
|
|
||||||
|
def test_admin_view(self):
|
||||||
|
url_1 = reverse("election.admin", args=[self.election_1.pk])
|
||||||
|
url_3 = reverse("election.admin", args=[self.election_3.pk])
|
||||||
|
url_4 = reverse("election.admin", args=[self.election_4.pk])
|
||||||
|
|
||||||
|
# Accès uniquement à sa propre élection
|
||||||
|
self.client.force_login(self.admin_1)
|
||||||
|
self.assertEqual(self.client.get(url_1).status_code, 200)
|
||||||
|
self.assertEqual(self.client.get(url_3).status_code, 404)
|
||||||
|
self.assertEqual(self.client.get(url_4).status_code, 404)
|
||||||
|
|
||||||
|
# Accès refusé pour un user lambda
|
||||||
|
self.client.force_login(self.base_user)
|
||||||
|
self.assertEqual(self.client.get(url_1).status_code, 403)
|
Loading…
Reference in a new issue