openforce/tests/flow_engine/test_rollback.py

41 lines
1.6 KiB
Python

import pytest
from rest_framework.test import APIClient
from django.contrib.auth.models import User
from crm_core.models import Account
from workflows.models import FlowDefinition
@pytest.mark.django_db(transaction=True)
def test_flow_failure_rolls_back_account_via_viewset_endpoint():
"""
Test that an active record-triggered flow failure rolls back the triggering record
when calling the AccountViewSet POST endpoint directly, WITHOUT any manual transaction.atomic wrapper in test!
"""
failing_dag = {
"nodes": [
{"id": "start", "type": "trigger", "data": {}},
{"id": "step_fail", "type": "subflow", "data": {"flow_id": 999999}}
],
"edges": [
{"source": "start", "target": "step_fail"}
]
}
FlowDefinition.objects.create(
name="Failing Trigger Flow",
trigger_type="record_created",
trigger_object="Account",
is_active=True,
graph=failing_dag
)
user = User.objects.create_user(username="test_rollback_user", password="password")
client = APIClient()
client.force_authenticate(user=user)
account_name = "Doomed Endpoint Account"
# Call the actual DRF endpoint without any transaction wrapper in the test
with pytest.raises(Exception):
client.post('/api/accounts/', {'name': account_name}, format='json')
# Verify that the Account was rolled back and does not exist in the database!
exists = Account.objects.filter(name=account_name).exists()
assert not exists, "Account creation via API endpoint MUST roll back when record-triggered flow fails!"