Browse Source

added test for prefilling of forms

rework
Holger Frey 7 years ago
parent
commit
329c268e37
  1. 2
      Makefile
  2. 4
      ordr/resources/account.py
  3. 9
      ordr/schemas/__init__.py
  4. 1
      setup.cfg
  5. 19
      tests/resources/base_child_resource.py
  6. 2
      tests/schemas/__init__.py

2
Makefile

@ -51,7 +51,7 @@ clean-test: ## remove test and coverage artifacts
lint: ## check style with flake8 lint: ## check style with flake8
flake8 ordr tests flake8 ordr tests
test: ## run tests quickly with the default Python, ignoring functional tests test: ## run tests quickly with the default Python, ignoring functional tests
py.test py.test

4
ordr/resources/account.py

@ -20,10 +20,10 @@ class RegistrationResource(BaseChildResource):
''' access controll list for the resource ''' ''' access controll list for the resource '''
return [(Allow, Everyone, 'view'), DENY_ALL] return [(Allow, Everyone, 'view'), DENY_ALL]
def get_registration_form(self, **override): def get_registration_form(self, **kwargs):
''' returns the registration form''' ''' returns the registration form'''
settings = { settings = {
'buttons': ('Create account', 'Cancel'), 'buttons': ('Create account', 'Cancel'),
} }
settings.update(override) settings.update(kwargs)
return self._prepare_form(RegistrationSchema, **settings) return self._prepare_form(RegistrationSchema, **settings)

9
ordr/schemas/__init__.py

@ -24,7 +24,7 @@ class CSRFSchema(colander.Schema):
) )
@classmethod @classmethod
def as_form(cls, request, url=None, **kwargs): def as_form(cls, request, action=None, **kwargs):
''' returns the schema as a form ''' returns the schema as a form
:param pyramid.request.Request request: the current request :param pyramid.request.Request request: the current request
@ -35,11 +35,10 @@ class CSRFSchema(colander.Schema):
:param kwargs: :param kwargs:
additional parameters for the form rendering. additional parameters for the form rendering.
''' '''
if url is None: if action is None:
url = request.resource_url(request.context, request.view_name) action = request.resource_url(request.context, request.view_name)
schema = cls().bind(request=request) schema = cls().bind(request=request)
form = deform.Form(schema, action=url, **kwargs) return deform.Form(schema, action=action, **kwargs)
return form
def includeme(config): def includeme(config):

1
setup.cfg

@ -26,4 +26,5 @@ test = pytest
testpaths = tests testpaths = tests
python_files = *.py python_files = *.py
collect_ignore = ['setup.py'] collect_ignore = ['setup.py']
xfail_strict = true

19
tests/resources/base_child_resource.py

@ -47,7 +47,7 @@ def test_base_child_prepare_form_url():
parent = DummyResource() parent = DummyResource()
request = DummyRequest() request = DummyRequest()
resource = BaseChildResource(request, 'a name', parent) resource = BaseChildResource(request, 'a name', parent)
form = resource._prepare_form(RegistrationSchema, url='/foo') form = resource._prepare_form(RegistrationSchema, action='/foo')
assert form.action == '/foo' assert form.action == '/foo'
@ -63,3 +63,20 @@ def test_base_child_prepare_form_settings():
assert len(form.buttons) == 2 assert len(form.buttons) == 2
assert isinstance(form.buttons[0], deform.Button) assert isinstance(form.buttons[0], deform.Button)
assert isinstance(form.buttons[1], 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'

2
tests/schemas/__init__.py

@ -8,7 +8,7 @@ def test_csrf_schema_form_with_custom_url():
from ordr.schemas import CSRFSchema from ordr.schemas import CSRFSchema
request = DummyRequest() 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.action == '/Nudge/Nudge'
assert form.buttons == [] assert form.buttons == []