From dc3015bd74ad92a6f61d09cca9a6f2d291d9d587 Mon Sep 17 00:00:00 2001 From: Daniel Martin Date: Sun, 18 Dec 2022 07:49:34 +0000 Subject: [PATCH] Fix api crash on background audio attached --- openlp/core/api/versions/v2/controller.py | 8 ++++++-- tests/openlp_core/lib/test_serviceitem.py | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/openlp/core/api/versions/v2/controller.py b/openlp/core/api/versions/v2/controller.py index 4dca5c603..efef26476 100644 --- a/openlp/core/api/versions/v2/controller.py +++ b/openlp/core/api/versions/v2/controller.py @@ -22,9 +22,11 @@ import logging from openlp.core.api.lib import login_required from openlp.core.common import ThemeLevel +from openlp.core.common.json import OpenLPJSONEncoder from openlp.core.common.registry import Registry from openlp.core.lib import image_to_data_uri +import json from flask import jsonify, request, abort, Blueprint, Response controller_views = Blueprint('controller', __name__) @@ -41,7 +43,8 @@ def controller_live_items(): live_item = current_item.to_dict() live_item['slides'][live_controller.selected_row]['selected'] = True live_item['id'] = str(current_item.unique_identifier) - return jsonify(live_item) + json_live_item = json.dumps(live_item, cls=OpenLPJSONEncoder) + return Response(json_live_item, mimetype='application/json') @controller_views.route('/live-item') @@ -53,7 +56,8 @@ def controller_live_item(): if current_item: live_item = current_item.to_dict(True, live_controller.selected_row) live_item['id'] = str(current_item.unique_identifier) - return jsonify(live_item) + json_live_item = json.dumps(live_item, cls=OpenLPJSONEncoder) + return Response(json_live_item, mimetype='application/json') @controller_views.route('/show', methods=['POST']) diff --git a/tests/openlp_core/lib/test_serviceitem.py b/tests/openlp_core/lib/test_serviceitem.py index 4ba7c0829..504d5375b 100644 --- a/tests/openlp_core/lib/test_serviceitem.py +++ b/tests/openlp_core/lib/test_serviceitem.py @@ -21,6 +21,7 @@ """ Package to test the openlp.core.lib package. """ +import json import os import pytest from pathlib import Path @@ -28,6 +29,7 @@ from unittest.mock import Mock, MagicMock, patch from openlp.core.common import ThemeLevel from openlp.core.common.enum import ServiceItemType +from openlp.core.common.json import OpenLPJSONEncoder from openlp.core.common.platform import is_win from openlp.core.common.registry import Registry from openlp.core.lib.formattingtags import FormattingTags @@ -442,6 +444,26 @@ def test_service_item_load_song_and_audio_from_service(mock_sha256_file_hash, st 'The tuple ("/test/abcd.mp3", "abcd") should be in the background_audio list' +@patch('openlp.core.lib.serviceitem.sha256_file_hash') +def test_service_item_to_dict_is_valid_json(mock_sha256_file_hash, state_media, settings, service_item_env): + """ + Test the Service Item - Converting to to_dict response to json + """ + # GIVEN: A new service item with song slide + service_item = ServiceItem(None) + service_item.add_icon = MagicMock() + FormattingTags.load_tags() + mock_sha256_file_hash.return_value = 'abcd' + line = convert_file_service_item(TEST_PATH, 'serviceitem-song-linked-audio.osj') + service_item.set_from_service(line, Path('/test/')) + + # WHEN: Generating a service item + service_dict = service_item.to_dict() + + # THEN: We should get back a valid json object + assert json.dumps(service_dict, cls=OpenLPJSONEncoder) is not None + + def test_service_item_get_theme_data_global_level(settings): """ Test the service item - get theme data when set to global theme level