Skip to content

Commit 08bfa9d

Browse files
authored
Merge pull request #149 from TotallyNotRobots/gonzobot+fix-cap-values
Fix cap value handling
2 parents 58f9b71 + abcf045 commit 08bfa9d

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

plugins/core/cap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async def handle_available_caps(conn, caplist, event, irc_paramlist, bot):
3434
results = await asyncio.gather(*tasks)
3535
if any(ok and (res or res is None) for ok, res in results):
3636
cap_queue[name_cf] = async_util.create_future(conn.loop)
37-
conn.cmd("CAP", "REQ", cap)
37+
conn.cmd("CAP", "REQ", name)
3838

3939
if irc_paramlist[2] != '*':
4040
await asyncio.gather(*cap_queue.values())

tests/core_tests/test_plugin_manager.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from cloudbot import hook
99
from cloudbot.plugin import PluginManager
10+
from tests.util.mock_module import MockModule
1011

1112

1213
@pytest.fixture()
@@ -30,11 +31,6 @@ def mock_manager(mock_bot):
3031
yield mgr
3132

3233

33-
class MockModule:
34-
def __init__(self, **kwargs):
35-
self.__dict__.update(kwargs)
36-
37-
3834
def test_get_plugin(mock_manager):
3935
assert mock_manager.get_plugin('plugins/test.py') is None
4036
assert mock_manager.find_plugin('test') is None

tests/plugin_tests/cap_test.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import asyncio
2+
from pathlib import Path
3+
from unittest.mock import MagicMock, patch
4+
5+
import pytest
6+
from irclib.parser import ParamList
7+
8+
from cloudbot import hook
9+
from cloudbot.event import Event
10+
from cloudbot.plugin import PluginManager
11+
from cloudbot.util import async_util
12+
from plugins.core import cap
13+
from tests.util.mock_module import MockModule
14+
15+
16+
@pytest.fixture()
17+
def patch_import_module():
18+
with patch("importlib.import_module") as mocked:
19+
yield mocked
20+
21+
22+
@pytest.mark.asyncio()
23+
async def test_cap_req(patch_import_module):
24+
caps = [
25+
"some-cap",
26+
"another-cap",
27+
"a.vendor/cap",
28+
"a-cap=with-value",
29+
"a.vendor/cap=with-value",
30+
]
31+
cap_names = [s.split("=")[0] for s in caps]
32+
33+
params = ParamList.parse("* LS :" + " ".join(caps))
34+
event = Event(irc_paramlist=params, bot=MagicMock(), conn=MagicMock(),)
35+
event.conn.loop = event.bot.loop = asyncio.get_event_loop()
36+
event.bot.config = {}
37+
event.conn.type = "irc"
38+
event.bot.base_dir = Path(".").resolve()
39+
event.bot.plugin_manager = manager = PluginManager(event.bot)
40+
41+
called = False
42+
43+
def func():
44+
nonlocal called
45+
called = True
46+
return True
47+
48+
for c in cap_names:
49+
func = hook.on_cap_available(c)(func)
50+
51+
patch_import_module.return_value = MockModule(func=func)
52+
await manager.load_plugin("plugins/test.py")
53+
54+
event.conn.memory = {}
55+
56+
cap.send_cap_ls(event.conn)
57+
event.conn.cmd.assert_called_with("CAP", "LS", "302")
58+
59+
event.conn.cmd.reset_mock()
60+
61+
calls = []
62+
63+
def cmd(cmd, subcmd, *args):
64+
calls.append((cmd, subcmd) + args)
65+
p = ParamList.parse("* ACK :" + " ".join(args))
66+
cmd_event = Event(irc_paramlist=p, bot=event.bot, conn=event.conn,)
67+
async_util.wrap_future(cap.on_cap(p, cmd_event), loop=event.loop)
68+
69+
with patch.object(event.conn, "cmd", new=cmd):
70+
res = await cap.on_cap(params, event)
71+
assert called
72+
assert res is None
73+
74+
caps = event.conn.memory["server_caps"]
75+
assert caps == {c: True for c in cap_names}
76+
77+
assert calls == [("CAP", "REQ", c) for c in cap_names]

tests/util/mock_module.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class MockModule:
2+
def __init__(self, **kwargs):
3+
self.__dict__.update(kwargs)

0 commit comments

Comments
 (0)