diff --git a/api/funkwhale_api/music/importers.py b/api/funkwhale_api/music/importers.py index ce7ded02b48822c2740c6c9e57765ea8b458dccc..fc4a98241e151bb3325d3dbd685511074ef3fc66 100644 --- a/api/funkwhale_api/music/importers.py +++ b/api/funkwhale_api/music/importers.py @@ -3,12 +3,19 @@ def load(model, *args, **kwargs): return importer.load(*args, **kwargs) +EXCLUDE_VALIDATION = {"Track": ["artist"]} + + class Importer(object): def __init__(self, model): self.model = model def load(self, cleaned_data, raw_data, import_hooks): mbid = cleaned_data.pop("mbid") + # let's validate data, just in case + instance = self.model(**cleaned_data) + exclude = EXCLUDE_VALIDATION.get(self.model.__name__, []) + instance.full_clean(exclude=["mbid", "uuid"] + exclude) m = self.model.objects.update_or_create(mbid=mbid, defaults=cleaned_data)[0] for hook in import_hooks: hook(m, cleaned_data, raw_data) diff --git a/api/tests/music/test_import.py b/api/tests/music/test_import.py index 2be267cffd28f9d9ab1b85fbe2b5f8e5834ed5a7..12185f88805db6d5191e39f1c9bb510a91c91d32 100644 --- a/api/tests/music/test_import.py +++ b/api/tests/music/test_import.py @@ -1,10 +1,15 @@ import json import os +import pytest +import uuid +from django import forms from django.urls import reverse from funkwhale_api.federation import actors from funkwhale_api.federation import serializers as federation_serializers +from funkwhale_api.music import importers +from funkwhale_api.music import models from funkwhale_api.music import tasks DATA_DIR = os.path.dirname(os.path.abspath(__file__)) @@ -237,3 +242,9 @@ def test__do_import_in_place_mbid(factories, tmpfile): assert bool(tf.audio_file) is False assert tf.source == "file://{}".format(path) assert tf.mimetype == "audio/ogg" + + +def test_importer_cleans(): + importer = importers.Importer(models.Artist) + with pytest.raises(forms.ValidationError): + importer.load({"name": "", "mbid": uuid.uuid4()}, {}, []) diff --git a/api/tests/users/test_models.py b/api/tests/users/test_models.py index ea760cc6c6b5a49f39903f1d641bd3d664819348..f4a27b40bb94334cea84f43b0c3d8ba9c1c99bda 100644 --- a/api/tests/users/test_models.py +++ b/api/tests/users/test_models.py @@ -126,4 +126,4 @@ def test_can_filter_closed_invitations(factories): used = factories["users.User"](invited=True).invitation assert models.Invitation.objects.count() == 3 - assert list(models.Invitation.objects.open(False)) == [expired, used] + assert list(models.Invitation.objects.order_by("id").open(False)) == [expired, used] diff --git a/changes/changelog.d/351.bugfix b/changes/changelog.d/351.bugfix new file mode 100644 index 0000000000000000000000000000000000000000..49fa9af445af7dc491807e1aa25552709d173376 --- /dev/null +++ b/changes/changelog.d/351.bugfix @@ -0,0 +1 @@ +Ensure we do not import artists with empty names (#351)