fix: force_close election

pull/55/head
Pierre-Louis Guhur 1 year ago
parent 24fb4e2b62
commit 5434d6facd

@ -445,7 +445,11 @@ def get_results(db: Session, election_ref: str) -> schemas.ResultsGet:
if db_election is None:
raise errors.NotFoundError("elections")
if db_election.hide_results and (db_election.date_end > datetime.now()):
if (
db_election.hide_results
and (db_election.date_end > datetime.now())
and not db_election.force_close
):
raise errors.ForbiddenError("The election is not closed")
query = db.query(

@ -127,7 +127,6 @@ class ElectionBase(BaseModel):
date_start: datetime = Field(default_factory=datetime.now)
date_end: datetime = Field(default_factory=_in_a_long_time)
hide_results: bool = True
force_close: bool = False
restricted: bool = False
class Config:
@ -136,6 +135,7 @@ class ElectionBase(BaseModel):
class ElectionGet(ElectionBase):
force_close: bool = False
grades: list[GradeGet] = Field(..., min_items=2, max_items=settings.max_grades)
candidates: list[CandidateGet] = Field(
..., min_items=2, max_items=settings.max_candidates
@ -223,6 +223,7 @@ class ElectionUpdate(ElectionBase):
ref: str
grades: list[GradeUpdate] = Field(..., min_items=2, max_items=settings.max_grades)
num_voters: int = Field(0, ge=0, le=settings.max_voters)
force_close: bool = False
candidates: list[CandidateUpdate] = Field(
..., min_items=2, max_items=settings.max_candidates
)

@ -1,5 +1,6 @@
import string
import copy
from datetime import datetime, timedelta
import typing as t
import random
from fastapi.testclient import TestClient
@ -13,7 +14,7 @@ from ..main import app
test_database_url = "sqlite:///./test.db"
test_engine = create_engine(
test_database_url, connect_args={"check_same_thread": False}, echo=True
test_database_url, connect_args={"check_same_thread": False}, echo=False
)
TestingSessionLocal: sessionmaker = sessionmaker( # type: ignore
autocommit=False, autoflush=False, bind=test_engine
@ -55,7 +56,9 @@ class RandomElection(t.TypedDict):
candidates: list[dict[str, str]]
grades: list[dict[str, int | str]]
restricted: t.NotRequired[bool]
hide_results: t.NotRequired[bool]
num_voters: t.NotRequired[int]
date_end: t.NotRequired[str]
def _random_election(num_candidates: int, num_grades: int) -> RandomElection:
@ -363,6 +366,7 @@ def test_cannot_ballot_box_stuffing():
def test_get_results():
# Create a random election
body = _random_election(10, 5)
body["hide_results"] = False
response = client.post("/elections", json=body)
assert response.status_code == 200, response.content
data = response.json()
@ -385,6 +389,42 @@ def test_get_results():
assert len(profile) == len(data["candidates"])
def test_get_results_with_hide_results():
# Create a random election
body = _random_election(10, 5)
body["hide_results"] = True
body["date_end"] = (datetime.now() + timedelta(days=1)).isoformat()
response = client.post("/elections", json=body)
assert response.status_code == 200, response.content
data = response.json()
election_ref = data["ref"]
admin_token = data["admin"]
# We create votes using the ID
votes = _generate_votes_from_response("id", data)
response = client.post(
f"/ballots", json={"votes": votes, "election_ref": election_ref}
)
assert response.status_code == 200, data
# But, we can't get the results
response = client.get(f"/results/{election_ref}")
assert response.status_code == 403, data
# So, we close the election
print("UPDATE", data["force_close"])
data["force_close"] = True
response = client.put(
f"/elections", json=data, headers={"Authorization": f"Bearer {admin_token}"}
)
assert response.status_code == 200, response.text
data = response.json()
# Now, we can access to the results
response = client.get(f"/results/{election_ref}")
assert response.status_code == 200, response.text
def test_update_election():
# Create a random election
body = _random_election(10, 5)
@ -452,11 +492,10 @@ def test_close_election():
response = client.post("/elections", json=body)
assert response.status_code == 200, response.content
data = response.json()
assert data["force_close"] == False
data["force_close"] = True
token = data["admin"]
# Check that the request fails with a wrong token
data["force_close"] = True
response = client.put(
f"/elections", json=data, headers={"Authorization": f"Bearer {token}WRONG"}
)

Loading…
Cancel
Save