diff --git a/src/aap_eda/core/models/base.py b/src/aap_eda/core/models/base.py index 0da5ac56c..f057f3637 100644 --- a/src/aap_eda/core/models/base.py +++ b/src/aap_eda/core/models/base.py @@ -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") @@ -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") diff --git a/tests/integration/api/test_event_stream_basic.py b/tests/integration/api/test_event_stream_basic.py index 8c9cb9550..ce9641ed6 100644 --- a/tests/integration/api/test_event_stream_basic.py +++ b/tests/integration/api/test_event_stream_basic.py @@ -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, @@ -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, @@ -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 = { @@ -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: @@ -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, diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 1de00c444..d097bbca8 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -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 @@ -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( @@ -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