-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_plugins.py
228 lines (179 loc) · 6.94 KB
/
test_plugins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# encoding: utf-8
"""
Tests for plugin loading via PCA
"""
import pytest
from pyutilib.component.core import PluginGlobals
from ckan.common import config
import ckan.logic as logic
import ckan.authz as authz
import ckan.plugins as plugins
from ckan.plugins.core import find_system_plugins
from ckan.lib.create_test_data import CreateTestData
from ckan.tests import factories
def _make_calls(*args):
out = []
for arg in args:
out.append(((arg,), {}))
return out
def get_calls(mock_observer_func):
"""Given a mock IPluginObserver method, returns the plugins that caused its
methods to be called, so basically a list of plugins that
loaded/unloaded"""
return [call_tuple[0][0].name for call_tuple in mock_observer_func.calls]
class IFoo(plugins.Interface):
pass
class IBar(plugins.Interface):
pass
class FooImpl(object):
plugins.implements(IFoo)
class BarImpl(object):
plugins.implements(IBar)
class FooBarImpl(object):
plugins.implements(IFoo)
plugins.implements(IBar)
def test_implemented_by():
assert IFoo.implemented_by(FooImpl)
assert IFoo.implemented_by(FooBarImpl)
assert not IFoo.implemented_by(BarImpl)
def test_implemented_by_raises_exception_on_instances():
with pytest.raises(TypeError):
IFoo.implemented_by(FooImpl())
def test_provided_by():
assert IFoo.provided_by(FooImpl())
assert IFoo.provided_by(FooBarImpl())
assert not IFoo.provided_by(BarImpl())
@pytest.fixture
def observer():
observer = plugins.load("test_observer_plugin")
observer.reset_calls()
yield observer
plugins.unload("test_observer_plugin")
def test_notified_on_load(observer):
with plugins.use_plugin("action_plugin"):
assert get_calls(observer.before_load) == ["action_plugin"]
assert get_calls(observer.after_load) == ["action_plugin"]
assert get_calls(observer.before_unload) == []
assert get_calls(observer.after_unload) == []
def test_notified_on_unload(observer):
with plugins.use_plugin("action_plugin") as action:
observer.reset_calls()
assert observer.before_load.calls == []
assert observer.after_load.calls == []
assert observer.before_unload.calls == _make_calls(action)
assert observer.after_unload.calls == _make_calls(action)
@pytest.fixture(autouse=True)
def reset_observer():
plugins.load("test_observer_plugin")
plugins.unload("test_observer_plugin")
def test_plugins_load(monkeypatch):
monkeypatch.setitem(config, "ckan.plugins", "mapper_plugin routes_plugin")
plugins.load_all()
# synchronous_search automatically gets loaded
current_plugins = set(
[
plugins.get_plugin(p)
for p in ["mapper_plugin", "routes_plugin", "synchronous_search"]
+ find_system_plugins()
]
)
assert set(plugins.core._PLUGINS_SERVICE.values()) == current_plugins
@pytest.mark.ckan_config("ckan.plugins", "mapper_plugin")
@pytest.mark.usefixtures("with_plugins")
def test_only_configured_plugins_loaded():
plugin = plugins.get_plugin("mapper_plugin")
# MapperPlugin should be loaded as it is listed in
assert plugin in plugins.PluginImplementations(plugins.IMapper)
# MapperPlugin2 and RoutesPlugin should NOT be loaded
assert len(plugins.PluginImplementations(plugins.IMapper)) == 1
@pytest.mark.ckan_config("ckan.plugins", "mapper_plugin")
@pytest.mark.usefixtures("with_plugins", "clean_db")
def test_mapper_plugin_fired_on_insert():
plugin = plugins.get_plugin("mapper_plugin")
CreateTestData.create_arbitrary([{"name": u"testpkg"}])
assert plugin.calls == [
("before_insert", "testpkg"),
("after_insert", "testpkg"),
]
@pytest.mark.ckan_config("ckan.plugins", "mapper_plugin")
@pytest.mark.usefixtures("with_plugins", "clean_db")
def test_mapper_plugin_fired_on_delete():
plugin = plugins.get_plugin("mapper_plugin")
CreateTestData.create_arbitrary([{"name": u"testpkg"}])
plugin.calls = []
# remove this data
user = factories.User()
context = {"user": user["name"]}
logic.get_action("package_delete")(context, {"id": "testpkg"})
# state=deleted doesn't trigger before_delete()
assert plugin.calls == []
from ckan import model
# purging the package does trigger before_delete()
model.Package.get("testpkg").purge()
model.Session.commit()
model.Session.remove()
assert plugin.calls == [
("before_delete", "testpkg"),
("after_delete", "testpkg"),
]
class TestPlugins:
def test_plugin_loading_order(self):
"""
Check that plugins are loaded in the order specified in the config
"""
config_plugins = config["ckan.plugins"]
config[
"ckan.plugins"
] = "test_observer_plugin mapper_plugin mapper_plugin2"
plugins.load_all()
observerplugin = plugins.get_plugin("test_observer_plugin")
expected_order = _make_calls(
plugins.get_plugin("mapper_plugin"),
plugins.get_plugin("mapper_plugin2"),
)
assert observerplugin.before_load.calls[:2] == expected_order
expected_order = _make_calls(
plugins.get_plugin("test_observer_plugin"),
plugins.get_plugin("mapper_plugin"),
plugins.get_plugin("mapper_plugin2"),
)
assert observerplugin.after_load.calls[:3] == expected_order
config[
"ckan.plugins"
] = "test_observer_plugin mapper_plugin2 mapper_plugin"
plugins.load_all()
expected_order = _make_calls(
plugins.get_plugin("mapper_plugin2"),
plugins.get_plugin("mapper_plugin"),
)
assert observerplugin.before_load.calls[:2] == expected_order
expected_order = _make_calls(
plugins.get_plugin("test_observer_plugin"),
plugins.get_plugin("mapper_plugin2"),
plugins.get_plugin("mapper_plugin"),
)
assert observerplugin.after_load.calls[:3] == expected_order
# cleanup
config["ckan.plugins"] = config_plugins
plugins.load_all()
def test_action_plugin_override(self):
status_show_original = logic.get_action("status_show")(None, {})
with plugins.use_plugin("action_plugin"):
assert (
logic.get_action("status_show")(None, {})
!= status_show_original
)
assert (
logic.get_action("status_show")(None, {}) == status_show_original
)
def test_auth_plugin_override(self):
package_list_original = authz.is_authorized("package_list", {})
with plugins.use_plugin("auth_plugin"):
assert (
authz.is_authorized("package_list", {})
!= package_list_original
)
assert authz.is_authorized("package_list", {}) == package_list_original
def test_inexistent_plugin_loading(self):
with pytest.raises(plugins.PluginNotFoundException):
plugins.load("inexistent-plugin")