1
|
|
|
""" |
2
|
|
|
:Copyright: 2007-2021 Jochen Kupperschmidt |
3
|
|
|
:License: MIT, see LICENSE for details. |
4
|
|
|
""" |
5
|
|
|
|
6
|
|
|
from irc.client import Event, NickMask, ServerConnection |
7
|
|
|
import pytest |
8
|
|
|
|
9
|
|
|
from weitersager.irc import create_bot, IrcChannel, IrcConfig, IrcServer |
10
|
|
|
from weitersager.signals import irc_channel_joined |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
@pytest.fixture |
14
|
|
|
def config(): |
15
|
|
|
server = IrcServer('irc.server.test') |
16
|
|
|
|
17
|
|
|
channels = {IrcChannel('#one'), IrcChannel('#two')} |
18
|
|
|
|
19
|
|
|
return IrcConfig( |
20
|
|
|
server=server, |
21
|
|
|
nickname='nick', |
22
|
|
|
realname='Nick', |
23
|
|
|
channels=channels, |
24
|
|
|
) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
@pytest.fixture |
28
|
|
|
def bot(config): |
29
|
|
|
bot = create_bot(config) |
30
|
|
|
|
31
|
|
|
yield bot |
32
|
|
|
|
33
|
|
|
bot.disconnect('Done.') |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
@pytest.fixture |
37
|
|
|
def nickmask(config): |
38
|
|
|
return NickMask(f'{config.nickname}!{config.nickname}@{config.server.host}') |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
def test_get_version(bot): |
42
|
|
|
assert bot.get_version() == 'Weitersager' |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def test_channel_joins(config, bot, nickmask, monkeypatch): |
46
|
|
|
class FakeSocket: |
47
|
|
|
def getpeername(self): |
48
|
|
|
return ('10.0.0.99', 6667) |
49
|
|
|
|
50
|
|
|
socket = FakeSocket() |
51
|
|
|
conn = ServerConnection(None) |
52
|
|
|
|
53
|
|
|
welcome_event = Event( |
54
|
|
|
type='welcome', source=config.server.host, target=config.nickname |
55
|
|
|
) |
56
|
|
|
|
57
|
|
|
def join(self, channel, key=''): |
58
|
|
|
join_event = Event(type='join', source=nickmask, target=channel) |
59
|
|
|
bot.on_join(conn, join_event) |
60
|
|
|
|
61
|
|
|
received_signal_data = [] |
62
|
|
|
|
63
|
|
|
@irc_channel_joined.connect |
64
|
|
|
def handle_irc_channel_joined(sender, **data): |
65
|
|
|
received_signal_data.append(data) |
66
|
|
|
|
67
|
|
|
with monkeypatch.context() as mpc: |
68
|
|
|
mpc.setattr(ServerConnection, 'socket', socket) |
69
|
|
|
mpc.setattr(ServerConnection, 'join', join) |
70
|
|
|
bot.on_welcome(conn, welcome_event) |
71
|
|
|
|
72
|
|
|
assert received_signal_data == [ |
73
|
|
|
{'channel_name': '#one'}, |
74
|
|
|
{'channel_name': '#two'}, |
75
|
|
|
] |
76
|
|
|
|