Skip to content

Commit

Permalink
Tests for multipart/form-data validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Birnberg authored and Andrew Birnberg committed Jul 5, 2019
1 parent 6a4b36d commit 935dc91
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,22 @@ def test_www_form_urlencoded(self):
headers=headers)
self.assertEqual(response.json['username'], 'man')

def test_multipart_form_data_one_field(self):
app = self.make_ordinary_app()
response = app.post('/signup',
{'username': 'man'},
content_type='multipart/form-data')
self.assertEqual(response.json['username'], 'man')

# Colander fails to parse multidict type return values
# Therefore, test requires different schema with multiple keys, '/form'
def test_multipart_form_data_multiple_fields(self):
app = self.make_ordinary_app()
response = app.post('/form',
{'field1': 'man', 'field2': 'woman'},
content_type='multipart/form-data')
self.assertEqual(response.json, {'field1': 'man', 'field2': 'woman'})


@skip_if_no_colander
class TestBoundSchemas(LoggingCatcher, TestCase):
Expand Down Expand Up @@ -737,6 +753,22 @@ def test_www_form_urlencoded(self):
headers=headers)
self.assertEqual(response.json['username'], 'man')

def test_multipart_form_data_one_field(self):
app = self.make_ordinary_app()
response = app.post('/m_signup',
{'username': 'man'},
content_type='multipart/form-data')
self.assertEqual(response.json['username'], 'man')

# Marshmallow fails to parse multidict type return values
# Therefore, test requires different schema with multiple keys, '/m_form'
def test_multipart_form_data_multiple_fields(self):
app = self.make_ordinary_app()
response = app.post('/m_form',
{'field1': 'man', 'field2': 'woman'},
content_type='multipart/form-data')
self.assertEqual(response.json, {'field1': 'man', 'field2': 'woman'})


@skip_if_no_marshmallow
class TestValidatorEdgeCasesMarshmallow(TestCase):
Expand Down
25 changes: 25 additions & 0 deletions tests/validationapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def post7(request):
foobaz = Service(name="foobaz", path="/foobaz")
email_service = Service(name='newsletter', path='/newsletter')
item_service = Service(name='item', path='/item/{item_id}')
form_service = Service(name="form", path="/form")


class SignupSchema(MappingSchema):
Expand Down Expand Up @@ -303,6 +304,16 @@ class ItemSchema(MappingSchema):
def item(request):
return request.validated['path']

class FormSchema(MappingSchema):
field1 = SchemaNode(String())
field2 = SchemaNode(String())


@form_service.post(schema=FormSchema(),
validators=(colander_body_validator,))
def form(request):
return request.validated

try:
import marshmallow
try:
Expand All @@ -328,6 +339,7 @@ def item(request):
m_foobaz = Service(name="m_foobaz", path="/m_foobaz")
m_email_service = Service(name='m_newsletter', path='/m_newsletter')
m_item_service = Service(name='m_item', path='/m_item/{item_id}')
m_form_service = Service(name="m_form", path="/m_form")


class MSignupSchema(marshmallow.Schema):
Expand Down Expand Up @@ -486,6 +498,19 @@ def m_item(request):
def m_item_fails(request):
return request.validated['path']

class MFormSchema(marshmallow.Schema):
class Meta:
strict = True
unknown = EXCLUDE
field1 = marshmallow.fields.String()
field2 = marshmallow.fields.String()


@m_form_service.post(
schema=MFormSchema, validators=(marshmallow_body_validator,))
def m_form(request):
return request.validated

def includeme(config):
config.include("cornice")
config.scan("tests.validationapp")
Expand Down

0 comments on commit 935dc91

Please sign in to comment.