mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-23 23:43:30 +00:00
xmpp-sendmessage: init script file, use in prosody test
This commit is contained in:
parent
65ca3ab5f4
commit
e03932bbca
@ -9,70 +9,30 @@ import ./make-test.nix {
|
||||
extraConfig = ''
|
||||
storage = "sql"
|
||||
'';
|
||||
virtualHosts.test = {
|
||||
domain = "example.com";
|
||||
enabled = true;
|
||||
};
|
||||
};
|
||||
environment.systemPackages = let
|
||||
sendMessage = pkgs.writeScriptBin "send-message" ''
|
||||
#!/usr/bin/env python3
|
||||
# Based on the sleekxmpp send_client example, look there for more details:
|
||||
# https://github.com/fritzy/SleekXMPP/blob/develop/examples/send_client.py
|
||||
import sleekxmpp
|
||||
|
||||
class SendMsgBot(sleekxmpp.ClientXMPP):
|
||||
"""
|
||||
A basic SleekXMPP bot that will log in, send a message,
|
||||
and then log out.
|
||||
"""
|
||||
def __init__(self, jid, password, recipient, message):
|
||||
sleekxmpp.ClientXMPP.__init__(self, jid, password)
|
||||
|
||||
self.recipient = recipient
|
||||
self.msg = message
|
||||
|
||||
self.add_event_handler("session_start", self.start, threaded=True)
|
||||
|
||||
def start(self, event):
|
||||
self.send_presence()
|
||||
self.get_roster()
|
||||
|
||||
self.send_message(mto=self.recipient,
|
||||
mbody=self.msg,
|
||||
mtype='chat')
|
||||
|
||||
self.disconnect(wait=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
xmpp = SendMsgBot("test1@localhost", "test1", "test2@localhost", "Hello World!")
|
||||
xmpp.register_plugin('xep_0030') # Service Discovery
|
||||
xmpp.register_plugin('xep_0199') # XMPP Ping
|
||||
|
||||
# TODO: verify certificate
|
||||
# If you want to verify the SSL certificates offered by a server:
|
||||
# xmpp.ca_certs = "path/to/ca/cert"
|
||||
|
||||
if xmpp.connect(('localhost', 5222)):
|
||||
xmpp.process(block=True)
|
||||
else:
|
||||
print("Unable to connect.")
|
||||
sys.exit(1)
|
||||
'';
|
||||
in [ (pkgs.python3.withPackages (ps: [ ps.sleekxmpp ])) sendMessage ];
|
||||
environment.systemPackages = [
|
||||
(pkgs.callPackage ./xmpp-sendmessage.nix {})
|
||||
];
|
||||
};
|
||||
|
||||
testScript = ''
|
||||
$machine->waitForUnit('prosody.service');
|
||||
$machine->succeed('prosodyctl status') =~ /Prosody is running/;
|
||||
|
||||
# set password to 'test' (it's asked twice)
|
||||
$machine->succeed('yes test1 | prosodyctl adduser test1@localhost');
|
||||
# set password to 'nothunter2' (it's asked twice)
|
||||
$machine->succeed('yes nothunter2 | prosodyctl adduser cthon98@example.com');
|
||||
# set password to 'y'
|
||||
$machine->succeed('yes | prosodyctl adduser test2@localhost');
|
||||
# correct password to 'test2'
|
||||
$machine->succeed('yes test2 | prosodyctl passwd test2@localhost');
|
||||
$machine->succeed('yes | prosodyctl adduser azurediamond@example.com');
|
||||
# correct password to 'hunter2'
|
||||
$machine->succeed('yes hunter2 | prosodyctl passwd azurediamond@example.com');
|
||||
|
||||
$machine->succeed("send-message");
|
||||
|
||||
$machine->succeed('prosodyctl deluser test1@localhost');
|
||||
$machine->succeed('prosodyctl deluser test2@localhost');
|
||||
$machine->succeed('prosodyctl deluser cthon98@example.com');
|
||||
$machine->succeed('prosodyctl deluser azurediamond@example.com');
|
||||
'';
|
||||
}
|
||||
|
46
nixos/tests/xmpp-sendmessage.nix
Normal file
46
nixos/tests/xmpp-sendmessage.nix
Normal file
@ -0,0 +1,46 @@
|
||||
{ writeScriptBin, python3, connectTo ? "localhost" }:
|
||||
writeScriptBin "send-message" ''
|
||||
#!${(python3.withPackages (ps: [ ps.sleekxmpp ])).interpreter}
|
||||
# Based on the sleekxmpp send_client example, look there for more details:
|
||||
# https://github.com/fritzy/SleekXMPP/blob/develop/examples/send_client.py
|
||||
import sleekxmpp
|
||||
|
||||
class SendMsgBot(sleekxmpp.ClientXMPP):
|
||||
"""
|
||||
A basic SleekXMPP bot that will log in, send a message,
|
||||
and then log out.
|
||||
"""
|
||||
def __init__(self, jid, password, recipient, message):
|
||||
sleekxmpp.ClientXMPP.__init__(self, jid, password)
|
||||
|
||||
self.recipient = recipient
|
||||
self.msg = message
|
||||
|
||||
self.add_event_handler("session_start", self.start, threaded=True)
|
||||
|
||||
def start(self, event):
|
||||
self.send_presence()
|
||||
self.get_roster()
|
||||
|
||||
self.send_message(mto=self.recipient,
|
||||
mbody=self.msg,
|
||||
mtype='chat')
|
||||
|
||||
self.disconnect(wait=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
xmpp = SendMsgBot("cthon98@example.com", "nothunter2", "azurediamond@example.com", "hey, if you type in your pw, it will show as stars")
|
||||
xmpp.register_plugin('xep_0030') # Service Discovery
|
||||
xmpp.register_plugin('xep_0199') # XMPP Ping
|
||||
|
||||
# TODO: verify certificate
|
||||
# If you want to verify the SSL certificates offered by a server:
|
||||
# xmpp.ca_certs = "path/to/ca/cert"
|
||||
|
||||
if xmpp.connect(('${connectTo}', 5222)):
|
||||
xmpp.process(block=True)
|
||||
else:
|
||||
print("Unable to connect.")
|
||||
sys.exit(1)
|
||||
''
|
Loading…
Reference in New Issue
Block a user