diff options
-rw-r--r-- | tests/end_to_end/__main__.py | 66 |
1 files changed, 54 insertions, 12 deletions
diff --git a/tests/end_to_end/__main__.py b/tests/end_to_end/__main__.py index 147e132..706d177 100644 --- a/tests/end_to_end/__main__.py +++ b/tests/end_to_end/__main__.py @@ -67,6 +67,8 @@ class XMPPComponent(slixmpp.BaseXMPP): self.failed = False self.accepting_server = None + self.saved_values = {} + def error(self, message): print("[31;1mFailure[0m: %s" % (message,)) self.scenario.steps = [] @@ -104,19 +106,28 @@ class XMPPComponent(slixmpp.BaseXMPP): pass -def check_xpath(xpaths, stanza): - for xpath in xpaths: - tree = lxml.etree.parse(io.StringIO(str(stanza))) - matched = tree.xpath(xpath, namespaces={'re': 'http://exslt.org/regular-expressions', - 'muc_user': 'http://jabber.org/protocol/muc#user', - 'disco_items': 'http://jabber.org/protocol/disco#items'}) +def match(stanza, xpath): + tree = lxml.etree.parse(io.StringIO(str(stanza))) + matched = tree.xpath(xpath, namespaces={'re': 'http://exslt.org/regular-expressions', + 'muc_user': 'http://jabber.org/protocol/muc#user', + 'disco_items': 'http://jabber.org/protocol/disco#items', + 'commands': 'http://jabber.org/protocol/commands', + 'dataform': 'jabber:x:data'}) + return matched + + +def check_xpath(xpaths, xmpp, after, stanza): + for i, xpath in enumerate(xpaths): + matched = match(stanza, xpath) if not matched: raise StanzaError("Received stanza ā%sā did not match expected xpath ā%sā" % (stanza, xpath)) + if after: + after(stanza, xmpp) -def check_xpath_optional(xpaths, stanza): +def check_xpath_optional(xpaths, xmpp, after, stanza): try: - check_xpath(xpaths, stanza) + check_xpath(xpaths, xmpp, after, stanza) except StanzaError: raise SkipStepError() @@ -149,6 +160,7 @@ class ProcessRunner: def __init__(self): self.process = None self.signal_sent = False + self.create = None @asyncio.coroutine def start(self): @@ -192,16 +204,18 @@ class IrcServerRunner(ProcessRunner): def send_stanza(stanza, xmpp, biboumi): - xmpp.send_raw(stanza.format_map(common_replacements)) + replacements = common_replacements + replacements.update(xmpp.saved_values) + xmpp.send_raw(stanza.format_map(replacements)) asyncio.get_event_loop().call_soon(xmpp.run_scenario) -def expect_stanza(xpaths, xmpp, biboumi, optional=False): +def expect_stanza(xpaths, xmpp, biboumi, optional=False, after=None): check_func = check_xpath if not optional else check_xpath_optional if isinstance(xpaths, str): - xmpp.stanza_checker = partial(check_func, [xpaths.format_map(common_replacements)]) + xmpp.stanza_checker = partial(check_func, [xpaths.format_map(common_replacements)], xmpp, after) elif isinstance(xpaths, tuple): - xmpp.stanza_checker = partial(check_func, [xpath.format_map(common_replacements) for xpath in xpaths]) + xmpp.stanza_checker = partial(check_func, [xpath.format_map(common_replacements) for xpath in xpaths], xmpp, after) else: print("Warning, from argument type passed to expect_stanza: %s" % (type(xpaths))) @@ -252,6 +266,8 @@ class BiboumiTest: else: failed = True + xmpp.saved_values.clear() + if xmpp.server: xmpp.accepting_server.close() @@ -357,6 +373,15 @@ def connection_sequence(irc_host, jid): return connection_begin_sequence(irc_host, jid) + connection_end_sequence(irc_host, jid) +def extract_attribute(xpath, name, stanza): + matched = match(stanza, xpath) + return matched[0].get(name) + + +def save_value(name, func, stanza, xmpp): + xmpp.saved_values[name] = func(stanza) + + if __name__ == '__main__': atexit.register(asyncio.get_event_loop().close) @@ -537,6 +562,23 @@ if __name__ == '__main__': partial(expect_stanza, ("/iq[@type='result']/disco_items:query[@node='http://jabber.org/protocol/commands']", "/iq/disco_items:query/disco_items:item[6]")), ], conf='fixed_server'), + + Scenario("execute_hello_adhoc_command", + [ + handshake_sequence(), + partial(send_stanza, "<iq type='set' id='hello-command1' from='{jid_one}/{resource_one}' to='{biboumi_host}'><command xmlns='http://jabber.org/protocol/commands' node='hello' action='execute' /></iq>"), + partial(expect_stanza, ("/iq[@type='result']/commands:command[@node='hello'][@sessionid][@status='executing']", + "/iq/commands:command/dataform:x[@type='form']/dataform:title[text()='Configure your name.']", + "/iq/commands:command/dataform:x[@type='form']/dataform:instructions[text()='Please provide your name.']", + "/iq/commands:command/dataform:x[@type='form']/dataform:field[@type='text-single']/dataform:required", + "/iq/commands:command/commands:actions/commands:next", + ), + after = partial(save_value, "sessionid", partial(extract_attribute, "/iq[@type='result']/commands:command[@node='hello']", "sessionid")) + + ), + partial(send_stanza, "<iq type='set' id='hello-command2' from='{jid_one}/{resource_one}' to='{biboumi_host}'><command xmlns='http://jabber.org/protocol/commands' node='hello' sessionid='{sessionid}' action='next'><x xmlns='jabber:x:data' type='submit'><field var='name'><value>COUCOU</value></field></x></command></iq>"), + partial(expect_stanza, "/iq[@type='result']/commands:command[@node='hello'][@status='completed']/commands:note[@type='info'][text()='Hello COUCOU!']") + ]), ) failures = 0 |