@@ -59,6 +59,7 @@ class RandomElection(t.TypedDict):
5959 hide_results : bool
6060 num_voters : int
6161 date_end : t .Optional [str ]
62+ auth_for_result : bool
6263
6364
6465def _random_election (num_candidates : int , num_grades : int ) -> RandomElection :
@@ -78,6 +79,7 @@ def _random_election(num_candidates: int, num_grades: int) -> RandomElection:
7879 "hide_results" : False ,
7980 "num_voters" : 0 ,
8081 "date_end" : None ,
82+ "auth_for_result" : False ,
8183 }
8284
8385
@@ -537,6 +539,47 @@ def test_get_results_with_hide_results():
537539 assert response .status_code == 200 , response .text
538540
539541
542+ def test_get_results_with_auth_for_result ():
543+ # Create a random election
544+ body = _random_election (10 , 5 )
545+ body ["auth_for_result" ] = True
546+ body ["date_end" ] = (datetime .now () + timedelta (days = 1 )).isoformat ()
547+ response = client .post ("/elections" , json = body )
548+ assert response .status_code == 200 , response .content
549+ data = response .json ()
550+ election_ref = data ["ref" ]
551+ admin_token = data ["admin" ]
552+
553+ # We create votes using the ID
554+ votes = _generate_votes_from_response ("id" , data )
555+ response = client .post (
556+ f"/ballots" , json = {"votes" : votes , "election_ref" : election_ref }
557+ )
558+ assert response .status_code == 200 , data
559+
560+ response = client .put (
561+ f"/elections" , json = data , headers = {"Authorization" : f"Bearer { admin_token } " }
562+ )
563+
564+ assert response .status_code == 200 , response .text
565+
566+ # But, we can't get the results
567+ response = client .get (f"/results/{ election_ref } " )
568+ assert response .status_code == 401 , data
569+
570+ # Now, we can access to the results
571+ response = client .get (f"/results/{ election_ref } " , headers = {"Authorization" : f"Bearer { admin_token } " })
572+ assert response .status_code == 200 , response .text
573+
574+ # Ensure other admin tokens can't access the results
575+ response = client .post ("/elections" , json = body )
576+ assert response .status_code == 200 , response .content
577+ data2 = response .json ()
578+ admin_token2 = data2 ["admin" ]
579+
580+ response = client .get (f"/results/{ election_ref } " , headers = {"Authorization" : f"Bearer { admin_token2 } " })
581+ assert response .status_code == 401 , data
582+
540583def test_update_election ():
541584 # Create a random election
542585 body = _random_election (10 , 5 )
@@ -545,21 +588,30 @@ def test_update_election():
545588 data = response .json ()
546589 new_name = f'{ data ["name" ]} _MODIFIED'
547590 data ["name" ] = new_name
548- ballot_token = data ["admin" ]
591+ admin_token = data ["admin" ]
549592
550593 # Check we can not update without the ballot_token
551594 response = client .put ("/elections" , json = data )
552595 assert response .status_code == 422 , response .content
553596
554597 # Check that the request fails with a wrong ballot_token
555598 response = client .put (
556- f"/elections" , json = data , headers = {"Authorization" : f"Bearer { ballot_token } WRONG" }
599+ f"/elections" , json = data , headers = {"Authorization" : f"Bearer { admin_token } WRONG" }
557600 )
558- assert response .status_code == 401 , response .text
601+ assert response .status_code == 401 , response .text
602+
603+ # Check that the request fails with a admnin token of other election
604+ response2 = client .post ("/elections" , json = body )
605+ data2 = response2 .json ()
606+ admin_token2 = data2 ["admin" ]
607+ response = client .put (
608+ f"/elections" , json = data , headers = {"Authorization" : f"Bearer { admin_token2 } " }
609+ )
610+ assert response .status_code == 403 , response .text
559611
560612 # But it works with the right ballot_token
561613 response = client .put (
562- f"/elections" , json = data , headers = {"Authorization" : f"Bearer { ballot_token } " }
614+ f"/elections" , json = data , headers = {"Authorization" : f"Bearer { admin_token } " }
563615 )
564616 assert response .status_code == 200 , response .text
565617 response2 = client .get (f"/elections/{ data ['ref' ]} " )
@@ -575,7 +627,7 @@ def test_update_election():
575627 data ["grades" ][0 ]["description" ] += "MODIFIED"
576628 data ["grades" ][0 ]["value" ] += 10
577629 response = client .put (
578- f"/elections" , json = data , headers = {"Authorization" : f"Bearer { ballot_token } " }
630+ f"/elections" , json = data , headers = {"Authorization" : f"Bearer { admin_token } " }
579631 )
580632 assert response .status_code == 200 , response .text
581633 data = response .json ()
@@ -586,14 +638,14 @@ def test_update_election():
586638 data2 = copy .deepcopy (data )
587639 del data2 ["candidates" ][- 1 ]
588640 response = client .put (
589- f"/elections" , json = data2 , headers = {"Authorization" : f"Bearer { ballot_token } " }
641+ f"/elections" , json = data2 , headers = {"Authorization" : f"Bearer { admin_token } " }
590642 )
591643 assert response .status_code == 403 , response .text
592644
593645 data2 = copy .deepcopy (data )
594646 data2 ["grades" ][0 ]["id" ] += 100
595647 response = client .put (
596- f"/elections" , json = data2 , headers = {"Authorization" : f"Bearer { ballot_token } " }
648+ f"/elections" , json = data2 , headers = {"Authorization" : f"Bearer { admin_token } " }
597649 )
598650 assert response .status_code == 403 , response .text
599651
0 commit comments