mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-12-03 01:44:49 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4372554414 |
No files matched your search
@@ -68,6 +68,7 @@ class MailRuleSerializer(OwnedObjectSerializer):
|
||||
account = AccountField(required=True)
|
||||
action_parameter = serializers.CharField(
|
||||
allow_null=True,
|
||||
allow_blank=True,
|
||||
required=False,
|
||||
default="",
|
||||
)
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
from typing import Final
|
||||
|
||||
import pytest
|
||||
from rest_framework import status
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
from paperless_mail.models import MailAccount
|
||||
from paperless_mail.models import MailRule
|
||||
from paperless_mail.tests.factories import MailAccountFactory
|
||||
|
||||
ENDPOINT: Final = "/api/mail_rules/"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
class TestMailRuleActionParameter:
|
||||
@pytest.fixture
|
||||
def account(self) -> MailAccount:
|
||||
return MailAccountFactory.create()
|
||||
|
||||
def test_rule_without_parameter_round_trips(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
account: MailAccount,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- A rule created without an action_parameter
|
||||
WHEN:
|
||||
- The returned rule is sent back unchanged with PUT
|
||||
THEN:
|
||||
- The update succeeds and the parameter stays blank
|
||||
"""
|
||||
created = admin_client.post(
|
||||
ENDPOINT,
|
||||
{
|
||||
"name": "omitted",
|
||||
"account": account.pk,
|
||||
"folder": "INBOX",
|
||||
"action": MailRule.MailAction.FLAG,
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
assert created.status_code == status.HTTP_201_CREATED
|
||||
assert created.data["action_parameter"] == ""
|
||||
|
||||
updated = admin_client.put(
|
||||
f"{ENDPOINT}{created.data['id']}/",
|
||||
created.data,
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert updated.status_code == status.HTTP_200_OK
|
||||
assert updated.data["action_parameter"] == ""
|
||||
|
||||
def test_create_with_blank_parameter(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
account: MailAccount,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- An action which takes no parameter
|
||||
WHEN:
|
||||
- A rule is created with a blank action_parameter
|
||||
THEN:
|
||||
- The rule is created
|
||||
"""
|
||||
response = admin_client.post(
|
||||
ENDPOINT,
|
||||
{
|
||||
"name": "blank",
|
||||
"account": account.pk,
|
||||
"folder": "INBOX",
|
||||
"action": MailRule.MailAction.FLAG,
|
||||
"action_parameter": "",
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_201_CREATED
|
||||
assert response.data["action_parameter"] == ""
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"action",
|
||||
[
|
||||
pytest.param(MailRule.MailAction.TAG, id="tag"),
|
||||
pytest.param(MailRule.MailAction.MOVE, id="move"),
|
||||
],
|
||||
)
|
||||
def test_blank_parameter_rejected_when_required(
|
||||
self,
|
||||
admin_client: APIClient,
|
||||
account: MailAccount,
|
||||
action: MailRule.MailAction,
|
||||
) -> None:
|
||||
"""
|
||||
GIVEN:
|
||||
- An action which requires a parameter
|
||||
WHEN:
|
||||
- A rule is created with a blank action_parameter
|
||||
THEN:
|
||||
- The request is rejected
|
||||
"""
|
||||
response = admin_client.post(
|
||||
ENDPOINT,
|
||||
{
|
||||
"name": "blank",
|
||||
"account": account.pk,
|
||||
"folder": "INBOX",
|
||||
"action": action,
|
||||
"action_parameter": "",
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
|
||||
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||
Reference in new issue
Block a user