forked from benadida/helios-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
802 lines (601 loc) · 32.1 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
"""
Unit Tests for Helios
"""
import unittest, datetime, re, urllib
import django_webtest
import models
import datatypes
from helios_auth import models as auth_models
from views import ELGAMAL_PARAMS
import views
import utils
from django.db import IntegrityError, transaction
from django.test.client import Client
from django.test import TestCase
from django.utils.html import escape as html_escape
from django.core import mail
from django.core.files import File
from django.core.urlresolvers import reverse
from django.conf import settings
from django.core.exceptions import PermissionDenied
import uuid
class ElectionModelTests(TestCase):
fixtures = ['users.json']
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_openreg(self):
self.election.openreg=True
self.election.save()
def setUp(self):
self.user = auth_models.User.objects.get(user_id='[email protected]', user_type='google')
self.fb_user = auth_models.User.objects.get(user_type='facebook')
self.election, self.created_p = self.create_election()
def test_create_election(self):
# election should be created
self.assertTrue(self.created_p)
# should have a creation time
self.assertNotEquals(self.election.created_at, None)
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_setup_trustee(self):
self.setup_trustee()
self.assertEquals(self.election.num_trustees, 1)
def test_add_voters_file(self):
election = self.election
FILE = "helios/fixtures/voter-file.csv"
vf = models.VoterFile.objects.create(election = election, voter_file = File(open(FILE), "voter_file.css"))
vf.process()
# make sure that we stripped things correctly
voter = election.voter_set.get(voter_login_id = 'benadida5')
self.assertEquals(voter.voter_email, '[email protected]')
self.assertEquals(voter.voter_name, 'Ben5 Adida')
def test_check_issues_before_freeze(self):
# should be three issues: no trustees, and no questions, and no voters
issues = self.election.issues_before_freeze
self.assertEquals(len(issues), 3)
self.setup_questions()
# should be two issues: no trustees, and no voters
issues = self.election.issues_before_freeze
self.assertEquals(len(issues), 2)
self.election.questions = None
self.setup_trustee()
# should be two issues: no questions, and no voters
issues = self.election.issues_before_freeze
self.assertEquals(len(issues), 2)
self.setup_questions()
# move to open reg
self.setup_openreg()
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': self.user.user_type}]
# without openreg, this should be false
self.assertFalse(self.election.user_eligible_p(self.user))
# what about after saving?
self.election.save()
e = models.Election.objects.get(uuid = self.election.uuid)
self.assertEquals(e.eligibility, [{'auth_system': self.user.user_type}])
self.election.openreg = True
# without openreg, and now true
self.assertTrue(self.election.user_eligible_p(self.user))
# try getting pretty eligibility, make sure it doesn't throw an exception
assert self.user.user_type in self.election.pretty_eligibility
def test_facebook_eligibility(self):
self.election.eligibility = [{'auth_system': 'facebook', 'constraint':[{'group': {'id': '123', 'name':'Fake Group'}}]}]
# without openreg, this should be false
self.assertFalse(self.election.user_eligible_p(self.fb_user))
self.election.openreg = True
# fake out the facebook constraint checking, since
# our access_token is obviously wrong
from helios_auth.auth_systems import facebook
def fake_check_constraint(constraint, user):
return constraint == {'group': {'id': '123', 'name':'Fake Group'}} and user == self.fb_user
facebook.check_constraint = fake_check_constraint
self.assertTrue(self.election.user_eligible_p(self.fb_user))
# also check that eligibility_category_id does the right thing
self.assertEquals(self.election.eligibility_category_id('facebook'), '123')
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()
self.setup_openreg()
# this time it should work
try_freeze()
# make sure it logged something
self.assertTrue(len(self.election.get_log().all()) > 0)
def test_archive(self):
self.election.archived_at = datetime.datetime.utcnow()
self.assertTrue(self.election.is_archived)
self.election.archived_at = None
self.assertFalse(self.election.is_archived)
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.assertFalse(voter_2 == 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_uuid(self.election, voter.uuid)
self.assertEquals(voter, voter_2)
self.assertEquals(voter.user, self.user)
class VoterModelTests(TestCase):
fixtures = ['users.json', 'election.json']
def setUp(self):
self.election = models.Election.objects.get(short_name='test')
def test_create_password_voter(self):
v = models.Voter(uuid = str(uuid.uuid1()), election = self.election, voter_login_id = 'voter_test_1', voter_name = 'Voter Test 1', voter_email='[email protected]')
v.generate_password()
v.save()
# password has been generated!
self.assertFalse(v.voter_password == None)
# can't generate passwords twice
self.assertRaises(Exception, lambda: v.generate_password())
# check that you can get at the voter user structure
self.assertEquals(v.user.user_id, v.voter_email)
class CastVoteModelTests(TestCase):
fixtures = ['users.json', 'election.json']
def setUp(self):
self.election = models.Election.objects.get(short_name='test')
self.user = auth_models.User.objects.get(user_id='[email protected]', user_type='google')
# register the voter
self.voter = models.Voter.register_user_in_election(self.user, self.election)
def test_cast_vote(self):
pass
class DatatypeTests(TestCase):
fixtures = ['users.json', 'election.json']
def setUp(self):
self.election = models.Election.objects.all()[0]
self.election.generate_trustee(ELGAMAL_PARAMS)
def test_instantiate(self):
ld_obj = datatypes.LDObject.instantiate(self.election.get_helios_trustee(), '2011/01/Trustee')
foo = ld_obj.serialize()
def test_from_dict(self):
ld_obj = datatypes.LDObject.fromDict({
'y' : '1234',
'p' : '23434',
'g' : '2343243242',
'q' : '2343242343434'}, type_hint = 'pkc/elgamal/PublicKey')
def test_dictobject_from_dict(self):
original_dict = {
'A' : '35423432',
'B' : '234324243'}
ld_obj = datatypes.LDObject.fromDict(original_dict, type_hint = 'legacy/EGZKProofCommitment')
self.assertEquals(original_dict, ld_obj.toDict())
##
## Black box tests
##
class DataFormatBlackboxTests(object):
def setUp(self):
self.election = models.Election.objects.all()[0]
def assertEqualsToFile(self, response, file_path):
expected = open(file_path)
self.assertEquals(response.content, expected.read())
expected.close()
def test_election(self):
response = self.client.get("/helios/elections/%s" % self.election.uuid, follow=False)
self.assertEqualsToFile(response, self.EXPECTED_ELECTION_FILE)
def test_election_metadata(self):
response = self.client.get("/helios/elections/%s/meta" % self.election.uuid, follow=False)
self.assertEqualsToFile(response, self.EXPECTED_ELECTION_METADATA_FILE)
def test_voters_list(self):
response = self.client.get("/helios/elections/%s/voters/" % self.election.uuid, follow=False)
self.assertEqualsToFile(response, self.EXPECTED_VOTERS_FILE)
def test_trustees_list(self):
response = self.client.get("/helios/elections/%s/trustees/" % self.election.uuid, follow=False)
self.assertEqualsToFile(response, self.EXPECTED_TRUSTEES_FILE)
def test_ballots_list(self):
response = self.client.get("/helios/elections/%s/ballots/" % self.election.uuid, follow=False)
self.assertEqualsToFile(response, self.EXPECTED_BALLOTS_FILE)
## now we have a set of fixtures and expected results for various formats
## note how TestCase is used as a "mixin" here, so that the generic DataFormatBlackboxTests
## does not register as a set of test cases to run, but each concrete data format does.
class LegacyElectionBlackboxTests(DataFormatBlackboxTests, TestCase):
fixtures = ['legacy-data.json']
EXPECTED_ELECTION_FILE = 'helios/fixtures/legacy-election-expected.json'
EXPECTED_ELECTION_METADATA_FILE = 'helios/fixtures/legacy-election-metadata-expected.json'
EXPECTED_VOTERS_FILE = 'helios/fixtures/legacy-election-voters-expected.json'
EXPECTED_TRUSTEES_FILE = 'helios/fixtures/legacy-trustees-expected.json'
EXPECTED_BALLOTS_FILE = 'helios/fixtures/legacy-ballots-expected.json'
#class V3_1_ElectionBlackboxTests(DataFormatBlackboxTests, TestCase):
# fixtures = ['v3.1-data.json']
# EXPECTED_ELECTION_FILE = 'helios/fixtures/v3.1-election-expected.json'
# EXPECTED_VOTERS_FILE = 'helios/fixtures/v3.1-election-voters-expected.json'
# EXPECTED_TRUSTEES_FILE = 'helios/fixtures/v3.1-trustees-expected.json'
# EXPECTED_BALLOTS_FILE = 'helios/fixtures/v3.1-ballots-expected.json'
class WebTest(django_webtest.WebTest):
def assertRedirects(self, response, url):
"""
reimplement this in case it's a WebOp response
and it seems to be screwing up in a few places too
thus the localhost exception
"""
if hasattr(response, 'location'):
assert url in response.location
else:
assert url in response._headers['location'][1]
if hasattr(response, 'status_code'):
assert response.status_code == 302
else:
assert response.status_int == 302
#self.assertEqual(response.status_code, 302)
#return super(django_webtest.WebTest, self).assertRedirects(response, url)
#if hasattr(response, 'status_code') and hasattr(response, 'location'):
#assert url in response.location, "redirected to %s instead of %s" % (response.location, url)
def assertContains(self, response, text):
if hasattr(response, 'status_code'):
assert response.status_code == 200
# return super(django_webtest.WebTest, self).assertContains(response, text)
else:
assert response.status_int == 200
if hasattr(response, "testbody"):
assert text in response.testbody, "missing text %s" % text
else:
if hasattr(response, "body"):
assert text in response.body, "missing text %s" % text
else:
assert text in response.content, "missing text %s" % text
##
## overall operation of the system
##
class ElectionBlackboxTests(WebTest):
fixtures = ['users.json', 'election.json']
def setUp(self):
self.election = models.Election.objects.all()[0]
self.user = auth_models.User.objects.get(user_id='[email protected]', user_type='google')
def assertContains(self, response, text):
if hasattr(response, 'status_code'):
assert response.status_code == 200
# return super(django_webtest.WebTest, self).assertContains(response, text)
else:
assert response.status_int == 200
if hasattr(response, "testbody"):
assert text in response.testbody, "missing text %s" % text
else:
if hasattr(response, "body"):
assert text in response.body, "missing text %s" % text
else:
assert text in response.content, "missing text %s" % text
def setup_login(self):
# set up the session
session = self.client.session
session['user'] = {'type': self.user.user_type, 'user_id': self.user.user_id}
session.save()
# set up the app, too
# this does not appear to work, boohoo
session = self.app.session
session['user'] = {'type': self.user.user_type, 'user_id': self.user.user_id}
def clear_login(self):
session = self.client.session
del session['user']
session.save()
def test_election_params(self):
response = self.client.get("/helios/elections/params")
self.assertEquals(response.content, views.ELGAMAL_PARAMS_LD_OBJECT.serialize())
def test_election_404(self):
response = self.client.get("/helios/elections/foobar")
self.assertEquals(response.status_code, 404)
def test_election_bad_trustee(self):
response = self.client.get("/helios/t/%s/[email protected]/badsecret" % self.election.short_name)
self.assertEquals(response.status_code, 404)
def test_get_election_shortcut(self):
response = self.client.get("/helios/e/%s" % self.election.short_name, follow=True)
self.assertContains(response, self.election.description)
def test_get_election_raw(self):
response = self.client.get("/helios/elections/%s" % self.election.uuid, follow=False)
self.assertEquals(response.content, self.election.toJSON())
def test_get_election(self):
response = self.client.get("/helios/elections/%s/view" % self.election.uuid, follow=False)
self.assertContains(response, self.election.description)
def test_get_election_questions(self):
response = self.client.get("/helios/elections/%s/questions" % self.election.uuid, follow=False)
for q in self.election.questions:
self.assertContains(response, q['question'])
def test_get_election_trustees(self):
response = self.client.get("/helios/elections/%s/trustees" % self.election.uuid, follow=False)
for t in self.election.trustee_set.all():
self.assertContains(response, t.name)
def test_get_election_voters(self):
response = self.client.get("/helios/elections/%s/voters/list" % self.election.uuid, follow=False)
# check total count of voters
if self.election.num_voters == 0:
self.assertContains(response, "no voters")
else:
self.assertContains(response, "(of %s)" % self.election.num_voters)
def test_get_election_voters_raw(self):
response = self.client.get("/helios/elections/%s/voters/" % self.election.uuid, follow=False)
self.assertEquals(len(utils.from_json(response.content)), self.election.num_voters)
def test_election_creation_not_logged_in(self):
response = self.client.post("/helios/elections/new", {
"short_name" : "test-complete",
"name" : "Test Complete",
"description" : "A complete election test",
"election_type" : "referendum",
"use_voter_aliases": "0",
"use_advanced_audit_features": "1",
"private_p" : "0"})
self.assertRedirects(response, "/auth/?return_url=/helios/elections/new")
def test_election_edit(self):
# a bogus call to set up the session
self.client.get("/")
self.setup_login()
response = self.client.get("/helios/elections/%s/edit" % self.election.uuid)
response = self.client.post("/helios/elections/%s/edit" % self.election.uuid, {
"short_name" : self.election.short_name + "-2",
"name" : self.election.name,
"description" : self.election.description,
"election_type" : self.election.election_type,
"use_voter_aliases": self.election.use_voter_aliases,
'csrf_token': self.client.session['csrf_token']
})
self.assertRedirects(response, "/helios/elections/%s/view" % self.election.uuid)
new_election = models.Election.objects.get(uuid = self.election.uuid)
self.assertEquals(new_election.short_name, self.election.short_name + "-2")
def _setup_complete_election(self, election_params={}):
"do the setup part of a whole election"
# a bogus call to set up the session
self.client.get("/")
# REPLACE with params?
self.setup_login()
# create the election
full_election_params = {
"short_name" : "test-complete",
"name" : "Test Complete",
"description" : "A complete election test",
"election_type" : "referendum",
"use_voter_aliases": "0",
"use_advanced_audit_features": "1",
"private_p" : "0"}
# override with the given
full_election_params.update(election_params)
response = self.client.post("/helios/elections/new", full_election_params)
# we are redirected to the election, let's extract the ID out of the URL
election_id = re.search('/elections/([^/]+)/', str(response['Location'])).group(1)
# helios is automatically added as a trustee
# check that helios is indeed a trustee
response = self.client.get("/helios/elections/%s/trustees/view" % election_id)
self.assertContains(response, "Trustee #1")
# add a few voters, via file upload
# this file now includes a UTF-8 encoded unicode character
# yes I know that's not how you spell Ernesto.
# I just needed some unicode quickly.
FILE = "helios/fixtures/voter-file.csv"
voters_file = open(FILE)
response = self.client.post("/helios/elections/%s/voters/upload" % election_id, {'voters_file': voters_file})
voters_file.close()
self.assertContains(response, "first few rows of this file")
# now we confirm the upload
response = self.client.post("/helios/elections/%s/voters/upload" % election_id, {'confirm_p': "1"})
self.assertRedirects(response, "/helios/elections/%s/voters/list" % election_id)
# and we want to check that there are now voters
response = self.client.get("/helios/elections/%s/voters/" % election_id)
NUM_VOTERS = 4
self.assertEquals(len(utils.from_json(response.content)), NUM_VOTERS)
# let's get a single voter
single_voter = models.Election.objects.get(uuid = election_id).voter_set.all()[0]
response = self.client.get("/helios/elections/%s/voters/%s" % (election_id, single_voter.uuid))
self.assertContains(response, '"uuid": "%s"' % single_voter.uuid)
response = self.client.get("/helios/elections/%s/voters/foobar" % election_id)
self.assertEquals(response.status_code, 404)
# add questions
response = self.client.post("/helios/elections/%s/save_questions" % election_id, {
'questions_json': utils.to_json([{"answer_urls": [None,None], "answers": ["Alice", "Bob"], "choice_type": "approval", "max": 1, "min": 0, "question": "Who should be president?", "result_type": "absolute", "short_name": "Who should be president?", "tally_type": "homomorphic"}]),
'csrf_token': self.client.session['csrf_token']})
self.assertContains(response, "SUCCESS")
# freeze election
response = self.client.post("/helios/elections/%s/freeze" % election_id, {
"csrf_token" : self.client.session['csrf_token']})
self.assertRedirects(response, "/helios/elections/%s/view" % election_id)
# email the voters
num_messages_before = len(mail.outbox)
response = self.client.post("/helios/elections/%s/voters/email" % election_id, {
"csrf_token" : self.client.session['csrf_token'],
"subject" : "your password",
"body" : "time to vote",
"suppress_election_links" : "0",
"send_to" : "all"
})
self.assertRedirects(response, "/helios/elections/%s/view" % election_id)
num_messages_after = len(mail.outbox)
self.assertEquals(num_messages_after - num_messages_before, NUM_VOTERS)
email_message = mail.outbox[num_messages_before]
assert "your password" in email_message.subject, "bad subject in email"
# get the username and password
username = re.search('voter ID: (.*)', email_message.body).group(1)
password = re.search('password: (.*)', email_message.body).group(1)
# now log out as administrator
self.clear_login()
self.assertEquals(self.client.session.has_key('user'), False)
# return the voter username and password to vote
return election_id, username, password
def _cast_ballot(self, election_id, username, password, need_login=True, check_user_logged_in=False):
"""
check_user_logged_in looks for the "you're already logged" message
"""
# vote by preparing a ballot via the server-side encryption
response = self.app.post("/helios/elections/%s/encrypt-ballot" % election_id, {
'answers_json': utils.to_json([[1]])})
self.assertContains(response, "answers")
# parse it as an encrypted vote with randomness, and make sure randomness is there
the_ballot = utils.from_json(response.testbody)
assert the_ballot['answers'][0].has_key('randomness'), "no randomness"
assert len(the_ballot['answers'][0]['randomness']) == 2, "not enough randomness"
# parse it as an encrypted vote, and re-serialize it
ballot = datatypes.LDObject.fromDict(utils.from_json(response.testbody), type_hint='legacy/EncryptedVote')
encrypted_vote = ballot.serialize()
# cast the ballot
response = self.app.post("/helios/elections/%s/cast" % election_id, {
'encrypted_vote': encrypted_vote})
self.assertRedirects(response, "%s/helios/elections/%s/cast_confirm" % (settings.SECURE_URL_HOST, election_id))
cast_confirm_page = response.follow()
if need_login:
if check_user_logged_in:
self.assertContains(cast_confirm_page, "You are logged in as")
self.assertContains(cast_confirm_page, "requires election-specific credentials")
# set the form
login_form = cast_confirm_page.form
login_form['voter_id'] = username
login_form['password'] = password
# we skip that intermediary page now
# cast_confirm_page = login_form.submit()
response = login_form.submit()
# self.assertRedirects(cast_confirm_page, "/helios/elections/%s/cast_confirm" % election_id)
# cast_confirm_page = cast_confirm_page.follow()
else:
# here we should be at the cast-confirm page and logged in
self.assertContains(cast_confirm_page, "I am ")
# confirm the vote, now with the actual form
cast_form = cast_confirm_page.form
if 'status_update' in cast_form.fields.keys():
cast_form['status_update'] = False
response = cast_form.submit()
self.assertRedirects(response, "%s/helios/elections/%s/cast_done" % (settings.URL_HOST, election_id))
# at this point an email should have gone out to the user
# at position num_messages after, since that was the len() before we cast this ballot
email_message = mail.outbox[len(mail.outbox) - 1]
url = re.search('http://[^/]+(/[^ \n]*)', email_message.body).group(1)
# check that we can get at that URL
if not need_login:
# confusing piece: if need_login is True, that means it was a public election
# that required login before casting a ballot.
# so if need_login is False, it was a private election, and we do need to re-login here
# we need to re-login if it's a private election, because all data, including ballots
# is otherwise private
login_page = self.app.get("/helios/elections/%s/password_voter_login" % election_id)
# if we redirected, that's because we can see the page, I think
if login_page.status_int != 302:
login_form = login_page.form
# try with extra spaces
login_form['voter_id'] = ' ' + username + ' '
login_form['password'] = ' ' + password + ' '
login_form.submit()
response = self.app.get(url)
self.assertContains(response, ballot.hash)
self.assertContains(response, html_escape(encrypted_vote))
# if we request the redirect to cast_done, the voter should be logged out, but not the user
response = self.app.get("/helios/elections/%s/cast_done" % election_id)
# FIXME: how to check this? We can't do it by checking session that we're doign webtes
# assert not self.client.session.has_key('CURRENT_VOTER')
def _do_tally(self, election_id):
# log back in as administrator
self.setup_login()
# encrypted tally
response = self.client.post("/helios/elections/%s/compute_tally" % election_id, {
"csrf_token" : self.client.session['csrf_token']
})
self.assertRedirects(response, "/helios/elections/%s/view" % election_id)
# should trigger helios decryption automatically
self.assertNotEquals(models.Election.objects.get(uuid=election_id).get_helios_trustee().decryption_proofs, None)
# combine decryptions
response = self.client.post("/helios/elections/%s/combine_decryptions" % election_id, {
"csrf_token" : self.client.session['csrf_token'],
})
# after tallying, we now are supposed to see the email screen
# with the right template value of 'result'
self.assertRedirects(response, "/helios/elections/%s/voters/email?template=result" % election_id)
# check that tally matches
response = self.client.get("/helios/elections/%s/result" % election_id)
self.assertEquals(utils.from_json(response.content), [[0,1]])
def test_do_complete_election(self):
election_id, username, password = self._setup_complete_election()
# cast a ballot while not logged in
self._cast_ballot(election_id, username, password, check_user_logged_in=False)
# cast a ballot while logged in as a user (not a voter)
self.setup_login()
## for now the above does not work, it's a testing problem
## where the cookie isn't properly set. We'll have to figure this out.
## FIXME FIXME FIXME
# self._cast_ballot(election_id, username, password, check_user_logged_in=True)
self._cast_ballot(election_id, username, password, check_user_logged_in=False)
self.clear_login()
self._do_tally(election_id)
def test_do_complete_election_private(self):
# private election
election_id, username, password = self._setup_complete_election({'private_p' : "1"})
# get the password_voter_login_form via the front page
# (which will test that redirects are doing the right thing)
response = self.app.get("/helios/elections/%s/view" % election_id)
# ensure it redirects
self.assertRedirects(response, "/helios/elections/%s/password_voter_login?%s" % (election_id, urllib.urlencode({"return_url": "/helios/elections/%s/view" % election_id})))
login_form = response.follow().form
login_form['voter_id'] = username
login_form['password'] = password
response = login_form.submit()
self.assertRedirects(response, "/helios/elections/%s/view" % election_id)
self._cast_ballot(election_id, username, password, need_login = False)
self._do_tally(election_id)
def test_election_voters_eligibility(self):
# create the election
self.client.get("/")
self.setup_login()
response = self.client.post("/helios/elections/new", {
"short_name" : "test-eligibility",
"name" : "Test Eligibility",
"description" : "An election test for voter eligibility",
"election_type" : "election",
"use_voter_aliases": "0",
"use_advanced_audit_features": "1",
"private_p" : "0"})
election_id = re.match("(.*)/elections/(.*)/view", response['Location']).group(2)
# update eligiblity
response = self.client.post("/helios/elections/%s/voters/eligibility" % election_id, {
"csrf_token" : self.client.session['csrf_token'],
"eligibility": "openreg"})
self.clear_login()
response = self.client.get("/helios/elections/%s/voters/list" % election_id)
self.assertContains(response, "Anyone can vote")
self.setup_login()
response = self.client.post("/helios/elections/%s/voters/eligibility" % election_id, {
"csrf_token" : self.client.session['csrf_token'],
"eligibility": "closedreg"})
self.clear_login()
response = self.client.get("/helios/elections/%s/voters/list" % election_id)
self.assertContains(response, "Only the voters listed here")
def test_do_complete_election_with_trustees(self):
"""
FIXME: do the this test
"""
pass