Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions helios/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,12 @@ def process(self):
except User.DoesNotExist:
user, _ = User.objects.get_or_create(user_type=voter['voter_type'], user_id=voter['voter_id'], defaults = {'name': voter['voter_id'], 'info': {}, 'token': None})
existing_voter = Voter.get_by_election_and_user(self.election, user)
# a voter of another type (e.g. a password voter, or a user of another
# auth system with the same username) may already hold this login ID;
# (election, voter_login_id) is unique, so registering would fail the
# whole upload partway through
if not existing_voter:
existing_voter = Voter.get_by_election_and_voter_id(self.election, user.user_id)
if not existing_voter:
Voter.register_user_in_election(user, self.election)
successful_voters += 1
Expand Down
15 changes: 15 additions & 0 deletions helios/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ def test_add_voters_file_counts_only_added_voters(self):
self.assertEqual(models.VoterFile.objects.get(id=vf.id).num_voters, 2)
self.assertEqual(vf.num_skipped, 2)

def test_add_voters_file_skips_login_id_taken_by_another_voter_type(self):
# a password voter already holds the login ID a GitHub row would get: the
# row is skipped instead of failing the upload on the unique constraint
models.VoterFile.objects.create(election=self.election,
voter_file_content="password,voter1,v1@example.com,Voter One\n").process()
vf = models.VoterFile.objects.create(election=self.election,
voter_file_content="github,voter1\ngithub,voter2\n")
added = vf.process()

self.assertEqual(added, 1)
self.assertEqual(vf.num_skipped, 1)
self.assertIsNotNone(models.VoterFile.objects.get(id=vf.id).processing_finished_at)
self.assertIsNone(self.election.voter_set.get(voter_login_id='voter1').user)
self.assertEqual(self.election.voter_set.get(voter_login_id='voter2').user.user_type, 'github')

def test_voter_file_process_task_skips_processed_file(self):
# a requeued task must not reprocess the file and reset its counts to zero
vf = models.VoterFile.objects.create(election=self.election, voter_file_content='password,v1,v1@example.com,Voter One\n')
Expand Down
Loading