mirror of
https://github.com/ae-utbm/sith.git
synced 2026-03-13 15:15:03 +00:00
Merge pull request #1281 from ae-utbm/test_election
add test_election_form
This commit is contained in:
@@ -6,6 +6,8 @@ from django.test import Client, TestCase
|
|||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now
|
||||||
from model_bakery import baker
|
from model_bakery import baker
|
||||||
|
from model_bakery.recipe import Recipe
|
||||||
|
from pytest_django.asserts import assertRedirects
|
||||||
|
|
||||||
from core.baker_recipes import subscriber_user
|
from core.baker_recipes import subscriber_user
|
||||||
from core.models import Group, User
|
from core.models import Group, User
|
||||||
@@ -52,6 +54,102 @@ class TestElectionUpdateView(TestElection):
|
|||||||
assert response.status_code == 403
|
assert response.status_code == 403
|
||||||
|
|
||||||
|
|
||||||
|
class TestElectionForm(TestCase):
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
cls.election = baker.make(Election, end_date=now() + timedelta(days=1))
|
||||||
|
cls.group = baker.make(Group)
|
||||||
|
cls.election.vote_groups.add(cls.group)
|
||||||
|
cls.election.edit_groups.add(cls.group)
|
||||||
|
lists = baker.make(
|
||||||
|
ElectionList, election=cls.election, _quantity=2, _bulk_create=True
|
||||||
|
)
|
||||||
|
cls.roles = baker.make(
|
||||||
|
Role, election=cls.election, _quantity=2, _bulk_create=True
|
||||||
|
)
|
||||||
|
users = baker.make(User, _quantity=4, _bulk_create=True)
|
||||||
|
recipe = Recipe(Candidature)
|
||||||
|
cls.cand = [
|
||||||
|
recipe.prepare(role=cls.roles[0], user=users[0], election_list=lists[0]),
|
||||||
|
recipe.prepare(role=cls.roles[0], user=users[1], election_list=lists[1]),
|
||||||
|
recipe.prepare(role=cls.roles[1], user=users[2], election_list=lists[0]),
|
||||||
|
recipe.prepare(role=cls.roles[1], user=users[3], election_list=lists[1]),
|
||||||
|
]
|
||||||
|
Candidature.objects.bulk_create(cls.cand)
|
||||||
|
cls.vote_url = reverse("election:vote", kwargs={"election_id": cls.election.id})
|
||||||
|
cls.detail_url = reverse(
|
||||||
|
"election:detail", kwargs={"election_id": cls.election.id}
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_election_good_form(self):
|
||||||
|
postes = (self.roles[0].title, self.roles[1].title)
|
||||||
|
votes = [
|
||||||
|
{postes[0]: "", postes[1]: str(self.cand[2].id)},
|
||||||
|
{postes[0]: "", postes[1]: ""},
|
||||||
|
{postes[0]: str(self.cand[0].id), postes[1]: str(self.cand[2].id)},
|
||||||
|
{postes[0]: str(self.cand[0].id), postes[1]: str(self.cand[3].id)},
|
||||||
|
]
|
||||||
|
voters = subscriber_user.make(_quantity=len(votes), _bulk_create=True)
|
||||||
|
self.group.users.set(voters)
|
||||||
|
|
||||||
|
for voter, vote in zip(voters, votes, strict=True):
|
||||||
|
assert self.election.can_vote(voter)
|
||||||
|
self.client.force_login(voter)
|
||||||
|
response = self.client.post(self.vote_url, data=vote)
|
||||||
|
assertRedirects(response, self.detail_url)
|
||||||
|
|
||||||
|
assert set(self.election.voters.all()) == set(voters)
|
||||||
|
assert self.election.results == {
|
||||||
|
postes[0]: {
|
||||||
|
self.cand[0].user.username: {"percent": 50.0, "vote": 2},
|
||||||
|
self.cand[1].user.username: {"percent": 0.0, "vote": 0},
|
||||||
|
"blank vote": {"percent": 50.0, "vote": 2},
|
||||||
|
"total vote": 4,
|
||||||
|
},
|
||||||
|
postes[1]: {
|
||||||
|
self.cand[2].user.username: {"percent": 50.0, "vote": 2},
|
||||||
|
self.cand[3].user.username: {"percent": 25.0, "vote": 1},
|
||||||
|
"blank vote": {"percent": 25.0, "vote": 1},
|
||||||
|
"total vote": 4,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
def test_election_bad_form(self):
|
||||||
|
postes = (self.roles[0].title, self.roles[1].title)
|
||||||
|
|
||||||
|
votes = [
|
||||||
|
{postes[0]: "", postes[1]: str(self.cand[0].id)}, # wrong candidate
|
||||||
|
{postes[0]: ""},
|
||||||
|
{
|
||||||
|
postes[0]: "0123456789", # unknow users
|
||||||
|
postes[1]: str(subscriber_user.make().id), # not a candidate
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
]
|
||||||
|
voters = subscriber_user.make(_quantity=len(votes), _bulk_create=True)
|
||||||
|
self.group.users.set(voters)
|
||||||
|
|
||||||
|
for voter, vote in zip(voters, votes, strict=True):
|
||||||
|
self.client.force_login(voter)
|
||||||
|
response = self.client.post(self.vote_url, data=vote)
|
||||||
|
assertRedirects(response, self.detail_url)
|
||||||
|
|
||||||
|
assert self.election.results == {
|
||||||
|
postes[0]: {
|
||||||
|
self.cand[0].user.username: {"percent": 0.0, "vote": 0},
|
||||||
|
self.cand[1].user.username: {"percent": 0.0, "vote": 0},
|
||||||
|
"blank vote": {"percent": 100.0, "vote": 2},
|
||||||
|
"total vote": 2,
|
||||||
|
},
|
||||||
|
postes[1]: {
|
||||||
|
self.cand[2].user.username: {"percent": 0.0, "vote": 0},
|
||||||
|
self.cand[3].user.username: {"percent": 0.0, "vote": 0},
|
||||||
|
"blank vote": {"percent": 100.0, "vote": 2},
|
||||||
|
"total vote": 2,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_election_create_list_permission(client: Client):
|
def test_election_create_list_permission(client: Client):
|
||||||
election = baker.make(Election, end_candidature=now() + timedelta(hours=1))
|
election = baker.make(Election, end_candidature=now() + timedelta(hours=1))
|
||||||
|
|||||||
Reference in New Issue
Block a user