Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hyagi committed Jan 26, 2024
1 parent ab03e8c commit 8f116d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pulpcore/app/serializers/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
25 changes: 25 additions & 0 deletions pulpcore/tests/unit/serializers/test_domain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
import random
import string
from types import SimpleNamespace

from rest_framework import serializers
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8f116d5

Please sign in to comment.