Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix new person form validation #1406

Merged
merged 3 commits into from Apr 7, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions ynr/apps/elections/uk/templates/candidates/_person_form.html
Expand Up @@ -12,6 +12,18 @@ <h2>Oops!</h2>
{{ form.non_field_errors.as_ul }}
{% else %}
<p>Please check your information matches our requirements, below.</p>
{% if form.errors %}
<dl>
{% for field, error_list in form.errors.items %}
<dt>{{ field }}</dt>
{% for error in error_list %}
<dd>{{ error }}</dd>
{% endfor %}
{% endfor %}
</dl>
{% endif %}


{% endif %}
</div>
{% endif %}
Expand Down
5 changes: 5 additions & 0 deletions ynr/apps/people/forms/fields.py
Expand Up @@ -26,6 +26,11 @@ def __init__(self, *args, **kwargs):

widget = BallotInputWidget

def prepare_value(self, value):
if isinstance(value, Ballot):
return value.ballot_paper_id
return value

def to_python(self, value):
if not value:
return value
Expand Down
@@ -1,12 +1,13 @@
from urllib.parse import urlsplit

from django.urls import reverse
from django_webtest import WebTest

from candidates.models import LoggedAction
from people.models import Person

from .auth import TestUserMixin
from .uk_examples import UK2015ExamplesMixin
from candidates.tests.auth import TestUserMixin
from candidates.tests.uk_examples import UK2015ExamplesMixin


class TestNewPersonView(TestUserMixin, UK2015ExamplesMixin, WebTest):
Expand Down Expand Up @@ -108,3 +109,42 @@ def test_new_person_submission(self):
last_logged_action = LoggedAction.objects.all().order_by("-created")[0]
self.assertEqual(last_logged_action.person_id, person.id)
self.assertEqual(last_logged_action.action_type, "person-create")

def test_new_person_view(self):
self.assertFalse(
Person.objects.filter(name="Elizabeth Bennet").exists()
)
url = reverse(
"person-create",
kwargs={
"ballot_paper_id": self.dulwich_post_ballot.ballot_paper_id
},
)
response = self.app.get(url, user=self.user)
self.assertEqual(response.status_code, 200)

# make sure we've got the PersonIdentifiers
self.assertTrue(
response.html.find(
"input", {"name": "tmp_person_identifiers-0-value"}
)
)
# make sure we've got the simple personal fields
self.assertTrue(response.html.find("input", {"id": "id_name"}))

self.assertTrue(
response.html.find("input", {"id": "id_ballot_paper_id"})
)
self.assertEqual(
response.html.find("input", {"id": "id_ballot_paper_id"})["value"],
self.dulwich_post_ballot.ballot_paper_id,
)
#
form = response.forms["new-candidate-form"]
form["name"] = "Elizabeth Bennet"

form["party_identifier_1"] = self.labour_party.ec_id
form["source"] = "Testing adding a new person to a post"
submission_response = form.submit()
self.assertEqual(submission_response.status_code, 302)
person = Person.objects.get(name="Elizabeth Bennet")