diff --git a/api/audit/models.py b/api/audit/models.py index 030b8f72ad81..cf9c71b69e74 100644 --- a/api/audit/models.py +++ b/api/audit/models.py @@ -144,7 +144,10 @@ def add_created_date(self) -> None: when="environment_document_updated", is_now=True, ) - def process_environment_update(self): + def process_environment_update(self) -> None: + if not self.project: + return + from environments.models import Environment from environments.tasks import process_environment_update diff --git a/api/tests/unit/audit/test_unit_audit_models.py b/api/tests/unit/audit/test_unit_audit_models.py index a6b49229b8f7..07d6ca67c8dd 100644 --- a/api/tests/unit/audit/test_unit_audit_models.py +++ b/api/tests/unit/audit/test_unit_audit_models.py @@ -1,3 +1,4 @@ +import pytest from pytest_mock import MockerFixture from audit.models import AuditLog @@ -228,3 +229,15 @@ def test_creating_audit_logs_for_change_request_does_not_trigger_process_environ # Then process_environment_update.delay.assert_not_called() assert audit_log.created_date != environment.updated_at + + +@pytest.mark.django_db +def test_audit_log__organisation__empty_instance__return_expected() -> None: + # Given + audit_log = AuditLog.objects.create() + + # When + organisation = audit_log.organisation + + # Then + assert organisation is None