diff tests/unit/test_plugin_xep_0420.py @ 4285:f1d0cde61af7

tests (unit): fix tests + black reformatting.
author Goffi <goffi@goffi.org>
date Sun, 14 Jul 2024 17:42:53 +0200
parents 4b842c1fb686
children
line wrap: on
line diff
--- a/tests/unit/test_plugin_xep_0420.py	Sun Jul 14 16:47:45 2024 +0200
+++ b/tests/unit/test_plugin_xep_0420.py	Sun Jul 14 17:42:53 2024 +0200
@@ -24,8 +24,13 @@
 
 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 @@
     "test_custom_affixes",
     "test_namespace_conversion",
     "test_non_encryptable_elements",
-    "test_schema_validation"
+    "test_schema_validation",
 ]
 
 
@@ -110,7 +115,7 @@
         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 @@
         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 @@
         SCEAffixPolicy.REQUIRED,
         SCEAffixPolicy.REQUIRED,
         SCEAffixPolicy.REQUIRED,
-        custom_policies={}
+        custom_policies={},
     )
 
     unpacking_profile = SCEProfile(
@@ -178,14 +183,16 @@
         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 @@
         SCEAffixPolicy.NOT_NEEDED,
         SCEAffixPolicy.NOT_NEEDED,
         SCEAffixPolicy.NOT_NEEDED,
-        custom_policies={}
+        custom_policies={},
     )
 
     unpacking_profile = SCEProfile(
@@ -219,18 +226,18 @@
         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 @@
         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 @@
         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 @@
         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 @@
         SCEAffixPolicy.REQUIRED,
         SCEAffixPolicy.NOT_NEEDED,
         SCEAffixPolicy.NOT_NEEDED,
-        custom_policies={}
+        custom_policies={},
     )
 
     stanza = string_to_domish(
@@ -314,7 +321,9 @@
             </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 @@
         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 @@
     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 @@
         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 @@
     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 @@
         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 @@
         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 @@
         SCEAffixPolicy.NOT_NEEDED,
         SCEAffixPolicy.NOT_NEEDED,
         SCEAffixPolicy.NOT_NEEDED,
-        custom_policies={}
+        custom_policies={},
     )
 
     stanza = domish.Element((None, "message"))
@@ -480,7 +501,7 @@
         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 @@
         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 @@
             </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 @@
         SCEAffixPolicy.NOT_NEEDED,
         SCEAffixPolicy.NOT_NEEDED,
         SCEAffixPolicy.NOT_NEEDED,
-        custom_policies={}
+        custom_policies={},
     )
 
     stanza = string_to_domish(
@@ -557,7 +582,9 @@
             <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)