test_trigger.test_ircv3_intents_trigger()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 27
Code Lines 24

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 24
dl 27
loc 27
rs 9.304
c 0
b 0
f 0
cc 1
nop 1
1
# coding=utf-8
2
"""Tests for message parsing"""
3
from __future__ import unicode_literals, absolute_import, print_function, division
4
5
import re
6
import pytest
7
import datetime
8
9
from sopel.test_tools import MockConfig
10
from sopel.trigger import PreTrigger, Trigger
11
from sopel.tools import Identifier
12
13
14
@pytest.fixture
15
def nick():
16
    return Identifier('Sopel')
17
18
19 View Code Duplication
def test_basic_pretrigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
20
    line = ':[email protected] PRIVMSG #Sopel :Hello, world'
21
    pretrigger = PreTrigger(nick, line)
22
    assert pretrigger.tags == {}
23
    assert pretrigger.hostmask == '[email protected]'
24
    assert pretrigger.line == line
25
    assert pretrigger.args == ['#Sopel', 'Hello, world']
26
    assert pretrigger.event == 'PRIVMSG'
27
    assert pretrigger.nick == Identifier('Foo')
28
    assert pretrigger.user == 'foo'
29
    assert pretrigger.host == 'example.com'
30
    assert pretrigger.sender == '#Sopel'
31
32
33 View Code Duplication
def test_pm_pretrigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
34
    line = ':[email protected] PRIVMSG Sopel :Hello, world'
35
    pretrigger = PreTrigger(nick, line)
36
    assert pretrigger.tags == {}
37
    assert pretrigger.hostmask == '[email protected]'
38
    assert pretrigger.line == line
39
    assert pretrigger.args == ['Sopel', 'Hello, world']
40
    assert pretrigger.event == 'PRIVMSG'
41
    assert pretrigger.nick == Identifier('Foo')
42
    assert pretrigger.user == 'foo'
43
    assert pretrigger.host == 'example.com'
44
    assert pretrigger.sender == Identifier('Foo')
45
46
47 View Code Duplication
def test_quit_pretrigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
48
    line = ':[email protected] QUIT :quit message text'
49
    pretrigger = PreTrigger(nick, line)
50
    assert pretrigger.tags == {}
51
    assert pretrigger.hostmask == '[email protected]'
52
    assert pretrigger.line == line
53
    assert pretrigger.args == ['quit message text']
54
    assert pretrigger.event == 'QUIT'
55
    assert pretrigger.nick == Identifier('Foo')
56
    assert pretrigger.user == 'foo'
57
    assert pretrigger.host == 'example.com'
58
    assert pretrigger.sender is None
59
60
61 View Code Duplication
def test_join_pretrigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
62
    line = ':[email protected] JOIN #Sopel'
63
    pretrigger = PreTrigger(nick, line)
64
    assert pretrigger.tags == {}
65
    assert pretrigger.hostmask == '[email protected]'
66
    assert pretrigger.line == line
67
    assert pretrigger.args == ['#Sopel']
68
    assert pretrigger.event == 'JOIN'
69
    assert pretrigger.nick == Identifier('Foo')
70
    assert pretrigger.user == 'foo'
71
    assert pretrigger.host == 'example.com'
72
    assert pretrigger.sender == Identifier('#Sopel')
73
74
75 View Code Duplication
def test_tags_pretrigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
76
    line = '@foo=bar;baz;sopel.chat/special=value :[email protected] PRIVMSG #Sopel :Hello, world'
77
    pretrigger = PreTrigger(nick, line)
78
    assert pretrigger.tags == {'baz': None,
79
                               'foo': 'bar',
80
                               'sopel.chat/special': 'value'}
81
    assert pretrigger.hostmask == '[email protected]'
82
    assert pretrigger.line == line
83
    assert pretrigger.args == ['#Sopel', 'Hello, world']
84
    assert pretrigger.event == 'PRIVMSG'
85
    assert pretrigger.nick == Identifier('Foo')
86
    assert pretrigger.user == 'foo'
87
    assert pretrigger.host == 'example.com'
88
    assert pretrigger.sender == '#Sopel'
89
90
91 View Code Duplication
def test_intents_pretrigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
92
    line = '@intent=ACTION :[email protected] PRIVMSG #Sopel :Hello, world'
93
    pretrigger = PreTrigger(nick, line)
94
    assert pretrigger.tags == {'intent': 'ACTION'}
95
    assert pretrigger.hostmask == '[email protected]'
96
    assert pretrigger.line == line
97
    assert pretrigger.args == ['#Sopel', 'Hello, world']
98
    assert pretrigger.event == 'PRIVMSG'
99
    assert pretrigger.nick == Identifier('Foo')
100
    assert pretrigger.user == 'foo'
101
    assert pretrigger.host == 'example.com'
102
    assert pretrigger.sender == '#Sopel'
103
104
105
def test_unusual_pretrigger(nick):
106
    line = 'PING'
107
    pretrigger = PreTrigger(nick, line)
108
    assert pretrigger.tags == {}
109
    assert pretrigger.hostmask is None
110
    assert pretrigger.line == line
111
    assert pretrigger.args == []
112
    assert pretrigger.event == 'PING'
113
114
115 View Code Duplication
def test_ctcp_intent_pretrigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
116
    line = ':[email protected] PRIVMSG Sopel :\x01VERSION\x01'
117
    pretrigger = PreTrigger(nick, line)
118
    assert pretrigger.tags == {'intent': 'VERSION'}
119
    assert pretrigger.hostmask == '[email protected]'
120
    assert pretrigger.line == line
121
    assert pretrigger.args == ['Sopel', '']
122
    assert pretrigger.event == 'PRIVMSG'
123
    assert pretrigger.nick == Identifier('Foo')
124
    assert pretrigger.user == 'foo'
125
    assert pretrigger.host == 'example.com'
126
    assert pretrigger.sender == Identifier('Foo')
127
128
129 View Code Duplication
def test_ctcp_data_pretrigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
130
    line = ':[email protected] PRIVMSG Sopel :\x01PING 1123321\x01'
131
    pretrigger = PreTrigger(nick, line)
132
    assert pretrigger.tags == {'intent': 'PING'}
133
    assert pretrigger.hostmask == '[email protected]'
134
    assert pretrigger.line == line
135
    assert pretrigger.args == ['Sopel', '1123321']
136
    assert pretrigger.event == 'PRIVMSG'
137
    assert pretrigger.nick == Identifier('Foo')
138
    assert pretrigger.user == 'foo'
139
    assert pretrigger.host == 'example.com'
140
    assert pretrigger.sender == Identifier('Foo')
141
142
143 View Code Duplication
def test_ircv3_extended_join_pretrigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
144
    line = ':[email protected] JOIN #Sopel bar :Real Name'
145
    pretrigger = PreTrigger(nick, line)
146
    assert pretrigger.tags == {'account': 'bar'}
147
    assert pretrigger.hostmask == '[email protected]'
148
    assert pretrigger.line == line
149
    assert pretrigger.args == ['#Sopel', 'bar', 'Real Name']
150
    assert pretrigger.event == 'JOIN'
151
    assert pretrigger.nick == Identifier('Foo')
152
    assert pretrigger.user == 'foo'
153
    assert pretrigger.host == 'example.com'
154
    assert pretrigger.sender == Identifier('#Sopel')
155
156
157 View Code Duplication
def test_ircv3_extended_join_trigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
158
    line = ':[email protected] JOIN #Sopel bar :Real Name'
159
    pretrigger = PreTrigger(nick, line)
160
161
    config = MockConfig()
162
    config.core.owner_account = 'bar'
163
164
    fakematch = re.match('.*', line)
165
166
    trigger = Trigger(config, pretrigger, fakematch)
167
    assert trigger.sender == '#Sopel'
168
    assert trigger.raw == line
169
    assert trigger.is_privmsg is False
170
    assert trigger.hostmask == '[email protected]'
171
    assert trigger.user == 'foo'
172
    assert trigger.nick == Identifier('Foo')
173
    assert trigger.host == 'example.com'
174
    assert trigger.event == 'JOIN'
175
    assert trigger.match == fakematch
176
    assert trigger.group == fakematch.group
177
    assert trigger.groups == fakematch.groups
178
    assert trigger.args == ['#Sopel', 'bar', 'Real Name']
179
    assert trigger.account == 'bar'
180
    assert trigger.tags == {'account': 'bar'}
181
    assert trigger.owner is True
182
    assert trigger.admin is True
183
184
185 View Code Duplication
def test_ircv3_intents_trigger(nick):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
186
    line = '@intent=ACTION :[email protected] PRIVMSG #Sopel :Hello, world'
187
    pretrigger = PreTrigger(nick, line)
188
189
    config = MockConfig()
190
    config.core.owner = 'Foo'
191
    config.core.admins = ['Bar']
192
193
    fakematch = re.match('.*', line)
194
195
    trigger = Trigger(config, pretrigger, fakematch)
196
    assert trigger.sender == '#Sopel'
197
    assert trigger.raw == line
198
    assert trigger.is_privmsg is False
199
    assert trigger.hostmask == '[email protected]'
200
    assert trigger.user == 'foo'
201
    assert trigger.nick == Identifier('Foo')
202
    assert trigger.host == 'example.com'
203
    assert trigger.event == 'PRIVMSG'
204
    assert trigger.match == fakematch
205
    assert trigger.group == fakematch.group
206
    assert trigger.groups == fakematch.groups
207
    assert trigger.groupdict == fakematch.groupdict
208
    assert trigger.args == ['#Sopel', 'Hello, world']
209
    assert trigger.tags == {'intent': 'ACTION'}
210
    assert trigger.admin is True
211
    assert trigger.owner is True
212
213
214
def test_ircv3_account_tag_trigger(nick):
215
    line = '@account=Foo :[email protected] PRIVMSG #Sopel :Hello, world'
216
    pretrigger = PreTrigger(nick, line)
217
218
    config = MockConfig()
219
    config.core.owner_account = 'Foo'
220
    config.core.admins = ['Bar']
221
222
    fakematch = re.match('.*', line)
223
224
    trigger = Trigger(config, pretrigger, fakematch)
225
    assert trigger.admin is True
226
    assert trigger.owner is True
227
228
229
def test_ircv3_server_time_trigger(nick):
230
    line = '@time=2016-01-09T03:15:42.000Z :[email protected] PRIVMSG #Sopel :Hello, world'
231
    pretrigger = PreTrigger(nick, line)
232
233
    config = MockConfig()
234
    config.core.owner = 'Foo'
235
    config.core.admins = ['Bar']
236
237
    fakematch = re.match('.*', line)
238
239
    trigger = Trigger(config, pretrigger, fakematch)
240
    assert trigger.time == datetime.datetime(2016, 1, 9, 3, 15, 42, 0)
241
242
    # Spec-breaking string
243
    line = '@time=2016-01-09T04:20 :[email protected] PRIVMSG #Sopel :Hello, world'
244
    pretrigger = PreTrigger(nick, line)
245
    assert pretrigger.time is not None
246