tests (unit): fix tests + black reformatting.
This commit is contained in:
parent
c9ab0cd9dd
commit
067d8f32bf
1 changed files with 229 additions and 194 deletions
|
|
@ -24,8 +24,13 @@ from libervia.backend.core import exceptions
|
|||
|
||||
from libervia.backend.plugins.plugin_xep_0334 import NS_HINTS
|
||||
from libervia.backend.plugins.plugin_xep_0420 import (
|
||||
NS_SCE, XEP_0420, AffixVerificationFailed, ProfileRequirementsNotMet, SCEAffixPolicy,
|
||||
SCECustomAffix, SCEProfile
|
||||
NS_SCE,
|
||||
XEP_0420,
|
||||
AffixVerificationFailed,
|
||||
ProfileRequirementsNotMet,
|
||||
SCEAffixPolicy,
|
||||
SCECustomAffix,
|
||||
SCEProfile,
|
||||
)
|
||||
from libervia.backend.tools.xml_tools import ElementParser
|
||||
from twisted.words.xish import domish
|
||||
|
|
@ -43,7 +48,7 @@ __all__ = [ # pylint: disable=unused-variable
|
|||
"test_custom_affixes",
|
||||
"test_namespace_conversion",
|
||||
"test_non_encryptable_elements",
|
||||
"test_schema_validation"
|
||||
"test_schema_validation",
|
||||
]
|
||||
|
||||
|
||||
|
|
@ -110,7 +115,7 @@ def test_unpack_matches_original() -> None: # pylint: disable=missing-function-
|
|||
SCEAffixPolicy.OPTIONAL,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.OPTIONAL,
|
||||
custom_policies={ CustomAffixImpl(): SCEAffixPolicy.NOT_NEEDED }
|
||||
custom_policies={CustomAffixImpl(): SCEAffixPolicy.NOT_NEEDED},
|
||||
)
|
||||
|
||||
stanza_string = (
|
||||
|
|
@ -142,19 +147,19 @@ def test_affixes_included() -> None: # pylint: disable=missing-function-docstri
|
|||
SCEAffixPolicy.OPTIONAL,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.OPTIONAL,
|
||||
custom_policies={ custom_affix: SCEAffixPolicy.OPTIONAL }
|
||||
custom_policies={custom_affix: SCEAffixPolicy.OPTIONAL},
|
||||
)
|
||||
|
||||
stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com" to="bar@example.com">
|
||||
<body>
|
||||
Make sure that both the REQUIRED and the OPTIONAL affixes are included.
|
||||
</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
affix_values = XEP_0420.unpack_stanza(
|
||||
profile,
|
||||
stanza,
|
||||
XEP_0420.pack_stanza(profile, stanza)
|
||||
profile, stanza, XEP_0420.pack_stanza(profile, stanza)
|
||||
)
|
||||
|
||||
assert affix_values.rpad is not None
|
||||
|
|
@ -170,7 +175,7 @@ def test_all_affixes_verified() -> None: # pylint: disable=missing-function-doc
|
|||
SCEAffixPolicy.REQUIRED,
|
||||
SCEAffixPolicy.REQUIRED,
|
||||
SCEAffixPolicy.REQUIRED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
unpacking_profile = SCEProfile(
|
||||
|
|
@ -178,14 +183,16 @@ def test_all_affixes_verified() -> None: # pylint: disable=missing-function-doc
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com" to="bar@example.com">
|
||||
<body>
|
||||
When unpacking, all affixes are loaded, even those marked as NOT_NEEDED.
|
||||
</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
envelope_serialized = XEP_0420.pack_stanza(packing_profile, stanza)
|
||||
|
||||
|
|
@ -211,7 +218,7 @@ def test_incomplete_affixes() -> None: # pylint: disable=missing-function-docst
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
unpacking_profile = SCEProfile(
|
||||
|
|
@ -219,18 +226,18 @@ def test_incomplete_affixes() -> None: # pylint: disable=missing-function-docst
|
|||
SCEAffixPolicy.REQUIRED,
|
||||
SCEAffixPolicy.REQUIRED,
|
||||
SCEAffixPolicy.REQUIRED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com" to="bar@example.com">
|
||||
<body>Check that all affixes REQUIRED by the profile are present.</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
with pytest.raises(ProfileRequirementsNotMet):
|
||||
XEP_0420.unpack_stanza(
|
||||
unpacking_profile,
|
||||
stanza,
|
||||
XEP_0420.pack_stanza(packing_profile, stanza)
|
||||
unpacking_profile, stanza, XEP_0420.pack_stanza(packing_profile, stanza)
|
||||
)
|
||||
|
||||
# Do the same but with a custom affix missing
|
||||
|
|
@ -241,7 +248,7 @@ def test_incomplete_affixes() -> None: # pylint: disable=missing-function-docst
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={ custom_affix: SCEAffixPolicy.NOT_NEEDED }
|
||||
custom_policies={custom_affix: SCEAffixPolicy.NOT_NEEDED},
|
||||
)
|
||||
|
||||
unpacking_profile = SCEProfile(
|
||||
|
|
@ -249,21 +256,21 @@ def test_incomplete_affixes() -> None: # pylint: disable=missing-function-docst
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={ custom_affix: SCEAffixPolicy.REQUIRED }
|
||||
custom_policies={custom_affix: SCEAffixPolicy.REQUIRED},
|
||||
)
|
||||
|
||||
stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com" to="bar@example.com">
|
||||
<body>
|
||||
Check that all affixes REQUIRED by the profile are present, including custom
|
||||
affixes.
|
||||
</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
with pytest.raises(ProfileRequirementsNotMet):
|
||||
XEP_0420.unpack_stanza(
|
||||
unpacking_profile,
|
||||
stanza,
|
||||
XEP_0420.pack_stanza(packing_profile, stanza)
|
||||
unpacking_profile, stanza, XEP_0420.pack_stanza(packing_profile, stanza)
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -273,18 +280,18 @@ def test_rpad_affix() -> None: # pylint: disable=missing-function-docstring
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
for _ in range(100):
|
||||
stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com" to="bar@example.com">
|
||||
<body>OK</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
affix_values = XEP_0420.unpack_stanza(
|
||||
profile,
|
||||
stanza,
|
||||
XEP_0420.pack_stanza(profile, stanza)
|
||||
profile, stanza, XEP_0420.pack_stanza(profile, stanza)
|
||||
)
|
||||
|
||||
# Test that the rpad exists and that the content elements are always padded to at
|
||||
|
|
@ -299,7 +306,7 @@ def test_time_affix() -> None: # pylint: disable=missing-function-docstring
|
|||
SCEAffixPolicy.REQUIRED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
stanza = string_to_domish(
|
||||
|
|
@ -314,7 +321,9 @@ def test_time_affix() -> None: # pylint: disable=missing-function-docstring
|
|||
</body>
|
||||
</content>
|
||||
<time stamp="1969-07-21T02:56:15Z"/>
|
||||
</envelope>""".encode("utf-8")
|
||||
</envelope>""".encode(
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized)
|
||||
assert affix_values.timestamp == datetime(1969, 7, 21, 2, 56, 15, tzinfo=timezone.utc)
|
||||
|
|
@ -326,12 +335,14 @@ def test_to_affix() -> None: # pylint: disable=missing-function-docstring
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.REQUIRED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com" to="bar@example.com">
|
||||
<body>Check that the ``to`` affix is correctly added.</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
envelope_serialized = XEP_0420.pack_stanza(profile, stanza)
|
||||
affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized)
|
||||
|
|
@ -355,11 +366,13 @@ def test_to_affix() -> None: # pylint: disable=missing-function-docstring
|
|||
assert affix_values.recipient is not None
|
||||
assert affix_values.recipient.userhost() == "bar@example.com"
|
||||
|
||||
stanza = string_to_domish("""<message from="foo@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com">
|
||||
<body>
|
||||
Check that a missing "to" attribute on the stanza fails stanza packing.
|
||||
</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
XEP_0420.pack_stanza(profile, stanza)
|
||||
|
|
@ -371,12 +384,14 @@ def test_from_affix() -> None: # pylint: disable=missing-function-docstring
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.REQUIRED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com" to="bar@example.com">
|
||||
<body>Check that the ``from`` affix is correctly added.</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
envelope_serialized = XEP_0420.pack_stanza(profile, stanza)
|
||||
affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized)
|
||||
|
|
@ -400,11 +415,13 @@ def test_from_affix() -> None: # pylint: disable=missing-function-docstring
|
|||
assert affix_values.sender is not None
|
||||
assert affix_values.sender.userhost() == "foo@example.com"
|
||||
|
||||
stanza = string_to_domish("""<message to="bar@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message to="bar@example.com">
|
||||
<body>
|
||||
Check that a missing "from" attribute on the stanza fails stanza packing.
|
||||
</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
XEP_0420.pack_stanza(profile, stanza)
|
||||
|
|
@ -418,7 +435,7 @@ def test_custom_affixes() -> None: # pylint: disable=missing-function-docstring
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={ custom_affix: SCEAffixPolicy.REQUIRED }
|
||||
custom_policies={custom_affix: SCEAffixPolicy.REQUIRED},
|
||||
)
|
||||
|
||||
unpacking_profile = SCEProfile(
|
||||
|
|
@ -426,41 +443,45 @@ def test_custom_affixes() -> None: # pylint: disable=missing-function-docstring
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com" to="bar@example.com">
|
||||
<body>
|
||||
If a custom affix is included in the envelope, but not excpected by the
|
||||
recipient, the schema validation should fail.
|
||||
</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
with pytest.raises(exceptions.ParsingError):
|
||||
XEP_0420.unpack_stanza(
|
||||
unpacking_profile,
|
||||
stanza,
|
||||
XEP_0420.pack_stanza(packing_profile, stanza)
|
||||
unpacking_profile, stanza, XEP_0420.pack_stanza(packing_profile, stanza)
|
||||
)
|
||||
|
||||
profile = packing_profile
|
||||
|
||||
stanza = string_to_domish("""<message from="foo@example.com/device0"
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com/device0"
|
||||
to="bar@example.com/Libervia.123">
|
||||
<body>The affix element should be returned as part of the affix values.</body>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
affix_values = XEP_0420.unpack_stanza(
|
||||
profile,
|
||||
stanza,
|
||||
XEP_0420.pack_stanza(profile, stanza)
|
||||
profile, stanza, XEP_0420.pack_stanza(profile, stanza)
|
||||
)
|
||||
|
||||
assert custom_affix in affix_values.custom
|
||||
assert affix_values.custom[custom_affix].getAttribute("recipient") == \
|
||||
"bar@example.com/Libervia.123"
|
||||
assert affix_values.custom[custom_affix].getAttribute("sender") == \
|
||||
"foo@example.com/device0"
|
||||
assert (
|
||||
affix_values.custom[custom_affix].getAttribute("recipient")
|
||||
== "bar@example.com/Libervia.123"
|
||||
)
|
||||
assert (
|
||||
affix_values.custom[custom_affix].getAttribute("sender")
|
||||
== "foo@example.com/device0"
|
||||
)
|
||||
|
||||
|
||||
def test_namespace_conversion() -> None: # pylint: disable=missing-function-docstring
|
||||
|
|
@ -469,7 +490,7 @@ def test_namespace_conversion() -> None: # pylint: disable=missing-function-doc
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
stanza = domish.Element((None, "message"))
|
||||
|
|
@ -480,7 +501,7 @@ def test_namespace_conversion() -> None: # pylint: disable=missing-function-doc
|
|||
content=(
|
||||
"This body element has namespace ``None``, which has to be replaced with"
|
||||
" jabber:client."
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
envelope_serialized = XEP_0420.pack_stanza(profile, stanza)
|
||||
|
|
@ -502,13 +523,15 @@ def test_non_encryptable_elements() -> None: # pylint: disable=missing-function
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com">
|
||||
stanza = string_to_domish(
|
||||
"""<message from="foo@example.com" to="bar@example.com">
|
||||
<body>This stanza includes a store hint which must not be encrypted.</body>
|
||||
<store xmlns="urn:xmpp:hints"/>
|
||||
</message>""")
|
||||
</message>"""
|
||||
)
|
||||
|
||||
envelope_serialized = XEP_0420.pack_stanza(profile, stanza)
|
||||
envelope = string_to_domish(envelope_serialized.decode("utf-8"))
|
||||
|
|
@ -529,7 +552,9 @@ def test_non_encryptable_elements() -> None: # pylint: disable=missing-function
|
|||
</body>
|
||||
<store xmlns="urn:xmpp:hints"/>
|
||||
</content>
|
||||
</envelope>""".encode("utf-8")
|
||||
</envelope>""".encode(
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
XEP_0420.unpack_stanza(profile, stanza, envelope_serialized)
|
||||
|
||||
|
|
@ -542,7 +567,7 @@ def test_schema_validation() -> None: # pylint: disable=missing-function-docstr
|
|||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
SCEAffixPolicy.NOT_NEEDED,
|
||||
custom_policies={}
|
||||
custom_policies={},
|
||||
)
|
||||
|
||||
stanza = string_to_domish(
|
||||
|
|
@ -557,7 +582,9 @@ def test_schema_validation() -> None: # pylint: disable=missing-function-docstr
|
|||
<store xmlns="urn:xmpp:hints"/>
|
||||
</content>
|
||||
<unknown-affix unknown-attr="unknown"/>
|
||||
</envelope>""".encode("utf-8")
|
||||
</envelope>""".encode(
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
with pytest.raises(exceptions.ParsingError):
|
||||
XEP_0420.unpack_stanza(profile, stanza, envelope_serialized)
|
||||
|
|
|
|||
Loading…
Reference in a new issue