Exemplo n.º 1
0
class TestAkismet(unittest.TestCase):
    akismet = None

    def setUp(self):
        try:
            akismet_api_key = os.environ['AKISMET_API_KEY']
        except KeyError:
            raise EnvironmentError('Provide AKISMET_API_KEY environment setting.')
        self.akismet = Akismet(akismet_api_key, is_test=True)

    def test_check(self):
        self.assertEqual(self.akismet.check('127.0.0.1', USER_AGENT, blog='http://127.0.0.1'), SpamStatus.Ham)

    def test_check_spam(self):
        self.assertEqual(self.akismet.check('127.0.0.1', EVIL_USER_AGENT, comment_author='viagra-test-123',
                                            blog='http://127.0.0.1'), SpamStatus.ProbableSpam)

    def test_invalid_api_key(self):
        akismet = Akismet('invalid_api_key', is_test=True)
        with self.assertRaises(AkismetServerError):
            akismet.check('127.0.0.1', EVIL_USER_AGENT, blog='http://127.0.0.1')

    def test_submit_spam(self):
        self.akismet.submit_spam('127.0.0.1', EVIL_USER_AGENT, blog='http://127.0.0.1')

    def test_submit_ham(self):
        self.akismet.submit_ham('127.0.0.1', USER_AGENT, blog='http://127.0.0.1')

    def test_datetime(self):
        blog_url = 'http://127.0.0.1'
        comment_date = datetime.datetime(2016, 4, 16, 15, 12, 5)
        comment_post_modified = datetime.datetime(2016, 4, 16, 16, 27, 31)
        data = self.akismet._get_parameters({'blog': blog_url, 'comment_post_modified': comment_post_modified,
                                             'comment_date': comment_date})
        for dtkey in ['comment_date', 'comment_post_modified']:
            self.assertIn('{0}_gmt'.format(dtkey), data)
            self.assertNotIn(dtkey, data)
            self.assertEqual(data['{0}_gmt'.format(dtkey)], locals()[dtkey].isoformat())

    def test_timeout(self):
        self.akismet = Akismet(os.environ['AKISMET_API_KEY'], timeout=0.000001, is_test=True)

        with self.assertRaises(requests.ConnectionError):
            self.akismet.submit_ham('127.0.0.1', USER_AGENT, blog='http://127.0.0.1')

    def test_require_blog_param(self):
        with self.assertRaises(MissingParameterError):
            self.akismet._get_parameters({})
Exemplo n.º 2
0
class TestAkismet(unittest.TestCase):
    akismet = None

    def setUp(self):
        try:
            akismet_api_key = os.environ['AKISMET_API_KEY']
        except KeyError:
            raise EnvironmentError(
                'Provide AKISMET_API_KEY environment setting.')
        self.akismet = Akismet(akismet_api_key, is_test=True)

    def test_check(self):
        self.assertEqual(
            self.akismet.check('127.0.0.1',
                               USER_AGENT,
                               blog='http://127.0.0.1'), SpamStatus.Ham)

    def test_check_spam(self):
        self.assertEqual(
            self.akismet.check('127.0.0.1',
                               EVIL_USER_AGENT,
                               comment_author='viagra-test-123',
                               blog='http://127.0.0.1'),
            SpamStatus.ProbableSpam)

    def test_invalid_api_key(self):
        akismet = Akismet('invalid_api_key', is_test=True)
        with self.assertRaises(AkismetServerError):
            akismet.check('127.0.0.1',
                          EVIL_USER_AGENT,
                          blog='http://127.0.0.1')

    def test_submit_spam(self):
        self.akismet.submit_spam('127.0.0.1',
                                 EVIL_USER_AGENT,
                                 blog='http://127.0.0.1')

    def test_submit_ham(self):
        self.akismet.submit_ham('127.0.0.1',
                                USER_AGENT,
                                blog='http://127.0.0.1')

    def test_datetime(self):
        blog_url = 'http://127.0.0.1'
        comment_date = datetime.datetime(2016, 4, 16, 15, 12, 5)
        comment_post_modified = datetime.datetime(2016, 4, 16, 16, 27, 31)
        data = self.akismet._get_parameters({
            'blog': blog_url,
            'comment_post_modified': comment_post_modified,
            'comment_date': comment_date
        })
        for dtkey in ['comment_date', 'comment_post_modified']:
            self.assertIn('{0}_gmt'.format(dtkey), data)
            self.assertNotIn(dtkey, data)
            self.assertEqual(data['{0}_gmt'.format(dtkey)],
                             locals()[dtkey].isoformat())

    def test_timeout(self):
        self.akismet = Akismet(os.environ['AKISMET_API_KEY'],
                               timeout=0.000001,
                               is_test=True)

        with self.assertRaises(requests.ConnectionError):
            self.akismet.submit_ham('127.0.0.1',
                                    USER_AGENT,
                                    blog='http://127.0.0.1')

    def test_require_blog_param(self):
        with self.assertRaises(MissingParameterError):
            self.akismet._get_parameters({})