~sirn/fanboi2

9a0e916affa5df44631e6ae2753ac2c759d5df63 — Kridsada Thanabulpong 7 years ago 1517a02
Make Akismet timeout when it could not receive any data in 2 seconds.

Preventing Akismet error from breaking the whole posting worker.
3 files changed, 27 insertions(+), 12 deletions(-)

M fanboi2/tests/test_utils.py
M fanboi2/utils.py
M setup.py
M fanboi2/tests/test_utils.py => fanboi2/tests/test_utils.py +10 -0
@@ 76,6 76,7 @@ class TestAkismet(unittest.TestCase):
            'https://hogehoge.rest.akismet.com/1.1/comment-check',
            headers=mock.ANY,
            data=mock.ANY,
            timeout=mock.ANY,
        )

    @mock.patch('requests.post')


@@ 88,8 89,17 @@ class TestAkismet(unittest.TestCase):
            'https://hogehoge.rest.akismet.com/1.1/comment-check',
            headers=mock.ANY,
            data=mock.ANY,
            timeout=mock.ANY,
        )

    @mock.patch('requests.post')
    def test_spam_timeout(self, api_call):
        import requests
        request = self._makeRequest()
        akismet = self._makeOne()
        api_call.side_effect = requests.Timeout('connection timed out')
        self.assertEqual(akismet.spam(request, 'buy viagra'), False)

    # noinspection PyTypeChecker
    @mock.patch('requests.post')
    def test_spam_no_key(self, api_call):

M fanboi2/utils.py => fanboi2/utils.py +16 -11
@@ 32,23 32,28 @@ class Akismet(object):
        return requests.post(
            'https://%s.rest.akismet.com/1.1/%s' % (self.key, name),
            headers={'User-Agent': "Fanboi2/%s | Akismet/0.1" % __VERSION__},
            data=data)
            data=data,
            timeout=2)

    def spam(self, request, message):
        """Returns :type:`True` if `message` is spam. Always returns
        :type:`False` if Akismet key is not set.
        :type:`False` if Akismet key is not set or the request to Akismet
        was timed out.
        """
        if self.key:
            request = serialize_request(request)
            return self._api_post('comment-check', data={
                'blog': request['application_url'],
                'user_ip': request['remote_addr'],
                'user_agent': request['user_agent'],
                'referrer': request['referrer'],
                'permalink': request['url'],
                'comment_type': 'comment',
                'comment_content': message,
            }).content == b'true'
            try:
                return self._api_post('comment-check', data={
                    'blog': request['application_url'],
                    'user_ip': request['remote_addr'],
                    'user_agent': request['user_agent'],
                    'referrer': request['referrer'],
                    'permalink': request['url'],
                    'comment_type': 'comment',
                    'comment_content': message,
                }).content == b'true'
            except requests.Timeout:
                return False
        return False



M setup.py => setup.py +1 -1
@@ 47,7 47,7 @@ requires = [
    ]

setup(name='fanboi2',
      version='0.8.1',
      version='0.8.2',
      description='fanboi2',
      long_description=readme + '\n\n' + changes,
      classifiers=[