瀏覽代碼

remove self info answers from pages after first one

Noemi Vanyi 8 年之前
父節點
當前提交
aa227180e2
共有 2 個文件被更改,包括 22 次插入4 次删除
  1. 2
    0
      searx/plugins/self_info.py
  2. 20
    4
      tests/unit/test_plugins.py

+ 2
- 0
searx/plugins/self_info.py 查看文件

@@ -29,6 +29,8 @@ p = re.compile('.*user[ -]agent.*', re.IGNORECASE)
29 29
 #  request: flask request object
30 30
 #  ctx: the whole local context of the pre search hook
31 31
 def post_search(request, ctx):
32
+    if ctx['search'].pageno > 1:
33
+        return True
32 34
     if ctx['search'].query == 'ip':
33 35
         x_forwarded_for = request.headers.getlist("X-Forwarded-For")
34 36
         if x_forwarded_for:

+ 20
- 4
tests/unit/test_plugins.py 查看文件

@@ -52,23 +52,39 @@ class SelfIPTest(SearxTestCase):
52 52
         request = Mock(user_plugins=store.plugins,
53 53
                        remote_addr='127.0.0.1')
54 54
         request.headers.getlist.return_value = []
55
-        ctx = get_search_mock(query='ip')
55
+        ctx = get_search_mock(query='ip', pageno=1)
56 56
         store.call('post_search', request, ctx)
57 57
         self.assertTrue('127.0.0.1' in ctx['search'].result_container.answers)
58 58
 
59
+        ctx = get_search_mock(query='ip', pageno=2)
60
+        store.call('post_search', request, ctx)
61
+        self.assertFalse('127.0.0.1' in ctx['search'].result_container.answers)
62
+
59 63
         # User agent test
60 64
         request = Mock(user_plugins=store.plugins,
61 65
                        user_agent='Mock')
62 66
         request.headers.getlist.return_value = []
63 67
 
64
-        ctx = get_search_mock(query='user-agent')
68
+        ctx = get_search_mock(query='user-agent', pageno=1)
65 69
         store.call('post_search', request, ctx)
66 70
         self.assertTrue('Mock' in ctx['search'].result_container.answers)
67 71
 
68
-        ctx = get_search_mock(query='user-agent')
72
+        ctx = get_search_mock(query='user-agent', pageno=2)
73
+        store.call('post_search', request, ctx)
74
+        self.assertFalse('Mock' in ctx['search'].result_container.answers)
75
+
76
+        ctx = get_search_mock(query='user-agent', pageno=1)
69 77
         store.call('post_search', request, ctx)
70 78
         self.assertTrue('Mock' in ctx['search'].result_container.answers)
71 79
 
72
-        ctx = get_search_mock(query='What is my User-Agent?')
80
+        ctx = get_search_mock(query='user-agent', pageno=2)
81
+        store.call('post_search', request, ctx)
82
+        self.assertFalse('Mock' in ctx['search'].result_container.answers)
83
+
84
+        ctx = get_search_mock(query='What is my User-Agent?', pageno=1)
73 85
         store.call('post_search', request, ctx)
74 86
         self.assertTrue('Mock' in ctx['search'].result_container.answers)
87
+
88
+        ctx = get_search_mock(query='What is my User-Agent?', pageno=2)
89
+        store.call('post_search', request, ctx)
90
+        self.assertFalse('Mock' in ctx['search'].result_container.answers)