@@ -59,6 +59,7 @@ class RandomElection(t.TypedDict):
5959 hide_results : bool
6060 num_voters : int
6161 date_end : t .Optional [str ]
62+ date_start : t .Optional [str ]
6263
6364
6465def _random_election (num_candidates : int , num_grades : int ) -> RandomElection :
@@ -71,6 +72,7 @@ def _random_election(num_candidates: int, num_grades: int) -> RandomElection:
7172 candidates = [{"name" : _random_string (10 )} for i in range (num_candidates )]
7273 name = _random_string (10 )
7374 return {
75+ "date_start" :None ,
7476 "candidates" : candidates ,
7577 "grades" : grades ,
7678 "name" : name ,
@@ -394,6 +396,56 @@ def test_cannot_update_vote_on_ended_election():
394396
395397 assert response .status_code == 403 , response .json ()
396398
399+ ## TODO: cannot change start_date if a people vote;
400+ ##
401+ def test_cannot_create_vote_on_unstarted_election ():
402+ """
403+ On an unstarted election, we are not allowed to create new votes
404+ """
405+ # Create a random election
406+ body = _random_election (10 , 5 )
407+ body ["date_start" ] = (datetime .now () + timedelta (days = 1 )).isoformat ()
408+ body ["date_end" ] = (datetime .now () + timedelta (days = 2 )).isoformat ()
409+ response = client .post ("/elections" , json = body )
410+ data = response .json ()
411+ assert response .status_code == 200 , data
412+ election_ref = data ["ref" ]
413+
414+ # We create votes using the ID
415+ votes = _generate_votes_from_response ("id" , data )
416+ response = client .post (
417+ f"/ballots" ,
418+ json = {"votes" : votes , "election_ref" : election_ref },
419+ )
420+ data = response .json ()
421+ assert response .status_code == 403 , data
422+
423+ def test_cannot_update_vote_on_unstarted_election ():
424+ """
425+ On an unstarted election, we are not allowed to create new votes
426+ """
427+ # Create a random election
428+ body = _random_election (10 , 5 )
429+ body ["restricted" ] = True
430+ body ["num_voters" ] = 1
431+ body ["date_start" ] = (datetime .now () + timedelta (days = 1 )).isoformat ()
432+ body ["date_end" ] = (datetime .now () + timedelta (days = 2 )).isoformat ()
433+ response = client .post ("/elections" , json = body )
434+ data = response .json ()
435+ assert response .status_code == 200 , data
436+ election_ref = data ["ref" ]
437+ tokens = data ["invites" ]
438+ assert len (tokens ) == 1
439+
440+ # We create votes using the ID
441+ votes = _generate_votes_from_response ("id" , data )
442+ response = client .put (
443+ f"/ballots" ,
444+ json = {"votes" : votes , "election_ref" : election_ref },
445+ headers = {"Authorization" : f"Bearer { tokens [0 ]} " },
446+ )
447+ data = response .json ()
448+ assert response .status_code == 403 , data
397449
398450def test_cannot_create_vote_on_restricted_election ():
399451 """
0 commit comments