From d7d4baefa3877afef462b37afec502ff9b42a9e5 Mon Sep 17 00:00:00 2001 From: mathieui Date: Mon, 8 Mar 2021 21:47:25 +0100 Subject: tests: Add XEP-0313 stanza tests --- tests/test_stanza_xep_0313.py | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/test_stanza_xep_0313.py (limited to 'tests') diff --git a/tests/test_stanza_xep_0313.py b/tests/test_stanza_xep_0313.py new file mode 100644 index 00000000..5c7b42a9 --- /dev/null +++ b/tests/test_stanza_xep_0313.py @@ -0,0 +1,82 @@ +import unittest +from slixmpp import JID, Iq, Message +from slixmpp.test import SlixTest +from slixmpp.plugins.xep_0313 import stanza +from slixmpp.plugins.xep_0004.stanza import Form +from slixmpp.plugins.xep_0297 import stanza as fstanza +from slixmpp.plugins.xep_0059 import stanza as rstanza +from slixmpp.xmlstream import register_stanza_plugin + + +class TestMAM(SlixTest): + + def setUp(self): + register_stanza_plugin(stanza.MAM, Form) + register_stanza_plugin(Iq, stanza.MAM) + register_stanza_plugin(Iq, stanza.Preferences) + register_stanza_plugin(Message, stanza.Result) + register_stanza_plugin(Iq, stanza.Fin) + register_stanza_plugin( + stanza.Result, + fstanza.Forwarded + ) + register_stanza_plugin(stanza.MAM, rstanza.Set) + register_stanza_plugin(stanza.Fin, rstanza.Set) + + def testMAMQuery(self): + """Test that we can build a simple MAM query.""" + iq = Iq() + iq['type'] = 'set' + iq['mam']['queryid'] = 'f27' + + self.check(iq, """ + + + + """) + + def testMAMQueryOptions(self): + """Test that we can build a mam query with all options.""" + iq = Iq() + iq['type'] = 'set' + iq['mam']['with'] = JID('juliet@capulet.lit') + iq['mam']['start'] = '2010-06-07T00:00:00Z' + iq['mam']['end'] = '2010-07-07T13:23:54Z' + iq['mam']['after_id'] = 'id1' + iq['mam']['before_id'] = 'id2' + iq['mam']['ids'] = ['a', 'b', 'c'] + + self.check(iq, """ + + + + + urn:xmpp:mam:2 + + + juliet@capulet.lit + + + 2010-06-07T00:00:00Z + + + 2010-07-07T13:23:54Z + + + id1 + + + id2 + + + a + b + c + + + + + """, use_values=False) + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestMAM) -- cgit v1.2.3 From 97a63b9f25f8dd124abf52fa06ed29ca11abe1d9 Mon Sep 17 00:00:00 2001 From: mathieui Date: Mon, 8 Mar 2021 22:15:42 +0100 Subject: XEP-0313: Update the API - add an iterate() method that makes this plugin more practical - add a get_fields method to retrieve the available search fields - add a get_archive_metadata method. This is a big chunk because git refused to split it further. --- tests/test_stanza_xep_0313.py | 1 - 1 file changed, 1 deletion(-) (limited to 'tests') diff --git a/tests/test_stanza_xep_0313.py b/tests/test_stanza_xep_0313.py index 5c7b42a9..d7bd3080 100644 --- a/tests/test_stanza_xep_0313.py +++ b/tests/test_stanza_xep_0313.py @@ -13,7 +13,6 @@ class TestMAM(SlixTest): def setUp(self): register_stanza_plugin(stanza.MAM, Form) register_stanza_plugin(Iq, stanza.MAM) - register_stanza_plugin(Iq, stanza.Preferences) register_stanza_plugin(Message, stanza.Result) register_stanza_plugin(Iq, stanza.Fin) register_stanza_plugin( -- cgit v1.2.3 From 3613cd5f1414c669769b997e3eadcfac8c9f687d Mon Sep 17 00:00:00 2001 From: mathieui Date: Tue, 9 Mar 2021 19:20:39 +0100 Subject: tests: Add a MAM stream test --- tests/test_stream_xep_0313.py | 340 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 340 insertions(+) create mode 100644 tests/test_stream_xep_0313.py (limited to 'tests') diff --git a/tests/test_stream_xep_0313.py b/tests/test_stream_xep_0313.py new file mode 100644 index 00000000..25a3a926 --- /dev/null +++ b/tests/test_stream_xep_0313.py @@ -0,0 +1,340 @@ +import unittest +from datetime import datetime +from slixmpp.test import SlixTest +from slixmpp import JID + + +class TestMAM(SlixTest): + + def setUp(self): + self.stream_start(plugins=['xep_0313']) + + def tearDown(self): + self.stream_close() + + def testRetrieveSimple(self): + """Test requesting MAM messages without RSM""" + + msgs = [] + + async def test(): + iq = await self.xmpp['xep_0313'].retrieve() + for message in iq['mam']['results']: + msgs.append(message) + + fut = self.xmpp.wrap(test()) + self.wait_() + self.send(""" + + + + """) + + self.recv(""" + + + + + + Hail to thee + + + + + """) + + self.recv(""" + + + 28482-98726-73623 + 28482-98726-73623 + + + """) + + self.run_coro(fut) + self.assertEqual( + msgs[0]['mam_result']['forwarded']['message']['body'], + "Hail to thee", + ) + self.assertEqual(len(msgs),1) + + def testRetrieveRSM(self): + """Test requesting MAM messages with RSM""" + + msgs = [] + + async def test(): + iterator = self.xmpp['xep_0313'].retrieve( + with_jid=JID('toto@titi'), + start='2010-06-07T00:00:00Z', + iterator=True, + ) + async for page in iterator: + for message in page['mam']['results']: + msgs.append(message) + + fut = self.xmpp.wrap(test()) + self.wait_() + self.send(""" + + + + + urn:xmpp:mam:2 + + + toto@titi + + + 2010-06-07T00:00:00Z + + + + 10 + + + + """) + + self.recv(""" + + + + + + Hail to thee + + + + + """) + + self.recv(""" + + + + 28482-98726-73623 + 28482-98726-73623 + 2 + + + + """) + + self.send(""" + + + + + urn:xmpp:mam:2 + + + toto@titi + + + 2010-06-07T00:00:00Z + + + + 10 + 28482-98726-73623 + + + + """) + + self.recv(""" + + + + + + Hi Y'all + + + + + """) + + self.recv(""" + + + + 28482-98726-73624 + 28482-98726-73624 + 2 + + + + """) + + self.run_coro(fut) + self.assertEqual( + msgs[0]['mam_result']['forwarded']['message']['body'], + "Hail to thee", + ) + self.assertEqual( + msgs[1]['mam_result']['forwarded']['message']['body'], + "Hi Y'all", + ) + self.assertEqual(len(msgs), 2) + + def testIterate(self): + """Test iterating over MAM messages with RSM""" + + msgs = [] + + async def test(): + iterator = self.xmpp['xep_0313'].iterate( + with_jid=JID('toto@titi'), + start='2010-06-07T00:00:00Z', + ) + async for message in iterator: + msgs.append(message) + + fut = self.xmpp.wrap(test()) + self.wait_() + self.send(""" + + + + + urn:xmpp:mam:2 + + + toto@titi + + + 2010-06-07T00:00:00Z + + + + 10 + + + + """) + + self.recv(""" + + + + + + Hail to thee + + + + + """) + + self.recv(""" + + + + 28482-98726-73623 + 28482-98726-73623 + 2 + + + + """) + + self.send(""" + + + + + urn:xmpp:mam:2 + + + toto@titi + + + 2010-06-07T00:00:00Z + + + + 10 + 28482-98726-73623 + + + + """) + + self.recv(""" + + + + + + Hi Y'all + + + + + """) + + self.recv(""" + + + + 28482-98726-73624 + 28482-98726-73624 + 2 + + + + """) + + self.run_coro(fut) + self.assertEqual( + msgs[0]['mam_result']['forwarded']['message']['body'], + "Hail to thee", + ) + self.assertEqual( + msgs[1]['mam_result']['forwarded']['message']['body'], + "Hi Y'all", + ) + self.assertEqual(len(msgs), 2) + + def test_get_metadata(self): + """Test a MAM metadata retrieval""" + fut = self.xmpp.wrap( + self.xmpp.plugin['xep_0313'].get_archive_metadata() + ) + self.wait_() + self.send(""" + + + + """) + self.recv(""" + + + + + + + """) + self.run_coro(fut) + result = fut.result() + self.assertEqual(result['mam_metadata']['start']['id'], "YWxwaGEg") + self.assertEqual( + result['mam_metadata']['start']['timestamp'], + datetime.fromisoformat('2008-08-22T21:09:04+00:00') + ) + + +suite = unittest.TestLoader().loadTestsFromTestCase(TestMAM) -- cgit v1.2.3 From 0115feaa31ef998b59da98c99c523ad34ed92651 Mon Sep 17 00:00:00 2001 From: mathieui Date: Tue, 9 Mar 2021 19:44:21 +0100 Subject: tests: Add a MAM metadata stanza test --- tests/test_stanza_xep_0313.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'tests') diff --git a/tests/test_stanza_xep_0313.py b/tests/test_stanza_xep_0313.py index d7bd3080..ac5fcb14 100644 --- a/tests/test_stanza_xep_0313.py +++ b/tests/test_stanza_xep_0313.py @@ -22,6 +22,10 @@ class TestMAM(SlixTest): register_stanza_plugin(stanza.MAM, rstanza.Set) register_stanza_plugin(stanza.Fin, rstanza.Set) + register_stanza_plugin(Iq, stanza.Metadata) + register_stanza_plugin(stanza.Metadata, stanza.Start) + register_stanza_plugin(stanza.Metadata, stanza.End) + def testMAMQuery(self): """Test that we can build a simple MAM query.""" iq = Iq() @@ -77,5 +81,25 @@ class TestMAM(SlixTest): """, use_values=False) + def testMAMMetadata(self): + """Test that we can build a MAM metadata payload""" + + iq = Iq() + iq['type'] = 'result' + iq['mam_metadata']['start']['id'] = 'YWxwaGEg' + iq['mam_metadata']['start']['timestamp'] = '2008-08-22T21:09:04Z' + iq['mam_metadata']['end']['id'] = 'b21lZ2Eg' + iq['mam_metadata']['end']['timestamp'] = '2020-04-20T14:34:21Z' + + self.check(iq, """ + + + + + + + """) + + suite = unittest.TestLoader().loadTestsFromTestCase(TestMAM) -- cgit v1.2.3