|
1
|
|
|
# coding=utf-8 |
|
2
|
|
|
"""Tests for core ``sopel.bot`` module""" |
|
3
|
|
|
from __future__ import unicode_literals, absolute_import, print_function, division |
|
4
|
|
|
|
|
5
|
|
|
import re |
|
6
|
|
|
|
|
7
|
|
|
import pytest |
|
8
|
|
|
|
|
9
|
|
|
from sopel import bot, config, plugins |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
@pytest.fixture |
|
13
|
|
|
def tmpconfig(tmpdir): |
|
14
|
|
|
conf_file = tmpdir.join('conf.ini') |
|
15
|
|
|
conf_file.write("\n".join([ |
|
16
|
|
|
"[core]", |
|
17
|
|
|
"owner=testnick", |
|
18
|
|
|
"nick = TestBot", |
|
19
|
|
|
"enable = coretasks" |
|
20
|
|
|
"" |
|
21
|
|
|
])) |
|
22
|
|
|
return config.Config(conf_file.strpath) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def test_register_unregister_plugin(tmpconfig): |
|
26
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
27
|
|
|
|
|
28
|
|
|
# since `setup` hasn't been run, there is no registered plugin |
|
29
|
|
|
assert not sopel.has_plugin('coretasks') |
|
30
|
|
|
|
|
31
|
|
|
# register the plugin |
|
32
|
|
|
plugin = plugins.handlers.PyModulePlugin('coretasks', 'sopel') |
|
33
|
|
|
plugin.load() |
|
34
|
|
|
plugin.register(sopel) |
|
35
|
|
|
|
|
36
|
|
|
# and now there is! |
|
37
|
|
|
assert sopel.has_plugin('coretasks') |
|
38
|
|
|
|
|
39
|
|
|
# unregister it |
|
40
|
|
|
plugin.unregister(sopel) |
|
41
|
|
|
assert not sopel.has_plugin('coretasks') |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def test_remove_plugin_unknown_plugin(tmpconfig): |
|
45
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
46
|
|
|
|
|
47
|
|
|
plugin = plugins.handlers.PyModulePlugin('admin', 'sopel.modules') |
|
48
|
|
|
with pytest.raises(plugins.exceptions.PluginNotRegistered): |
|
49
|
|
|
sopel.remove_plugin(plugin, [], [], [], []) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def test_remove_plugin_unregistered_plugin(tmpconfig): |
|
53
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
54
|
|
|
|
|
55
|
|
|
# register the plugin |
|
56
|
|
|
plugin = plugins.handlers.PyModulePlugin('coretasks', 'sopel') |
|
57
|
|
|
plugin.load() |
|
58
|
|
|
plugin.register(sopel) |
|
59
|
|
|
|
|
60
|
|
|
# Unregister the plugin |
|
61
|
|
|
plugin.unregister(sopel) |
|
62
|
|
|
|
|
63
|
|
|
# And now it must raise an exception |
|
64
|
|
|
with pytest.raises(plugins.exceptions.PluginNotRegistered): |
|
65
|
|
|
sopel.remove_plugin(plugin, [], [], [], []) |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
def test_reload_plugin_unregistered_plugin(tmpconfig): |
|
69
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
70
|
|
|
|
|
71
|
|
|
# register the plugin |
|
72
|
|
|
plugin = plugins.handlers.PyModulePlugin('coretasks', 'sopel') |
|
73
|
|
|
plugin.load() |
|
74
|
|
|
plugin.register(sopel) |
|
75
|
|
|
|
|
76
|
|
|
# Unregister the plugin |
|
77
|
|
|
plugin.unregister(sopel) |
|
78
|
|
|
|
|
79
|
|
|
# And now it must raise an exception |
|
80
|
|
|
with pytest.raises(plugins.exceptions.PluginNotRegistered): |
|
81
|
|
|
sopel.reload_plugin(plugin.name) |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
def test_search_url_callbacks(tmpconfig): |
|
85
|
|
|
"""Test search_url_callbacks for a registered URL.""" |
|
86
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
87
|
|
|
|
|
88
|
|
|
def url_handler(*args, **kwargs): |
|
89
|
|
|
return None |
|
90
|
|
|
|
|
91
|
|
|
sopel.register_url_callback(r'https://example\.com', url_handler) |
|
92
|
|
|
results = list(sopel.search_url_callbacks('https://example.com')) |
|
93
|
|
|
assert len(results) == 1, 'Expected 1 handler; found %d' % len(results) |
|
94
|
|
|
assert url_handler in results[0], 'Once registered, handler must be found' |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
View Code Duplication |
def test_search_url_callbacks_pattern(tmpconfig): |
|
|
|
|
|
|
98
|
|
|
"""Test search_url_callbacks for a registered regex pattern.""" |
|
99
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
100
|
|
|
|
|
101
|
|
|
def url_handler(*args, **kwargs): |
|
102
|
|
|
return None |
|
103
|
|
|
|
|
104
|
|
|
sopel.register_url_callback(r'https://(www\.)?example\.com', url_handler) |
|
105
|
|
|
results = list(sopel.search_url_callbacks('https://example.com')) |
|
106
|
|
|
assert len(results) == 1, 'Expected 1 handler; found %d' % len(results) |
|
107
|
|
|
assert url_handler in results[0], 'Once registered, handler must be found' |
|
108
|
|
|
|
|
109
|
|
|
results = list(sopel.search_url_callbacks('https://www.example.com')) |
|
110
|
|
|
assert len(results) == 1, 'Regex pattern must match both URLs' |
|
111
|
|
|
assert url_handler in results[0] |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
def test_search_url_callbacks_compiled_pattern(tmpconfig): |
|
115
|
|
|
"""Test search_url_callbacks for a registered compiled regex pattern.""" |
|
116
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
117
|
|
|
url_regex = re.compile(r'https://(www\.)?example\.com') |
|
118
|
|
|
|
|
119
|
|
|
def url_handler(*args, **kwargs): |
|
120
|
|
|
return None |
|
121
|
|
|
|
|
122
|
|
|
sopel.register_url_callback(url_regex, url_handler) |
|
123
|
|
|
results = list(sopel.search_url_callbacks('https://example.com')) |
|
124
|
|
|
assert len(results) == 1, 'Expected 1 handler; found %d' % len(results) |
|
125
|
|
|
assert url_handler in results[0], 'Once registered, handler must be found' |
|
126
|
|
|
|
|
127
|
|
|
results = list(sopel.search_url_callbacks('https://www.example.com')) |
|
128
|
|
|
assert len(results) == 1, 'Regex pattern must match both URLs' |
|
129
|
|
|
assert url_handler in results[0] |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
def test_search_url_callbacks_not_found(tmpconfig): |
|
133
|
|
|
"""Test search_url_callbacks when pattern does not match.""" |
|
134
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
135
|
|
|
results = sopel.search_url_callbacks('https://example.com') |
|
136
|
|
|
assert not list(results), 'No handler registered; must return an empty list' |
|
137
|
|
|
|
|
138
|
|
|
def url_handler(*args, **kwargs): |
|
139
|
|
|
return None |
|
140
|
|
|
|
|
141
|
|
|
sopel.register_url_callback(r'https://(www\.)?example\.com', url_handler) |
|
142
|
|
|
|
|
143
|
|
|
results = sopel.search_url_callbacks('https://not-example.com') |
|
144
|
|
|
assert not list(results), 'URL must not match any pattern' |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
View Code Duplication |
def test_register_url_callback_twice(tmpconfig): |
|
|
|
|
|
|
148
|
|
|
"""Test register_url_callback replace URL callbacks for a pattern.""" |
|
149
|
|
|
test_pattern = r'https://(www\.)?example\.com' |
|
150
|
|
|
|
|
151
|
|
|
def url_handler(*args, **kwargs): |
|
152
|
|
|
return None |
|
153
|
|
|
|
|
154
|
|
|
def url_handler_replacement(*args, **kwargs): |
|
155
|
|
|
return None |
|
156
|
|
|
|
|
157
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
158
|
|
|
sopel.register_url_callback(test_pattern, url_handler) |
|
159
|
|
|
|
|
160
|
|
|
results = list(sopel.search_url_callbacks('https://www.example.com')) |
|
161
|
|
|
assert url_handler in results[0] |
|
162
|
|
|
|
|
163
|
|
|
sopel.register_url_callback(test_pattern, url_handler_replacement) |
|
164
|
|
|
|
|
165
|
|
|
results = list(sopel.search_url_callbacks('https://www.example.com')) |
|
166
|
|
|
assert len(results) == 1, 'There must be one and only one callback' |
|
167
|
|
|
assert url_handler_replacement in results[0], ( |
|
168
|
|
|
'Handler must have been replaced') |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
def test_unregister_url_callback(tmpconfig): |
|
172
|
|
|
"""Test unregister_url_callback removes URL callback for a pattern.""" |
|
173
|
|
|
test_pattern = r'https://(www\.)?example\.com' |
|
174
|
|
|
|
|
175
|
|
|
def url_handler(*args, **kwargs): |
|
176
|
|
|
return None |
|
177
|
|
|
|
|
178
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
179
|
|
|
# make sure we can always call it |
|
180
|
|
|
sopel.unregister_url_callback(test_pattern) |
|
181
|
|
|
|
|
182
|
|
|
# now register a pattern, make sure it still work |
|
183
|
|
|
sopel.register_url_callback(test_pattern, url_handler) |
|
184
|
|
|
assert list(sopel.search_url_callbacks('https://www.example.com')) |
|
185
|
|
|
|
|
186
|
|
|
# unregister this pattern |
|
187
|
|
|
sopel.unregister_url_callback(test_pattern) |
|
188
|
|
|
|
|
189
|
|
|
# now it is not possible to find a callback for this pattern |
|
190
|
|
|
results = list(sopel.search_url_callbacks('https://www.example.com')) |
|
191
|
|
|
assert not results, 'Unregistered URL callback must not work anymore' |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
def test_unregister_url_callback_unknown_pattern(tmpconfig): |
|
195
|
|
|
"""Test unregister_url_callback pass when pattern is unknown.""" |
|
196
|
|
|
test_pattern = r'https://(www\.)?example\.com' |
|
197
|
|
|
|
|
198
|
|
|
def url_handler(*args, **kwargs): |
|
199
|
|
|
return None |
|
200
|
|
|
|
|
201
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
202
|
|
|
|
|
203
|
|
|
# now register a pattern, make sure it still work |
|
204
|
|
|
sopel.register_url_callback(test_pattern, url_handler) |
|
205
|
|
|
assert list(sopel.search_url_callbacks('https://www.example.com')) |
|
206
|
|
|
|
|
207
|
|
|
# unregister another pattern (that doesn't exist) |
|
208
|
|
|
sopel.unregister_url_callback(r'http://localhost') |
|
209
|
|
|
|
|
210
|
|
|
# the existing pattern still work |
|
211
|
|
|
assert list(sopel.search_url_callbacks('https://www.example.com')) |
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
def test_unregister_url_callback_compiled_pattern(tmpconfig): |
|
215
|
|
|
"""Test unregister_url_callback works with a compiled regex.""" |
|
216
|
|
|
test_pattern = r'https://(www\.)?example\.com' |
|
217
|
|
|
url_regex = re.compile(test_pattern) |
|
218
|
|
|
|
|
219
|
|
|
def url_handler(*args, **kwargs): |
|
220
|
|
|
return None |
|
221
|
|
|
|
|
222
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
223
|
|
|
|
|
224
|
|
|
# now register a pattern, make sure it still work |
|
225
|
|
|
sopel.register_url_callback(test_pattern, url_handler) |
|
226
|
|
|
assert list(sopel.search_url_callbacks('https://www.example.com')) |
|
227
|
|
|
|
|
228
|
|
|
# unregister using the compiled version |
|
229
|
|
|
sopel.unregister_url_callback(url_regex) |
|
230
|
|
|
|
|
231
|
|
|
assert not list(sopel.search_url_callbacks('https://www.example.com')) |
|
232
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
def test_multiple_url_callback(tmpconfig): |
|
235
|
|
|
"""Test multiple URL callbacks for the same URL.""" |
|
236
|
|
|
test_pattern_example = r'https://(www\.)?example\.com' |
|
237
|
|
|
test_pattern_global = r'https://.*\.com' |
|
238
|
|
|
|
|
239
|
|
|
def url_handler(*args, **kwargs): |
|
240
|
|
|
return None |
|
241
|
|
|
|
|
242
|
|
|
def url_handler_global(*args, **kwargs): |
|
243
|
|
|
return None |
|
244
|
|
|
|
|
245
|
|
|
sopel = bot.Sopel(tmpconfig, daemon=False) |
|
246
|
|
|
sopel.register_url_callback(test_pattern_example, url_handler) |
|
247
|
|
|
sopel.register_url_callback(test_pattern_global, url_handler_global) |
|
248
|
|
|
|
|
249
|
|
|
results = list(sopel.search_url_callbacks('https://example.com')) |
|
250
|
|
|
assert len(results) == 2 |
|
251
|
|
|
handlers = [result[0] for result in results] |
|
252
|
|
|
|
|
253
|
|
|
assert url_handler in handlers |
|
254
|
|
|
assert url_handler_global in handlers |
|
255
|
|
|
|
|
256
|
|
|
# now unregister one of them: the other must still work |
|
257
|
|
|
sopel.unregister_url_callback(test_pattern_example) |
|
258
|
|
|
|
|
259
|
|
|
results = list(sopel.search_url_callbacks('https://example.com')) |
|
260
|
|
|
assert len(results) == 1, 'Exactly one handler must remain' |
|
261
|
|
|
assert url_handler_global in results[0], 'Wrong remaining handler' |
|
262
|
|
|
|