test_akismetcomments.py :  » Blog » PyBlosxom » pyblosxom-1.5-rc1 » plugins » tests » comments » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Blog » PyBlosxom 
PyBlosxom » pyblosxom 1.5 rc1 » plugins » tests » comments » test_akismetcomments.py
"""
Tests for the akismetcomments plugin.
"""

__author__ = 'Ryan Barrett <pyblosxom@ryanb.org>'
__url__ = 'http://pyblosxom.sourceforge.net/wiki/index.php/Framework_for_testing_plugins'

from plugins.tests.test_base import PluginTest
from plugins.comments.plugins import akismet,akismetcomments
import sys

class MockAkismet:
    """A mock Akismet class."""
    GOOD_KEY = 'my_test_key'
    IPADDRESS = '12.34.56.78'
    BLOG_URL = 'http://blog.url/'
    comment_check_return = None
    comment_check_error = False

    def __init__(self, key=None, blog_url=None, agent=None):
        self.key = key
        assert MockAkismet.BLOG_URL == blog_url

    def verify_key(self):
        return self.key == MockAkismet.GOOD_KEY

    def comment_check(self, comment, data=None, build_data=True, DEBUG=False):
        if MockAkismet.comment_check_error:
            MockAkismet.comment_check_error = False
            raise akismet.AkismetError()
        else:
            assert 'foo' == comment
            ret = MockAkismet.comment_check_return
            MockAkismet.comment_check_return = None
            return ret

    @classmethod
    def inject_comment_check(cls, ret):
        cls.comment_check_return = ret

    @classmethod
    def inject_comment_check_error(cls):
        cls.comment_check_error = True

class TestAkismetComments(PluginTest):
    """Test class for the akismetcomments plugin.
    """
    def setUp(self):
        PluginTest.setUp(self, akismetcomments)

        akismet.Akismet = MockAkismet

        self.config['base_url'] = MockAkismet.BLOG_URL
        self.config['akismet_api_key'] = MockAkismet.GOOD_KEY
        self.args['comment'] = {'description': "foo",
                                'ipaddress': MockAkismet.IPADDRESS}

    def test_verify_installation(self):
        """verify_installation should check for an api key and verify it."""
        self.assertEquals(
            True, akismetcomments.verify_installation(self.request))

        # try without an akismet_api_key config var
        del self.config['akismet_api_key']
        self.assertEquals(
            False, akismetcomments.verify_installation(self.request))

        # try with an import error
        akismet = sys.modules['plugins.comments.plugins.akismet']
        del sys.modules['plugins.comments.plugins.akismet']
        self.assertEquals(
            False, akismetcomments.verify_installation(self.request))
        sys.modules['plugins.comments.plugins.akismet'] = akismet

        # try with a key that doesn't verify
        self.config['akismet_api_key'] = 'bad_key'
        orig_verify_key = akismet.Akismet.verify_key
        self.assertEquals(False, akismetcomments.verify_installation(self.request))

    def test_comment_reject(self):
        """comment_reject() should pass the comment through to akismet."""
        # no comment to reject
        assert 'comment' not in self.data
        self.assertEquals(
            False,
            akismetcomments.cb_comment_reject(self.args))

        self.set_form_data({})
        self.assertEquals(
            False, akismetcomments.cb_comment_reject(self.args))
        self.set_form_data({'body': 'body'})

    def test_bad_api_key_reject(self):
        # bad api key
        self.config['akismet_api_key'] = 'bad_key'
        self.assertEquals(
            False, akismetcomments.cb_comment_reject(self.args))
        self.config['akismet_api_key'] = MockAkismet.GOOD_KEY

    def test_akismet_error(self):
        # akismet error
        MockAkismet.inject_comment_check_error()
        print akismet.Akismet.comment_check_error
        self.assertEquals(
            (True, 'Missing essential data (e.g., a UserAgent string).'),
            akismetcomments.cb_comment_reject(self.args))

    def test_akismet_ham(self):
        # akismet says ham
        MockAkismet.inject_comment_check(False)
        self.assertEquals(
            False, akismetcomments.cb_comment_reject(self.args))

    def test_akismet_spam(self):
        # akismet says spam
        MockAkismet.inject_comment_check(True)
        self.assertEquals(
            (True, 'I\'m sorry, but your comment was rejected by the <a href="'
             'http://akismet.com/">Akismet</a> spam filtering system.'),
            akismetcomments.cb_comment_reject(self.args))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.