incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [1/2] git commit: [5580] Spam filter replies; make spam filters exceptionless
Date Mon, 21 Jan 2013 20:34:43 GMT
[5580] Spam filter replies; make spam filters exceptionless


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/8a9b8f3c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/8a9b8f3c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/8a9b8f3c

Branch: refs/heads/master
Commit: 8a9b8f3c0bf6cecfae6405c060f15d725a45c055
Parents: 7c8d7a6
Author: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Authored: Mon Jan 14 22:33:50 2013 +0000
Committer: Cory Johns <johnsca@geek.net>
Committed: Mon Jan 21 20:24:51 2013 +0000

----------------------------------------------------------------------
 Allura/allura/controllers/discuss.py               |    4 ++-
 Allura/allura/lib/spam/__init__.py                 |    5 +++-
 Allura/allura/tests/unit/spam/test_spam_filter.py  |   22 ++++++++++++---
 .../forgediscussion/tests/functional/test_forum.py |    4 ++-
 4 files changed, 28 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/8a9b8f3c/Allura/allura/controllers/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/discuss.py b/Allura/allura/controllers/discuss.py
index f040a7f..6b7eb7e 100644
--- a/Allura/allura/controllers/discuss.py
+++ b/Allura/allura/controllers/discuss.py
@@ -323,7 +323,8 @@ class PostController(BaseController):
     def reply(self, **kw):
         require_access(self.thread, 'post')
         kw = self.W.edit_post.to_python(kw, None)
-        self.thread.add_post(parent_id=self.post._id, **kw)
+        p = self.thread.add_post(parent_id=self.post._id, **kw)
+        is_spam = g.spam_checker.check(kw['text'], artifact=p, user=c.user)
         redirect(request.referer)
 
     @h.vardec
@@ -475,6 +476,7 @@ class PostRestController(PostController):
         require_access(self.thread, 'post')
         kw = self.W.edit_post.to_python(kw, None)
         post = self.thread.post(parent_id=self.post._id, **kw)
+        is_spam = g.spam_checker.check(kw['text'], artifact=post, user=c.user)
         self.thread.num_replies += 1
         redirect(post.slug.split('/')[-1] + '/')
 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/8a9b8f3c/Allura/allura/lib/spam/__init__.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/__init__.py b/Allura/allura/lib/spam/__init__.py
index 5c5841d..6e9d54d 100644
--- a/Allura/allura/lib/spam/__init__.py
+++ b/Allura/allura/lib/spam/__init__.py
@@ -1,5 +1,6 @@
 import logging
 
+from allura.lib.decorators import exceptionless
 
 log = logging.getLogger(__name__)
 
@@ -22,4 +23,6 @@ class SpamFilter(object):
         if not method:
             return cls(config)
         result = entry_points[method]
-        return result(config)
+        filter_obj = result(config)
+        filter_obj.check = exceptionless(False, log=log)(filter_obj.check)
+        return filter_obj

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/8a9b8f3c/Allura/allura/tests/unit/spam/test_spam_filter.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/spam/test_spam_filter.py b/Allura/allura/tests/unit/spam/test_spam_filter.py
index 990cb70..37b4dd4 100644
--- a/Allura/allura/tests/unit/spam/test_spam_filter.py
+++ b/Allura/allura/tests/unit/spam/test_spam_filter.py
@@ -1,12 +1,16 @@
 # -*- coding: utf-8 -*-
-
 import mock
 import unittest
-import urllib
 
 from allura.lib.spam import SpamFilter
 
 
+class MockFilter(SpamFilter):
+    def check(*args, **kw):
+        raise Exception("test exception")
+        return True
+
+
 class TestSpamFilter(unittest.TestCase):
     def test_check(self):
         # default no-op impl always returns False
@@ -20,7 +24,17 @@ class TestSpamFilter(unittest.TestCase):
 
     def test_get_method(self):
         config = {'spam.method': 'mock'}
-        entry_points = {'mock': mock.Mock}
+        entry_points = {'mock': MockFilter}
         checker = SpamFilter.get(config, entry_points)
-        self.assertTrue(isinstance(checker, mock.Mock))
+        self.assertTrue(isinstance(checker, MockFilter))
+
+    @mock.patch('allura.lib.spam.log')
+    def test_exceptionless_check(self, log):
+        config = {'spam.method': 'mock'}
+        entry_points = {'mock': MockFilter}
+        checker = SpamFilter.get(config, entry_points)
+        result = checker.check()
+        self.assertFalse(result)
+        self.assertTrue(log.exception.called)
+
 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/8a9b8f3c/ForgeDiscussion/forgediscussion/tests/functional/test_forum.py
----------------------------------------------------------------------
diff --git a/ForgeDiscussion/forgediscussion/tests/functional/test_forum.py b/ForgeDiscussion/forgediscussion/tests/functional/test_forum.py
index 7d6f4e4..8937e68 100644
--- a/ForgeDiscussion/forgediscussion/tests/functional/test_forum.py
+++ b/ForgeDiscussion/forgediscussion/tests/functional/test_forum.py
@@ -422,7 +422,8 @@ class TestForum(TestController):
                 extra_environ=dict(username='*anonymous')).follow()
         assert 'Post awaiting moderation' in r
 
-    def test_thread(self):
+    @mock.patch('forgediscussion.controllers.root.g.spam_checker')
+    def test_thread(self, spam_checker):
         r = self.app.get('/discussion/create_topic/')
         f = r.html.find('form',{'action':'/p/test/discussion/save_new_topic'})
         params = dict()
@@ -446,6 +447,7 @@ class TestForum(TestController):
                 params[field['name']] = field.has_key('value') and field['value'] or ''
         params[f.find('textarea')['name']] = 'bbb'
         thread = self.app.post(str(rep_url), params=params)
+        spam_checker.check.call_args[0] == 'bbb', spam_checker.check.call_args[0]
         thread = self.app.get(url)
         # beautiful soup is getting some unicode error here - test without it
         assert thread.html.findAll('div',{'class':'display_post'})[0].find('p').string ==
'aaa'


Mime
View raw message