Skip to content
Merged
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
12 changes: 8 additions & 4 deletions helios/templates/new_trustee.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

{% block content %}
<h2 class="title">{{election.name}} &mdash; New Trustee <span style="font-size:0.7em;">[<a href="{% url "election@trustees@view" election.uuid %}">cancel</a>]</span></h2>


{% if error %}
<p class="error" style="color: red;">{{error}}</p>
{% endif %}

<form method="post" action="">
<input type="hidden" name="csrf_token" value="{{csrf_token}}" />
Name: <input type="text" name="name" size="60" /><br /><br />
Email: <input type="text" name="email" size="60" /><br /><br />
Name: <input type="text" name="name" size="60" value="{{name|default:''}}" /><br /><br />
Email: <input type="text" name="email" size="60" value="{{email|default:''}}" /><br /><br />

<input class="button" type="submit" value="Add Trustee" />
</form>

{% endblock %}
{% endblock %}
44 changes: 44 additions & 0 deletions helios/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4629,3 +4629,47 @@ def test_dry_run_changes_nothing(self):
stuck.refresh_from_db()
self.assertEqual(stuck.status, TaskResultStatus.RUNNING)
self.assertIn('would requeue 1 tasks', out.getvalue())


class NewTrusteeViewTests(WebTest):
"""Adding a trustee from the admin's trustee page"""
fixtures = ['users.json']
allow_database_queries = True

def setUp(self):
self.user = auth_models.User.objects.filter(user_type='google').first()
self.election, _ = models.Election.get_or_create(
short_name='test-new-trustee',
name='Test New Trustee Election',
description='Test Election for adding trustees',
admin=self.user
)
if not self.election.uuid:
self.election.uuid = str(uuid.uuid4())
self.election.save()
self.client.get("/")
session = self.client.session
session['user'] = {'type': self.user.user_type, 'user_id': self.user.user_id}
session.save()
self.url = f'/helios/elections/{self.election.uuid}/trustees/new'

def _post(self, name, email):
return self.client.post(self.url, {
'csrf_token': self.client.session['csrf_token'],
'name': name,
'email': email,
})

def test_new_trustee_is_added(self):
response = self._post('Trustee One', 'trustee1@example.com')
self.assertStatusCode(response, 302)
self.assertEqual(models.Trustee.objects.filter(election=self.election, email='trustee1@example.com').count(), 1)

def test_duplicate_trustee_email_shows_error_instead_of_500(self):
self._post('Trustee One', 'trustee1@example.com')
# submitting the same email again (e.g. a double-click or retry) used to raise IntegrityError
response = self._post('Trustee One Again', 'trustee1@example.com')
self.assertStatusCode(response, 200)
self.assertContains(response, 'There is already a trustee with that email address')
self.assertContains(response, 'value="Trustee One Again"')
self.assertEqual(models.Trustee.objects.filter(election=self.election, email='trustee1@example.com').count(), 1)
9 changes: 8 additions & 1 deletion helios/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,14 @@ def new_trustee(request, election):
# get the public key and the hash, and add it
name = request.POST['name']
email = request.POST['email']


# an election has at most one trustee per email address (unique_together),
# so tell the admin rather than failing on the database constraint
if Trustee.objects.filter(election = election, email = email).exists():
return render_template(request, 'new_trustee', {
'election' : election, 'name' : name, 'email' : email,
'error' : 'There is already a trustee with that email address for this election.'})

trustee = Trustee(uuid = str(uuid.uuid4()), election = election, name=name, email=email)
trustee.save()
return HttpResponseRedirect(settings.SECURE_URL_HOST + reverse(url_names.election.ELECTION_TRUSTEES_VIEW, args=[election.uuid]))
Expand Down
Loading