Eos/eos/base/tests.py

121 lines
4.3 KiB
Python
Raw Permalink Normal View History

# Eos - Verifiable elections
2018-01-11 22:06:21 +11:00
# Copyright © 2017-18 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/>.
from eos.core.tests import *
from eos.base.election import *
from eos.base.workflow import *
from eos.core.objects import *
class ElectionTestCase(EosTestCase):
@classmethod
def setUpClass(cls):
2017-12-07 15:32:39 +11:00
cls.db_connect_and_reset()
def do_task_assert(self, election, task, next_task):
2018-01-11 22:06:21 +11:00
self.assertEqual(election.workflow.get_task(task).status, WorkflowTaskStatus.READY)
if next_task is not None:
2018-01-11 22:06:21 +11:00
self.assertEqual(election.workflow.get_task(next_task).status, WorkflowTaskStatus.NOT_READY)
election.workflow.get_task(task).enter()
2018-01-11 22:06:21 +11:00
self.assertEqual(election.workflow.get_task(task).status, WorkflowTaskStatus.EXITED)
if next_task is not None:
2018-01-11 22:06:21 +11:00
self.assertEqual(election.workflow.get_task(next_task).status, WorkflowTaskStatus.READY)
@py_only
def test_run_election(self):
# Set up election
election = Election()
election.workflow = WorkflowBase()
# Check _instance
self.assertEqual(election.workflow._instance, (election, 'workflow'))
2017-09-24 15:19:07 +10:00
# Check workflow behaviour
2018-01-11 22:06:21 +11:00
self.assertEqual(election.workflow.get_task('eos.base.workflow.TaskConfigureElection').status, WorkflowTaskStatus.READY)
2017-09-24 15:19:07 +10:00
self.assertEqual(election.workflow.get_task('does.not.exist'), None)
2017-09-24 15:19:07 +10:00
# Set election details
election.name = 'Test Election'
for i in range(3):
2017-11-25 03:18:45 +11:00
voter = Voter(name=['Alice', 'Bob', 'Charlie'][i])
election.voters.append(voter)
# Check _instance
2017-09-24 13:32:46 +10:00
self.assertEqual(voter._instance, (election.voters, i))
2017-12-11 11:25:01 +11:00
question = ApprovalQuestion(prompt='President', choices=[Choice(name='John Smith'), Choice(name='Joe Bloggs'), Choice(name='John Q. Public')])
election.questions.append(question)
2017-12-11 11:25:01 +11:00
question = ApprovalQuestion(prompt='Chairman', choices=[Choice(name='John Doe'), Choice(name='Andrew Citizen')])
election.questions.append(question)
election.save()
# Check that it saved
2017-11-27 19:40:01 +11:00
self.assertEqual(Election.get_all()[0], election)
self.assertEqualJSON(EosObject.deserialise_and_unwrap(EosObject.serialise_and_wrap(election)).serialise(), election.serialise())
# Freeze election
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
election_hash = SHA256().update_obj(election).hash_as_b64()
# 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):
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)
vote = Vote(voter_id=election.voters[i]._id, ballot=ballot, cast_at=DateTimeField.now())
vote.save()
2017-09-24 01:23:43 +10:00
#election.save()
# Close voting
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()