From 1e3d77a295c988295626334e9cfa96eca2752482 Mon Sep 17 00:00:00 2001 From: Olga Bulat Date: Mon, 1 Apr 2024 15:56:43 +0300 Subject: [PATCH 1/2] Add logging when source has invalid value Signed-off-by: Olga Bulat --- api/api/serializers/media_serializers.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/api/api/serializers/media_serializers.py b/api/api/serializers/media_serializers.py index e574b738cca..c47219b693b 100644 --- a/api/api/serializers/media_serializers.py +++ b/api/api/serializers/media_serializers.py @@ -1,3 +1,4 @@ +import logging from collections import namedtuple from django.conf import settings @@ -29,6 +30,8 @@ from api.utils.url import add_protocol +logger = logging.getLogger(__name__) + ####################### # Request serializers # ####################### @@ -396,7 +399,14 @@ def validate_source(self, value): return value else: sources = value.lower().split(",") - valid_sources = [source for source in sources if source in allowed_sources] + valid_sources = set( + [source for source in sources if source in allowed_sources] + ) + if len(sources) > len(valid_sources): + invalid_sources = set(sources).difference(valid_sources) + logger.warning( + f"Invalid sources in search query: {invalid_sources}; sources query: '{value}'" + ) return ",".join(valid_sources) def validate_excluded_source(self, input_sources): From 718e75d412915a6a8c00c3068739f7ec5747122e Mon Sep 17 00:00:00 2001 From: Olga Bulat Date: Mon, 1 Apr 2024 15:57:35 +0300 Subject: [PATCH 2/2] Add a test Signed-off-by: Olga Bulat --- .../serializers/test_media_serializers.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/api/test/unit/serializers/test_media_serializers.py b/api/test/unit/serializers/test_media_serializers.py index 3b854980c73..ea2500a2681 100644 --- a/api/test/unit/serializers/test_media_serializers.py +++ b/api/test/unit/serializers/test_media_serializers.py @@ -99,6 +99,27 @@ def test_media_serializer_adds_license_url_if_missing( assert repr["license_url"] == "https://creativecommons.org/publicdomain/zero/1.0/" +def test_media_serializer_logs_when_invalid_or_duplicate_source(media_type_config): + sources = { + "image": ("flickr,flickr,invalid", "flickr"), + "audio": ("freesound,freesound,invalid", "freesound"), + } + with patch("api.serializers.media_serializers.logger.warning") as mock_logger: + serializer_class = media_type_config.search_request_serializer( + context={"media_type": media_type_config.media_type}, + data={"source": sources[media_type_config.media_type][0]}, + ) + assert serializer_class.is_valid() + assert ( + serializer_class.validated_data["source"] + == sources[media_type_config.media_type][1] + ) + mock_logger.assert_called_with( + f"Invalid sources in search query: {{'invalid'}}; " + f"sources query: '{sources[media_type_config.media_type][0]}'" + ) + + @pytest.mark.parametrize( "has_sensitive_text", (True, False),