Sfoglia il codice sorgente

[enh] plugin tests

Adam Tauber 10 anni fa
parent
commit
f57149f912
1 ha cambiato i file con 51 aggiunte e 0 eliminazioni
  1. 51
    0
      searx/tests/test_plugins.py

+ 51
- 0
searx/tests/test_plugins.py Vedi File

@@ -0,0 +1,51 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+from searx.testing import SearxTestCase
4
+from searx import plugins
5
+from mock import Mock
6
+
7
+
8
+class PluginStoreTest(SearxTestCase):
9
+
10
+    def test_PluginStore_init(self):
11
+        store = plugins.PluginStore()
12
+        self.assertTrue(isinstance(store.plugins, list) and len(store.plugins) == 0)
13
+
14
+    def test_PluginStore_register(self):
15
+        store = plugins.PluginStore()
16
+        testplugin = plugins.Plugin()
17
+        store.register(testplugin)
18
+
19
+        self.assertTrue(len(store.plugins) == 1)
20
+
21
+    def test_PluginStore_call(self):
22
+        store = plugins.PluginStore()
23
+        testplugin = plugins.Plugin()
24
+        store.register(testplugin)
25
+        setattr(testplugin, 'asdf', Mock())
26
+        request = Mock(user_plugins=[])
27
+        store.call('asdf', request, Mock())
28
+
29
+        self.assertFalse(testplugin.asdf.called)
30
+
31
+        request.user_plugins.append(testplugin)
32
+        store.call('asdf', request, Mock())
33
+
34
+        self.assertTrue(testplugin.asdf.called)
35
+
36
+
37
+class SelfIPTest(SearxTestCase):
38
+
39
+    def test_PluginStore_init(self):
40
+        store = plugins.PluginStore()
41
+        store.register(plugins.self_ip)
42
+
43
+        self.assertTrue(len(store.plugins) == 1)
44
+
45
+        request = Mock(user_plugins=store.plugins,
46
+                       remote_addr='127.0.0.1')
47
+        request.headers.getlist.return_value = []
48
+        ctx = {'search': Mock(answers=set(),
49
+                              query='ip')}
50
+        store.call('pre_search', request, ctx)
51
+        self.assertTrue('127.0.0.1' in ctx['search'].answers)