Skip to content

Commit 94e234c

Browse files
authored
fix(api): use raw FK ids in membership post_delete signal to avoid cascade lookup failures (#10497)
1 parent 8267fc4 commit 94e234c

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

api/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ All notable changes to the **Prowler API** are documented in this file.
2222
- `MANAGE_ACCOUNT` permission no longer required for listing and creating tenants [(#10468)](https://github.com/prowler-cloud/prowler/pull/10468)
2323
- Finding groups muted filter, counters, metadata extraction and mute reaggregation [(#10477)](https://github.com/prowler-cloud/prowler/pull/10477)
2424
- Finding groups `check_title__icontains` resolution, `name__icontains` resource filter and `resource_group` field in `/resources` response [(#10486)](https://github.com/prowler-cloud/prowler/pull/10486)
25+
- Membership `post_delete` signal using raw FK ids to avoid `DoesNotExist` during cascade deletions [(#10497)](https://github.com/prowler-cloud/prowler/pull/10497)
2526

2627
### 🔐 Security
2728

api/src/backend/api/signals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def revoke_membership_api_keys(sender, instance, **kwargs): # noqa: F841
6161
in that tenant should be revoked to prevent further access.
6262
"""
6363
TenantAPIKey.objects.filter(
64-
entity=instance.user, tenant_id=instance.tenant.id
64+
entity_id=instance.user_id, tenant_id=instance.tenant_id
6565
).update(revoked=True)
6666

6767

api/src/backend/conftest.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ def disable_logging():
111111
logging.disable(logging.CRITICAL)
112112

113113

114-
@pytest.fixture(scope="session", autouse=True)
115-
def create_test_user(django_db_setup, django_db_blocker):
114+
@pytest.fixture(scope="session")
115+
def _session_test_user(django_db_setup, django_db_blocker):
116+
"""Create the test user once per session. Internal; use create_test_user instead."""
116117
with django_db_blocker.unblock():
117118
user = User.objects.create_user(
118119
name="testing",
@@ -122,6 +123,21 @@ def create_test_user(django_db_setup, django_db_blocker):
122123
return user
123124

124125

126+
@pytest.fixture(autouse=True)
127+
def create_test_user(_session_test_user, django_db_blocker):
128+
"""Re-create the session-scoped test user when a TransactionTestCase
129+
has truncated the users table."""
130+
with django_db_blocker.unblock():
131+
if not User.objects.filter(pk=_session_test_user.pk).exists():
132+
User.objects.create_user(
133+
id=_session_test_user.pk,
134+
name="testing",
135+
email=TEST_USER,
136+
password=TEST_PASSWORD,
137+
)
138+
return _session_test_user
139+
140+
125141
@pytest.fixture(scope="function")
126142
def create_test_user_rbac(django_db_setup, django_db_blocker, tenants_fixture):
127143
with django_db_blocker.unblock():

0 commit comments

Comments
 (0)