diff --git a/pulpcore/app/serializers/domain.py b/pulpcore/app/serializers/domain.py index 692b9f47999..36d200a7f3d 100644 --- a/pulpcore/app/serializers/domain.py +++ b/pulpcore/app/serializers/domain.py @@ -324,6 +324,11 @@ def validate_name(self, value): """Ensure name is not 'api' or 'content'.""" if value.lower() in ("api", "content"): raise serializers.ValidationError(_("Name can not be 'api' or 'content'.")) + + """Ensure name has less than 50 characters.""" + if len(value) > 50: + raise serializers.ValidationError(_("Name can have 50 characters at most.")) + return value def _validate_storage_backend(self, storage_class, storage_settings): diff --git a/pulpcore/tests/unit/serializers/test_domain.py b/pulpcore/tests/unit/serializers/test_domain.py index 68c852db5f5..83df38579c5 100644 --- a/pulpcore/tests/unit/serializers/test_domain.py +++ b/pulpcore/tests/unit/serializers/test_domain.py @@ -1,4 +1,6 @@ import pytest +import random +import string from types import SimpleNamespace from rest_framework import serializers @@ -113,6 +115,29 @@ def test_using_setting_names(storage_class, serializer_class, all_settings): assert storage_settings == all_settings +def test_max_name_length(storage_class, serializer_class, all_settings): + settings_name_map = serializer_class.SETTING_MAPPING + settings = {k.upper(): all_settings[v] for k, v in settings_name_map.items()} + + letters = string.ascii_lowercase + name = ''.join(random.choice(letters) for i in range(50)) + + domain = SimpleNamespace(storage_class=storage_class, redirect_to_object_storage=True, name=name,storage_settings={}) + data = {"storage_settings": settings} + serializer = DomainSerializer(domain, data=data, partial=True) + + serializer.is_valid(raise_exception=True) + assert serializer.instance.name == name + + name = ''.join(random.choice(letters) for i in range(51)) + domain = SimpleNamespace(storage_class=storage_class, redirect_to_object_storage=True, name=name,storage_settings={}) + serializer = DomainSerializer(domain, data=data, partial=True) + + with pytest.raises(serializers.ValidationError) as exc_info: + serializer.validate_name(name) + assert "Name can have 50 characters at most" in str(exc_info.value) + + class DomainSettingsBaseMixin: storage_class = None serializer_class = None