|
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_announcer, 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
|
|
|
commands=[], |
|
24
|
|
|
channels=channels, |
|
25
|
|
|
) |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
@pytest.fixture |
|
29
|
|
|
def bot(config): |
|
30
|
|
|
announcer = create_announcer(config) |
|
31
|
|
|
|
|
32
|
|
|
yield announcer.bot |
|
33
|
|
|
|
|
34
|
|
|
announcer.shutdown() |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
@pytest.fixture |
|
38
|
|
|
def nickmask(config): |
|
39
|
|
|
return NickMask(f'{config.nickname}!{config.nickname}@{config.server.host}') |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def test_get_version(bot): |
|
43
|
|
|
assert bot.get_version() == 'Weitersager' |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def test_channel_joins(config, bot, nickmask, monkeypatch): |
|
47
|
|
|
class FakeSocket: |
|
48
|
|
|
def getpeername(self): |
|
49
|
|
|
return ('10.0.0.99', 6667) |
|
50
|
|
|
|
|
51
|
|
|
socket = FakeSocket() |
|
52
|
|
|
conn = ServerConnection(None) |
|
53
|
|
|
|
|
54
|
|
|
welcome_event = Event( |
|
55
|
|
|
type='welcome', source=config.server.host, target=config.nickname |
|
56
|
|
|
) |
|
57
|
|
|
|
|
58
|
|
|
def join(self, channel, key=''): |
|
59
|
|
|
join_event = Event(type='join', source=nickmask, target=channel) |
|
60
|
|
|
bot.on_join(conn, join_event) |
|
61
|
|
|
|
|
62
|
|
|
received_signal_data = [] |
|
63
|
|
|
|
|
64
|
|
|
@irc_channel_joined.connect |
|
65
|
|
|
def handle_irc_channel_joined(sender, **data): |
|
66
|
|
|
received_signal_data.append(data) |
|
67
|
|
|
|
|
68
|
|
|
with monkeypatch.context() as mpc: |
|
69
|
|
|
mpc.setattr(ServerConnection, 'socket', socket) |
|
70
|
|
|
mpc.setattr(ServerConnection, 'join', join) |
|
71
|
|
|
bot.on_welcome(conn, welcome_event) |
|
72
|
|
|
|
|
73
|
|
|
assert received_signal_data == [ |
|
74
|
|
|
{'channel_name': '#one'}, |
|
75
|
|
|
{'channel_name': '#two'}, |
|
76
|
|
|
] |
|
77
|
|
|
|