@@ -111,6 +111,46 @@ def test_create_election():
111111 data = response .json ()
112112 assert data ["name" ] == body ["name" ]
113113
114+ def test_start_end_date_are_valid ():
115+ # cannot create an election where the start date is after the end date
116+ body = _random_election (2 , 2 )
117+ body ["date_start" ] = (datetime .now () + timedelta (days = 1 )).isoformat ()
118+ body ["date_end" ] = (datetime .now ()).isoformat ()
119+ response = client .post ("/elections" , json = body )
120+ assert response .status_code == 422 , response .text
121+
122+ body ["date_start" ] = (datetime .now ()).isoformat ()
123+ body ["date_end" ] = (datetime .now () + timedelta (days = 1 )).isoformat ()
124+ response = client .post ("/elections" , json = body )
125+ assert response .status_code == 200 , response .text
126+ election_data = response .json ()
127+ del election_data ["candidates" ]
128+ del election_data ["grades" ]
129+ del election_data ["name" ]
130+ del election_data ["restricted" ]
131+ del election_data ["hide_results" ]
132+ del election_data ["auth_for_result" ]
133+ admin_token = election_data ["admin" ]
134+ election_ref = election_data ["ref" ]
135+
136+ # update election should not be allowed if the start date is after the end date
137+ election_data ["date_start" ] = (datetime .now () + timedelta (days = 1 )).isoformat ()
138+ election_data ["date_end" ] = (datetime .now ()).isoformat ()
139+ response = client .put ("/elections" , json = election_data , headers = {"Authorization" : f"Bearer { admin_token } " })
140+ assert response .status_code == 422 , response .text
141+
142+ # update election should be allowed if the start date is before the end date
143+ del election_data ["date_start" ]
144+ election_data ["date_end" ] = (datetime .now () - timedelta (days = 1 )).isoformat ()
145+ response = client .put ("/elections" , json = election_data , headers = {"Authorization" : f"Bearer { admin_token } " })
146+ assert response .status_code == 403 , response .text
147+
148+ # update election should be allowed if the start date is before the end date
149+ del election_data ["date_end" ]
150+ election_data ["date_start" ] = (datetime .now () + timedelta (days = 2 )).isoformat ()
151+ response = client .put ("/elections" , json = election_data , headers = {"Authorization" : f"Bearer { admin_token } " })
152+ assert response .status_code == 403 , response .text
153+
114154
115155def test_get_election ():
116156 body = _random_election (3 , 4 )
@@ -309,13 +349,14 @@ def test_cannot_create_vote_on_ended_election():
309349 """
310350 # Create a random election
311351 body = _random_election (10 , 5 )
352+ body ["date_start" ] = (datetime .now () - timedelta (days = 2 )).isoformat ()
312353 body ["date_end" ] = (datetime .now () - timedelta (days = 1 )).isoformat ()
313354 response = client .post ("/elections" , json = body )
314355 election_data = response .json ()
315356 assert response .status_code == 200 , election_data
316357 assert len (election_data ["invites" ]) == 0
317358 election_ref = election_data ["ref" ]
318- ballot_token = election_data ["admin" ]
359+ admin_token = election_data ["admin" ]
319360
320361 # We create votes using the ID
321362 votes = _generate_votes_from_response ("id" , election_data )
@@ -330,7 +371,7 @@ def test_cannot_create_vote_on_ended_election():
330371 response = client .put (
331372 f"/elections" ,
332373 json = {"force_close" : True , "date_end" :(datetime .now () + timedelta (days = 1 )).isoformat (), "ref" : election_ref },
333- headers = {"Authorization" : f"Bearer { ballot_token } " },
374+ headers = {"Authorization" : f"Bearer { admin_token } " },
334375 )
335376 assert response .status_code == 200 , response .json ()
336377
@@ -386,10 +427,14 @@ def test_cannot_update_vote_on_ended_election():
386427 # Test for date_end in the past
387428 response = client .put (
388429 f"/elections" ,
389- json = {"force_close" : False , "date_end" :(datetime .now () - timedelta (days = 1 )).isoformat (), "ref" : election_ref },
430+ json = {"force_close" : False , "date_start" :( datetime . now () - timedelta ( days = 2 )). isoformat (), " date_end" :(datetime .now () - timedelta (days = 1 )).isoformat (), "ref" : election_ref },
390431 headers = {"Authorization" : f"Bearer { election_token } " },
391432 )
392433
434+ assert response .status_code == 200 , response .json ()["date_end" ]
435+
436+ print (response .json ()["date_end" ])
437+
393438 response = client .put (
394439 f"/ballots" ,
395440 json = {"votes" : votes },
0 commit comments