1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
########################################################################
# File name: test_e2e.py
# This file is part of: aioxmpp
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
#
########################################################################
import asyncio
import aioxmpp
import aioxmpp.adhoc
from aioxmpp.utils import namespaces
from aioxmpp.e2etest import (
require_feature,
blocking,
blocking_timed,
TestCase,
skip_with_quirk,
Quirk,
)
class TestAdHocClient(TestCase):
@require_feature(namespaces.xep0050_commands, multiple=True)
@blocking
async def setUp(self, commands_providers):
services = [aioxmpp.AdHocClient]
self.peers = commands_providers
self.client, = await asyncio.gather(
self.provisioner.get_connected_client(
services=services,
),
)
self.svc = self.client.summon(aioxmpp.AdHocClient)
async def _get_ping_peer(self):
for peer in self.peers:
for item in await self.svc.get_commands(peer):
if item.node == "ping":
return peer
self.assertTrue(
False,
"found no peer which offers ad-hoc-ping; consider setting "
"the #no-adhoc-ping quirk"
)
@blocking_timed
async def test_get_list(self):
items = []
for peer in self.peers:
items.extend(await self.svc.get_commands(
peer
))
self.assertTrue(items)
@skip_with_quirk(Quirk.NO_ADHOC_PING)
@blocking_timed
async def test_ping(self):
ping_peer = await self._get_ping_peer()
session = await self.svc.execute(ping_peer, "ping")
self.assertTrue(session.response.notes)
await session.close()
@skip_with_quirk(Quirk.NO_ADHOC_PING)
@blocking_timed
async def test_ping_with_async_cm(self):
ping_peer = await self._get_ping_peer()
session = await self.svc.execute(ping_peer, "ping")
async with session:
self.assertTrue(session.response.notes)
class TestAdHocServer(TestCase):
@blocking
async def setUp(self):
self.client, self.server = await asyncio.gather(
self.provisioner.get_connected_client(
services=[aioxmpp.adhoc.AdHocClient],
),
self.provisioner.get_connected_client(
services=[aioxmpp.adhoc.AdHocServer],
),
)
self.client_svc = self.server.summon(aioxmpp.adhoc.AdHocClient)
self.server_svc = self.server.summon(aioxmpp.adhoc.AdHocServer)
async def _trivial_handler(self, stanza):
return aioxmpp.adhoc.xso.Command(
"simple",
notes=[
aioxmpp.adhoc.xso.Note(
aioxmpp.adhoc.xso.NoteType.INFO,
"some info!"
)
]
)
@blocking_timed
async def test_advertises_command_support(self):
self.assertTrue(await self.client_svc.supports_commands(
self.server.local_jid,
))
@blocking_timed
async def test_respond_to_command_listing(self):
self.server_svc.register_stateless_command(
"simple",
{
aioxmpp.structs.LanguageTag.fromstr("en"): "Simple command",
aioxmpp.structs.LanguageTag.fromstr("de"): "Einfacher Befehl",
},
self._trivial_handler,
)
commands = await self.client_svc.get_commands(
self.server.local_jid
)
self.assertEqual(len(commands), 1)
self.assertEqual(
commands[0].node,
"simple",
)
self.assertEqual(
commands[0].name,
"Simple command",
)
@blocking_timed
async def test_respond_to_command_info_query(self):
self.server_svc.register_stateless_command(
"simple",
{
aioxmpp.structs.LanguageTag.fromstr("en"): "Simple command",
aioxmpp.structs.LanguageTag.fromstr("de"): "Einfacher Befehl",
},
self._trivial_handler,
features={"foo"},
)
info = await self.client_svc.get_command_info(
self.server.local_jid,
"simple",
)
self.assertSetEqual(
info.features,
{
namespaces.xep0050_commands,
namespaces.xep0030_info,
"foo",
}
)
self.assertCountEqual(
[
("automation", "command-node",
aioxmpp.structs.LanguageTag.fromstr("en"), "Simple command"),
("automation", "command-node",
aioxmpp.structs.LanguageTag.fromstr("de"), "Einfacher Befehl"),
],
[
(ident.category, ident.type_, ident.lang, ident.name)
for ident in info.identities
]
)
@blocking_timed
async def test_execute_simple_command(self):
self.server_svc.register_stateless_command(
"simple",
{
aioxmpp.structs.LanguageTag.fromstr("en"): "Simple command",
aioxmpp.structs.LanguageTag.fromstr("de"): "Einfacher Befehl",
},
self._trivial_handler,
)
session = await self.client_svc.execute(
self.server.local_jid,
"simple",
)
self.assertEqual(
len(session.response.notes),
1
)
self.assertEqual(session.response.notes[0].body, "some info!")
self.assertIsNone(session.first_payload)
await session.close()
@blocking_timed
async def test_properly_fail_for_unknown_command(self):
with self.assertRaises(aioxmpp.XMPPCancelError) as ctx:
session = await self.client_svc.execute(
self.server.local_jid,
"simple",
)
self.assertEqual(
ctx.exception.condition,
aioxmpp.ErrorCondition.ITEM_NOT_FOUND
)
|