1 { writeScriptBin, writeText, python3, connectTo ? "localhost" }:
3 dummyFile = writeText "dummy-file" ''
6 Please find this *really* important attachment.
11 in writeScriptBin "send-message" ''
12 #!${(python3.withPackages (ps: [ ps.slixmpp ])).interpreter}
15 from types import MethodType
17 from slixmpp import ClientXMPP
18 from slixmpp.exceptions import IqError, IqTimeout
21 class CthonTest(ClientXMPP):
23 def __init__(self, jid, password):
24 ClientXMPP.__init__(self, jid, password)
25 self.add_event_handler("session_start", self.session_start)
26 self.test_succeeded = False
28 async def session_start(self, event):
30 # Exceptions in event handlers are printed to stderr but not
31 # propagated, they do not make the script terminate with a non-zero
32 # exit code. We use the `test_succeeded` flag as a workaround and
33 # check it later at the end of the script to exit with a proper
35 # Additionally, this flag ensures that this event handler has been
36 # actually run by ClientXMPP, which may well not be the case.
37 await self.test_xmpp_server()
38 self.test_succeeded = True
40 # Even if an exception happens in `test_xmpp_server()`, we still
41 # need to disconnect explicitly, otherwise the process will hang
43 self.disconnect(wait=True)
45 async def test_xmpp_server(self):
46 log = logging.getLogger(__name__)
49 # Sending a test message
50 self.send_message(mto="azurediamond@example.com", mbody="Hello, this is dog.", mtype="chat")
51 log.info('Message sent')
53 # Test http upload (XEP_0363)
55 url = await self['xep_0363'].upload_file("${dummyFile}",timeout=10)
57 log.error("ERROR: Cannot run upload command. XEP_0363 seems broken")
59 log.info('Upload success!')
62 # TODO: use join_muc_wait() after slixmpp 1.8.0 is released.
63 self.plugin['xep_0045'].join_muc('testMucRoom', 'cthon98')
64 log.info('MUC join success!')
65 log.info('XMPP SCRIPT TEST SUCCESS')
68 if __name__ == '__main__':
69 logging.basicConfig(level=logging.DEBUG,
70 format='%(levelname)-8s %(message)s')
72 ct = CthonTest('cthon98@example.com', 'nothunter2')
73 ct.register_plugin('xep_0071')
74 ct.register_plugin('xep_0128')
76 ct.register_plugin('xep_0363')
78 ct.register_plugin('xep_0045')
79 ct.connect(("server", 5222))
80 ct.process(forever=False)
82 if not ct.test_succeeded: