From eac5ad50a8ff47b24d74bdb3c878c171e37fceed Mon Sep 17 00:00:00 2001 From: Nicoco K Date: Tue, 9 Mar 2021 19:24:43 +0100 Subject: XEP-0050 Make handle_command_xxx async --- tests/test_stream_xep_0050.py | 448 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 445 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/test_stream_xep_0050.py b/tests/test_stream_xep_0050.py index 37cd233d..519c860e 100644 --- a/tests/test_stream_xep_0050.py +++ b/tests/test_stream_xep_0050.py @@ -47,7 +47,6 @@ class TestAdHocCommands(SlixTest): session['has_next'] = False return session - self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', 'Do Foo', handle_command) @@ -418,8 +417,6 @@ class TestAdHocCommands(SlixTest): """) - - def testMultiPayloads(self): """Test using commands with multiple payloads.""" results = [] @@ -519,6 +516,451 @@ class TestAdHocCommands(SlixTest): self.assertEqual(results, [['form_1'], ['form_2']], "Command handler was not executed: %s" % results) + def testZeroStepCommandAsync(self): + """Test running a command with no steps.""" + + async def handle_command(iq, session): + form = self.xmpp['xep_0004'].make_form(ftype='result') + form.addField(var='foo', ftype='text-single', + label='Foo', value='bar') + + session['payload'] = form + session['next'] = None + session['has_next'] = False + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + + + bar + + + + + """) + + def testOneStepCommandAsync(self): + """Test running a single step command.""" + results = [] + + async def handle_command(iq, session): + + async def handle_form(form, session): + results.append(form.get_values()['foo']) + session['payload'] = None + + form = self.xmpp['xep_0004'].make_form('form') + form.addField(var='foo', ftype='text-single', label='Foo') + + session['payload'] = form + session['next'] = handle_form + session['has_next'] = False + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + + + + + + + + + """) + + self.recv(""" + + + + + blah + + + + + """) + + self.send(""" + + + + """) + + self.assertEqual(results, ['blah'], + "Command handler was not executed: %s" % results) + + def testTwoStepCommandAsync(self): + """Test using a two-stage command.""" + results = [] + + async def handle_command(iq, session): + + async def handle_step2(form, session): + results.append(form.get_values()['bar']) + session['payload'] = None + + async def handle_step1(form, session): + results.append(form.get_values()['foo']) + + form = self.xmpp['xep_0004'].make_form('form') + form.addField(var='bar', ftype='text-single', label='Bar') + + session['payload'] = form + session['next'] = handle_step2 + session['has_next'] = False + + return session + + form = self.xmpp['xep_0004'].make_form('form') + form.addField(var='foo', ftype='text-single', label='Foo') + + session['payload'] = form + session['next'] = handle_step1 + session['has_next'] = True + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + + + + + + + + + """) + + self.recv(""" + + + + + blah + + + + + """) + + self.send(""" + + + + + + + + + + + """) + + self.recv(""" + + + + + meh + + + + + """) + self.send(""" + + + + """) + + self.assertEqual(results, ['blah', 'meh'], + "Command handler was not executed: %s" % results) + + def testCancelCommandAsync(self): + """Test canceling command.""" + results = [] + + async def handle_command(iq, session): + + async def handle_form(form, session): + results.append(form['values']['foo']) + + async def handle_cancel(iq, session): + results.append('canceled') + + form = self.xmpp['xep_0004'].make_form('form') + form.addField(var='foo', ftype='text-single', label='Foo') + + session['payload'] = form + session['next'] = handle_form + session['cancel'] = handle_cancel + session['has_next'] = False + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + + + + + + + + + """) + + self.recv(""" + + + + + blah + + + + + """) + + self.send(""" + + + + """) + + self.assertEqual(results, ['canceled'], + "Cancelation handler not executed: %s" % results) + + def testCommandNoteAsync(self): + """Test adding notes to commands.""" + + async def handle_command(iq, session): + form = self.xmpp['xep_0004'].make_form(ftype='result') + form.addField(var='foo', ftype='text-single', + label='Foo', value='bar') + + session['payload'] = form + session['next'] = None + session['has_next'] = False + session['notes'] = [('info', 'testing notes')] + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + testing notes + + + bar + + + + + """) + + def testMultiPayloadsAsync(self): + """Test using commands with multiple payloads.""" + results = [] + + async def handle_command(iq, session): + + async def handle_form(forms, session): + for form in forms: + results.append(form.get_values()['FORM_TYPE']) + session['payload'] = None + + form1 = self.xmpp['xep_0004'].make_form('form') + form1.addField(var='FORM_TYPE', ftype='hidden', value='form_1') + form1.addField(var='foo', ftype='text-single', label='Foo') + + form2 = self.xmpp['xep_0004'].make_form('form') + form2.addField(var='FORM_TYPE', ftype='hidden', value='form_2') + form2.addField(var='foo', ftype='text-single', label='Foo') + + session['payload'] = [form1, form2] + session['next'] = handle_form + session['has_next'] = False + + return session + + self.xmpp['xep_0050'].add_command('tester@localhost', 'foo', + 'Do Foo', handle_command) + + self.recv(""" + + + + """) + + self.send(""" + + + + + + + + form_1 + + + + + + form_2 + + + + + + """) + + self.recv(""" + + + + + form_1 + + + bar + + + + + form_2 + + + bar + + + + + """) + + self.send(""" + + + + """) + + self.assertEqual(results, [['form_1'], ['form_2']], + "Command handler was not executed: %s" % results) + def testClientAPI(self): """Test using client-side API for commands.""" results = [] -- cgit v1.2.3