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 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)