allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [2/7] git commit: [#5571] ticket:302 added submit ham
Date Wed, 22 May 2013 22:31:42 GMT
[#5571] ticket:302 added submit ham


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

Branch: refs/heads/master
Commit: 5b7cbaa5fb6d920d794b2b0b91548c57348eecd0
Parents: 9badd06
Author: Yuriy Arhipov <yuriyarhipovua@yandex.ru>
Authored: Tue Apr 9 18:09:43 2013 +0400
Committer: Cory Johns <cjohns@slashdotmedia.com>
Committed: Wed May 22 20:54:26 2013 +0000

----------------------------------------------------------------------
 Allura/allura/controllers/discuss.py               |    5 +-
 Allura/allura/lib/spam/__init__.py                 |    3 +
 Allura/allura/lib/spam/akismetfilter.py            |   11 +++--
 Allura/allura/lib/spam/mollomfilter.py             |    3 +
 Allura/allura/model/discuss.py                     |   19 +++------
 Allura/allura/tests/model/test_discussion.py       |   16 ++++----
 Allura/allura/tests/unit/spam/test_akismet.py      |    7 +++
 Allura/allura/tests/unit/spam/test_mollom.py       |   30 ++++----------
 Allura/allura/tests/unit/spam/test_spam_filter.py  |    5 --
 .../forgetracker/tests/functional/test_root.py     |    6 +-
 10 files changed, 49 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b7cbaa5/Allura/allura/controllers/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/discuss.py b/Allura/allura/controllers/discuss.py
index e3284fc..5f65515 100644
--- a/Allura/allura/controllers/discuss.py
+++ b/Allura/allura/controllers/discuss.py
@@ -328,10 +328,10 @@ class PostController(BaseController):
         if kw.pop('delete', None):
             self.post.delete()
         elif kw.pop('spam', None):
-            self.post.status = 'spam'
-            g.spam_checker.submit_spam(self.post.text, artifact=self.post, user=c.user)
+            self.post.spam()
         elif kw.pop('approve', None):
             self.post.status = 'ok'
+            g.spam_checker.submit_ham(self.post.text, artifact=self.post, user=c.user)
         self.thread.update_stats()
         return dict(result ='success')
 
@@ -437,6 +437,7 @@ class ModerationController(BaseController):
                         posted.spam()
                     elif approve and posted.status != 'ok':
                         posted.status = 'ok'
+                        g.spam_checker.submit_ham(posted.text, artifact=posted, user=c.user)
                         posted.thread.last_post_date = max(
                             posted.thread.last_post_date,
                             posted.mod_date)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b7cbaa5/Allura/allura/lib/spam/__init__.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/__init__.py b/Allura/allura/lib/spam/__init__.py
index d8b52aa..8e6607a 100644
--- a/Allura/allura/lib/spam/__init__.py
+++ b/Allura/allura/lib/spam/__init__.py
@@ -35,6 +35,9 @@ class SpamFilter(object):
     def submit_spam(self, text, artifact=None, user=None, content_type='comment', **kw):
         log.info("No spam checking enabled")
 
+    def submit_ham(self, text, artifact=None, user=None, content_type='comment', **kw):
+        log.info("No spam checking enabled")
+
     @classmethod
     def get(cls, config, entry_points):
         """Return an instance of the SpamFilter impl specified in ``config``.

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b7cbaa5/Allura/allura/lib/spam/akismetfilter.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/akismetfilter.py b/Allura/allura/lib/spam/akismetfilter.py
index 13e0032..25cc82c 100644
--- a/Allura/allura/lib/spam/akismetfilter.py
+++ b/Allura/allura/lib/spam/akismetfilter.py
@@ -85,7 +85,10 @@ class AkismetSpamFilter(SpamFilter):
                                                     content_type=content_type),
                                  build_data=False)
 
-
-
-
-
+    def submit_ham(self, text, artifact=None, user=None, content_type='comment'):
+        self.service.submit_ham(text,
+                                 data=self.get_data(text=text,
+                                                    artifact=artifact,
+                                                    user=user,
+                                                    content_type=content_type),
+                                 build_data=False)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b7cbaa5/Allura/allura/lib/spam/mollomfilter.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/mollomfilter.py b/Allura/allura/lib/spam/mollomfilter.py
index a9d586b..e0b2922 100644
--- a/Allura/allura/lib/spam/mollomfilter.py
+++ b/Allura/allura/lib/spam/mollomfilter.py
@@ -79,3 +79,6 @@ class MollomSpamFilter(SpamFilter):
 
     def submit_spam(self, text, artifact=None, user=None, content_type='comment', **kw):
         self.service.sendFeedback(artifact.spam_check_id, 'spam')
+
+    def submit_ham(self, *args, **kw):
+        log.info("Mollom doesn't support reporting a ham")

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b7cbaa5/Allura/allura/model/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
index 3938a43..26ed77e 100644
--- a/Allura/allura/model/discuss.py
+++ b/Allura/allura/model/discuss.py
@@ -236,17 +236,11 @@ class Thread(Artifact, ActivityObject):
             Feed.post(self.primary(), title=p.subject, description=p.text, link=link)
         return p
 
-    def check_spam(self, post):
-        result = True
-        for role in c.user.project_role(c.project).roles:
-            if ((ProjectRole.query.get(_id=role).name == 'Admin') or
-                    (ProjectRole.query.get(_id=role).name == 'Developer')):
-                return True
-
-        if g.spam_checker.check('%s\n%s' % (post.subject, post.text), artifact=post, user=c.user):
-            result = False
-        return result
-
+    def is_spam(self, post):
+        if c.user in c.project.users_with_role('Admin', 'Developer'):
+            return False
+        else:
+            return g.spam_checker.check('%s\n%s' % (post.subject, post.text), artifact=post,
user=c.user)
 
     def post(self, text, message_id=None, parent_id=None,
              timestamp=None, ignore_security=False, **kw):
@@ -271,8 +265,7 @@ class Thread(Artifact, ActivityObject):
         if message_id is not None:
             kwargs['_id'] = message_id
         post = self.post_class()(**kwargs)
-
-        if ignore_security or self.check_spam(post):
+        if ignore_security or not self.is_spam(post):
             log.info('Auto-approving message from %s', c.user.username)
             file_info = kw.get('file_info', None)
             post.approve(file_info, notify=kw.get('notify', True))

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b7cbaa5/Allura/allura/tests/model/test_discussion.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/model/test_discussion.py b/Allura/allura/tests/model/test_discussion.py
index 68611b3..6b1cca3 100644
--- a/Allura/allura/tests/model/test_discussion.py
+++ b/Allura/allura/tests/model/test_discussion.py
@@ -358,23 +358,23 @@ def test_post_notify():
             assert False, 'send_simple must not be called'
 
 @with_setup(setUp, tearDown)
-def test_check_spam_for_admin():
+@patch('allura.model.discuss.c.project.users_with_role')
+def test_is_spam_for_admin(users):
+    users.return_value = [c.user,]
     d = M.Discussion(shortname='test', name='test')
     t = M.Thread(discussion_id=d._id, subject='Test Thread')
     t.post('This is a post')
     post = M.Post.query.get(text='This is a post')
-    assert t.check_spam(post), t.check_spam(post)
+    assert not t.is_spam(post), t.is_spam(post)
 
 @with_setup(setUp, tearDown)
-@patch('allura.model.discuss.c.user.project_role')
-def test_check_spam(role):
+@patch('allura.model.discuss.c.project.users_with_role')
+def test_is_spam(role):
     d = M.Discussion(shortname='test', name='test')
     t = M.Thread(discussion_id=d._id, subject='Test Thread')
-    role.roles.return_value = []
+    role.return_value = []
     with mock.patch('allura.controllers.discuss.g.spam_checker') as spam_checker:
         spam_checker.check.return_value = True
         post = mock.Mock()
-        assert not t.check_spam(post)
+        assert t.is_spam(post), t.is_spam(post)
         assert spam_checker.check.call_count == 1, spam_checker.call_count
-
-

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b7cbaa5/Allura/allura/tests/unit/spam/test_akismet.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/spam/test_akismet.py b/Allura/allura/tests/unit/spam/test_akismet.py
index bb9a90e..e49e46b 100644
--- a/Allura/allura/tests/unit/spam/test_akismet.py
+++ b/Allura/allura/tests/unit/spam/test_akismet.py
@@ -129,3 +129,10 @@ class TestAkismet(unittest.TestCase):
         self.akismet.submit_spam(self.content)
         self.akismet.service.submit_spam.assert_called_once_with(self.content, data=self.expected_data,
build_data=False)
 
+    @mock.patch('allura.lib.spam.akismetfilter.c')
+    @mock.patch('allura.lib.spam.akismetfilter.request')
+    def test_submit_ham(self, request, c):
+        request.headers = self.fake_headers
+        c.user = None
+        self.akismet.submit_ham(self.content)
+        self.akismet.service.submit_ham.assert_called_once_with(self.content, data=self.expected_data,
build_data=False)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b7cbaa5/Allura/allura/tests/unit/spam/test_mollom.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/unit/spam/test_mollom.py b/Allura/allura/tests/unit/spam/test_mollom.py
index 7828908..e1ee1fe 100644
--- a/Allura/allura/tests/unit/spam/test_mollom.py
+++ b/Allura/allura/tests/unit/spam/test_mollom.py
@@ -52,25 +52,23 @@ class TestMollom(unittest.TestCase):
         self.expected_data = dict(
             postBody=self.content.encode('utf8'),
             authorIP='some ip')
+        self.artifact = mock.Mock()
+        self.artifact.spam_check_id = 'test_id'
 
     @mock.patch('allura.lib.spam.mollomfilter.c')
     @mock.patch('allura.lib.spam.mollomfilter.request')
     def test_check(self, request, c):
         request.headers = self.fake_headers
         c.user = None
-        artifact = mock.Mock()
-        artifact.spam_check_id = 'test_id'
-        self.mollom.check(self.content, artifact = artifact)
+        self.mollom.check(self.content, artifact = self.artifact)
         self.mollom.service.checkContent.assert_called_once_with(**self.expected_data)
 
     @mock.patch('allura.lib.spam.mollomfilter.c')
     @mock.patch('allura.lib.spam.mollomfilter.request')
     def test_check_with_user(self, request, c):
-        artifact = mock.Mock()
-        artifact.spam_check_id = 'test_id'
         request.headers = self.fake_headers
         c.user = None
-        self.mollom.check(self.content, user=self.fake_user, artifact=artifact)
+        self.mollom.check(self.content, user=self.fake_user, artifact=self.artifact)
         expected_data = self.expected_data
         expected_data.update(authorName=u'Søme User'.encode('utf8'),
                 authorMail='user@domain')
@@ -79,11 +77,9 @@ class TestMollom(unittest.TestCase):
     @mock.patch('allura.lib.spam.mollomfilter.c')
     @mock.patch('allura.lib.spam.mollomfilter.request')
     def test_check_with_implicit_user(self, request, c):
-        artifact = mock.Mock()
-        artifact.spam_check_id = 'test_id'
         request.headers = self.fake_headers
         c.user = self.fake_user
-        self.mollom.check(self.content, artifact=artifact)
+        self.mollom.check(self.content, artifact=self.artifact)
         expected_data = self.expected_data
         expected_data.update(authorName=u'Søme User'.encode('utf8'),
                 authorMail='user@domain')
@@ -92,22 +88,14 @@ class TestMollom(unittest.TestCase):
     @mock.patch('allura.lib.spam.mollomfilter.c')
     @mock.patch('allura.lib.spam.mollomfilter.request')
     def test_check_with_fallback_ip(self, request, c):
-        artifact = mock.Mock()
-        artifact.spam_check_id = 'test_id'
         self.expected_data['authorIP'] = 'fallback ip'
         self.fake_headers.pop('X_FORWARDED_FOR')
         request.headers = self.fake_headers
         request.remote_addr = self.fake_headers['REMOTE_ADDR']
         c.user = None
-        self.mollom.check(self.content, artifact=artifact)
+        self.mollom.check(self.content, artifact=self.artifact)
         self.mollom.service.checkContent.assert_called_once_with(**self.expected_data)
 
-    @mock.patch('allura.lib.spam.mollomfilter.c')
-    @mock.patch('allura.lib.spam.mollomfilter.request')
-    def test_submit_spam(self, request, c):
-        request.headers = self.fake_headers
-        c.user = None
-        artifact = mock.Mock()
-        artifact.spam_check_id = 'test_id'
-        self.mollom.submit_spam('test', artifact=artifact)
-        assert 'test_id' in self.mollom.service.sendFeedback.call_args[0]
+    def test_submit_spam(self):
+        self.mollom.submit_spam('test', artifact=self.artifact)
+        assert self.mollom.service.sendFeedback.call_args[0] == ('test_id', 'spam'), self.mollom.service.sendFeedback.call_args[0]

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b7cbaa5/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 a871aaa..5255503 100644
--- a/Allura/allura/tests/unit/spam/test_spam_filter.py
+++ b/Allura/allura/tests/unit/spam/test_spam_filter.py
@@ -34,9 +34,6 @@ class TestSpamFilter(unittest.TestCase):
         # default no-op impl always returns False
         self.assertFalse(SpamFilter({}).check('foo'))
 
-    def test_submit_spam(self):
-        SpamFilter({}).submit_spam('foo')
-
     def test_get_default(self):
         config = {}
         entry_points = None
@@ -57,5 +54,3 @@ class TestSpamFilter(unittest.TestCase):
         result = checker.check()
         self.assertFalse(result)
         self.assertTrue(log.exception.called)
-
-

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5b7cbaa5/ForgeTracker/forgetracker/tests/functional/test_root.py
----------------------------------------------------------------------
diff --git a/ForgeTracker/forgetracker/tests/functional/test_root.py b/ForgeTracker/forgetracker/tests/functional/test_root.py
index 814edb8..a77e9c6 100644
--- a/ForgeTracker/forgetracker/tests/functional/test_root.py
+++ b/ForgeTracker/forgetracker/tests/functional/test_root.py
@@ -1972,9 +1972,9 @@ class TestEmailMonitoring(TrackerTestController):
 
     @td.with_tool('test', 'Tickets', 'doc-bugs', post_install_hook=post_install_hook)
     @patch('forgetracker.model.ticket.Notification.send_direct')
-    @patch('allura.model.discuss.Thread.check_spam')
-    def test_notifications_moderators(self, check_spam, send_direct):
-        check_spam.return_value = False
+    @patch('allura.model.discuss.Thread.is_spam')
+    def test_notifications_moderators(self, is_spam, send_direct):
+        is_spam.return_value = True
         self.new_ticket(summary='test moderation', mount_point='/doc-bugs/')
         self.app.post('/doc-bugs/1/update_ticket',{
             'summary':'test moderation',


Mime
View raw message