diff options
Diffstat (limited to 'wsme/tests/test_restjson.py')
-rw-r--r-- | wsme/tests/test_restjson.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/wsme/tests/test_restjson.py b/wsme/tests/test_restjson.py index 8099ff1..4ed6423 100644 --- a/wsme/tests/test_restjson.py +++ b/wsme/tests/test_restjson.py @@ -101,6 +101,10 @@ class Obj(wsme.types.Base): name = wsme.types.text +class NestedObj(wsme.types.Base): + o = Obj + + class CRUDResult(object): data = Obj message = wsme.types.text @@ -499,6 +503,37 @@ class TestRestJson(wsme.tests.protocol.RestOnlyProtocolTestCase): "invalid literal for int() with base 10: '%s'" % value ) + def test_parse_unexpected_attribute(self): + o = { + "id": "1", + "name": "test", + "other": "unknown", + "other2": "still unknown", + } + for ba in True, False: + jd = o if ba else {"o": o} + try: + parse(json.dumps(jd), {'o': Obj}, ba) + raise AssertionError("Object should not parse correcty.") + except wsme.exc.UnknownAttribute as e: + self.assertEqual(e.attributes, set(['other', 'other2'])) + + def test_parse_unexpected_nested_attribute(self): + no = { + "o": { + "id": "1", + "name": "test", + "other": "unknown", + }, + } + for ba in False, True: + jd = no if ba else {"no": no} + try: + parse(json.dumps(jd), {'no': NestedObj}, ba) + except wsme.exc.UnknownAttribute as e: + self.assertEqual(e.attributes, set(['other'])) + self.assertEqual(e.fieldname, "no.o") + def test_nest_result(self): self.root.protocols[0].nest_result = True r = self.app.get('/returntypes/getint.json') @@ -682,6 +717,33 @@ class TestRestJson(wsme.tests.protocol.RestOnlyProtocolTestCase): assert result['data']['name'] == u("test") assert result['message'] == "read" + def test_unexpected_extra_arg(self): + headers = { + 'Content-Type': 'application/json', + } + data = {"id": 1, "name": "test"} + content = json.dumps({"data": data, "other": "unexpected"}) + res = self.app.put( + '/crud', + content, + headers=headers, + expect_errors=True) + self.assertEqual(res.status_int, 400) + + def test_unexpected_extra_attribute(self): + """Expect a failure if we send an unexpected object attribute.""" + headers = { + 'Content-Type': 'application/json', + } + data = {"id": 1, "name": "test", "other": "unexpected"} + content = json.dumps({"data": data}) + res = self.app.put( + '/crud', + content, + headers=headers, + expect_errors=True) + self.assertEqual(res.status_int, 400) + def test_body_arg(self): headers = { 'Content-Type': 'application/json', |