You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2016/01/18 20:11:33 UTC

[1/2] allura git commit: [#8047] akismet ham/spam report sends original user, and not current request info

Repository: allura
Updated Branches:
  refs/heads/db/8047 [created] 8e334ce68


[#8047] akismet ham/spam report sends original user, and not current request info


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

Branch: refs/heads/db/8047
Commit: a7b79d9bed9c78d13220d6b886d7e8d08f71356f
Parents: 26799a1
Author: Dave Brondsema <da...@brondsema.net>
Authored: Mon Jan 18 13:58:42 2016 -0500
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Mon Jan 18 13:58:42 2016 -0500

----------------------------------------------------------------------
 Allura/allura/controllers/discuss.py          |  6 ++---
 Allura/allura/lib/spam/akismetfilter.py       | 16 +++++++------
 Allura/allura/model/discuss.py                |  2 +-
 Allura/allura/tests/unit/spam/test_akismet.py | 26 +++++++++++++---------
 4 files changed, 28 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/a7b79d9b/Allura/allura/controllers/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/discuss.py b/Allura/allura/controllers/discuss.py
index 4c0c748..c22e772 100644
--- a/Allura/allura/controllers/discuss.py
+++ b/Allura/allura/controllers/discuss.py
@@ -357,8 +357,7 @@ class PostController(BaseController):
         elif kw.pop('approve', None):
             if self.post.status != 'ok':
                 self.post.approve()
-                g.spam_checker.submit_ham(
-                    self.post.text, artifact=self.post, user=c.user)
+                g.spam_checker.submit_ham(self.post.text, artifact=self.post, user=self.post.author())
                 self.post.thread.post_to_feed(self.post)
         self.thread.update_stats()
         return dict(result='success')
@@ -473,8 +472,7 @@ class ModerationController(BaseController):
                         posted.spam()
                     elif approve and posted.status != 'ok':
                         posted.approve()
-                        g.spam_checker.submit_ham(
-                            posted.text, artifact=posted, user=c.user)
+                        g.spam_checker.submit_ham(posted.text, artifact=posted, user=posted.author())
                         posted.thread.post_to_feed(posted)
         redirect(request.referer)
 

http://git-wip-us.apache.org/repos/asf/allura/blob/a7b79d9b/Allura/allura/lib/spam/akismetfilter.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/akismetfilter.py b/Allura/allura/lib/spam/akismetfilter.py
index 4913970..34f84d0 100644
--- a/Allura/allura/lib/spam/akismetfilter.py
+++ b/Allura/allura/lib/spam/akismetfilter.py
@@ -57,7 +57,7 @@ class AkismetSpamFilter(SpamFilter):
             config.get('spam.key'), config.get('base_url'))
         self.service.verify_key()
 
-    def get_data(self, text, artifact=None, user=None, content_type='comment', **kw):
+    def get_data(self, text, artifact=None, user=None, content_type='comment', request=None, **kw):
         kw['comment_content'] = text
         kw['comment_type'] = content_type
         if artifact:
@@ -65,11 +65,11 @@ class AkismetSpamFilter(SpamFilter):
         user = user or c.user
         if user:
             kw['comment_author'] = user.display_name or user.username
-            kw['comment_author_email'] = user.email_addresses[
-                0] if user.email_addresses else ''
-        kw['user_ip'] = utils.ip_address(request)
-        kw['user_agent'] = request.headers.get('USER_AGENT')
-        kw['referrer'] = request.headers.get('REFERER')
+            kw['comment_author_email'] = user.email_addresses[0] if user.email_addresses else ''
+        if request:
+            kw['user_ip'] = utils.ip_address(request)
+            kw['user_agent'] = request.headers.get('USER_AGENT')
+            kw['referrer'] = request.headers.get('REFERER')
         # kw will be urlencoded, need to utf8-encode
         for k, v in kw.items():
             kw[k] = h.really_unicode(v).encode('utf8')
@@ -81,7 +81,9 @@ class AkismetSpamFilter(SpamFilter):
                                          data=self.get_data(text=text,
                                                             artifact=artifact,
                                                             user=user,
-                                                            content_type=content_type),
+                                                            content_type=content_type,
+                                                            request=request,
+                                                            ),
                                          build_data=False)
         log.info("spam=%s (akismet): %s" % (str(res), log_msg))
         return res

http://git-wip-us.apache.org/repos/asf/allura/blob/a7b79d9b/Allura/allura/model/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
index 69bf556..d90a14a 100644
--- a/Allura/allura/model/discuss.py
+++ b/Allura/allura/model/discuss.py
@@ -755,7 +755,7 @@ class Post(Message, VersionedArtifact, ActivityObject):
     def spam(self):
         self.status = 'spam'
         self.thread.num_replies = max(0, self.thread.num_replies - 1)
-        g.spam_checker.submit_spam(self.text, artifact=self, user=c.user)
+        g.spam_checker.submit_spam(self.text, artifact=self, user=self.author())
 
 
 class DiscussionAttachment(BaseAttachment):

http://git-wip-us.apache.org/repos/asf/allura/blob/a7b79d9b/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 79802c3..d821a66 100644
--- a/Allura/allura/tests/unit/spam/test_akismet.py
+++ b/Allura/allura/tests/unit/spam/test_akismet.py
@@ -117,21 +117,27 @@ class TestAkismet(unittest.TestCase):
             data=expected_data, build_data=False)
 
     @mock.patch('allura.lib.spam.akismetfilter.c')
-    @mock.patch('allura.lib.spam.akismetfilter.request')
-    def test_submit_spam(self, request, c):
-        request.headers = self.fake_headers
-        request.remote_addr = 'some ip'
+    def test_submit_spam(self, c):
         c.user = None
+
         self.akismet.submit_spam(self.content)
+
+        # no IP addr, UA, etc, since this isn't the original request
+        expected_data = dict(comment_content=u'spåm text'.encode('utf8'),
+                             comment_type='comment',
+                             )
         self.akismet.service.submit_spam.assert_called_once_with(
-            self.content, data=self.expected_data, build_data=False)
+            self.content, data=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
-        request.remote_addr = 'some ip'
+    def test_submit_ham(self, c):
         c.user = None
+
         self.akismet.submit_ham(self.content)
+
+        # no IP addr, UA, etc, since this isn't the original request
+        expected_data = dict(comment_content=u'spåm text'.encode('utf8'),
+                             comment_type='comment',
+                             )
         self.akismet.service.submit_ham.assert_called_once_with(
-            self.content, data=self.expected_data, build_data=False)
+            self.content, data=expected_data, build_data=False)


[2/2] allura git commit: [#8047] factor out a main_url() method and use it in spam filters to provide a better URL

Posted by br...@apache.org.
[#8047] factor out a main_url() method and use it in spam filters to provide a better URL


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

Branch: refs/heads/db/8047
Commit: 8e334ce6851b198de753e531f68feb03b90ddd16
Parents: a7b79d9
Author: Dave Brondsema <da...@brondsema.net>
Authored: Mon Jan 18 14:07:33 2016 -0500
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Mon Jan 18 14:11:17 2016 -0500

----------------------------------------------------------------------
 Allura/allura/lib/spam/akismetfilter.py       |  7 +++++-
 Allura/allura/lib/spam/mollomfilter.py        |  9 +++++--
 Allura/allura/model/discuss.py                | 28 ++++++++++++++++++----
 Allura/allura/tests/unit/spam/test_akismet.py |  2 +-
 4 files changed, 37 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/8e334ce6/Allura/allura/lib/spam/akismetfilter.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/akismetfilter.py b/Allura/allura/lib/spam/akismetfilter.py
index 34f84d0..6f199d0 100644
--- a/Allura/allura/lib/spam/akismetfilter.py
+++ b/Allura/allura/lib/spam/akismetfilter.py
@@ -61,7 +61,12 @@ class AkismetSpamFilter(SpamFilter):
         kw['comment_content'] = text
         kw['comment_type'] = content_type
         if artifact:
-            kw['permalink'] = artifact.url()
+            try:
+                # if its a comment, get wiki, ticket, etc URL
+                url = artifact.main_url()
+            except:
+                url = artifact.url()
+            kw['permalink'] = url
         user = user or c.user
         if user:
             kw['comment_author'] = user.display_name or user.username

http://git-wip-us.apache.org/repos/asf/allura/blob/8e334ce6/Allura/allura/lib/spam/mollomfilter.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/mollomfilter.py b/Allura/allura/lib/spam/mollomfilter.py
index 3651a94..88a34c8 100644
--- a/Allura/allura/lib/spam/mollomfilter.py
+++ b/Allura/allura/lib/spam/mollomfilter.py
@@ -67,10 +67,15 @@ class MollomSpamFilter(SpamFilter):
         log_msg = text
         kw['postBody'] = text
         if artifact:
+            try:
+                # if its a comment, get wiki, ticket, etc URL
+                url = artifact.main_url()
+            except:
+                url = artifact.url()
             # Should be able to send url, but can't right now due to a bug in
             # the PyMollom lib
-            # kw['url'] = artifact.url()
-            log_msg = artifact.url()
+            # kw['url'] = url
+            log_msg = url
         user = user or c.user
         if user:
             kw['authorName'] = user.display_name or user.username

http://git-wip-us.apache.org/repos/asf/allura/blob/8e334ce6/Allura/allura/model/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
index d90a14a..291f554 100644
--- a/Allura/allura/model/discuss.py
+++ b/Allura/allura/model/discuss.py
@@ -627,6 +627,28 @@ class Post(Message, VersionedArtifact, ActivityObject):
         else:  # pragma no cover
             return None
 
+    def parent_artifact(self):
+        """
+        :return: the artifact (e.g Ticket, Wiki Page) that this Post belongs to.  May return None.
+        """
+        aref = ArtifactReference.query.get(_id=self.thread.ref_id)
+        if aref and aref.artifact:
+            return aref.artifact
+        else:
+            return None
+
+    def main_url(self):
+        """
+        :return: the URL for the artifact (e.g Ticket, Wiki Page) that this Post belongs to,
+                 else the default thread URL
+        """
+        parent_artifact = self.parent_artifact()
+        if parent_artifact:
+            url = parent_artifact.url()
+        else:
+            url = self.thread.url()
+        return url
+
     def url_paginated(self):
         '''Return link to the thread with a #target that poins to this comment.
 
@@ -659,11 +681,7 @@ class Post(Message, VersionedArtifact, ActivityObject):
             page = find_i(posts) / limit
 
         slug = h.urlquote(self.slug)
-        aref = ArtifactReference.query.get(_id=self.thread.ref_id)
-        if aref and aref.artifact:
-            url = aref.artifact.url()
-        else:
-            url = self.thread.url()
+        url = self.main_url()
         if page == 0:
             return '%s?limit=%s#%s' % (url, limit, slug)
         return '%s?limit=%s&page=%s#%s' % (url, limit, page, slug)

http://git-wip-us.apache.org/repos/asf/allura/blob/8e334ce6/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 d821a66..1435c35 100644
--- a/Allura/allura/tests/unit/spam/test_akismet.py
+++ b/Allura/allura/tests/unit/spam/test_akismet.py
@@ -37,7 +37,7 @@ class TestAkismet(unittest.TestCase):
             # akismet can be successfully urlencoded
             urllib.urlencode(kw.get('data', {}))
         self.akismet.service.comment_check = mock.Mock(side_effect=side_effect)
-        self.fake_artifact = mock.Mock(**{'url.return_value': 'artifact url'})
+        self.fake_artifact = mock.Mock(**{'main_url.return_value': 'artifact url'})
         self.fake_user = mock.Mock(display_name=u'Søme User',
                                    email_addresses=['user@domain'])
         self.fake_headers = dict(