Update plugin: XEP-0317 (hats)

Merge changes from nicoco's MR that I missed, improving tests and
interface.
This commit is contained in:
nicoco 2024-01-19 21:26:08 +01:00 committed by mathieui
parent 85b7210115
commit b8205a9ae4
4 changed files with 60 additions and 12 deletions

View File

@ -1,6 +1,4 @@
from slixmpp.plugins import BasePlugin from slixmpp.plugins import BasePlugin
from slixmpp import Presence
from slixmpp.xmlstream import register_stanza_plugin
from . import stanza from . import stanza
@ -8,13 +6,11 @@ class XEP_0317(BasePlugin):
""" """
XEP-0317: Hats XEP-0317: Hats
""" """
name = 'xep_0317' name = 'xep_0317'
description = 'XEP-0317: Hats' description = 'XEP-0317: Hats'
dependencies = {'xep_0030', 'xep_0050'} dependencies = {'xep_0030', 'xep_0045', 'xep_0050'}
stanza = stanza stanza = stanza
namespace = stanza.NS namespace = stanza.NS
def plugin_init(self): def plugin_init(self):
register_stanza_plugin(stanza.Hat, stanza.Hats) stanza.register_plugin()
register_stanza_plugin(stanza.Hats, Presence)

View File

@ -1,4 +1,5 @@
from slixmpp.xmlstream import ElementBase from slixmpp import Presence
from slixmpp.xmlstream import ElementBase, register_stanza_plugin
NS = 'urn:xmpp:hats:0' NS = 'urn:xmpp:hats:0'
@ -25,6 +26,13 @@ class Hats(ElementBase):
namespace = NS namespace = NS
plugin_attrib = 'hats' plugin_attrib = 'hats'
def add_hats(self, data: list[tuple[str, str]]) -> None:
for uri, title in data:
hat = Hat()
hat["uri"] = uri
hat["title"] = title
self.append(hat)
class Hat(ElementBase): class Hat(ElementBase):
""" """
@ -38,6 +46,12 @@ class Hat(ElementBase):
""" """
name = 'hat' name = 'hat'
namespace = NS
plugin_attrib = 'hat' plugin_attrib = 'hat'
namespace = NS
interfaces = {'title', 'uri'} interfaces = {'title', 'uri'}
plugin_multi_attrib = "hats"
def register_plugin() -> None:
register_stanza_plugin(Hats, Hat, iterable=True)
register_stanza_plugin(Presence, Hats)

View File

@ -76,6 +76,7 @@ from slixmpp.plugins.xep_0297 import XEP_0297
from slixmpp.plugins.xep_0300 import XEP_0300 from slixmpp.plugins.xep_0300 import XEP_0300
from slixmpp.plugins.xep_0308 import XEP_0308 from slixmpp.plugins.xep_0308 import XEP_0308
from slixmpp.plugins.xep_0313 import XEP_0313 from slixmpp.plugins.xep_0313 import XEP_0313
from slixmpp.plugins.xep_0317 import XEP_0317
from slixmpp.plugins.xep_0319 import XEP_0319 from slixmpp.plugins.xep_0319 import XEP_0319
from slixmpp.plugins.xep_0332 import XEP_0332 from slixmpp.plugins.xep_0332 import XEP_0332
from slixmpp.plugins.xep_0333 import XEP_0333 from slixmpp.plugins.xep_0333 import XEP_0333
@ -171,6 +172,7 @@ class PluginsDict(TypedDict):
xep_0300: XEP_0300 xep_0300: XEP_0300
xep_0308: XEP_0308 xep_0308: XEP_0308
xep_0313: XEP_0313 xep_0313: XEP_0313
xep_0317: XEP_0317
xep_0319: XEP_0319 xep_0319: XEP_0319
xep_0332: XEP_0332 xep_0332: XEP_0332
xep_0333: XEP_0333 xep_0333: XEP_0333

View File

@ -2,16 +2,15 @@ import unittest
from slixmpp import Presence from slixmpp import Presence
from slixmpp.test import SlixTest from slixmpp.test import SlixTest
import slixmpp.plugins.xep_0317 as xep_0317 import slixmpp.plugins.xep_0317 as xep_0317
from slixmpp.xmlstream import register_stanza_plugin from slixmpp.plugins.xep_0317 import stanza
class TestStanzaHats(SlixTest): class TestStanzaHats(SlixTest):
def setUp(self): def setUp(self):
register_stanza_plugin(Presence, xep_0317.Hats) stanza.register_plugin()
register_stanza_plugin(xep_0317.Hats, xep_0317.Hats)
def testCreateHats(self): def test_create_hats(self):
raw_xml = """ raw_xml = """
<hats xmlns="urn:xmpp:hats:0"> <hats xmlns="urn:xmpp:hats:0">
<hat uri="http://example.com/hats#Teacher" title="Teacher"/> <hat uri="http://example.com/hats#Teacher" title="Teacher"/>
@ -27,5 +26,42 @@ class TestStanzaHats(SlixTest):
self.check(hats, raw_xml, use_values=False) self.check(hats, raw_xml, use_values=False)
def test_set_single_hat(self):
presence = Presence()
presence["hats"]["hat"]["uri"] = "test-uri"
presence["hats"]["hat"]["title"] = "test-title"
self.check(
presence, # language=XML
"""
<presence>
<hats xmlns='urn:xmpp:hats:0'>
<hat uri='test-uri' title='test-title'/>
</hats>
</presence>
""",
)
def test_set_multi_hat(self):
presence = Presence()
presence["hats"].add_hats([("uri1", "title1"), ("uri2", "title2")])
self.check(
presence, # language=XML
"""
<presence>
<hats xmlns='urn:xmpp:hats:0'>
<hat uri='uri1' title='title1'/>
<hat uri='uri2' title='title2'/>
</hats>
</presence>
""",
)
def test_get_hats(self):
presence = Presence()
presence["hats"].add_hats([("uri1", "title1"), ("uri2", "title2")])
for i, hat in enumerate(presence["hats"]["hats"], start=1):
self.assertEqual(hat["uri"], f"uri{i}")
self.assertEqual(hat["title"], f"title{i}")
suite = unittest.TestLoader().loadTestsFromTestCase(TestStanzaHats) suite = unittest.TestLoader().loadTestsFromTestCase(TestStanzaHats)