fastsmtp-cli 0.8.0__tar.gz → 0.9.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/PKG-INFO +1 -1
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/pyproject.toml +1 -1
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/__init__.py +1 -1
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/client.py +45 -20
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/commands/rules.py +164 -18
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/output.py +38 -10
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/tests/test_api_contract.py +15 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/tests/test_commands_reconciliation.py +250 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/tests/test_output.py +3 -1
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/.gitignore +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/LICENSE +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/README.md +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/commands/__init__.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/commands/auth.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/commands/config.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/commands/domains.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/commands/operations.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/commands/options.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/commands/recipients.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/commands/users.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/config.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/src/fastsmtp_cli/main.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/tests/conftest.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/tests/test_cli_config.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/tests/test_client.py +0 -0
- {fastsmtp_cli-0.8.0 → fastsmtp_cli-0.9.0}/tests/test_commands.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: fastsmtp-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.9.0
|
|
4
4
|
Summary: Remote CLI client for FastSMTP server
|
|
5
5
|
Project-URL: Homepage, https://jsenecal.github.io/fastsmtp/cli/fsmtp/
|
|
6
6
|
Project-URL: Documentation, https://jsenecal.github.io/fastsmtp/cli/fsmtp/
|
|
@@ -90,6 +90,29 @@ def _flags(**flags: bool) -> dict[str, str]:
|
|
|
90
90
|
return {name: "true" for name, value in flags.items() if value}
|
|
91
91
|
|
|
92
92
|
|
|
93
|
+
def _put_rule_conditions(
|
|
94
|
+
data: dict[str, Any],
|
|
95
|
+
field: str | None,
|
|
96
|
+
operator: str | None,
|
|
97
|
+
value: str | None,
|
|
98
|
+
case_sensitive: bool | None,
|
|
99
|
+
conditions: list[dict[str, Any]] | None,
|
|
100
|
+
match: str | None,
|
|
101
|
+
) -> None:
|
|
102
|
+
"""Add a rule's test to a request body: whichever shape the caller gave.
|
|
103
|
+
|
|
104
|
+
Only what was given is sent. The server refuses the flat fields and
|
|
105
|
+
``conditions`` together, so that check stays on the server rather than
|
|
106
|
+
being duplicated here.
|
|
107
|
+
"""
|
|
108
|
+
flat = {"field": field, "operator": operator, "value": value, "case_sensitive": case_sensitive}
|
|
109
|
+
data.update({name: item for name, item in flat.items() if item is not None})
|
|
110
|
+
if conditions is not None:
|
|
111
|
+
data["conditions"] = conditions
|
|
112
|
+
if match is not None:
|
|
113
|
+
data["match"] = match
|
|
114
|
+
|
|
115
|
+
|
|
93
116
|
class FastSMTPClient:
|
|
94
117
|
"""HTTP client for FastSMTP API."""
|
|
95
118
|
|
|
@@ -525,28 +548,30 @@ class FastSMTPClient:
|
|
|
525
548
|
self,
|
|
526
549
|
domain_id: UUID | str,
|
|
527
550
|
ruleset_id: UUID | str,
|
|
528
|
-
field: str,
|
|
529
|
-
operator: str,
|
|
530
|
-
value: str,
|
|
551
|
+
field: str | None = None,
|
|
552
|
+
operator: str | None = None,
|
|
553
|
+
value: str | None = None,
|
|
531
554
|
action: str = "forward",
|
|
532
|
-
case_sensitive: bool =
|
|
555
|
+
case_sensitive: bool | None = None,
|
|
533
556
|
webhook_url_override: str | None = None,
|
|
534
557
|
add_tags: list[str] | None = None,
|
|
535
558
|
preserve_raw: bool = False,
|
|
559
|
+
conditions: list[dict[str, Any]] | None = None,
|
|
560
|
+
match: str | None = None,
|
|
536
561
|
) -> dict:
|
|
537
562
|
"""Create a new rule.
|
|
538
563
|
|
|
564
|
+
Give either ``conditions`` (each a dict as built by
|
|
565
|
+
``fastsmtp_cli.commands.rules.parse_condition``) or the flat
|
|
566
|
+
``field``/``operator``/``value``/``case_sensitive`` one-condition
|
|
567
|
+
shape, which every server version accepts. The server refuses both at
|
|
568
|
+
once. ``match`` (``all`` or ``any``) defaults to ``all`` server-side.
|
|
569
|
+
|
|
539
570
|
The rule is appended to the end of the ruleset; use
|
|
540
571
|
:meth:`reorder_rules` to change evaluation order.
|
|
541
572
|
"""
|
|
542
|
-
data: dict[str, Any] = {
|
|
543
|
-
|
|
544
|
-
"operator": operator,
|
|
545
|
-
"value": value,
|
|
546
|
-
"action": action,
|
|
547
|
-
"case_sensitive": case_sensitive,
|
|
548
|
-
"preserve_raw": preserve_raw,
|
|
549
|
-
}
|
|
573
|
+
data: dict[str, Any] = {"action": action, "preserve_raw": preserve_raw}
|
|
574
|
+
_put_rule_conditions(data, field, operator, value, case_sensitive, conditions, match)
|
|
550
575
|
if webhook_url_override is not None:
|
|
551
576
|
data["webhook_url_override"] = webhook_url_override
|
|
552
577
|
if add_tags:
|
|
@@ -584,24 +609,24 @@ class FastSMTPClient:
|
|
|
584
609
|
webhook_url_override: NullableStr = UNSET,
|
|
585
610
|
add_tags: list[str] | None = None,
|
|
586
611
|
preserve_raw: bool | None = None,
|
|
612
|
+
conditions: list[dict[str, Any]] | None = None,
|
|
613
|
+
match: str | None = None,
|
|
587
614
|
) -> dict:
|
|
588
615
|
"""Update a rule.
|
|
589
616
|
|
|
617
|
+
``conditions`` replaces the rule's whole condition list. The flat
|
|
618
|
+
``field``/``operator``/``value``/``case_sensitive`` edit a rule's only
|
|
619
|
+
condition in place, and the server refuses them on a rule holding
|
|
620
|
+
several conditions or a negated one.
|
|
621
|
+
|
|
590
622
|
``webhook_url_override`` is three-valued: a string sets it, ``None``
|
|
591
623
|
clears the override (sent as JSON null) so the rule falls back to the
|
|
592
624
|
recipient's webhook URL, ``UNSET`` leaves it untouched.
|
|
593
625
|
"""
|
|
594
626
|
data: dict[str, Any] = {}
|
|
595
|
-
|
|
596
|
-
data["field"] = field
|
|
597
|
-
if operator is not None:
|
|
598
|
-
data["operator"] = operator
|
|
599
|
-
if value is not None:
|
|
600
|
-
data["value"] = value
|
|
627
|
+
_put_rule_conditions(data, field, operator, value, case_sensitive, conditions, match)
|
|
601
628
|
if action is not None:
|
|
602
629
|
data["action"] = action
|
|
603
|
-
if case_sensitive is not None:
|
|
604
|
-
data["case_sensitive"] = case_sensitive
|
|
605
630
|
if not isinstance(webhook_url_override, _Unset):
|
|
606
631
|
data["webhook_url_override"] = webhook_url_override
|
|
607
632
|
if add_tags is not None:
|
|
@@ -4,9 +4,15 @@ Rulesets and rules are nested under a domain on the server, so every command
|
|
|
4
4
|
here takes the domain ID first. Rules have no name, no per-rule ``priority`` and
|
|
5
5
|
no ``is_enabled``: their evaluation order is the ruleset's ``order``, changed
|
|
6
6
|
with ``fsmtp rules rule reorder``.
|
|
7
|
+
|
|
8
|
+
A rule tests 1 to 16 conditions combined by ``--match all|any`` (issue #182),
|
|
9
|
+
given as repeatable ``--condition 'FIELD OPERATOR [VALUE]'`` and
|
|
10
|
+
``--not-condition`` options. ``--field/--operator/--value`` remain as the
|
|
11
|
+
one-condition shorthand, sent in the flat shape every server version accepts.
|
|
7
12
|
"""
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
import re
|
|
15
|
+
from typing import Annotated, Any
|
|
10
16
|
|
|
11
17
|
import typer
|
|
12
18
|
|
|
@@ -38,6 +44,9 @@ VALID_FIELDS = (
|
|
|
38
44
|
FIELD_HEADER_PREFIX = "header:"
|
|
39
45
|
VALID_OPERATORS = ("equals", "contains", "regex", "starts_with", "ends_with", "exists")
|
|
40
46
|
VALID_ACTIONS = ("forward", "drop", "tag", "quarantine")
|
|
47
|
+
VALID_MATCH_MODES = ("all", "any")
|
|
48
|
+
|
|
49
|
+
CONDITION_FORMAT = "'FIELD OPERATOR [VALUE]'"
|
|
41
50
|
|
|
42
51
|
|
|
43
52
|
def _validate_field(field: str | None) -> None:
|
|
@@ -67,6 +76,95 @@ def _validate_action(action: str | None) -> None:
|
|
|
67
76
|
raise typer.Exit(1)
|
|
68
77
|
|
|
69
78
|
|
|
79
|
+
def _validate_match(match: str | None) -> None:
|
|
80
|
+
"""Reject match modes the server would reject."""
|
|
81
|
+
if match is None or match in VALID_MATCH_MODES:
|
|
82
|
+
return
|
|
83
|
+
print_error(f"Invalid match mode. Must be one of: {', '.join(VALID_MATCH_MODES)}")
|
|
84
|
+
raise typer.Exit(1)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# FIELD and OPERATOR are single words; VALUE is everything after exactly one
|
|
88
|
+
# separating space, so a value's own leading spaces survive.
|
|
89
|
+
_CONDITION_RE = re.compile(r"^\s*(\S+)\s+(\S+)(?: (.*))?$", re.DOTALL)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def parse_condition(
|
|
93
|
+
spec: str, negate: bool = False, case_sensitive: bool = False
|
|
94
|
+
) -> dict[str, Any]:
|
|
95
|
+
"""Parse ``FIELD OPERATOR [VALUE]`` into the condition object the API takes.
|
|
96
|
+
|
|
97
|
+
VALUE is everything after the single space that follows the operator,
|
|
98
|
+
leading and inner spaces included, and may be omitted only for ``exists``.
|
|
99
|
+
"""
|
|
100
|
+
parsed = _CONDITION_RE.match(spec)
|
|
101
|
+
if parsed is None:
|
|
102
|
+
print_error(f"Invalid condition '{spec}'. Expected {CONDITION_FORMAT}")
|
|
103
|
+
raise typer.Exit(1)
|
|
104
|
+
field, operator, value = parsed.group(1), parsed.group(2), parsed.group(3) or ""
|
|
105
|
+
_validate_field(field)
|
|
106
|
+
_validate_operator(operator)
|
|
107
|
+
if operator != "exists" and value == "":
|
|
108
|
+
print_error(f"Condition '{spec}' needs a VALUE for operator '{operator}'")
|
|
109
|
+
raise typer.Exit(1)
|
|
110
|
+
return {
|
|
111
|
+
"field": field,
|
|
112
|
+
"operator": operator,
|
|
113
|
+
"value": value,
|
|
114
|
+
"case_sensitive": case_sensitive,
|
|
115
|
+
"negate": negate,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _conditions_from_options(
|
|
120
|
+
conditions: list[str] | None,
|
|
121
|
+
not_conditions: list[str] | None,
|
|
122
|
+
case_sensitive: bool,
|
|
123
|
+
shorthand: list[str | None],
|
|
124
|
+
) -> list[dict[str, Any]] | None:
|
|
125
|
+
"""Build the condition list from the options, or ``None`` if none were given.
|
|
126
|
+
|
|
127
|
+
``--condition`` entries come first, then ``--not-condition`` entries, each
|
|
128
|
+
in the order given: the two options are collected separately, so their
|
|
129
|
+
interleaving on the command line is not preserved. Order only affects which
|
|
130
|
+
condition short-circuits evaluation, never the result.
|
|
131
|
+
"""
|
|
132
|
+
specs = [(spec, False) for spec in conditions or []]
|
|
133
|
+
specs += [(spec, True) for spec in not_conditions or []]
|
|
134
|
+
if not specs:
|
|
135
|
+
return None
|
|
136
|
+
if any(option is not None for option in shorthand):
|
|
137
|
+
print_error("Use --condition/--not-condition or --field/--operator/--value, not both")
|
|
138
|
+
raise typer.Exit(1)
|
|
139
|
+
return [parse_condition(spec, negate, case_sensitive) for spec, negate in specs]
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
MatchOption = Annotated[
|
|
143
|
+
str | None,
|
|
144
|
+
typer.Option(
|
|
145
|
+
"--match",
|
|
146
|
+
"-m",
|
|
147
|
+
help="How conditions combine: all (every one must hold, the default) or any",
|
|
148
|
+
),
|
|
149
|
+
]
|
|
150
|
+
ConditionOption = Annotated[
|
|
151
|
+
list[str] | None,
|
|
152
|
+
typer.Option(
|
|
153
|
+
"--condition",
|
|
154
|
+
"-c",
|
|
155
|
+
help=f"Condition as {CONDITION_FORMAT} (repeatable), e.g. 'from ends_with @example.com'",
|
|
156
|
+
),
|
|
157
|
+
]
|
|
158
|
+
NotConditionOption = Annotated[
|
|
159
|
+
list[str] | None,
|
|
160
|
+
typer.Option(
|
|
161
|
+
"--not-condition",
|
|
162
|
+
help=f"Negated condition as {CONDITION_FORMAT} (repeatable). True when the field is "
|
|
163
|
+
"absent, and for attachments when no part matches",
|
|
164
|
+
),
|
|
165
|
+
]
|
|
166
|
+
|
|
167
|
+
|
|
70
168
|
# RuleSet commands
|
|
71
169
|
@app.command("list")
|
|
72
170
|
def list_rulesets(
|
|
@@ -270,23 +368,25 @@ def get_rule(
|
|
|
270
368
|
def create_rule(
|
|
271
369
|
domain_id: Annotated[str, typer.Argument(help="Domain ID")],
|
|
272
370
|
ruleset_id: Annotated[str, typer.Argument(help="RuleSet ID")],
|
|
371
|
+
match: MatchOption = None,
|
|
372
|
+
conditions: ConditionOption = None,
|
|
373
|
+
not_conditions: NotConditionOption = None,
|
|
273
374
|
field: Annotated[
|
|
274
|
-
str,
|
|
275
|
-
typer.Option("--field", "-F", help="Field to match
|
|
276
|
-
],
|
|
375
|
+
str | None,
|
|
376
|
+
typer.Option("--field", "-F", help="Field to match (one-condition shorthand)"),
|
|
377
|
+
] = None,
|
|
277
378
|
operator: Annotated[
|
|
278
|
-
str,
|
|
379
|
+
str | None,
|
|
279
380
|
typer.Option(
|
|
280
381
|
"--operator",
|
|
281
382
|
"-O",
|
|
282
383
|
help="Operator (equals, contains, regex, starts_with, ends_with, exists)",
|
|
283
|
-
prompt=True,
|
|
284
384
|
),
|
|
285
|
-
],
|
|
385
|
+
] = None,
|
|
286
386
|
value: Annotated[
|
|
287
|
-
str,
|
|
288
|
-
typer.Option("--value", "-V", help="Value to match"
|
|
289
|
-
],
|
|
387
|
+
str | None,
|
|
388
|
+
typer.Option("--value", "-V", help="Value to match"),
|
|
389
|
+
] = None,
|
|
290
390
|
action: Annotated[
|
|
291
391
|
str,
|
|
292
392
|
typer.Option("--action", "-a", help="Action (forward, drop, tag, quarantine)"),
|
|
@@ -315,10 +415,23 @@ def create_rule(
|
|
|
315
415
|
typer.Option("--profile", "-p", help="Profile to use"),
|
|
316
416
|
] = None,
|
|
317
417
|
) -> None:
|
|
318
|
-
"""Create a new rule, appended to the end of the ruleset.
|
|
319
|
-
|
|
320
|
-
|
|
418
|
+
"""Create a new rule, appended to the end of the ruleset.
|
|
419
|
+
|
|
420
|
+
Give the rule's conditions with --condition/--not-condition, or one
|
|
421
|
+
condition with --field/--operator/--value (prompted for when neither is
|
|
422
|
+
given). --case-sensitive applies to every condition.
|
|
423
|
+
"""
|
|
424
|
+
_validate_match(match)
|
|
321
425
|
_validate_action(action)
|
|
426
|
+
condition_list = _conditions_from_options(
|
|
427
|
+
conditions, not_conditions, case_sensitive, [field, operator, value]
|
|
428
|
+
)
|
|
429
|
+
if condition_list is None:
|
|
430
|
+
field = field if field is not None else typer.prompt("Field")
|
|
431
|
+
operator = operator if operator is not None else typer.prompt("Operator")
|
|
432
|
+
value = value if value is not None else typer.prompt("Value")
|
|
433
|
+
_validate_field(field)
|
|
434
|
+
_validate_operator(operator)
|
|
322
435
|
|
|
323
436
|
try:
|
|
324
437
|
with FastSMTPClient(profile_name=profile) as client:
|
|
@@ -329,10 +442,13 @@ def create_rule(
|
|
|
329
442
|
operator=operator,
|
|
330
443
|
value=value,
|
|
331
444
|
action=action,
|
|
332
|
-
|
|
445
|
+
# Inside each condition when there is a list; flat otherwise.
|
|
446
|
+
case_sensitive=None if condition_list else case_sensitive,
|
|
333
447
|
webhook_url_override=webhook_url,
|
|
334
448
|
add_tags=tags,
|
|
335
449
|
preserve_raw=preserve_raw,
|
|
450
|
+
conditions=condition_list,
|
|
451
|
+
match=match,
|
|
336
452
|
)
|
|
337
453
|
print_success("Rule created")
|
|
338
454
|
print_rule(rule)
|
|
@@ -345,9 +461,16 @@ def create_rule(
|
|
|
345
461
|
def update_rule(
|
|
346
462
|
domain_id: Annotated[str, typer.Argument(help="Domain ID")],
|
|
347
463
|
rule_id: Annotated[str, typer.Argument(help="Rule ID")],
|
|
464
|
+
match: MatchOption = None,
|
|
465
|
+
conditions: ConditionOption = None,
|
|
466
|
+
not_conditions: NotConditionOption = None,
|
|
348
467
|
field: Annotated[
|
|
349
468
|
str | None,
|
|
350
|
-
typer.Option(
|
|
469
|
+
typer.Option(
|
|
470
|
+
"--field",
|
|
471
|
+
"-F",
|
|
472
|
+
help="Field to match; edits a single-condition rule in place",
|
|
473
|
+
),
|
|
351
474
|
] = None,
|
|
352
475
|
operator: Annotated[
|
|
353
476
|
str | None,
|
|
@@ -389,15 +512,36 @@ def update_rule(
|
|
|
389
512
|
typer.Option("--profile", "-p", help="Profile to use"),
|
|
390
513
|
] = None,
|
|
391
514
|
) -> None:
|
|
392
|
-
"""Update a rule.
|
|
393
|
-
|
|
515
|
+
"""Update a rule.
|
|
516
|
+
|
|
517
|
+
--condition/--not-condition replace the rule's whole condition list.
|
|
518
|
+
--field/--operator/--value edit the condition of a rule holding exactly
|
|
519
|
+
one, non-negated condition; the server refuses them on any other rule.
|
|
520
|
+
"""
|
|
521
|
+
options = [
|
|
522
|
+
match,
|
|
523
|
+
conditions,
|
|
524
|
+
not_conditions,
|
|
525
|
+
field,
|
|
526
|
+
operator,
|
|
527
|
+
value,
|
|
528
|
+
action,
|
|
529
|
+
case_sensitive,
|
|
530
|
+
webhook_url,
|
|
531
|
+
tags,
|
|
532
|
+
preserve_raw,
|
|
533
|
+
]
|
|
394
534
|
if all(option is None for option in options):
|
|
395
535
|
print_error("At least one option must be provided")
|
|
396
536
|
raise typer.Exit(1)
|
|
397
537
|
|
|
538
|
+
_validate_match(match)
|
|
398
539
|
_validate_field(field)
|
|
399
540
|
_validate_operator(operator)
|
|
400
541
|
_validate_action(action)
|
|
542
|
+
condition_list = _conditions_from_options(
|
|
543
|
+
conditions, not_conditions, bool(case_sensitive), [field, operator, value]
|
|
544
|
+
)
|
|
401
545
|
|
|
402
546
|
try:
|
|
403
547
|
with FastSMTPClient(profile_name=profile) as client:
|
|
@@ -408,10 +552,12 @@ def update_rule(
|
|
|
408
552
|
operator=operator,
|
|
409
553
|
value=value,
|
|
410
554
|
action=action,
|
|
411
|
-
case_sensitive=case_sensitive,
|
|
555
|
+
case_sensitive=None if condition_list else case_sensitive,
|
|
412
556
|
webhook_url_override=clearable_str(webhook_url),
|
|
413
557
|
add_tags=tags,
|
|
414
558
|
preserve_raw=preserve_raw,
|
|
559
|
+
conditions=condition_list,
|
|
560
|
+
match=match,
|
|
415
561
|
)
|
|
416
562
|
print_success("Rule updated")
|
|
417
563
|
print_rule(rule)
|
|
@@ -531,6 +531,41 @@ def print_ruleset(ruleset: dict) -> None:
|
|
|
531
531
|
print_rules_table(rules)
|
|
532
532
|
|
|
533
533
|
|
|
534
|
+
def rule_conditions(rule: dict) -> list[dict]:
|
|
535
|
+
"""A rule's conditions. A pre-#182 server sends only the flat fields: one condition."""
|
|
536
|
+
conditions = rule.get("conditions")
|
|
537
|
+
if conditions is not None:
|
|
538
|
+
return list(conditions)
|
|
539
|
+
return [
|
|
540
|
+
{
|
|
541
|
+
"field": rule.get("field"),
|
|
542
|
+
"operator": rule.get("operator"),
|
|
543
|
+
"value": rule.get("value"),
|
|
544
|
+
"case_sensitive": rule.get("case_sensitive"),
|
|
545
|
+
"negate": False,
|
|
546
|
+
}
|
|
547
|
+
]
|
|
548
|
+
|
|
549
|
+
|
|
550
|
+
def format_condition(condition: dict) -> str:
|
|
551
|
+
"""``[NOT ]field operator 'value'`` as plain text; escape it before printing."""
|
|
552
|
+
text = f"{condition.get('field') or '-'} {condition.get('operator') or '-'}"
|
|
553
|
+
if condition.get("operator") != "exists":
|
|
554
|
+
text += f" '{condition.get('value') or ''}'"
|
|
555
|
+
if condition.get("negate"):
|
|
556
|
+
text = f"NOT {text}"
|
|
557
|
+
if condition.get("case_sensitive"):
|
|
558
|
+
text += " (case-sensitive)"
|
|
559
|
+
return text
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
def format_conditions(rule: dict, separator: str | None = None) -> str:
|
|
563
|
+
"""All of a rule's conditions, joined by AND/OR to match its ``match`` mode."""
|
|
564
|
+
if separator is None:
|
|
565
|
+
separator = " OR " if rule.get("match") == "any" else " AND "
|
|
566
|
+
return separator.join(format_condition(c) for c in rule_conditions(rule))
|
|
567
|
+
|
|
568
|
+
|
|
534
569
|
def print_rules_table(rules: list[dict]) -> None:
|
|
535
570
|
"""Print rules as a table."""
|
|
536
571
|
table = Table(title="Rules")
|
|
@@ -543,15 +578,10 @@ def print_rules_table(rules: list[dict]) -> None:
|
|
|
543
578
|
table.add_column("Preserve Raw")
|
|
544
579
|
|
|
545
580
|
for rule in rules:
|
|
546
|
-
field_name = rule.get("field", "-")
|
|
547
|
-
op = rule.get("operator", "-")
|
|
548
|
-
val = rule.get("value", "-")
|
|
549
|
-
condition = f"{field_name} {op} '{val}'"
|
|
550
|
-
|
|
551
581
|
table.add_row(
|
|
552
582
|
short_id(rule.get("id")),
|
|
553
583
|
str(rule.get("order", 0)),
|
|
554
|
-
field(truncate(
|
|
584
|
+
field(truncate(format_conditions(rule), 50)),
|
|
555
585
|
field(rule.get("action")),
|
|
556
586
|
field(", ".join(rule.get("add_tags") or [])),
|
|
557
587
|
yes_no(rule.get("preserve_raw")),
|
|
@@ -569,10 +599,8 @@ def print_rule(rule: dict) -> None:
|
|
|
569
599
|
table.add_row("ID", field(rule.get("id")))
|
|
570
600
|
table.add_row("RuleSet ID", field(rule.get("ruleset_id")))
|
|
571
601
|
table.add_row("Order", str(rule.get("order", 0)))
|
|
572
|
-
table.add_row("
|
|
573
|
-
table.add_row("
|
|
574
|
-
table.add_row("Value", field(rule.get("value")))
|
|
575
|
-
table.add_row("Case Sensitive", yes_no(rule.get("case_sensitive")))
|
|
602
|
+
table.add_row("Match", field(rule.get("match") or "all"))
|
|
603
|
+
table.add_row("Conditions", field(format_conditions(rule, separator="\n")))
|
|
576
604
|
table.add_row("Action", field(rule.get("action")))
|
|
577
605
|
table.add_row("Webhook Override", field(rule.get("webhook_url_override")))
|
|
578
606
|
table.add_row("Add Tags", field(", ".join(rule.get("add_tags") or [])))
|
|
@@ -302,3 +302,18 @@ def test_clearable_fields_accept_null(
|
|
|
302
302
|
f"{http_method.upper()} {template} no longer accepts null for {field!r}, "
|
|
303
303
|
f"but the CLI sends one to clear it (schema: {field_schema})"
|
|
304
304
|
)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def test_condition_payload_matches_server_schema(openapi: dict[str, Any]) -> None:
|
|
308
|
+
"""The nested condition objects the CLI builds must match ``RuleConditionSchema``.
|
|
309
|
+
|
|
310
|
+
The generic check above only sees top-level body fields; ``conditions`` is
|
|
311
|
+
a list of objects, so its shape gets its own assertion.
|
|
312
|
+
"""
|
|
313
|
+
from fastsmtp_cli.commands.rules import parse_condition
|
|
314
|
+
|
|
315
|
+
schema = openapi["components"]["schemas"]["RuleConditionSchema"]
|
|
316
|
+
sent = parse_condition("header:X-Test equals a value", negate=True, case_sensitive=True)
|
|
317
|
+
|
|
318
|
+
assert set(sent) <= set(schema["properties"]), sorted(set(sent) - set(schema["properties"]))
|
|
319
|
+
assert set(schema.get("required", [])) <= set(sent)
|
|
@@ -614,6 +614,256 @@ class TestRuleCommands:
|
|
|
614
614
|
assert "Order" in output
|
|
615
615
|
|
|
616
616
|
|
|
617
|
+
def condition(field: str, operator: str, value: str = "", **flags: bool) -> dict:
|
|
618
|
+
"""A condition as the CLI sends it."""
|
|
619
|
+
return {
|
|
620
|
+
"field": field,
|
|
621
|
+
"operator": operator,
|
|
622
|
+
"value": value,
|
|
623
|
+
"case_sensitive": flags.get("case_sensitive", False),
|
|
624
|
+
"negate": flags.get("negate", False),
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
class TestRuleConditionCommands:
|
|
629
|
+
"""``--match``/``--condition``/``--not-condition`` build compound rules (issue #182)."""
|
|
630
|
+
|
|
631
|
+
LOOP_GUARD_ARGS = [
|
|
632
|
+
"--match",
|
|
633
|
+
"all",
|
|
634
|
+
"--condition",
|
|
635
|
+
r"from regex ^(noreply|support)@example\.com$",
|
|
636
|
+
"--condition",
|
|
637
|
+
"header:Auto-Submitted exists",
|
|
638
|
+
"--not-condition",
|
|
639
|
+
"header:Auto-Submitted equals no",
|
|
640
|
+
"--action",
|
|
641
|
+
"drop",
|
|
642
|
+
]
|
|
643
|
+
|
|
644
|
+
@pytest.fixture
|
|
645
|
+
def create_route(self, domain_id, ruleset_id, rule_id):
|
|
646
|
+
with respx.mock:
|
|
647
|
+
yield respx.post(f"{BASE}/api/v1/domains/{domain_id}/rulesets/{ruleset_id}/rules").mock(
|
|
648
|
+
return_value=httpx.Response(201, json=rule_payload(rule_id, ruleset_id))
|
|
649
|
+
)
|
|
650
|
+
|
|
651
|
+
def _create(self, domain_id: str, ruleset_id: str, *args: str):
|
|
652
|
+
return runner.invoke(app, ["rules", "rule", "create", domain_id, ruleset_id, *args])
|
|
653
|
+
|
|
654
|
+
def test_a_value_keeps_its_leading_spaces(
|
|
655
|
+
self, temp_config, domain_id, ruleset_id, create_route
|
|
656
|
+
):
|
|
657
|
+
result = self._create(domain_id, ruleset_id, "--condition", "subject starts_with [EXT]")
|
|
658
|
+
|
|
659
|
+
assert result.exit_code == 0, result.output
|
|
660
|
+
assert body_of(create_route)["conditions"][0]["value"] == " [EXT]"
|
|
661
|
+
|
|
662
|
+
def test_a_missing_value_is_refused_before_any_request(
|
|
663
|
+
self, temp_config, domain_id, ruleset_id, create_route
|
|
664
|
+
):
|
|
665
|
+
result = self._create(domain_id, ruleset_id, "--condition", "from contains")
|
|
666
|
+
|
|
667
|
+
assert result.exit_code == 1
|
|
668
|
+
assert "needs a VALUE" in strip_ansi(result.output)
|
|
669
|
+
assert not create_route.called
|
|
670
|
+
|
|
671
|
+
def test_create_sends_the_condition_list(
|
|
672
|
+
self, temp_config, domain_id, ruleset_id, create_route
|
|
673
|
+
):
|
|
674
|
+
result = self._create(domain_id, ruleset_id, *self.LOOP_GUARD_ARGS)
|
|
675
|
+
|
|
676
|
+
assert result.exit_code == 0, result.output
|
|
677
|
+
assert body_of(create_route) == {
|
|
678
|
+
"match": "all",
|
|
679
|
+
"conditions": [
|
|
680
|
+
condition("from", "regex", r"^(noreply|support)@example\.com$"),
|
|
681
|
+
condition("header:Auto-Submitted", "exists"),
|
|
682
|
+
condition("header:Auto-Submitted", "equals", "no", negate=True),
|
|
683
|
+
],
|
|
684
|
+
"action": "drop",
|
|
685
|
+
"preserve_raw": False,
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
def test_value_keeps_its_spaces(self, temp_config, domain_id, ruleset_id, create_route):
|
|
689
|
+
result = self._create(domain_id, ruleset_id, "--condition", "subject contains hello world")
|
|
690
|
+
|
|
691
|
+
assert result.exit_code == 0, result.output
|
|
692
|
+
assert body_of(create_route)["conditions"] == [
|
|
693
|
+
condition("subject", "contains", "hello world")
|
|
694
|
+
]
|
|
695
|
+
|
|
696
|
+
def test_case_sensitive_applies_to_every_condition(
|
|
697
|
+
self, temp_config, domain_id, ruleset_id, create_route
|
|
698
|
+
):
|
|
699
|
+
result = self._create(
|
|
700
|
+
domain_id,
|
|
701
|
+
ruleset_id,
|
|
702
|
+
"--condition",
|
|
703
|
+
"subject contains X",
|
|
704
|
+
"--not-condition",
|
|
705
|
+
"from contains Y",
|
|
706
|
+
"--case-sensitive",
|
|
707
|
+
)
|
|
708
|
+
|
|
709
|
+
assert result.exit_code == 0, result.output
|
|
710
|
+
assert body_of(create_route)["conditions"] == [
|
|
711
|
+
condition("subject", "contains", "X", case_sensitive=True),
|
|
712
|
+
condition("from", "contains", "Y", case_sensitive=True, negate=True),
|
|
713
|
+
]
|
|
714
|
+
|
|
715
|
+
def test_shorthand_still_sends_the_flat_shape(
|
|
716
|
+
self, temp_config, domain_id, ruleset_id, create_route
|
|
717
|
+
):
|
|
718
|
+
"""Flat on the wire, so the shorthand keeps working against a pre-#182 server."""
|
|
719
|
+
result = self._create(
|
|
720
|
+
domain_id, ruleset_id, "--field", "subject", "--operator", "exists", "--value", ""
|
|
721
|
+
)
|
|
722
|
+
|
|
723
|
+
assert result.exit_code == 0, result.output
|
|
724
|
+
body = body_of(create_route)
|
|
725
|
+
assert "conditions" not in body
|
|
726
|
+
assert (body["field"], body["operator"], body["value"]) == ("subject", "exists", "")
|
|
727
|
+
|
|
728
|
+
def test_without_either_shape_it_prompts_for_one_condition(
|
|
729
|
+
self, temp_config, domain_id, ruleset_id, create_route
|
|
730
|
+
):
|
|
731
|
+
result = runner.invoke(
|
|
732
|
+
app,
|
|
733
|
+
["rules", "rule", "create", domain_id, ruleset_id],
|
|
734
|
+
input="subject\ncontains\nhello\n",
|
|
735
|
+
)
|
|
736
|
+
|
|
737
|
+
assert result.exit_code == 0, result.output
|
|
738
|
+
body = body_of(create_route)
|
|
739
|
+
assert (body["field"], body["operator"], body["value"]) == ("subject", "contains", "hello")
|
|
740
|
+
|
|
741
|
+
def test_shorthand_and_conditions_cannot_mix(
|
|
742
|
+
self, temp_config, domain_id, ruleset_id, create_route
|
|
743
|
+
):
|
|
744
|
+
result = self._create(
|
|
745
|
+
domain_id, ruleset_id, "--condition", "subject exists", "--field", "from"
|
|
746
|
+
)
|
|
747
|
+
|
|
748
|
+
assert result.exit_code == 1
|
|
749
|
+
assert "not both" in strip_ansi(result.output)
|
|
750
|
+
assert not create_route.called
|
|
751
|
+
|
|
752
|
+
@pytest.mark.parametrize(
|
|
753
|
+
("spec", "message"),
|
|
754
|
+
[
|
|
755
|
+
("subject", "FIELD OPERATOR"),
|
|
756
|
+
("subject not_equals x", "Invalid operator"),
|
|
757
|
+
("nope equals x", "Invalid field"),
|
|
758
|
+
],
|
|
759
|
+
)
|
|
760
|
+
def test_malformed_conditions_are_refused_locally(
|
|
761
|
+
self, temp_config, domain_id, ruleset_id, create_route, spec, message
|
|
762
|
+
):
|
|
763
|
+
result = self._create(domain_id, ruleset_id, "--condition", spec)
|
|
764
|
+
|
|
765
|
+
assert result.exit_code == 1
|
|
766
|
+
assert message in strip_ansi(result.output)
|
|
767
|
+
assert not create_route.called
|
|
768
|
+
|
|
769
|
+
@respx.mock
|
|
770
|
+
def test_update_replaces_the_conditions(self, temp_config, domain_id, rule_id, ruleset_id):
|
|
771
|
+
route = respx.put(f"{BASE}/api/v1/domains/{domain_id}/rules/{rule_id}").mock(
|
|
772
|
+
return_value=httpx.Response(200, json=rule_payload(rule_id, ruleset_id))
|
|
773
|
+
)
|
|
774
|
+
|
|
775
|
+
result = runner.invoke(
|
|
776
|
+
app,
|
|
777
|
+
[
|
|
778
|
+
"rules",
|
|
779
|
+
"rule",
|
|
780
|
+
"update",
|
|
781
|
+
domain_id,
|
|
782
|
+
rule_id,
|
|
783
|
+
"--match",
|
|
784
|
+
"any",
|
|
785
|
+
"--condition",
|
|
786
|
+
"subject contains a",
|
|
787
|
+
"--not-condition",
|
|
788
|
+
"to exists",
|
|
789
|
+
],
|
|
790
|
+
)
|
|
791
|
+
|
|
792
|
+
assert result.exit_code == 0, result.output
|
|
793
|
+
assert body_of(route) == {
|
|
794
|
+
"match": "any",
|
|
795
|
+
"conditions": [
|
|
796
|
+
condition("subject", "contains", "a"),
|
|
797
|
+
condition("to", "exists", negate=True),
|
|
798
|
+
],
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
@respx.mock
|
|
802
|
+
def test_update_match_alone(self, temp_config, domain_id, rule_id, ruleset_id):
|
|
803
|
+
route = respx.put(f"{BASE}/api/v1/domains/{domain_id}/rules/{rule_id}").mock(
|
|
804
|
+
return_value=httpx.Response(200, json=rule_payload(rule_id, ruleset_id))
|
|
805
|
+
)
|
|
806
|
+
|
|
807
|
+
result = runner.invoke(
|
|
808
|
+
app, ["rules", "rule", "update", domain_id, rule_id, "--match", "any"]
|
|
809
|
+
)
|
|
810
|
+
|
|
811
|
+
assert result.exit_code == 0, result.output
|
|
812
|
+
assert body_of(route) == {"match": "any"}
|
|
813
|
+
|
|
814
|
+
@respx.mock
|
|
815
|
+
def test_update_shorthand_and_conditions_cannot_mix(self, temp_config, domain_id, rule_id):
|
|
816
|
+
route = respx.put(f"{BASE}/api/v1/domains/{domain_id}/rules/{rule_id}")
|
|
817
|
+
|
|
818
|
+
result = runner.invoke(
|
|
819
|
+
app,
|
|
820
|
+
[
|
|
821
|
+
"rules",
|
|
822
|
+
"rule",
|
|
823
|
+
"update",
|
|
824
|
+
domain_id,
|
|
825
|
+
rule_id,
|
|
826
|
+
"--value",
|
|
827
|
+
"x",
|
|
828
|
+
"--condition",
|
|
829
|
+
"subject exists",
|
|
830
|
+
],
|
|
831
|
+
)
|
|
832
|
+
|
|
833
|
+
assert result.exit_code == 1
|
|
834
|
+
assert not route.called
|
|
835
|
+
|
|
836
|
+
@respx.mock
|
|
837
|
+
def test_get_prints_conditions_one_per_line(self, temp_config, domain_id, ruleset_id, rule_id):
|
|
838
|
+
rule = rule_payload(
|
|
839
|
+
rule_id,
|
|
840
|
+
ruleset_id,
|
|
841
|
+
match="all",
|
|
842
|
+
conditions=[
|
|
843
|
+
condition("from", "ends_with", "@example.com"),
|
|
844
|
+
condition("header:Auto-Submitted", "equals", "no", negate=True),
|
|
845
|
+
],
|
|
846
|
+
field=None,
|
|
847
|
+
operator=None,
|
|
848
|
+
value=None,
|
|
849
|
+
case_sensitive=None,
|
|
850
|
+
)
|
|
851
|
+
respx.get(f"{BASE}/api/v1/domains/{domain_id}/rulesets/{ruleset_id}").mock(
|
|
852
|
+
return_value=httpx.Response(
|
|
853
|
+
200, json=ruleset_payload(ruleset_id, domain_id, rules=[rule])
|
|
854
|
+
)
|
|
855
|
+
)
|
|
856
|
+
|
|
857
|
+
result = runner.invoke(app, ["rules", "rule", "get", domain_id, ruleset_id, rule_id])
|
|
858
|
+
lines = strip_ansi(result.output).splitlines()
|
|
859
|
+
|
|
860
|
+
assert result.exit_code == 0, result.output
|
|
861
|
+
assert any("from ends_with '@example.com'" in line for line in lines)
|
|
862
|
+
negated = [line for line in lines if "NOT header:Auto-Submitted equals 'no'" in line]
|
|
863
|
+
assert negated and "from ends_with" not in negated[0]
|
|
864
|
+
assert any("Match" in line and "all" in line for line in lines)
|
|
865
|
+
|
|
866
|
+
|
|
617
867
|
class TestMemberCommands:
|
|
618
868
|
"""Member role updates use PUT, which is what the server serves."""
|
|
619
869
|
|
|
@@ -871,7 +871,9 @@ class TestServerShapedOutput:
|
|
|
871
871
|
assert "Order" in output
|
|
872
872
|
assert "Preserve Raw" in output
|
|
873
873
|
assert "spam, review" in output
|
|
874
|
-
|
|
874
|
+
# No ``conditions`` key, as from a pre-#182 server: the flat fields
|
|
875
|
+
# render as the one condition they describe.
|
|
876
|
+
assert "subject contains '[SPAM]' (case-sensitive)" in output
|
|
875
877
|
|
|
876
878
|
def test_rules_table_reads_order_not_priority(self, capsys):
|
|
877
879
|
print_rules_table(
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|