summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2013-10-14 14:19:23 +0000
committerGerrit Code Review <review@openstack.org>2013-10-14 14:19:23 +0000
commitb011b2c83fbfc5905591f3d6e80103a6fc8be626 (patch)
treef69cd9f3827ed62267b34ce522d24e1fbf0d3ba7
parent5b283a613fbb2340377e2e4484964fa1dcb836f3 (diff)
parentb67405d486a0d09abde6423818734bbb5a978bc4 (diff)
downloadwsme-b011b2c83fbfc5905591f3d6e80103a6fc8be626.tar.gz
Merge "Validate body when using Pecan"
-rw-r--r--tests/pecantest/test/controllers/ws.py6
-rw-r--r--tests/pecantest/test/tests/test_ws.py11
-rw-r--r--wsme/rest/json.py2
-rw-r--r--wsme/rest/xml.py2
4 files changed, 19 insertions, 2 deletions
diff --git a/tests/pecantest/test/controllers/ws.py b/tests/pecantest/test/controllers/ws.py
index 9036bcd..ebef4e9 100644
--- a/tests/pecantest/test/controllers/ws.py
+++ b/tests/pecantest/test/controllers/ws.py
@@ -14,6 +14,12 @@ class Author(Base):
firstname = text
books = wsattr(['Book'])
+ @staticmethod
+ def validate(author):
+ if author.firstname == 'Robert':
+ raise wsme.exc.ClientSideError("I don't like this author!")
+ return author
+
class Book(Base):
id = int
diff --git a/tests/pecantest/test/tests/test_ws.py b/tests/pecantest/test/tests/test_ws.py
index 6002796..2bc484c 100644
--- a/tests/pecantest/test/tests/test_ws.py
+++ b/tests/pecantest/test/tests/test_ws.py
@@ -59,6 +59,17 @@ class TestWS(FunctionalTest):
assert '<id>1</id>' in body
assert '<firstname>aname</firstname>' in body
+ def test_post_body_parameter_validation(self):
+ res = self.app.post(
+ '/authors', '{"firstname": "Robert"}',
+ headers={"Content-Type": "application/json"},
+ expect_errors=True
+ )
+ self.assertEqual(res.status_int, 400)
+ a = json.loads(res.body.decode('utf-8'))
+ self.assertEqual(a['faultcode'], 'Client')
+ self.assertEqual(a['faultstring'], "I don't like this author!")
+
def test_post_body_parameter(self):
res = self.app.post(
'/authors', '{"firstname": "test"}',
diff --git a/wsme/rest/json.py b/wsme/rest/json.py
index 180ee2c..77277ce 100644
--- a/wsme/rest/json.py
+++ b/wsme/rest/json.py
@@ -140,7 +140,7 @@ def fromjson(datatype, value):
elif attrdef.mandatory:
raise InvalidInput(attrdef.name, None,
"Mandatory field missing.")
- return obj
+ return wsme.types.validate_value(datatype, obj)
elif wsme.types.isusertype(datatype):
value = datatype.frombasetype(
fromjson(datatype.basetype, value))
diff --git a/wsme/rest/xml.py b/wsme/rest/xml.py
index 1ebc3d5..9a836c3 100644
--- a/wsme/rest/xml.py
+++ b/wsme/rest/xml.py
@@ -105,7 +105,7 @@ def fromxml(datatype, element):
elif attrdef.mandatory:
raise InvalidInput(attrdef.name, None,
"Mandatory field missing.")
- return obj
+ return wsme.types.validate_value(datatype, obj)
if datatype is wsme.types.bytes:
return element.text.encode('ascii')
return datatype(element.text)