mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-04 10:14:24 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e0a4e5bdb |
No files matched your search
@@ -1330,6 +1330,11 @@ class SavedViewFilterRuleSerializer(serializers.ModelSerializer[SavedViewFilterR
|
|||||||
|
|
||||||
class SavedViewSerializer(OwnedObjectSerializer):
|
class SavedViewSerializer(OwnedObjectSerializer):
|
||||||
filter_rules = SavedViewFilterRuleSerializer(many=True)
|
filter_rules = SavedViewFilterRuleSerializer(many=True)
|
||||||
|
display_fields = serializers.ListField(
|
||||||
|
child=serializers.CharField(),
|
||||||
|
required=False,
|
||||||
|
allow_null=True,
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = SavedView
|
model = SavedView
|
||||||
@@ -1471,11 +1476,13 @@ class SavedViewSerializer(OwnedObjectSerializer):
|
|||||||
attrs = super().validate(attrs)
|
attrs = super().validate(attrs)
|
||||||
if "display_fields" in attrs and attrs["display_fields"] is not None:
|
if "display_fields" in attrs and attrs["display_fields"] is not None:
|
||||||
for field in attrs["display_fields"]:
|
for field in attrs["display_fields"]:
|
||||||
if (
|
# i.e. 'custom_field_' followed by the id of a custom field
|
||||||
SavedView.DisplayFields.CUSTOM_FIELD[:-2] in field
|
custom_field = re.fullmatch(
|
||||||
): # i.e. check for 'custom_field_' prefix
|
rf"{re.escape(SavedView.DisplayFields.CUSTOM_FIELD[:-2])}(\d+)",
|
||||||
field_id = int(re.search(r"\d+", field)[0])
|
field,
|
||||||
if not CustomField.objects.filter(id=field_id).exists():
|
)
|
||||||
|
if custom_field:
|
||||||
|
if not CustomField.objects.filter(id=custom_field[1]).exists():
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
f"Invalid field: {field}",
|
f"Invalid field: {field}",
|
||||||
)
|
)
|
||||||
@@ -3238,14 +3245,10 @@ class WorkflowTriggerSerializer(serializers.ModelSerializer[WorkflowTrigger]):
|
|||||||
]
|
]
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
# The id is only meaningful when this is nested in a workflow. On its own
|
|
||||||
# a client chosen id would collide with, or overwrite, another row
|
|
||||||
validated_data.pop("id", None)
|
|
||||||
WorkflowTriggerSerializer.normalize_workflow_trigger_sources(validated_data)
|
WorkflowTriggerSerializer.normalize_workflow_trigger_sources(validated_data)
|
||||||
return super().create(validated_data)
|
return super().create(validated_data)
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
def update(self, instance, validated_data):
|
||||||
validated_data.pop("id", None)
|
|
||||||
WorkflowTriggerSerializer.normalize_workflow_trigger_sources(validated_data)
|
WorkflowTriggerSerializer.normalize_workflow_trigger_sources(validated_data)
|
||||||
return super().update(instance, validated_data)
|
return super().update(instance, validated_data)
|
||||||
|
|
||||||
@@ -3287,34 +3290,6 @@ class WorkflowActionWebhookSerializer(
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def save_action_email_and_webhook(action, email_data, webhook_data) -> None:
|
|
||||||
"""
|
|
||||||
Store the nested email and webhook of a workflow action on the action's own rows.
|
|
||||||
An id in the payload is never used, it could name a row that belongs to another
|
|
||||||
action. An action with no row of that kind gets a new one. The data must already
|
|
||||||
be cleaned by the nested serializers, an unknown key would be a model error.
|
|
||||||
"""
|
|
||||||
created = False
|
|
||||||
for data, model, attribute in (
|
|
||||||
(email_data, WorkflowActionEmail, "email"),
|
|
||||||
(webhook_data, WorkflowActionWebhook, "webhook"),
|
|
||||||
):
|
|
||||||
if data is None:
|
|
||||||
continue
|
|
||||||
values = {key: value for key, value in data.items() if key != "id"}
|
|
||||||
existing = getattr(action, attribute)
|
|
||||||
if existing is not None:
|
|
||||||
# Change the loaded row, a response is built from it
|
|
||||||
for key, value in values.items():
|
|
||||||
setattr(existing, key, value)
|
|
||||||
existing.save()
|
|
||||||
else:
|
|
||||||
setattr(action, attribute, model.objects.create(**values))
|
|
||||||
created = True
|
|
||||||
if created:
|
|
||||||
action.save()
|
|
||||||
|
|
||||||
|
|
||||||
class WorkflowActionSerializer(serializers.ModelSerializer[WorkflowAction]):
|
class WorkflowActionSerializer(serializers.ModelSerializer[WorkflowAction]):
|
||||||
id = serializers.IntegerField(required=False, allow_null=True)
|
id = serializers.IntegerField(required=False, allow_null=True)
|
||||||
assign_correspondent = CorrespondentField(allow_null=True, required=False)
|
assign_correspondent = CorrespondentField(allow_null=True, required=False)
|
||||||
@@ -3366,22 +3341,6 @@ class WorkflowActionSerializer(serializers.ModelSerializer[WorkflowAction]):
|
|||||||
"ai_overwrite_existing",
|
"ai_overwrite_existing",
|
||||||
]
|
]
|
||||||
|
|
||||||
def create(self, validated_data):
|
|
||||||
validated_data.pop("id", None)
|
|
||||||
email_data = validated_data.pop("email", None)
|
|
||||||
webhook_data = validated_data.pop("webhook", None)
|
|
||||||
instance = super().create(validated_data)
|
|
||||||
save_action_email_and_webhook(instance, email_data, webhook_data)
|
|
||||||
return instance
|
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
|
||||||
validated_data.pop("id", None)
|
|
||||||
email_data = validated_data.pop("email", None)
|
|
||||||
webhook_data = validated_data.pop("webhook", None)
|
|
||||||
instance = super().update(instance, validated_data)
|
|
||||||
save_action_email_and_webhook(instance, email_data, webhook_data)
|
|
||||||
return instance
|
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
if "assign_title" in attrs and attrs["assign_title"] is not None:
|
if "assign_title" in attrs and attrs["assign_title"] is not None:
|
||||||
if len(attrs["assign_title"]) == 0:
|
if len(attrs["assign_title"]) == 0:
|
||||||
@@ -3544,10 +3503,6 @@ class WorkflowSerializer(serializers.ModelSerializer[Workflow]):
|
|||||||
) -> None:
|
) -> None:
|
||||||
set_triggers = []
|
set_triggers = []
|
||||||
set_actions = []
|
set_actions = []
|
||||||
# Only rows already attached to this workflow may be updated by id, an id
|
|
||||||
# from anywhere else would take over or overwrite another workflow's row
|
|
||||||
own_trigger_ids = set(instance.triggers.values_list("pk", flat=True))
|
|
||||||
own_action_ids = set(instance.actions.values_list("pk", flat=True))
|
|
||||||
|
|
||||||
if triggers is not None and triggers is not serializers.empty:
|
if triggers is not None and triggers is not serializers.empty:
|
||||||
for trigger in triggers:
|
for trigger in triggers:
|
||||||
@@ -3580,10 +3535,8 @@ class WorkflowSerializer(serializers.ModelSerializer[Workflow]):
|
|||||||
)
|
)
|
||||||
# Convert sources to strings to handle django-multiselectfield v1.0 changes
|
# Convert sources to strings to handle django-multiselectfield v1.0 changes
|
||||||
WorkflowTriggerSerializer.normalize_workflow_trigger_sources(trigger)
|
WorkflowTriggerSerializer.normalize_workflow_trigger_sources(trigger)
|
||||||
trigger_id = trigger.pop("id", None)
|
|
||||||
trigger_instance, _ = WorkflowTrigger.objects.update_or_create(
|
trigger_instance, _ = WorkflowTrigger.objects.update_or_create(
|
||||||
# A row that is not already part of this workflow is never reused
|
id=trigger.get("id"),
|
||||||
id=trigger_id if trigger_id in own_trigger_ids else None,
|
|
||||||
defaults=trigger,
|
defaults=trigger,
|
||||||
)
|
)
|
||||||
if filter_has_tags is not None:
|
if filter_has_tags is not None:
|
||||||
@@ -3641,24 +3594,30 @@ class WorkflowSerializer(serializers.ModelSerializer[Workflow]):
|
|||||||
email_data = action.pop("email", None)
|
email_data = action.pop("email", None)
|
||||||
webhook_data = action.pop("webhook", None)
|
webhook_data = action.pop("webhook", None)
|
||||||
|
|
||||||
action_id = action.pop("id", None)
|
|
||||||
action_instance, _ = WorkflowAction.objects.update_or_create(
|
action_instance, _ = WorkflowAction.objects.update_or_create(
|
||||||
# A row that is not already part of this workflow is never reused
|
id=action.get("id"),
|
||||||
id=action_id if action_id in own_action_ids else None,
|
|
||||||
defaults=action,
|
defaults=action,
|
||||||
)
|
)
|
||||||
|
|
||||||
if email_data is not None:
|
if email_data is not None:
|
||||||
serializer = WorkflowActionEmailSerializer(data=email_data)
|
serializer = WorkflowActionEmailSerializer(data=email_data)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
email_data = serializer.validated_data
|
email, _ = WorkflowActionEmail.objects.update_or_create(
|
||||||
|
id=email_data.get("id"),
|
||||||
|
defaults=serializer.validated_data,
|
||||||
|
)
|
||||||
|
action_instance.email = email
|
||||||
|
action_instance.save()
|
||||||
|
|
||||||
if webhook_data is not None:
|
if webhook_data is not None:
|
||||||
serializer = WorkflowActionWebhookSerializer(data=webhook_data)
|
serializer = WorkflowActionWebhookSerializer(data=webhook_data)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
webhook_data = serializer.validated_data
|
webhook, _ = WorkflowActionWebhook.objects.update_or_create(
|
||||||
|
id=webhook_data.get("id"),
|
||||||
save_action_email_and_webhook(action_instance, email_data, webhook_data)
|
defaults=serializer.validated_data,
|
||||||
|
)
|
||||||
|
action_instance.webhook = webhook
|
||||||
|
action_instance.save()
|
||||||
|
|
||||||
if assign_tags is not None:
|
if assign_tags is not None:
|
||||||
action_instance.assign_tags.set(assign_tags)
|
action_instance.assign_tags.set(assign_tags)
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import pytest
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.test import APIClient
|
||||||
|
|
||||||
|
from documents.models import CustomField
|
||||||
|
from documents.models import SavedView
|
||||||
|
|
||||||
|
|
||||||
|
class TestSavedViewDisplayFields:
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"value",
|
||||||
|
[
|
||||||
|
pytest.param(["custom_field_abc"], id="no-id"),
|
||||||
|
pytest.param(["custom_field_"], id="empty-id"),
|
||||||
|
pytest.param(["xcustom_field_{id}"], id="prefixed"),
|
||||||
|
pytest.param(["custom_field_{id}x"], id="suffixed"),
|
||||||
|
pytest.param([1], id="not-a-string"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_malformed_display_fields_are_a_400(
|
||||||
|
self,
|
||||||
|
admin_client: APIClient,
|
||||||
|
admin_user: User,
|
||||||
|
value: object,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
GIVEN:
|
||||||
|
- A saved view and a custom field
|
||||||
|
WHEN:
|
||||||
|
- Its display fields are patched to something that names no field
|
||||||
|
THEN:
|
||||||
|
- The response is a 400 and the stored display fields are unchanged
|
||||||
|
"""
|
||||||
|
custom_field = CustomField.objects.create(
|
||||||
|
name="cf",
|
||||||
|
data_type=CustomField.FieldDataType.STRING,
|
||||||
|
)
|
||||||
|
view = SavedView.objects.create(
|
||||||
|
name="view",
|
||||||
|
owner=admin_user,
|
||||||
|
sort_field="created",
|
||||||
|
display_fields=["title"],
|
||||||
|
)
|
||||||
|
if isinstance(value, list):
|
||||||
|
value = [
|
||||||
|
item.format(id=custom_field.pk) if isinstance(item, str) else item
|
||||||
|
for item in value
|
||||||
|
]
|
||||||
|
|
||||||
|
response = admin_client.patch(
|
||||||
|
f"/api/saved_views/{view.pk}/",
|
||||||
|
{"display_fields": value},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||||
|
view.refresh_from_db()
|
||||||
|
assert view.display_fields == ["title"]
|
||||||
|
|
||||||
|
def test_a_custom_field_display_name_is_accepted(
|
||||||
|
self,
|
||||||
|
admin_client: APIClient,
|
||||||
|
admin_user: User,
|
||||||
|
) -> None:
|
||||||
|
custom_field = CustomField.objects.create(
|
||||||
|
name="cf",
|
||||||
|
data_type=CustomField.FieldDataType.STRING,
|
||||||
|
)
|
||||||
|
view = SavedView.objects.create(
|
||||||
|
name="view",
|
||||||
|
owner=admin_user,
|
||||||
|
sort_field="created",
|
||||||
|
)
|
||||||
|
|
||||||
|
response = admin_client.patch(
|
||||||
|
f"/api/saved_views/{view.pk}/",
|
||||||
|
{"display_fields": ["title", f"custom_field_{custom_field.pk}"]},
|
||||||
|
format="json",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == status.HTTP_200_OK
|
||||||
@@ -1,308 +0,0 @@
|
|||||||
import json
|
|
||||||
|
|
||||||
from django.http import HttpResponse
|
|
||||||
from rest_framework import status
|
|
||||||
from rest_framework.test import APIClient
|
|
||||||
|
|
||||||
from documents.models import WorkflowAction
|
|
||||||
from documents.models import WorkflowActionEmail
|
|
||||||
from documents.models import WorkflowTrigger
|
|
||||||
|
|
||||||
EMAIL = {
|
|
||||||
"subject": "original",
|
|
||||||
"body": "body",
|
|
||||||
"to": "someone@example.com",
|
|
||||||
"include_document": False,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def workflow_body(name: str, *, trigger: dict, action: dict) -> dict:
|
|
||||||
return {
|
|
||||||
"name": name,
|
|
||||||
"order": 0,
|
|
||||||
"enabled": True,
|
|
||||||
"triggers": [
|
|
||||||
{"type": 2, "sources": [1], "matching_algorithm": 0, **trigger},
|
|
||||||
],
|
|
||||||
"actions": [{"type": 3, "email": EMAIL, **action}],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def send_json(client: APIClient, method: str, path: str, body: object) -> HttpResponse:
|
|
||||||
return client.generic(
|
|
||||||
method,
|
|
||||||
path,
|
|
||||||
data=json.dumps(body),
|
|
||||||
content_type="application/json",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def make_workflow(client: APIClient, name: str) -> dict:
|
|
||||||
response = send_json(
|
|
||||||
client,
|
|
||||||
"POST",
|
|
||||||
"/api/workflows/",
|
|
||||||
workflow_body(name, trigger={}, action={}),
|
|
||||||
)
|
|
||||||
assert response.status_code == status.HTTP_201_CREATED
|
|
||||||
return response.data
|
|
||||||
|
|
||||||
|
|
||||||
class TestWorkflowRowOwnership:
|
|
||||||
def test_update_cannot_take_over_another_workflows_action(
|
|
||||||
self,
|
|
||||||
admin_client: APIClient,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- Two workflows, each with an email action
|
|
||||||
WHEN:
|
|
||||||
- The first is updated with the id of the second one's action and a changed
|
|
||||||
email
|
|
||||||
THEN:
|
|
||||||
- The second workflow's action and email are untouched, the first gets its
|
|
||||||
own action
|
|
||||||
"""
|
|
||||||
first = make_workflow(admin_client, "first")
|
|
||||||
second = make_workflow(admin_client, "second")
|
|
||||||
foreign_action = second["actions"][0]
|
|
||||||
foreign_email_id = foreign_action["email"]["id"]
|
|
||||||
|
|
||||||
response = send_json(
|
|
||||||
admin_client,
|
|
||||||
"PUT",
|
|
||||||
f"/api/workflows/{first['id']}/",
|
|
||||||
workflow_body(
|
|
||||||
"first",
|
|
||||||
trigger={"id": first["triggers"][0]["id"]},
|
|
||||||
action={
|
|
||||||
"id": foreign_action["id"],
|
|
||||||
"email": {**EMAIL, "subject": "taken over"},
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == status.HTTP_200_OK
|
|
||||||
assert response.data["actions"][0]["id"] != foreign_action["id"]
|
|
||||||
assert WorkflowActionEmail.objects.get(pk=foreign_email_id).subject == (
|
|
||||||
"original"
|
|
||||||
)
|
|
||||||
second_actions = list(
|
|
||||||
WorkflowAction.objects.filter(workflows__pk=second["id"]),
|
|
||||||
)
|
|
||||||
assert [a.pk for a in second_actions] == [foreign_action["id"]]
|
|
||||||
|
|
||||||
def test_update_cannot_take_over_another_workflows_trigger(
|
|
||||||
self,
|
|
||||||
admin_client: APIClient,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- Two workflows
|
|
||||||
WHEN:
|
|
||||||
- The first is updated with the id of the second one's trigger and a
|
|
||||||
different filter
|
|
||||||
THEN:
|
|
||||||
- The second workflow's trigger is untouched
|
|
||||||
"""
|
|
||||||
first = make_workflow(admin_client, "first")
|
|
||||||
second = make_workflow(admin_client, "second")
|
|
||||||
foreign_trigger_id = second["triggers"][0]["id"]
|
|
||||||
|
|
||||||
response = send_json(
|
|
||||||
admin_client,
|
|
||||||
"PUT",
|
|
||||||
f"/api/workflows/{first['id']}/",
|
|
||||||
workflow_body(
|
|
||||||
"first",
|
|
||||||
trigger={"id": foreign_trigger_id, "filter_filename": "taken"},
|
|
||||||
action={"id": first["actions"][0]["id"]},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == status.HTTP_200_OK
|
|
||||||
assert response.data["triggers"][0]["id"] != foreign_trigger_id
|
|
||||||
assert WorkflowTrigger.objects.get(pk=foreign_trigger_id).filter_filename != (
|
|
||||||
"taken"
|
|
||||||
)
|
|
||||||
assert list(
|
|
||||||
WorkflowTrigger.objects.filter(workflows__pk=second["id"]).values_list(
|
|
||||||
"pk",
|
|
||||||
flat=True,
|
|
||||||
),
|
|
||||||
) == [foreign_trigger_id]
|
|
||||||
|
|
||||||
def test_nested_email_id_of_another_action_is_ignored(
|
|
||||||
self,
|
|
||||||
admin_client: APIClient,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- Two workflows with email actions
|
|
||||||
WHEN:
|
|
||||||
- The first is updated, keeping its own action, with the second action's
|
|
||||||
email id in the nested email
|
|
||||||
THEN:
|
|
||||||
- The other email is untouched and the action's own email is the one
|
|
||||||
changed
|
|
||||||
"""
|
|
||||||
first = make_workflow(admin_client, "first")
|
|
||||||
second = make_workflow(admin_client, "second")
|
|
||||||
own_action = first["actions"][0]
|
|
||||||
own_email_id = own_action["email"]["id"]
|
|
||||||
foreign_email_id = second["actions"][0]["email"]["id"]
|
|
||||||
|
|
||||||
response = send_json(
|
|
||||||
admin_client,
|
|
||||||
"PUT",
|
|
||||||
f"/api/workflows/{first['id']}/",
|
|
||||||
workflow_body(
|
|
||||||
"first",
|
|
||||||
trigger={"id": first["triggers"][0]["id"]},
|
|
||||||
action={
|
|
||||||
"id": own_action["id"],
|
|
||||||
"email": {**EMAIL, "id": foreign_email_id, "subject": "mine"},
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == status.HTTP_200_OK
|
|
||||||
assert WorkflowActionEmail.objects.get(pk=foreign_email_id).subject == (
|
|
||||||
"original"
|
|
||||||
)
|
|
||||||
assert response.data["actions"][0]["email"]["id"] == own_email_id
|
|
||||||
assert WorkflowActionEmail.objects.get(pk=own_email_id).subject == "mine"
|
|
||||||
|
|
||||||
def test_nested_email_id_beyond_the_database_range_is_ignored(
|
|
||||||
self,
|
|
||||||
admin_client: APIClient,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- A workflow with an email action
|
|
||||||
WHEN:
|
|
||||||
- It is updated with a nested email id larger than a column can hold
|
|
||||||
THEN:
|
|
||||||
- The request succeeds and the action keeps its own email row
|
|
||||||
"""
|
|
||||||
workflow = make_workflow(admin_client, "workflow")
|
|
||||||
action = workflow["actions"][0]
|
|
||||||
|
|
||||||
response = send_json(
|
|
||||||
admin_client,
|
|
||||||
"PUT",
|
|
||||||
f"/api/workflows/{workflow['id']}/",
|
|
||||||
workflow_body(
|
|
||||||
"workflow",
|
|
||||||
trigger={"id": workflow["triggers"][0]["id"]},
|
|
||||||
action={
|
|
||||||
"id": action["id"],
|
|
||||||
"email": {**EMAIL, "id": 2**63},
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == status.HTTP_200_OK
|
|
||||||
assert response.data["actions"][0]["email"]["id"] == action["email"]["id"]
|
|
||||||
|
|
||||||
def test_create_ignores_ids_of_existing_rows(
|
|
||||||
self,
|
|
||||||
admin_client: APIClient,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- An existing workflow
|
|
||||||
WHEN:
|
|
||||||
- A new workflow is created that carries that workflow's trigger and
|
|
||||||
action ids
|
|
||||||
THEN:
|
|
||||||
- New rows are created and the existing ones are untouched
|
|
||||||
"""
|
|
||||||
existing = make_workflow(admin_client, "existing")
|
|
||||||
|
|
||||||
response = send_json(
|
|
||||||
admin_client,
|
|
||||||
"POST",
|
|
||||||
"/api/workflows/",
|
|
||||||
workflow_body(
|
|
||||||
"new",
|
|
||||||
trigger={
|
|
||||||
"id": existing["triggers"][0]["id"],
|
|
||||||
"filter_filename": "taken",
|
|
||||||
},
|
|
||||||
action={"id": existing["actions"][0]["id"]},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == status.HTTP_201_CREATED
|
|
||||||
assert response.data["triggers"][0]["id"] != existing["triggers"][0]["id"]
|
|
||||||
assert response.data["actions"][0]["id"] != existing["actions"][0]["id"]
|
|
||||||
assert WorkflowTrigger.objects.get(
|
|
||||||
pk=existing["triggers"][0]["id"],
|
|
||||||
).filter_filename != ("taken")
|
|
||||||
|
|
||||||
def test_editing_a_workflow_keeps_its_row_ids(
|
|
||||||
self,
|
|
||||||
admin_client: APIClient,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- A workflow read back from the API, as the UI does before an edit
|
|
||||||
WHEN:
|
|
||||||
- It is sent back with a changed name and email subject
|
|
||||||
THEN:
|
|
||||||
- Its trigger, action and email keep their ids and no rows are added
|
|
||||||
"""
|
|
||||||
workflow = make_workflow(admin_client, "workflow")
|
|
||||||
body = admin_client.get(f"/api/workflows/{workflow['id']}/").data
|
|
||||||
body = json.loads(json.dumps(body))
|
|
||||||
body["name"] = "renamed"
|
|
||||||
body["actions"][0]["email"]["subject"] = "edited"
|
|
||||||
|
|
||||||
response = send_json(
|
|
||||||
admin_client,
|
|
||||||
"PUT",
|
|
||||||
f"/api/workflows/{workflow['id']}/",
|
|
||||||
body,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == status.HTTP_200_OK
|
|
||||||
assert response.data["triggers"][0]["id"] == workflow["triggers"][0]["id"]
|
|
||||||
assert response.data["actions"][0]["id"] == workflow["actions"][0]["id"]
|
|
||||||
assert (
|
|
||||||
response.data["actions"][0]["email"]["id"]
|
|
||||||
== (workflow["actions"][0]["email"]["id"])
|
|
||||||
)
|
|
||||||
assert response.data["actions"][0]["email"]["subject"] == "edited"
|
|
||||||
assert WorkflowAction.objects.count() == 1
|
|
||||||
assert WorkflowActionEmail.objects.count() == 1
|
|
||||||
assert WorkflowTrigger.objects.count() == 1
|
|
||||||
|
|
||||||
def test_a_new_action_in_an_update_is_added_beside_the_existing_one(
|
|
||||||
self,
|
|
||||||
admin_client: APIClient,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
GIVEN:
|
|
||||||
- A workflow with one action
|
|
||||||
WHEN:
|
|
||||||
- It is updated with that action plus a new one without an id
|
|
||||||
THEN:
|
|
||||||
- The first action keeps its id and a second one is created
|
|
||||||
"""
|
|
||||||
workflow = make_workflow(admin_client, "workflow")
|
|
||||||
body = json.loads(
|
|
||||||
json.dumps(admin_client.get(f"/api/workflows/{workflow['id']}/").data),
|
|
||||||
)
|
|
||||||
body["actions"].append({"type": 1, "assign_title": "added"})
|
|
||||||
|
|
||||||
response = send_json(
|
|
||||||
admin_client,
|
|
||||||
"PUT",
|
|
||||||
f"/api/workflows/{workflow['id']}/",
|
|
||||||
body,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == status.HTTP_200_OK
|
|
||||||
assert len(response.data["actions"]) == 2
|
|
||||||
assert response.data["actions"][0]["id"] == workflow["actions"][0]["id"]
|
|
||||||
Reference in new issue
Block a user