view antispam/tests/utils_tests.py @ 693:ad69236e8501

For issue #52, update many 3rd party Javascript libraries. Updated to jquery 1.10.2, jquery ui 1.10.3. This broke a lot of stuff. - Found a newer version of the jquery cycle all plugin (3.0.3). - Updated JPlayer to 2.4.0. - Updated to MarkItUp 1.1.14. This also required me to add multiline attributes set to true on various buttons in the markdown set. - As per a stackoverflow post, added some code to get multiline titles in a jQuery UI dialog. They removed that functionality but allow you to put it back. Tweaked the MarkItUp preview CSS to show blockquotes in italic. Did not update TinyMCE at this time. I'm not using the JQuery version and this version appears to work ok for now. What I should do is make a repo for MarkItUp and do a vendor branch thing so I don't have to futz around diffing directories to figure out if I'll lose changes when I update.
author Brian Neal <bgneal@gmail.com>
date Wed, 04 Sep 2013 19:55:20 -0500
parents 08d905e38a86
children
line wrap: on
line source
"""
Tests for the antispam application.

"""
import datetime

from django.test import TestCase
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache

from antispam import SPAM_PHRASE_KEY
from antispam.models import SpamPhrase
from antispam.utils import contains_spam, deactivate_spammer

from comments.models import Comment
from polls.models import Poll
from elsewhere.models import WebsiteProfile
from shoutbox.models import Shout
from bio.models import STA_SPAMMER


class AntispamCase(TestCase):

    def test_no_phrases(self):
        """
        Tests that an empty spam phrase table works.

        """
        cache.delete(SPAM_PHRASE_KEY)
        self.assertFalse(contains_spam("Here is some random text."))

    def test_phrases(self):
        """
        Simple test of some phrases.

        """
        SpamPhrase.objects.create(phrase="grytner")
        SpamPhrase.objects.create(phrase="allday.ru")
        SpamPhrase.objects.create(phrase="stefa.pl")

        self.assert_(contains_spam("grytner"))
        self.assert_(contains_spam("11grytner"))
        self.assert_(contains_spam("11grytner>"))
        self.assert_(contains_spam("1djkl jsd stefa.pl"))
        self.assert_(contains_spam("1djkl jsd <stefa.pl---sd8"))
        self.assert_(contains_spam("1dsdjallday.rukl jsd <stefa.pl---sd8"))
        self.assert_(contains_spam(" 1djallday.rukl"))
        self.assertFalse(contains_spam("this one is spam free."))

    def test_deactivate_spammer(self):
        """
        Test the deactivate_spammer() function.

        """
        user = User.objects.create_user('spammer_guy', '', 'password')
        user.save()

        profile = user.get_profile()
        profile.location = 'Spamville'
        profile.country = 'US'
        profile.birthday = datetime.date.today()
        profile.occupation = 'Spammer'
        profile.interests = 'Spamming websites'
        profile.profile_text = 'I spam a lot.'
        profile.signature = 'I spammed you!'
        profile.save()

        now=datetime.datetime.now()

        # create a poll item to comment on
        poll = Poll(start_date=now,
                end_date=now,
                is_enabled=True,
                question='?')
        poll.save()

        comment = Comment(
            content_type=ContentType.objects.get_for_model(poll),
            object_id=poll.pk,
            user=user,
            comment='Spam Spam Spam',
            ip_address='127.0.0.1',
            is_public=True,
            is_removed=False)
        comment.save()

        website = WebsiteProfile(user=user, name='spam', url='spam')
        website.save()

        shout = Shout(user=user, shout_date=now, shout='spam')
        shout.save()

        deactivate_spammer(user)

        profile = user.get_profile()
        self.assertFalse(profile.location)
        self.assertFalse(profile.country)
        self.assertIsNone(profile.birthday)
        self.assertFalse(profile.occupation)
        self.assertFalse(profile.interests)
        self.assertFalse(profile.profile_text)
        self.assertFalse(profile.profile_html)
        self.assertFalse(profile.signature)
        self.assertFalse(profile.signature_html)
        self.assertEqual(profile.status, STA_SPAMMER)

        status_date = datetime.date(year=profile.status_date.year,
                month=profile.status_date.month,
                day=profile.status_date.day)
        self.assertEqual(status_date, datetime.date.today())

        self.assertEqual(Comment.objects.filter(user=user).count(), 0)
        self.assertEqual(WebsiteProfile.objects.filter(user=user).count(), 0)
        self.assertEqual(Shout.objects.filter(user=user).count(), 0)