2017-09-22 15:59:15 +10:00
|
|
|
# Eos - Verifiable elections
|
|
|
|
# Copyright © 2017 RunasSudo (Yingtong Li)
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-09-25 15:19:48 +10:00
|
|
|
from eos.core.tests import *
|
2017-09-22 15:59:15 +10:00
|
|
|
|
|
|
|
from eos.base.election import *
|
|
|
|
from eos.base.workflow import *
|
|
|
|
from eos.core.objects import *
|
|
|
|
|
2017-09-25 11:56:10 +10:00
|
|
|
class ElectionTestCase(EosTestCase):
|
2017-09-22 15:59:15 +10:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-09-28 16:46:30 +10:00
|
|
|
client.drop_database('test')
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2017-09-28 16:46:30 +10:00
|
|
|
def do_task_assert(self, election, task, next_task):
|
2017-09-24 13:25:16 +10:00
|
|
|
self.assertEqual(election.workflow.get_task(task).status, WorkflowTask.Status.READY)
|
2017-09-28 16:46:30 +10:00
|
|
|
if next_task is not None:
|
|
|
|
self.assertEqual(election.workflow.get_task(next_task).status, WorkflowTask.Status.NOT_READY)
|
|
|
|
election.workflow.get_task(task).enter()
|
2017-09-24 13:25:16 +10:00
|
|
|
self.assertEqual(election.workflow.get_task(task).status, WorkflowTask.Status.EXITED)
|
2017-09-28 16:46:30 +10:00
|
|
|
if next_task is not None:
|
|
|
|
self.assertEqual(election.workflow.get_task(next_task).status, WorkflowTask.Status.READY)
|
2017-09-25 15:19:48 +10:00
|
|
|
|
|
|
|
@py_only
|
2017-09-22 15:59:15 +10:00
|
|
|
def test_run_election(self):
|
|
|
|
# Set up election
|
|
|
|
election = Election()
|
2017-09-24 13:25:16 +10:00
|
|
|
election.workflow = WorkflowBase()
|
|
|
|
|
|
|
|
# Check _instance
|
|
|
|
self.assertEqual(election.workflow._instance, (election, 'workflow'))
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2017-09-24 15:19:07 +10:00
|
|
|
# Check workflow behaviour
|
2017-09-22 15:59:15 +10:00
|
|
|
self.assertEqual(election.workflow.get_task('eos.base.workflow.TaskConfigureElection').status, WorkflowTask.Status.READY)
|
2017-09-24 15:19:07 +10:00
|
|
|
self.assertEqual(election.workflow.get_task('does.not.exist'), None)
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2017-09-24 15:19:07 +10:00
|
|
|
# Set election details
|
2017-09-22 15:59:15 +10:00
|
|
|
election.name = 'Test Election'
|
|
|
|
|
|
|
|
for i in range(3):
|
2017-11-25 03:18:45 +11:00
|
|
|
voter = Voter(name=['Alice', 'Bob', 'Charlie'][i])
|
2017-09-24 13:25:16 +10:00
|
|
|
election.voters.append(voter)
|
|
|
|
# Check _instance
|
2017-09-24 13:32:46 +10:00
|
|
|
self.assertEqual(voter._instance, (election.voters, i))
|
2017-09-22 15:59:15 +10:00
|
|
|
|
2017-09-24 01:23:43 +10:00
|
|
|
question = ApprovalQuestion(prompt='President', choices=['John Smith', 'Joe Bloggs', 'John Q. Public'])
|
2017-09-22 15:59:15 +10:00
|
|
|
election.questions.append(question)
|
|
|
|
|
2017-09-24 01:23:43 +10:00
|
|
|
question = ApprovalQuestion(prompt='Chairman', choices=['John Doe', 'Andrew Citizen'])
|
2017-09-22 15:59:15 +10:00
|
|
|
election.questions.append(question)
|
|
|
|
|
2017-09-28 16:46:30 +10:00
|
|
|
election.save()
|
2017-09-22 15:59:15 +10:00
|
|
|
|
|
|
|
# Check that it saved
|
2017-09-28 16:46:30 +10:00
|
|
|
self.assertEqual(db[Election._db_name].find_one()['value'], election.serialise())
|
|
|
|
self.assertEqual(EosObject.deserialise_and_unwrap(db[Election._db_name].find_one()).serialise(), election.serialise())
|
2017-09-25 15:19:48 +10:00
|
|
|
|
|
|
|
self.assertEqualJSON(EosObject.deserialise_and_unwrap(EosObject.serialise_and_wrap(election)).serialise(), election.serialise())
|
2017-09-22 15:59:15 +10:00
|
|
|
|
|
|
|
# Freeze election
|
2017-09-28 16:46:30 +10:00
|
|
|
self.do_task_assert(election, 'eos.base.workflow.TaskConfigureElection', 'eos.base.workflow.TaskOpenVoting')
|
|
|
|
election.save()
|
2017-09-24 01:23:43 +10:00
|
|
|
|
2017-09-24 15:19:07 +10:00
|
|
|
# Try to freeze it again
|
|
|
|
try:
|
|
|
|
election.workflow.get_task('eos.base.workflow.TaskConfigureElection').exit()
|
|
|
|
self.fail()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
2017-11-24 20:26:18 +11:00
|
|
|
election_hash = SHA256().update_obj(election).hash_as_b64()
|
|
|
|
|
2017-09-28 16:46:30 +10:00
|
|
|
# Open voting
|
|
|
|
self.do_task_assert(election, 'eos.base.workflow.TaskOpenVoting', 'eos.base.workflow.TaskCloseVoting')
|
|
|
|
election.save()
|
|
|
|
|
2017-09-24 01:23:43 +10:00
|
|
|
# Cast ballots
|
|
|
|
VOTES = [[[0], [0]], [[0, 1], [1]], [[2], [0]]]
|
|
|
|
|
|
|
|
for i in range(3):
|
2017-11-24 20:26:18 +11:00
|
|
|
ballot = Ballot(election_id=election._id, election_hash=election_hash)
|
2017-09-24 01:23:43 +10:00
|
|
|
for j in range(2):
|
|
|
|
answer = ApprovalAnswer(choices=VOTES[i][j])
|
|
|
|
encrypted_answer = NullEncryptedAnswer(answer=answer)
|
|
|
|
ballot.encrypted_answers.append(encrypted_answer)
|
2017-11-24 20:26:18 +11:00
|
|
|
vote = Vote(ballot=ballot, cast_at=DateTimeField.now())
|
2017-11-23 23:10:57 +11:00
|
|
|
election.voters[i].votes.append(vote)
|
2017-09-24 01:23:43 +10:00
|
|
|
|
2017-09-28 16:46:30 +10:00
|
|
|
election.save()
|
2017-09-24 13:25:16 +10:00
|
|
|
|
|
|
|
# Close voting
|
2017-09-28 16:46:30 +10:00
|
|
|
self.do_task_assert(election, 'eos.base.workflow.TaskCloseVoting', 'eos.base.workflow.TaskDecryptVotes')
|
|
|
|
election.save()
|
|
|
|
|
|
|
|
# "Decrypt" votes
|
|
|
|
self.do_task_assert(election, 'eos.base.workflow.TaskDecryptVotes', 'eos.base.workflow.TaskReleaseResults')
|
|
|
|
election.save()
|
|
|
|
|
|
|
|
# Check result
|
|
|
|
RESULTS = [[[0], [0, 1], [2]], [[0], [1], [0]]]
|
|
|
|
for i in range(len(RESULTS)):
|
|
|
|
votes1 = RESULTS[i]
|
|
|
|
votes2 = [x.choices for x in election.results[i].answers]
|
|
|
|
self.assertEqual(sorted(votes1), sorted(votes2))
|
|
|
|
|
|
|
|
# Release result
|
|
|
|
self.do_task_assert(election, 'eos.base.workflow.TaskReleaseResults', None)
|
|
|
|
election.save()
|