summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2022-09-12 18:18:51 +0000
committermathieui <mathieui@mathieui.net>2022-09-12 18:18:51 +0000
commit97b0c7ffac306402155443dfa06c832e758806eb (patch)
tree7019bc57276ce27adacf825352618e0ea1d6ab13 /tests
parentafdfa1ee5715d9f1e96aadf8cbcd71f0082d8ab7 (diff)
parentc2ece57dee13ca8971bff66ff6e4899e9d2cfe71 (diff)
downloadslixmpp-97b0c7ffac306402155443dfa06c832e758806eb.tar.gz
slixmpp-97b0c7ffac306402155443dfa06c832e758806eb.tar.bz2
slixmpp-97b0c7ffac306402155443dfa06c832e758806eb.tar.xz
slixmpp-97b0c7ffac306402155443dfa06c832e758806eb.zip
Merge branch 'xep0055' into 'master'
Add XEP-0055 (Jabber Search) See merge request poezio/slixmpp!204
Diffstat (limited to 'tests')
-rw-r--r--tests/test_stanza_xep_0055.py59
-rw-r--r--tests/test_stream_xep_0055.py170
2 files changed, 229 insertions, 0 deletions
diff --git a/tests/test_stanza_xep_0055.py b/tests/test_stanza_xep_0055.py
new file mode 100644
index 00000000..9ff45efa
--- /dev/null
+++ b/tests/test_stanza_xep_0055.py
@@ -0,0 +1,59 @@
+import unittest
+
+from slixmpp import register_stanza_plugin, Iq
+from slixmpp.test import SlixTest
+
+from slixmpp.plugins.xep_0055 import stanza
+
+
+class TestJabberSearch(SlixTest):
+ def setUp(self):
+ register_stanza_plugin(Iq, stanza.Search)
+ self.stream_start(plugins={"xep_0055"})
+
+ def testRequestSearchFields(self):
+ iq = self.Iq()
+ iq.set_from("juliet@capulet.com/balcony")
+ iq.set_to("characters.shakespeare.lit")
+ iq.set_type("get")
+ iq.enable("search")
+ iq["id"] = "0"
+ self.check(
+ iq,
+ """
+ <iq type='get'
+ from='juliet@capulet.com/balcony'
+ to='characters.shakespeare.lit'>
+ <query xmlns='jabber:iq:search'/>
+ </iq>
+ """,
+ )
+
+ def testSendSearch(self):
+ iq = self.xmpp["xep_0055"].make_search_iq(
+ ifrom="juliet@capulet.com/balcony", ito="characters.shakespeare.lit"
+ )
+ iq["search"]["form"].add_field(var="x-gender", value="male")
+ self.check(
+ iq,
+ """
+ <iq type='set'
+ from='juliet@capulet.com/balcony'
+ to='characters.shakespeare.lit'>
+ <query xmlns='jabber:iq:search'>
+ <x xmlns='jabber:x:data' type='submit'>
+ <field type='hidden' var='FORM_TYPE'>
+ <value>jabber:iq:search</value>
+ </field>
+ <field var='x-gender'>
+ <value>male</value>
+ </field>
+ </x>
+ </query>
+ </iq>
+ """,
+ use_values=False,
+ )
+
+
+suite = unittest.TestLoader().loadTestsFromTestCase(TestJabberSearch)
diff --git a/tests/test_stream_xep_0055.py b/tests/test_stream_xep_0055.py
new file mode 100644
index 00000000..fa028d8b
--- /dev/null
+++ b/tests/test_stream_xep_0055.py
@@ -0,0 +1,170 @@
+import unittest
+from slixmpp.test import SlixTest
+
+
+class TestJabberSearch(SlixTest):
+ def setUp(self):
+ self.stream_start(
+ mode="component",
+ plugin_config={
+ "xep_0055": {
+ "form_fields": {"first", "last"},
+ "form_instructions": "INSTRUCTIONS",
+ "form_title": "User Directory Search",
+ }
+ },
+ jid="characters.shakespeare.lit",
+ plugins={"xep_0055"}
+ )
+ self.xmpp["xep_0055"].api.register(get_results, "search_query")
+ self.xmpp["xep_0055"].api.register(get_results, "search_query")
+
+ def tearDown(self):
+ self.stream_close()
+
+ def testRequestingSearchFields(self):
+ self.recv(
+ """
+ <iq type='get'
+ from='juliet@capulet.com/balcony'
+ to='characters.shakespeare.lit'
+ id='search3'
+ xml:lang='en'>
+ <query xmlns='jabber:iq:search'/>
+ </iq>
+ """
+ )
+ self.send(
+ """
+ <iq type='result'
+ from='characters.shakespeare.lit'
+ to='juliet@capulet.com/balcony'
+ id='search3'
+ xml:lang='en'>
+ <query xmlns='jabber:iq:search'>
+ <x xmlns='jabber:x:data' type='form'>
+ <title>User Directory Search</title>
+ <instructions>INSTRUCTIONS</instructions>
+ <field type='hidden'
+ var='FORM_TYPE'>
+ <value>jabber:iq:search</value>
+ </field>
+ <field var='first'/>
+ <field var='last'/>
+ </x>
+ </query>
+ </iq>
+ """,
+ use_values=False,
+ )
+
+ def testSearchResult(self):
+ self.recv(
+ """
+ <iq type='get'
+ from='juliet@capulet.com/balcony'
+ to='characters.shakespeare.lit'
+ id='search2'
+ xml:lang='en'>
+ <query xmlns='jabber:iq:search'>
+ <x xmlns='jabber:x:data' type='submit'>
+ <field type='hidden' var='FORM_TYPE'>
+ <value>jabber:iq:search</value>
+ </field>
+ <field var='last'>
+ <value>Montague</value>
+ </field>
+ </x>
+ </query>
+ </iq>
+ """
+ )
+ self.send(
+ """
+ <iq type='result'
+ from='characters.shakespeare.lit'
+ to='juliet@capulet.com/balcony'
+ id='search2'
+ xml:lang='en'>
+ <query xmlns='jabber:iq:search'>
+ <x xmlns='jabber:x:data' type='result'>
+ <field type='hidden' var='FORM_TYPE'>
+ <value>jabber:iq:search</value>
+ </field>
+ <reported>
+ <field var='first' label='Given Name' />
+ <field var='last' label='Family Name' />
+ </reported>
+ <item>
+ <field var='first'><value>Benvolio</value></field>
+ <field var='last'><value>Montague</value></field>
+ </item>
+ </x>
+ </query>
+ </iq>
+ """,
+ use_values=False, # TypeError: element indices must be integers without that
+ )
+
+ def testSearchNoResult(self):
+ self.xmpp["xep_0055"].api.register(get_results, "search_query")
+ self.recv(
+ """
+ <iq type='get'
+ from='juliet@capulet.com/balcony'
+ to='characters.shakespeare.lit'
+ id='search2'
+ xml:lang='en'>
+ <query xmlns='jabber:iq:search'>
+ <x xmlns='jabber:x:data' type='submit'>
+ <field type='hidden' var='FORM_TYPE'>
+ <value>jabber:iq:search</value>
+ </field>
+ <field var='last'>
+ <value>Capulet</value>
+ </field>
+ </x>
+ </query>
+ </iq>
+ """
+ )
+ self.send(
+ """
+ <iq type='result'
+ from='characters.shakespeare.lit'
+ to='juliet@capulet.com/balcony'
+ id='search2'
+ xml:lang='en'>
+ <query xmlns='jabber:iq:search'>
+ <x xmlns='jabber:x:data' type='result'>
+ <field type='hidden' var='FORM_TYPE'>
+ <value>jabber:iq:search</value>
+ </field>
+ <reported>
+ <field var='first' label='Given Name' />
+ <field var='last' label='Family Name' />
+ </reported>
+ </x>
+ </query>
+ </iq>
+ """,
+ use_values=False, # TypeError: element indices must be integers without that
+ )
+
+async def get_results(jid, node, ifrom, iq):
+ reply = iq.reply()
+ form = reply["search"]["form"]
+ form["type"] = "result"
+
+ form.add_reported("first", label="Given Name")
+ form.add_reported("last", label="Family Name")
+
+ d = iq["search"]["form"].get_values()
+
+ if d["last"] == "Montague":
+ form.add_item({"first": "Benvolio", "last": "Montague"})
+
+ return reply
+
+
+suite = unittest.TestLoader().loadTestsFromTestCase(TestJabberSearch)