From c870911cd7ebfe42d9da6af1ed2053d6d4aa06fe Mon Sep 17 00:00:00 2001 From: Thomas Herve Date: Fri, 17 Feb 2017 13:32:15 +0100 Subject: [PATCH] Check body type when patching subscription When passing an invalid body type to subscription patching, we can an ugly error in the logs "xx has no attribute get". Let's verify that's it's a dict in validation to get a proper error. Change-Id: I0c251b15a02f481c1d80afd7908a665c861b8aa7 --- .../tests/unit/transport/wsgi/v2_0/test_subscriptions.py | 9 +++++++++ zaqar/transport/validation.py | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/zaqar/tests/unit/transport/wsgi/v2_0/test_subscriptions.py b/zaqar/tests/unit/transport/wsgi/v2_0/test_subscriptions.py index 0a3315c6f..a16a5e285 100644 --- a/zaqar/tests/unit/transport/wsgi/v2_0/test_subscriptions.py +++ b/zaqar/tests/unit/transport/wsgi/v2_0/test_subscriptions.py @@ -364,6 +364,15 @@ class TestSubscriptionsMongoDB(base.V2Base): headers=self.headers) self.assertEqual(falcon.HTTP_400, self.srmock.status) + def test_patch_invalid_body(self): + resp = self.simulate_patch(self.subscription_path + '/x', + body='[1]', + headers=self.headers) + self.assertEqual(falcon.HTTP_400, self.srmock.status) + resp_doc = jsonutils.loads(resp[0]) + self.assertEqual('Subscriptions must be a dict.', + resp_doc['description']) + def test_delete_works(self): self._create_subscription() resp = self.simulate_get(self.subscription_path, diff --git a/zaqar/transport/validation.py b/zaqar/transport/validation.py index cbb002a42..9d003c498 100644 --- a/zaqar/transport/validation.py +++ b/zaqar/transport/validation.py @@ -521,6 +521,10 @@ class Validator(object): if not subscription: raise ValidationFailed(_(u'No subscription to create.')) + if not isinstance(subscription, dict): + msg = _('Subscriptions must be a dict.') + raise ValidationFailed(msg) + subscriber = subscription.get('subscriber', None) subscriber_type = None