Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GetOrCreateSerializerMixin not working with pulp_domain #6078

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed `GetOrCreateSerializerMixin` not accepting pulp_domain for the natural key creation.
4 changes: 4 additions & 0 deletions pulpcore/app/serializers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ def get_or_create(cls, natural_key, default_values=None):
if default_values:
data.update(default_values)
data.update(natural_key)
if "pulp_domain" in natural_key:
del data["pulp_domain"]
serializer = cls(data=data)
try:
serializer.is_valid(raise_exception=True)
Expand All @@ -402,6 +404,8 @@ def get_or_create(cls, natural_key, default_values=None):
# validation failed with 400 'unique' error code only
result = cls.Meta.model.objects.get(**natural_key)
try:
if "pulp_domain" in natural_key:
serializer.validated_data["pulp_domain"] = natural_key["pulp_domain"]
result = result or serializer.create(serializer.validated_data)
except IntegrityError:
# recover from a race condition, where another thread just created the object
Expand Down
33 changes: 32 additions & 1 deletion pulpcore/tests/unit/serializers/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

from rest_framework import serializers

from pulpcore.app.serializers import validate_unknown_fields
from pulpcore.app.serializers import (
validate_unknown_fields,
RBACContentGuardSerializer,
GetOrCreateSerializerMixin,
)
from pulpcore.app.models import RBACContentGuard
from pulpcore.app.util import get_domain


def test_unknown_field():
Expand Down Expand Up @@ -108,3 +114,28 @@ def test_ignored_fields_no_side_effects():
initial_data = {"field1": 1, "csrfmiddlewaretoken": 2}
defined_fields = {"field1": 1}
validate_unknown_fields(initial_data, defined_fields)


@pytest.fixture
def guard_fixture(db):
return RBACContentGuard.objects.get_or_create(name="test")[0]


class GuardSerializer(GetOrCreateSerializerMixin, RBACContentGuardSerializer):
pass


def test_mixin_get(guard_fixture):
"""Test GetOrCreateSerializerMixin's get functionality."""
natural_key = {"name": "test", "pulp_domain": get_domain()}
guard = GuardSerializer.get_or_create(natural_key)
assert guard.pk == guard_fixture.pk


def test_mixin_create(guard_fixture):
"""Test GetOrCreateSerializerMixin's create functionality.'"""
natural_key = {"name": "test2", "pulp_domain": get_domain()}
default_data = {"description": "hello"}
guard = GuardSerializer.get_or_create(natural_key, default_data)
assert guard.pk != guard_fixture.pk
assert guard.description == default_data["description"]