Skip to content

Commit

Permalink
fix: [AAP-38546] - Skip anonymous user in updating created_by/modifie…
Browse files Browse the repository at this point in the history
…d_by fields (#1185)
  • Loading branch information
hsong-rh authored Jan 14, 2025
1 parent ad5b198 commit 7a38086
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
20 changes: 13 additions & 7 deletions src/aap_eda/core/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from crum import get_current_user
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.db import models

__all__ = ("BaseOrgModel", "UniqueNamedModel", "PrimordialModel")
Expand Down Expand Up @@ -72,13 +73,18 @@ class Meta:
def save(self, *args, **kwargs):
update_fields = kwargs.get("update_fields", [])
current_user = get_current_user()
if (
not self.pk and current_user and not self.created_by
): # Set `created_by` only for new objects
self.created_by = current_user
if "created_by" not in update_fields:
update_fields.append("created_by")
if current_user: # Always update `modified_by`
if current_user:
if isinstance(current_user, AnonymousUser):
super().save(*args, **kwargs)
return

# Set `created_by` only for new objects
if not self.pk and not self.created_by:
self.created_by = current_user
if "created_by" not in update_fields:
update_fields.append("created_by")

# Always update `modified_by`
self.modified_by = current_user
if "modified_by" not in update_fields:
update_fields.append("modified_by")
Expand Down
22 changes: 17 additions & 5 deletions tests/integration/api/test_event_stream_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from rest_framework import status
from rest_framework.test import APIClient

from aap_eda.core import enums
from aap_eda.core import enums, models
from tests.integration.api.test_event_stream import (
create_event_stream,
create_event_stream_credential,
Expand All @@ -37,7 +37,9 @@
)
@pytest.mark.django_db
def test_post_event_stream_with_basic_auth(
admin_client: APIClient,
base_client: APIClient,
admin_user: models.User,
anonymous_user: models.User,
preseed_credential_types,
auth_status,
bogus_password,
Expand All @@ -51,8 +53,9 @@ def test_post_event_stream_with_basic_auth(
"http_header_key": "Authorization",
}

base_client.force_authenticate(user=admin_user)
obj = create_event_stream_credential(
admin_client, enums.EventStreamCredentialType.BASIC.value, inputs
base_client, enums.EventStreamCredentialType.BASIC.value, inputs
)

data_in = {
Expand All @@ -62,7 +65,7 @@ def test_post_event_stream_with_basic_auth(
"organization_id": get_default_test_org().id,
"test_mode": True,
}
event_stream = create_event_stream(admin_client, data_in)
event_stream = create_event_stream(base_client, data_in)
if bogus_password:
user_pass = f"{username}:{bogus_password}"
else:
Expand All @@ -76,7 +79,16 @@ def test_post_event_stream_with_basic_auth(
"Authorization": auth_value,
"Content-Type": content_type,
}
response = admin_client.post(
response = base_client.post(
event_stream_post_url(event_stream.uuid),
headers=headers,
data=data_bytes,
content_type=content_type,
)
assert response.status_code == auth_status

base_client.force_authenticate(user=anonymous_user)
response = base_client.post(
event_stream_post_url(event_stream.uuid),
headers=headers,
data=data_bytes,
Expand Down
15 changes: 15 additions & 0 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import pytest
from ansible_base.rbac.models import DABPermission, RoleDefinition
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.contrib.contenttypes.models import ContentType
from django.test import override_settings
from rest_framework.test import APIClient
Expand Down Expand Up @@ -97,6 +98,11 @@ def admin_user(default_organization, admin_info):
return user


@pytest.fixture
def anonymous_user():
return AnonymousUser()


@pytest.fixture
def default_user_awx_token(default_user: models.User):
return models.AwxToken.objects.create(
Expand Down Expand Up @@ -138,6 +144,15 @@ def admin_client(base_client: APIClient, admin_user: models.User) -> APIClient:
return base_client


@pytest.fixture
def anonymous_client(
base_client: APIClient, anonymous_user: models.User
) -> APIClient:
"""Return a pre-configured instance of an APIClient with anonymous_user."""
base_client.force_authenticate(user=anonymous_user)
return base_client


@pytest.fixture
def superuser_client(
base_client: APIClient, super_user: models.User
Expand Down

0 comments on commit 7a38086

Please sign in to comment.