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