Skip to content

Commit

Permalink
[back] tests: add missing tests for the proof_of_vote API (#1998)
Browse files Browse the repository at this point in the history
GresilleSiffle authored Aug 1, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent b34e8f9 commit 0779379
Showing 2 changed files with 112 additions and 0 deletions.
File renamed without changes.
112 changes: 112 additions & 0 deletions backend/tournesol/tests/test_api_exports_proof.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import csv
import io

from django.test import TestCase, override_settings
from rest_framework import status
from rest_framework.test import APIClient

from core.models import User
from tournesol.models import Poll
from tournesol.tests.factories.comparison import ComparisonFactory


@override_settings(SECRET_KEY="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
class ExportProofOfVote(TestCase):
"""
Test case of the API /exports/polls/{poll_name}/proof_of_vote/
"""

def setUp(self):
self.client = APIClient()
self.default_poll = Poll.default_poll()

def test_anon_cant_get_proof_of_vote(self):
"""
An anonymous user should not be able to download the proof of vote.
"""
response = self.client.get(f"/exports/polls/{self.default_poll.name}/proof_of_vote/")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_auth_non_staff_cant_get_proof_of_vote(self):
"""
An authenticated non-administrator user should not be able to download the proof of vote.
"""
user = User.objects.create_user(
username="test_non_saff",
email="[email protected]",
is_active=True,
is_staff=False,
)

self.client.force_authenticate(user)
response = self.client.get(f"/exports/polls/{self.default_poll.name}/proof_of_vote/")
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_auth_staff_can_get_proof_of_vote_empty(self):
"""
An authenticated administrator user should be able to download an empty proof of vote.
"""
user = User.objects.create_user(
username="test_staff",
email="[email protected]",
is_active=True,
is_staff=True,
)

self.client.force_authenticate(user)
response = self.client.get(f"/exports/polls/{self.default_poll.name}/proof_of_vote/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.headers["Content-Type"], "text/csv")

csv_lines = [line for line in csv.reader(io.StringIO(response.content.decode()))]
self.assertEqual(len(csv_lines), 1)
self.assertEqual(
csv_lines[0],
[
"user_id",
"username",
"email",
"n_comparisons",
"signature",
]
)

def test_auth_staff_can_get_proof_of_vote_non_empty(self):
"""
An authenticated administrator user should be able to download the proof of vote.
"""
user = User.objects.create_user(
username="test_staff",
email="[email protected]",
is_active=True,
is_staff=True,
)

ComparisonFactory(poll=self.default_poll, user=user)
self.client.force_authenticate(user)
response = self.client.get(f"/exports/polls/{self.default_poll.name}/proof_of_vote/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.headers["Content-Type"], "text/csv")

csv_lines = [line for line in csv.reader(io.StringIO(response.content.decode()))]
self.assertEqual(len(csv_lines), 2)
self.assertEqual(
csv_lines[0],
[
"user_id",
"username",
"email",
"n_comparisons",
"signature",
]
)
self.assertEqual(
csv_lines[1],
[
str(user.id),
"test_staff",
"[email protected]",
"1",
self.default_poll.get_user_proof(user.id, "proof_of_vote"),
]
)

0 comments on commit 0779379

Please sign in to comment.