From d3f97358d9b3b8ee72ccdfc65577f643b632c56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 24 Jan 2018 12:59:47 +0100 Subject: [PATCH] Work around Eve not supporting returning binary data --- pillar/api/organizations/hooks.py | 21 +++++++++++++++++++++ tests/test_api/test_organizations.py | 16 ++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/pillar/api/organizations/hooks.py b/pillar/api/organizations/hooks.py index 84671fd1..e8e9dc7f 100644 --- a/pillar/api/organizations/hooks.py +++ b/pillar/api/organizations/hooks.py @@ -16,6 +16,24 @@ def pre_get_organizations(request, lookup): lookup['$or'] = [{'admin_uid': user.user_id}, {'members': user.user_id}] +def on_fetched_item_organizations(org_doc: dict): + """Filter out binary data. + + Eve cannot return binary data, at least not until we upgrade to a version + that depends on Cerberus >= 1.0. + """ + + for ipr in org_doc.get('ip_ranges') or []: + ipr.pop('start', None) + ipr.pop('end', None) + ipr.pop('prefix', None) # not binary, but useless without the other fields. + + +def on_fetched_resource_organizations(response: dict): + for org_doc in response.get('_items', []): + on_fetched_item_organizations(org_doc) + + def pre_post_organizations(request): user = current_user() if not user.has_cap('create-organization'): @@ -25,3 +43,6 @@ def pre_post_organizations(request): def setup_app(app): app.on_pre_GET_organizations += pre_get_organizations app.on_pre_POST_organizations += pre_post_organizations + + app.on_fetched_item_organizations += on_fetched_item_organizations + app.on_fetched_resource_organizations += on_fetched_resource_organizations diff --git a/tests/test_api/test_organizations.py b/tests/test_api/test_organizations.py index a6747abe..0dfd11cd 100644 --- a/tests/test_api/test_organizations.py +++ b/tests/test_api/test_organizations.py @@ -828,6 +828,22 @@ class IPRangeTest(AbstractOrgTest): ip_ranges.doc('2a03:b0c0:0:1010::8fe:6ef1/120'), ], db_org['ip_ranges']) + def test_ipranges_get_via_eve(self): + self.test_patch_set_ip_ranges_happy() + r = self.get(f'/api/organizations/{self.org_id}', auth_token='token') + from_eve = r.json() + + # Eve cannot return binary data, at least not until we upgrade to a version + # that depends on Cerberus >= 1.0. + expect_ranges = [{'human': '::ffff:192.168.3.0/120'}, + {'human': '::ffff:192.168.3.1/128'}, + {'human': '2a03:b0c0:0:1010::8fe:6e00/120'}] + self.assertEqual(expect_ranges, from_eve['ip_ranges']) + + r = self.get(f'/api/organizations', auth_token='token') + from_eve = r.json() + self.assertEqual(expect_ranges, from_eve['_items'][0]['ip_ranges']) + def test_patch_unset_ip_ranges_happy(self): """Setting to empty list should just delete the entire key.""" ipranges = [