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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
|
import pytest
import slixmpp.test
from slixmpp import JID
import slidge.command.chat_command
from slidge.command import Command, Confirmation
from slidge.slixfix.delivery_receipt import DeliveryReceipt
from slidge.util.test import SlixTestPlus
class MockSession:
def __init__(self, jid):
self.logged = "logged" in jid.user
@pytest.fixture(autouse=True)
def mock(monkeypatch, MockRE):
monkeypatch.setattr(Command, "_get_session", lambda s, j: MockSession(j))
monkeypatch.setattr(
slixmpp.test.ComponentXMPP,
"get_session_from_stanza",
lambda self, stanza: MockSession(stanza.get_from()),
raising=False,
)
monkeypatch.setattr(
slixmpp.test.ComponentXMPP,
"jid_validator",
MockRE,
raising=False,
)
monkeypatch.setattr(
slixmpp.test.ComponentXMPP,
"get_session_from_jid",
lambda s, j: MockSession(j),
raising=False,
)
class CommandAdmin(Command):
NAME = "Command number one"
CHAT_COMMAND = "command1"
test_results = []
async def run(self, _session, _ifrom):
return Confirmation(
prompt="Confirm?", handler=self.finish, success="It worked!"
)
async def finish(self, _session, _ifrom):
self.test_results.append("yup")
class CommandAdminConfirmFail(CommandAdmin):
NAME = "Command number two"
CHAT_COMMAND = "command2"
async def run_admin(self):
return Confirmation(
prompt="Confirm?", handler=self.finish, success="It worked!"
)
async def finish(self, _session, _ifrom):
raise RuntimeError("Ploup")
class TestChatCommands(SlixTestPlus):
def setUp(self):
self.stream_start(
mode="component",
plugins=["xep_0050"],
jid="slidge.whatever.ass",
server="whatever.ass",
)
self.commands = slidge.command.chat_command.ChatCommandProvider(self.xmpp)
self.commands.register(CommandAdmin(self.xmpp))
self.commands.register(CommandAdminConfirmFail(self.xmpp))
self.xmpp.delivery_receipt = DeliveryReceipt(self.xmpp)
super().setUp()
def test_non_existing(self):
self.recv( # language=XML
f"""
<message from='admin@whatever.ass/cheogram'
to='{self.xmpp.boundjid.bare}'
type='chat'
id='not-found'>
<body>non-existing</body>
</message>
"""
)
t = self.commands.UNKNOWN.format("non-existing")
self.send( # language=XML
f"""
<message xmlns="jabber:component:accept"
from="slidge.whatever.ass"
to="admin@whatever.ass/cheogram"
type="chat">
<body>{t}</body>
</message>
"""
)
self.send( # language=XML
f"""
<message xmlns="jabber:component:accept"
from="slidge.whatever.ass"
to="admin@whatever.ass/cheogram"
type="error"
id='not-found'>
<error type="cancel">
<item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
<text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">{t}</text>
</error>
</message>
""",
use_values=False,
)
def test_other_destination(self):
self.recv( # language=XML
f"""
<message from='admin@whatever.ass/cheogram'
to='something@{self.xmpp.boundjid.bare}'
type='chat'
id='not-found'>
<body>help</body>
</message>
"""
)
assert self.next_sent() is None
def test_command_help(self):
self.recv( # language=XML
f"""
<message from='admin@whatever.ass/cheogram'
to='{self.xmpp.boundjid.bare}'
type='chat'
id='help'>
<body>help</body>
</message>
"""
)
self.send( # language=XML
f"""
<message xmlns="jabber:component:accept"
from="slidge.whatever.ass"
to="admin@whatever.ass/cheogram"
type="chat">
<body>Available commands:\ncommand1 -- Command number one\ncommand2 -- Command number two</body>
</message>
"""
)
def test_input(self):
fut = self.run_coro(
self.commands.input(JID("user@whatever.ass/x"), "blabla", blocking=False)
)
self.send( # language=XML
"""
<message xmlns="jabber:component:accept"
type="chat"
to="user@whatever.ass/x"
from="slidge.whatever.ass">
<body>blabla</body>
</message>
"""
)
self.recv( # language=XML
"""
<message from='user@whatever.ass/y'
to='slidge.whatever.ass'>
<body>reply</body>
</message>
"""
)
assert fut.result() == "reply"
def test_confirm_no(self):
self.recv( # language=XML
f"""
<message from='admin@whatever.ass/cheogram'
to='{self.xmpp.boundjid.bare}'
type='chat'
id='help'>
<body>command1</body>
</message>
"""
)
self.send( # language=XML
f"""
<message xmlns="jabber:component:accept"
type="chat"
to="admin@whatever.ass/cheogram"
from="{self.xmpp.boundjid.bare}">
<body>Confirm?</body>
</message>
"""
)
self.recv( # language=XML
f"""
<message from='admin@whatever.ass/cheogram'
to='{self.xmpp.boundjid.bare}'
type='chat'
id='help'>
<body>no</body>
</message>
"""
)
self.send( # language=XML
f"""
<message xmlns="jabber:component:accept"
type="chat"
to="admin@whatever.ass/cheogram"
from="{self.xmpp.boundjid.bare}">
<body>Canceled</body>
</message>
"""
)
assert len(CommandAdmin.test_results) == 0
def test_confirm_yes(self):
self.recv( # language=XML
f"""
<message from='admin@whatever.ass/cheogram'
to='{self.xmpp.boundjid.bare}'
type='chat'
id='help'>
<body>command1</body>
</message>
"""
)
self.send( # language=XML
f"""
<message xmlns="jabber:component:accept"
type="chat"
to="admin@whatever.ass/cheogram"
from="{self.xmpp.boundjid.bare}">
<body>Confirm?</body>
</message>
"""
)
self.recv( # language=XML
f"""
<message from='admin@whatever.ass/cheogram'
to='{self.xmpp.boundjid.bare}'
type='chat'
id='help'>
<body>yes</body>
</message>
"""
)
self.send( # language=XML
f"""
<message xmlns="jabber:component:accept"
type="chat"
to="admin@whatever.ass/cheogram"
from="{self.xmpp.boundjid.bare}">
<body>End of command.</body>
</message>
"""
)
assert CommandAdmin.test_results.pop() == "yup"
assert len(CommandAdmin.test_results) == 0
|