Work around Eve not supporting returning binary data

This commit is contained in:
Sybren A. Stüvel 2018-01-24 12:59:47 +01:00
parent c44f0489bc
commit d3f97358d9
2 changed files with 37 additions and 0 deletions

View File

@ -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

View File

@ -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 = [