From c4d19d44e8b024b40a04ef80d0599b55f8b9a3b1 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 3 Jul 2021 09:31:51 +0200 Subject: [PATCH] test_labeled_responses: Actually check 'label' tags aren't relayed The existing assertion's comment said it checked the label wasn't relayed, but the code actually let any tag through. --- irctest/patma.py | 11 +++++++++ irctest/self_tests/test_cases.py | 23 ++++++++++++++++++- .../server_tests/test_labeled_responses.py | 13 +++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/irctest/patma.py b/irctest/patma.py index 19a4780..fd9db2b 100644 --- a/irctest/patma.py +++ b/irctest/patma.py @@ -35,6 +35,14 @@ class StrRe(Operator): return f"StrRe(r'{self.regexp}')" +@dataclasses.dataclass(frozen=True) +class NotStrRe(Operator): + regexp: str + + def __repr__(self) -> str: + return f"NotStrRe(r'{self.regexp}')" + + @dataclasses.dataclass(frozen=True) class RemainingKeys(Operator): """Used in a dict pattern to match all remaining keys. @@ -71,6 +79,9 @@ def match_string(got: Optional[str], expected: Union[str, Operator, None]) -> bo elif isinstance(expected, StrRe): if got is None or not re.match(expected.regexp, got): return False + elif isinstance(expected, NotStrRe): + if got is None or re.match(expected.regexp, got): + return False elif isinstance(expected, Operator): raise NotImplementedError(f"Unsupported operator: {expected}") elif got != expected: diff --git a/irctest/self_tests/test_cases.py b/irctest/self_tests/test_cases.py index 9bd479e..e31ea51 100644 --- a/irctest/self_tests/test_cases.py +++ b/irctest/self_tests/test_cases.py @@ -4,7 +4,7 @@ import pytest from irctest import cases from irctest.irc_utils.message_parser import parse_message -from irctest.patma import ANYDICT, ANYSTR, StrRe +from irctest.patma import ANYDICT, ANYSTR, AnyOptStr, NotStrRe, RemainingKeys, StrRe # fmt: off MESSAGE_SPECS: List[Tuple[Dict, List[str], List[str]]] = [ @@ -131,6 +131,27 @@ MESSAGE_SPECS: List[Tuple[Dict, List[str], List[str]]] = [ ":foo!baz@qux PRIVMSG #chan hello", ] ), + ( + # the specification: + dict( + tags={"tag1": "bar", RemainingKeys(NotStrRe("tag2")): AnyOptStr()}, + command="PRIVMSG", + params=["#chan", "hello"], + ), + # matches: + [ + "@tag1=bar PRIVMSG #chan :hello", + "@tag1=bar :foo!baz@qux PRIVMSG #chan :hello", + "@tag1=bar;tag3= PRIVMSG #chan :hello", + ], + # and does not match: + [ + "PRIVMG #chan :hello", + "@tag1=value1 PRIVMSG #chan :hello", + "@tag1=bar;tag2= PRIVMSG #chan :hello", + "@tag1=bar;tag2=baz PRIVMSG #chan :hello", + ] + ), ] # fmt: on diff --git a/irctest/server_tests/test_labeled_responses.py b/irctest/server_tests/test_labeled_responses.py index 239abf5..4ad03ab 100644 --- a/irctest/server_tests/test_labeled_responses.py +++ b/irctest/server_tests/test_labeled_responses.py @@ -8,7 +8,7 @@ so there may be many false positives. import re from irctest import cases -from irctest.patma import ANYDICT, StrRe +from irctest.patma import ANYDICT, AnyOptStr, NotStrRe, RemainingKeys, StrRe class LabeledResponsesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper): @@ -297,7 +297,11 @@ class LabeledResponsesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper self.assertMessageMatch( m2, command="TAGMSG", - tags={"+draft/reply": msgid, "+draft/react": "l😃l", **ANYDICT}, + tags={ + "+draft/reply": msgid, + "+draft/react": "l😃l", + RemainingKeys(NotStrRe("label")): AnyOptStr(), + }, ) self.assertNotIn( "label", @@ -360,6 +364,11 @@ class LabeledResponsesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper self.assertMessageMatch( mt, command="TAGMSG", + tags={ + "+draft/reply": msgid, + "+draft/react": "l😃l", + RemainingKeys(NotStrRe("label")): AnyOptStr(), + }, fail_msg="No TAGMSG received by the target after sending one out", ) self.assertNotIn(