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.
This commit is contained in:
Valentin Lorentz 2021-07-03 09:31:51 +02:00
parent 5674bb030a
commit c4d19d44e8
3 changed files with 44 additions and 3 deletions

View File

@ -35,6 +35,14 @@ class StrRe(Operator):
return f"StrRe(r'{self.regexp}')" 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) @dataclasses.dataclass(frozen=True)
class RemainingKeys(Operator): class RemainingKeys(Operator):
"""Used in a dict pattern to match all remaining keys. """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): elif isinstance(expected, StrRe):
if got is None or not re.match(expected.regexp, got): if got is None or not re.match(expected.regexp, got):
return False return False
elif isinstance(expected, NotStrRe):
if got is None or re.match(expected.regexp, got):
return False
elif isinstance(expected, Operator): elif isinstance(expected, Operator):
raise NotImplementedError(f"Unsupported operator: {expected}") raise NotImplementedError(f"Unsupported operator: {expected}")
elif got != expected: elif got != expected:

View File

@ -4,7 +4,7 @@ import pytest
from irctest import cases from irctest import cases
from irctest.irc_utils.message_parser import parse_message 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 # fmt: off
MESSAGE_SPECS: List[Tuple[Dict, List[str], List[str]]] = [ 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", ":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 # fmt: on

View File

@ -8,7 +8,7 @@ so there may be many false positives.
import re import re
from irctest import cases 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): class LabeledResponsesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper):
@ -297,7 +297,11 @@ class LabeledResponsesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper
self.assertMessageMatch( self.assertMessageMatch(
m2, m2,
command="TAGMSG", 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( self.assertNotIn(
"label", "label",
@ -360,6 +364,11 @@ class LabeledResponsesTestCase(cases.BaseServerTestCase, cases.OptionalityHelper
self.assertMessageMatch( self.assertMessageMatch(
mt, mt,
command="TAGMSG", 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", fail_msg="No TAGMSG received by the target after sending one out",
) )
self.assertNotIn( self.assertNotIn(