From 329c268e37fe8d55ff6503d66ae46b653de0db8d Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Wed, 18 Apr 2018 11:20:43 +0200 Subject: [PATCH] added test for prefilling of forms --- Makefile | 2 +- ordr/resources/account.py | 4 ++-- ordr/schemas/__init__.py | 9 ++++----- setup.cfg | 1 + tests/resources/base_child_resource.py | 19 ++++++++++++++++++- tests/schemas/__init__.py | 2 +- 6 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 759e7f5..aa3aa73 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ clean-test: ## remove test and coverage artifacts lint: ## check style with flake8 flake8 ordr tests - + test: ## run tests quickly with the default Python, ignoring functional tests py.test diff --git a/ordr/resources/account.py b/ordr/resources/account.py index cbcb891..8aae785 100644 --- a/ordr/resources/account.py +++ b/ordr/resources/account.py @@ -20,10 +20,10 @@ class RegistrationResource(BaseChildResource): ''' access controll list for the resource ''' return [(Allow, Everyone, 'view'), DENY_ALL] - def get_registration_form(self, **override): + def get_registration_form(self, **kwargs): ''' returns the registration form''' settings = { 'buttons': ('Create account', 'Cancel'), } - settings.update(override) + settings.update(kwargs) return self._prepare_form(RegistrationSchema, **settings) diff --git a/ordr/schemas/__init__.py b/ordr/schemas/__init__.py index 7989d24..f3a31f7 100644 --- a/ordr/schemas/__init__.py +++ b/ordr/schemas/__init__.py @@ -24,7 +24,7 @@ class CSRFSchema(colander.Schema): ) @classmethod - def as_form(cls, request, url=None, **kwargs): + def as_form(cls, request, action=None, **kwargs): ''' returns the schema as a form :param pyramid.request.Request request: the current request @@ -35,11 +35,10 @@ class CSRFSchema(colander.Schema): :param kwargs: additional parameters for the form rendering. ''' - if url is None: - url = request.resource_url(request.context, request.view_name) + if action is None: + action = request.resource_url(request.context, request.view_name) schema = cls().bind(request=request) - form = deform.Form(schema, action=url, **kwargs) - return form + return deform.Form(schema, action=action, **kwargs) def includeme(config): diff --git a/setup.cfg b/setup.cfg index e598b63..6d09db1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -26,4 +26,5 @@ test = pytest testpaths = tests python_files = *.py collect_ignore = ['setup.py'] +xfail_strict = true diff --git a/tests/resources/base_child_resource.py b/tests/resources/base_child_resource.py index 94753ca..40c98b0 100644 --- a/tests/resources/base_child_resource.py +++ b/tests/resources/base_child_resource.py @@ -47,7 +47,7 @@ def test_base_child_prepare_form_url(): parent = DummyResource() request = DummyRequest() resource = BaseChildResource(request, 'a name', parent) - form = resource._prepare_form(RegistrationSchema, url='/foo') + form = resource._prepare_form(RegistrationSchema, action='/foo') assert form.action == '/foo' @@ -63,3 +63,20 @@ def test_base_child_prepare_form_settings(): assert len(form.buttons) == 2 assert isinstance(form.buttons[0], deform.Button) assert isinstance(form.buttons[1], deform.Button) + + +def test_base_child_prepare_form_prefill(): + from ordr.resources.helpers import BaseChildResource + from ordr.schemas.account import RegistrationSchema + parent = DummyResource() + request = DummyRequest() + resource = BaseChildResource(request, 'a name', parent) + prefill = { + 'first_name': 'John', + 'last_name': 'Doe', + 'email': 'johndoe@example.com' + } + form = resource._prepare_form(RegistrationSchema, prefill=prefill) + assert form['first_name'].cstruct == 'John' + assert form['last_name'].cstruct == 'Doe' + assert form['email'].cstruct == 'johndoe@example.com' diff --git a/tests/schemas/__init__.py b/tests/schemas/__init__.py index 2257fd7..59d25aa 100644 --- a/tests/schemas/__init__.py +++ b/tests/schemas/__init__.py @@ -8,7 +8,7 @@ def test_csrf_schema_form_with_custom_url(): from ordr.schemas import CSRFSchema request = DummyRequest() - form = CSRFSchema.as_form(request, url='/Nudge/Nudge') + form = CSRFSchema.as_form(request, action='/Nudge/Nudge') assert form.action == '/Nudge/Nudge' assert form.buttons == []