From 5ffc7ec9083e34d1c841943b1533da4611890c13 Mon Sep 17 00:00:00 2001 From: Ben Adida <ben@adida.net> Date: Sun, 22 Aug 2010 20:48:15 -0700 Subject: [PATCH] more tests for helios --- helios/fixtures/users.json | 1 + helios/models.py | 15 +++- helios/tests.py | 146 ++++++++++++++++++++++++++++++++++--- helios/views.py | 6 +- reset.sh | 1 + 5 files changed, 153 insertions(+), 16 deletions(-) create mode 100644 helios/fixtures/users.json diff --git a/helios/fixtures/users.json b/helios/fixtures/users.json new file mode 100644 index 0000000..f764ccb --- /dev/null +++ b/helios/fixtures/users.json @@ -0,0 +1 @@ +[{"pk": 1, "model": "auth.user", "fields": {"info": "{u'password': u'test'}", "user_id": "foobar", "name": "Foo Bar", "user_type": "password", "token": null, "admin_p": false}}] \ No newline at end of file diff --git a/helios/models.py b/helios/models.py index e0fc2f6..4913f3c 100644 --- a/helios/models.py +++ b/helios/models.py @@ -298,6 +298,9 @@ class Election(models.Model, electionalgs.Election): """ election is frozen when the voter registration, questions, and trustees are finalized """ + if len(self.issues_before_freeze) > 0: + raise Exception("cannot freeze an election that has issues") + self.frozen_at = datetime.datetime.utcnow() # voters hash @@ -488,9 +491,19 @@ class Voter(models.Model, electionalgs.Voter): vote = JSONField(electionalgs.EncryptedVote, null=True) vote_hash = models.CharField(max_length = 100, null=True) cast_at = models.DateTimeField(auto_now_add=False, null=True) - + + @classmethod + def register_user_in_election(cls, user, election): + voter_uuid = str(uuid.uuid4()) + voter = Voter(uuid= voter_uuid, voter_type = user.user_type, voter_id = user.user_id, election = election, name = user.name) + voter.save() + return voter + @classmethod def get_by_election(cls, election, cast=None, order_by='voter_id', after=None, limit=None): + """ + FIXME: review this for non-GAE? + """ query = cls.objects.filter(election = election) # the boolean check is not stupid, this is ternary logic diff --git a/helios/tests.py b/helios/tests.py index fb929a3..b01341c 100644 --- a/helios/tests.py +++ b/helios/tests.py @@ -2,10 +2,11 @@ Unit Tests for Helios """ -import unittest -import models +import unittest, datetime +import models from auth import models as auth_models +from views import ELGAMAL_PARAMS from django.db import IntegrityError, transaction @@ -14,22 +15,147 @@ from django.test import TestCase from django.core import mail -class ElectionModelTests(unittest.TestCase): +class ElectionModelTests(TestCase): + fixtures = ['users.json'] - def setUp(self): - self.user = auth_models.User.objects.create(user_id='foobar', name='Foo Bar', user_type='password', info={}) - - def test_create_election(self): - self.election, created_p = models.Election.get_or_create( + def create_election(self): + return models.Election.get_or_create( short_name='demo', name='Demo Election', description='Demo Election Description', admin=self.user) + def setup_questions(self): + QUESTIONS = [{"answer_urls": [None, None, None], "answers": ["a", "b", "c"], "choice_type": "approval", "max": 1, "min": 0, "question": "w?", "result_type": "absolute", "short_name": "w?", "tally_type": "homomorphic"}] + self.election.questions = QUESTIONS + + def setup_trustee(self): + self.election.generate_trustee(ELGAMAL_PARAMS) + + def setUp(self): + self.user = auth_models.User.objects.get(user_id='foobar') + self.election, self.created_p = self.create_election() + + def test_create_election(self): # election should be created - self.assertTrue(created_p) + self.assertTrue(self.created_p) # should have a creation time self.assertNotEquals(self.election.created_at, None) - #self.assert + self.assertTrue(self.election.created_at < datetime.datetime.utcnow()) + + def test_find_election(self): + election = models.Election.get_by_user_as_admin(self.user)[0] + self.assertEquals(self.election, election) + + election = models.Election.get_by_uuid(self.election.uuid) + self.assertEquals(self.election, election) + + election = models.Election.get_by_short_name(self.election.short_name) + self.assertEquals(self.election, election) + + def test_add_voters_file(self): + pass + + def test_check_issues_before_freeze(self): + # should be two issues: no trustees, and no questions + issues = self.election.issues_before_freeze + self.assertEquals(len(issues), 2) + + self.setup_questions() + + # should be one issue: no trustees + issues = self.election.issues_before_freeze + self.assertEquals(len(issues), 1) + + self.election.questions = None + + self.setup_trustee() + + # should be one issue: no trustees + issues = self.election.issues_before_freeze + self.assertEquals(len(issues), 1) + + self.setup_questions() + + issues = self.election.issues_before_freeze + self.assertEquals(len(issues), 0) + + def test_helios_trustee(self): + self.election.generate_trustee(ELGAMAL_PARAMS) + + self.assertTrue(self.election.has_helios_trustee()) + + trustee = self.election.get_helios_trustee() + self.assertNotEquals(trustee, None) + + def test_log(self): + LOGS = ["testing 1", "testing 2", "testing 3"] + + for l in LOGS: + self.election.append_log(l) + + pulled_logs = [l.log for l in self.election.get_log().all()] + pulled_logs.reverse() + + self.assertEquals(LOGS,pulled_logs) + + def test_eligibility(self): + self.election.eligibility = [{'auth_system': 'password'}] + + # without openreg, this should be false + self.assertFalse(self.election.user_eligible_p(self.user)) + + self.election.openreg = True + + # without openreg, and now true + self.assertTrue(self.election.user_eligible_p(self.user)) + + def test_freeze(self): + # freezing without trustees and questions, no good + def try_freeze(): + self.election.freeze() + self.assertRaises(Exception, try_freeze) + + self.setup_questions() + self.setup_trustee() + + # this time it should work + try_freeze() + + # make sure it logged something + self.assertTrue(len(self.election.get_log().all()) > 0) + + def test_voter_registration(self): + # before adding a voter + voters = models.Voter.get_by_election(self.election) + self.assertTrue(len(voters) == 0) + + # make sure no voter yet + voter = models.Voter.get_by_election_and_user(self.election, self.user) + self.assertTrue(voter == None) + + # make sure no voter at all across all elections + voters = models.Voter.get_by_user(self.user) + self.assertTrue(len(voters) == 0) + + # register the voter + voter = models.Voter.register_user_in_election(self.user, self.election) + + # make sure voter is there now + voter_2 = models.Voter.get_by_election_and_user(self.election, self.user) + self.assertFalse(voter == None) + self.assertEquals(voter, voter_2) + + # make sure voter is there in this call too + voters = models.Voter.get_by_user(self.user) + self.assertTrue(len(voters) == 1) + self.assertEquals(voter, voters[0]) + + voter_2 = models.Voter.get_by_election_and_voter_id(self.election, voter.voter_id) + self.assertEquals(voter, voter_2) + + voter_2 = models.Voter.get_by_election_and_uuid(self.election, voter.uuid) + self.assertEquals(voter, voter_2) + self.assertEquals(voter.user, self.user) diff --git a/helios/views.py b/helios/views.py index db8d604..23920af 100644 --- a/helios/views.py +++ b/helios/views.py @@ -682,11 +682,7 @@ def _register_voter(election, user): if not _check_eligibility(election, user): return None - voter_uuid = str(uuid.uuid1()) - voter = Voter(uuid= voter_uuid, voter_type = user.user_type, voter_id = user.user_id, election = election, name = user.name) - - voter.save() - return voter + return Voter.register_user_in_election(user, election) @election_view() def one_election_register(request, election): diff --git a/reset.sh b/reset.sh index b5e97dd..eb22b5c 100755 --- a/reset.sh +++ b/reset.sh @@ -2,4 +2,5 @@ dropdb helios createdb helios python manage.py syncdb +python manage.py migrate echo "from auth.models import User; User.update_or_create(user_type='password',user_id='benadida',info={'password':'test'})" | python manage.py shell \ No newline at end of file -- GitLab