# HG changeset patch # User Brian Neal # Date 1351816530 18000 # Node ID 08d905e38a863cf01a81d05545849d47f0877172 # Parent 5d79b09aa52b857dd6e2281897eed9a3bc082afe For issue 23, clear spammer profile fields, shouts, & elsewhere links upon deactivation. diff -r 5d79b09aa52b -r 08d905e38a86 antispam/tests/utils_tests.py --- a/antispam/tests/utils_tests.py Wed Oct 31 20:23:12 2012 -0500 +++ b/antispam/tests/utils_tests.py Thu Nov 01 19:35:30 2012 -0500 @@ -1,12 +1,23 @@ """ 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 +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): @@ -14,6 +25,7 @@ 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.")) @@ -21,6 +33,7 @@ def test_phrases(self): """ Simple test of some phrases. + """ SpamPhrase.objects.create(phrase="grytner") SpamPhrase.objects.create(phrase="allday.ru") @@ -35,3 +48,68 @@ 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) diff -r 5d79b09aa52b -r 08d905e38a86 antispam/utils.py --- a/antispam/utils.py Wed Oct 31 20:23:12 2012 -0500 +++ b/antispam/utils.py Thu Nov 01 19:35:30 2012 -0500 @@ -79,14 +79,23 @@ profile = user.get_profile() profile.status = STA_SPAMMER profile.status_date = datetime.datetime.now() - profile.reset_text_fields() + profile.reset() if profile.avatar: profile.avatar.delete(save=False) profile.save() + # delete comments & forum posts Comment.objects.filter(user=user).delete() delete_user_posts(user) + # delete elsewhere links + user.social_network_profiles.all().delete() + user.instant_messenger_profiles.all().delete() + user.website_profiles.all().delete() + + # delete shouts + user.shout_set.all().delete() + logging.info("User deactivated for spam: %s", user.username) diff -r 5d79b09aa52b -r 08d905e38a86 bio/models.py --- a/bio/models.py Wed Oct 31 20:23:12 2012 -0500 +++ b/bio/models.py Thu Nov 01 19:35:30 2012 -0500 @@ -151,13 +151,15 @@ user_is_active.boolean = True user_is_active.short_description = "Is Active" - def reset_text_fields(self): + def reset(self): """ - Reset profile text fields to empty defaults. + Reset profile fields to empty defaults. This function is useful when a spammer is identified. """ self.location = '' + self.country = '' + self.birthday = None self.occupation = '' self.interests = '' self.profile_text = ''