comparison antispam/tests/utils_tests.py @ 625:08d905e38a86

For issue 23, clear spammer profile fields, shouts, & elsewhere links upon deactivation.
author Brian Neal <bgneal@gmail.com>
date Thu, 01 Nov 2012 19:35:30 -0500
parents ee87ea74d46b
children
comparison
equal deleted inserted replaced
624:5d79b09aa52b 625:08d905e38a86
1 """ 1 """
2 Tests for the antispam application. 2 Tests for the antispam application.
3
3 """ 4 """
5 import datetime
6
4 from django.test import TestCase 7 from django.test import TestCase
8 from django.contrib.auth.models import User
9 from django.contrib.contenttypes.models import ContentType
5 from django.core.cache import cache 10 from django.core.cache import cache
6 11
7 from antispam import SPAM_PHRASE_KEY 12 from antispam import SPAM_PHRASE_KEY
8 from antispam.models import SpamPhrase 13 from antispam.models import SpamPhrase
9 from antispam.utils import contains_spam 14 from antispam.utils import contains_spam, deactivate_spammer
15
16 from comments.models import Comment
17 from polls.models import Poll
18 from elsewhere.models import WebsiteProfile
19 from shoutbox.models import Shout
20 from bio.models import STA_SPAMMER
10 21
11 22
12 class AntispamCase(TestCase): 23 class AntispamCase(TestCase):
13 24
14 def test_no_phrases(self): 25 def test_no_phrases(self):
15 """ 26 """
16 Tests that an empty spam phrase table works. 27 Tests that an empty spam phrase table works.
28
17 """ 29 """
18 cache.delete(SPAM_PHRASE_KEY) 30 cache.delete(SPAM_PHRASE_KEY)
19 self.assertFalse(contains_spam("Here is some random text.")) 31 self.assertFalse(contains_spam("Here is some random text."))
20 32
21 def test_phrases(self): 33 def test_phrases(self):
22 """ 34 """
23 Simple test of some phrases. 35 Simple test of some phrases.
36
24 """ 37 """
25 SpamPhrase.objects.create(phrase="grytner") 38 SpamPhrase.objects.create(phrase="grytner")
26 SpamPhrase.objects.create(phrase="allday.ru") 39 SpamPhrase.objects.create(phrase="allday.ru")
27 SpamPhrase.objects.create(phrase="stefa.pl") 40 SpamPhrase.objects.create(phrase="stefa.pl")
28 41
33 self.assert_(contains_spam("1djkl jsd <stefa.pl---sd8")) 46 self.assert_(contains_spam("1djkl jsd <stefa.pl---sd8"))
34 self.assert_(contains_spam("1dsdjallday.rukl jsd <stefa.pl---sd8")) 47 self.assert_(contains_spam("1dsdjallday.rukl jsd <stefa.pl---sd8"))
35 self.assert_(contains_spam(" 1djallday.rukl")) 48 self.assert_(contains_spam(" 1djallday.rukl"))
36 self.assertFalse(contains_spam("this one is spam free.")) 49 self.assertFalse(contains_spam("this one is spam free."))
37 50
51 def test_deactivate_spammer(self):
52 """
53 Test the deactivate_spammer() function.
54
55 """
56 user = User.objects.create_user('spammer_guy', '', 'password')
57 user.save()
58
59 profile = user.get_profile()
60 profile.location = 'Spamville'
61 profile.country = 'US'
62 profile.birthday = datetime.date.today()
63 profile.occupation = 'Spammer'
64 profile.interests = 'Spamming websites'
65 profile.profile_text = 'I spam a lot.'
66 profile.signature = 'I spammed you!'
67 profile.save()
68
69 now=datetime.datetime.now()
70
71 # create a poll item to comment on
72 poll = Poll(start_date=now,
73 end_date=now,
74 is_enabled=True,
75 question='?')
76 poll.save()
77
78 comment = Comment(
79 content_type=ContentType.objects.get_for_model(poll),
80 object_id=poll.pk,
81 user=user,
82 comment='Spam Spam Spam',
83 ip_address='127.0.0.1',
84 is_public=True,
85 is_removed=False)
86 comment.save()
87
88 website = WebsiteProfile(user=user, name='spam', url='spam')
89 website.save()
90
91 shout = Shout(user=user, shout_date=now, shout='spam')
92 shout.save()
93
94 deactivate_spammer(user)
95
96 profile = user.get_profile()
97 self.assertFalse(profile.location)
98 self.assertFalse(profile.country)
99 self.assertIsNone(profile.birthday)
100 self.assertFalse(profile.occupation)
101 self.assertFalse(profile.interests)
102 self.assertFalse(profile.profile_text)
103 self.assertFalse(profile.profile_html)
104 self.assertFalse(profile.signature)
105 self.assertFalse(profile.signature_html)
106 self.assertEqual(profile.status, STA_SPAMMER)
107
108 status_date = datetime.date(year=profile.status_date.year,
109 month=profile.status_date.month,
110 day=profile.status_date.day)
111 self.assertEqual(status_date, datetime.date.today())
112
113 self.assertEqual(Comment.objects.filter(user=user).count(), 0)
114 self.assertEqual(WebsiteProfile.objects.filter(user=user).count(), 0)
115 self.assertEqual(Shout.objects.filter(user=user).count(), 0)