comparison antispam/tests/test_utils.py @ 744:8789299c75b1

Django 1.6: test discovery as per unittest.
author Brian Neal <bgneal@gmail.com>
date Sun, 29 Dec 2013 14:45:26 -0600
parents antispam/tests/utils_tests.py@08d905e38a86
children 9e803323a0d0
comparison
equal deleted inserted replaced
743:66d46d31d543 744:8789299c75b1
1 """
2 Tests for the antispam application.
3
4 """
5 import datetime
6
7 from django.test import TestCase
8 from django.contrib.auth.models import User
9 from django.contrib.contenttypes.models import ContentType
10 from django.core.cache import cache
11
12 from antispam import SPAM_PHRASE_KEY
13 from antispam.models import SpamPhrase
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
21
22
23 class AntispamCase(TestCase):
24
25 def test_no_phrases(self):
26 """
27 Tests that an empty spam phrase table works.
28
29 """
30 cache.delete(SPAM_PHRASE_KEY)
31 self.assertFalse(contains_spam("Here is some random text."))
32
33 def test_phrases(self):
34 """
35 Simple test of some phrases.
36
37 """
38 SpamPhrase.objects.create(phrase="grytner")
39 SpamPhrase.objects.create(phrase="allday.ru")
40 SpamPhrase.objects.create(phrase="stefa.pl")
41
42 self.assert_(contains_spam("grytner"))
43 self.assert_(contains_spam("11grytner"))
44 self.assert_(contains_spam("11grytner>"))
45 self.assert_(contains_spam("1djkl jsd stefa.pl"))
46 self.assert_(contains_spam("1djkl jsd <stefa.pl---sd8"))
47 self.assert_(contains_spam("1dsdjallday.rukl jsd <stefa.pl---sd8"))
48 self.assert_(contains_spam(" 1djallday.rukl"))
49 self.assertFalse(contains_spam("this one is spam free."))
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)