Child: [424999] (diff)

Download this file

test_spam.py    81 lines (72 with data), 3.4 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import mock
import unittest
try:
from allura.lib.spam.akismetservice import Akismet
except ImportError:
Akismet = None
@unittest.skipIf(Akismet is None, "Can't import Akismet")
class TestAkismet(unittest.TestCase):
def setUp(self):
self.akismet = Akismet()
self.akismet.comment_check = mock.Mock()
self.fake_artifact = mock.Mock(**{'url.return_value': 'artifact url'})
self.fake_user = mock.Mock(display_name='Some User',
email_addresses=['user@domain'])
self.fake_environ = dict(
REMOTE_ADDR='some ip',
HTTP_USER_AGENT='some browser',
HTTP_REFERER='some url')
self.expected_data = dict(
comment_type='comment',
user_ip='some ip',
user_agent='some browser',
referrer='some url')
@mock.patch('allura.lib.spam.akismetservice.c')
@mock.patch('allura.lib.spam.akismetservice.request')
def test_check(self, request, c):
request.environ = self.fake_environ
c.user = None
self.akismet.check('spam text')
self.akismet.comment_check.assert_called_once_with('spam text',
data=self.expected_data, build_data=False)
@mock.patch('allura.lib.spam.akismetservice.c')
@mock.patch('allura.lib.spam.akismetservice.request')
def test_check_with_explicit_content_type(self, request, c):
request.environ = self.fake_environ
c.user = None
self.akismet.check('spam text', content_type='some content type')
self.expected_data['comment_type'] = 'some content type'
self.akismet.comment_check.assert_called_once_with('spam text',
data=self.expected_data, build_data=False)
@mock.patch('allura.lib.spam.akismetservice.c')
@mock.patch('allura.lib.spam.akismetservice.request')
def test_check_with_artifact(self, request, c):
request.environ = self.fake_environ
c.user = None
self.akismet.check('spam text', artifact=self.fake_artifact)
expected_data = self.expected_data
expected_data['permalink'] = 'artifact url'
self.akismet.comment_check.assert_called_once_with('spam text',
data=expected_data, build_data=False)
@mock.patch('allura.lib.spam.akismetservice.c')
@mock.patch('allura.lib.spam.akismetservice.request')
def test_check_with_user(self, request, c):
request.environ = self.fake_environ
c.user = None
self.akismet.check('spam text', user=self.fake_user)
expected_data = self.expected_data
expected_data.update(comment_author='Some User',
comment_author_email='user@domain')
self.akismet.comment_check.assert_called_once_with('spam text',
data=expected_data, build_data=False)
@mock.patch('allura.lib.spam.akismetservice.c')
@mock.patch('allura.lib.spam.akismetservice.request')
def test_check_with_implicit_user(self, request, c):
request.environ = self.fake_environ
c.user = self.fake_user
self.akismet.check('spam text')
expected_data = self.expected_data
expected_data.update(comment_author='Some User',
comment_author_email='user@domain')
self.akismet.comment_check.assert_called_once_with('spam text',
data=expected_data, build_data=False)