Skip to content

Commit

Permalink
Fix timeouts on related coverage (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
marwoodandrew authored Dec 14, 2023
1 parent ff632eb commit ab8d973
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
9 changes: 3 additions & 6 deletions newsroom/agenda/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,10 @@ def related_wire_items(wire_id):
wire_ids.append(cov['delivery_id'])

wire_items = get_entities_elastic_or_mongo_or_404(wire_ids, 'items')
aggregations = {"uid": {"terms": {"field": "_uid"}}}
permissioned_result = get_resource_service('wire_search').get_items(wire_ids, size=0, aggregations=aggregations,
# Find those items that the user is permitted to view
permissioned_result = get_resource_service('wire_search').get_items(wire_ids, size=len(wire_ids),
apply_permissions=True)
buckets = permissioned_result.hits['aggregations']['uid']['buckets']
permissioned_ids = []
for b in buckets:
permissioned_ids.append(b['key'].replace('items#', ''))
permissioned_ids = [item.get("_id") for item in permissioned_result] if permissioned_result else []

for wire_item in wire_items:
set_item_permission(wire_item, wire_item.get('_id') in permissioned_ids)
Expand Down
49 changes: 48 additions & 1 deletion tests/test_agenda.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pytz
from flask import json
from flask import json, session
from datetime import datetime
from urllib import parse
from unittest import mock

import newsroom.auth # noqa - Fix cyclic import when running single test file
from newsroom.utils import get_location_string, get_agenda_dates, get_public_contacts, get_entity_or_404, \
get_local_date, get_end_date
from newsroom.agenda.views import related_wire_items
from .fixtures import items, init_items, agenda_items, init_agenda_items, init_auth, init_company, PUBLIC_USER_ID # noqa
from .utils import post_json, delete_json, get_json, get_admin_user_id, mock_send_email
from copy import deepcopy
Expand Down Expand Up @@ -569,3 +570,49 @@ def test_filter_events_only(client):
assert 'urn:conference' == data['_items'][0]['_id']
assert 'planning_items' not in data['_items'][0]
assert 'coverages' not in data['_items'][0]


def test_related_wire_items(client, app):
test_planning_with_coveragre = deepcopy(test_planning)
test_planning_with_coveragre["coverages"] = [
{
"coverage_id": "d01ce39aed17",
"delivery_id": "812f8bb5a5d7",
"coverage_type": "text"
},
{
"coverage_id": "250363d911b0",
"delivery_id": "05e339456ea0",
"coverage_type": "text"
},
{
"coverage_id": "7a53221bca0a",
"planning_id": "a1f6f076f7b4",
"coverage_type": "picture"
},
{
"coverage_id": "954757c9881c",
"delivery_id": "97d3b5cd0861",
"coverage_type": "text"
}
]

coverage_items = [{"_id": "812f8bb5a5d7", 'service': [{'code': 'a', 'name': 'Service A'}]},
{"_id": "05e339456ea0", 'service': [{'code': 'b', 'name': 'Service B'}]},
{"_id": "97d3b5cd0861", 'service': [{'code': 'a', 'name': 'Service A'}]}]
app.data.insert('agenda', [test_planning_with_coveragre])
app.data.insert('items', coverage_items)
app.data.insert('products', [{
'_id': ObjectId('5e65964bf5db68883df561d0'), 'name': 'Sport1', 'description': 'sport product 1',
'is_enabled': True, 'product_type': 'wire',
'navigations': [],
'companies': ['1'],
'query': 'service.code:a'
}])
with app.test_request_context():
session['user'] = PUBLIC_USER_ID
session['name'] = 'Test'
resp = related_wire_items("05e339456ea0")
data = json.loads(resp[0].get_data())
assert data.get('agenda_item').get('_id') == 'foo'
assert (next(item for item in data.get('wire_items') if item["_id"] == "05e339456ea0").get("_access")) is False

0 comments on commit ab8d973

Please sign in to comment.