Skip to content

Commit 2a790e3

Browse files
committed
Some tests added
1 parent d186b09 commit 2a790e3

3 files changed

Lines changed: 261 additions & 9 deletions

File tree

tests.py

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,79 @@ def setUp(self):
1212
self.ct = CleanTalk(auth_key='7emegy4e')
1313

1414
def test_blacklisted(self):
15-
response = self.ct.request(message = 'abc',
16-
sender_ip = '196.19.250.114',
17-
sender_email = 'stop_email@example.com',
18-
sender_nickname = 'spam_bot',
19-
submit_time = 12,
20-
js_on = 1)
15+
response = self.ct.request(
16+
message = 'abc', # Comment visitor to the site
17+
sender_ip = '196.19.250.114', # IP address of the visitor
18+
sender_email = 'stop_email@example.com', # Email IP of the visitor
19+
sender_nickname = 'spam_bot', # Nickname of the visitor
20+
submit_time = 12, # The time taken to fill the comment form in seconds
21+
js_on = 1 # The presence of JavaScript for the site visitor, 0|1
22+
)
23+
print(response)
24+
#make sure that 'allow' is 0
25+
self.assertFalse(response['allow'])
26+
27+
def test_correct_ip(self):
28+
response = self.ct.request(
29+
message = 'abc', # Comment visitor to the site
30+
sender_ip = '109.188.126.23', # IP address of the visitor
31+
sender_email = '', # Email IP of the visitor
32+
sender_nickname = 'spam_bot', # Nickname of the visitor
33+
submit_time = 12, # The time taken to fill the comment form in seconds
34+
js_on = 1 # The presence of JavaScript for the site visitor, 0|1
35+
)
36+
print(response)
37+
self.assertTrue(response['allow'])
38+
39+
def test_correct_email(self):
40+
response = self.ct.request(
41+
message = 'abc', # Comment visitor to the site
42+
sender_ip = '', # IP address of the visitor
43+
sender_email = 'chelovek_cheloveku_volk@zombizombizombi.ru', # Email IP of the visitor
44+
sender_nickname = 'spam_bot', # Nickname of the visitor
45+
submit_time = 12, # The time taken to fill the comment form in seconds
46+
js_on = 1 # The presence of JavaScript for the site visitor, 0|1
47+
)
48+
print(response)
49+
self.assertTrue(response['allow'])
2150

51+
def test_incorrect_js_and_submit_time(self):
52+
response = self.ct.request(
53+
message = 'abc', # Comment visitor to the site
54+
sender_ip = '', # IP address of the visitor
55+
sender_email = 'aa-shi@yandex.ru', # Email IP of the visitor
56+
sender_nickname = 'aa-shi', # Nickname of the visitor
57+
submit_time = 1, # The time taken to fill the comment form in seconds
58+
js_on = 0 # The presence of JavaScript for the site visitor, 0|1
59+
)
2260
print(response)
23-
#make sure that response contain 'allow'
24-
self.assertTrue('allow' in response)
25-
#make sure that 'allow' is true
2661
self.assertFalse(response['allow'])
2762

63+
def test_js_null(self):
64+
#bad user
65+
response = self.ct.request(
66+
message = 'abc', # Comment visitor to the site
67+
sender_ip = '196.19.250.114', # IP address of the visitor
68+
sender_email = 'stop_email@example.com', # Email IP of the visitor
69+
sender_nickname = 'spam_bot', # Nickname of the visitor
70+
submit_time = 12, # The time taken to fill the comment form in seconds
71+
js_on = None # The presence of JavaScript for the site visitor, 0|1
72+
)
73+
print(response)
74+
self.assertFalse(response['allow'])
75+
76+
#good user
77+
response = self.ct.request(
78+
message = 'abc', # Comment visitor to the site
79+
sender_ip = '', # IP address of the visitor
80+
sender_email = 'aa-shi@yandex.ru', # Email IP of the visitor
81+
sender_nickname = 'aa-shi', # Nickname of the visitor
82+
submit_time = 12, # The time taken to fill the comment form in seconds
83+
js_on = None # The presence of JavaScript for the site visitor, 0|1
84+
)
85+
print(response)
86+
self.assertTrue(response['allow'])
87+
2888

2989
if __name__ == '__main__':
3090
unittest.main()

Новая папка/cleantalk.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/python
2+
#coding=utf-8
3+
4+
from __future__ import unicode_literals
5+
try:
6+
from urllib.request import urlopen, Request
7+
from urllib.parse import urlparse
8+
from urllib.parse import urlencode
9+
10+
except ImportError:
11+
from urllib2 import urlopen, Request
12+
from urlparse import urlparse
13+
from urllib import urlencode
14+
15+
import json
16+
17+
18+
class CleanTalk:
19+
"""Python API for CleanTalk.org"""
20+
VERSION = 1.0
21+
ENCODING = 'utf-8'
22+
user_agent = 'Mozilla/5.0'
23+
24+
def __init__(self,
25+
auth_key,
26+
server_url = 'http://moderate.cleantalk.ru',
27+
api_url='/api2.0',
28+
connection_timeout=3,
29+
method_name='check_message',
30+
agent = None):
31+
32+
"""
33+
This method constructs a new CleanTalk object and returns it.
34+
:param auth_key:
35+
:param server_url:
36+
:param api_url:
37+
:param connection_timeout:
38+
:param method_name:
39+
"""
40+
self.__server_url = server_url
41+
self.__api_url = api_url
42+
self.__connection_timeout = connection_timeout
43+
self.__method_name = method_name
44+
self.__auth_key = auth_key
45+
if agent:
46+
self.__agent = agent
47+
else:
48+
self.__agent = 'python-api-' + str(CleanTalk.VERSION)
49+
50+
def request(self, message, sender_ip, sender_email, sender_nickname, submit_time, js_on, example = '', method_name = None):
51+
"""
52+
This method will dispatch call to servers.
53+
Exceptions can be raised: ValueError on bad json, URLError on bad url, HTTPError, HTTPException on http-error
54+
:param message: Comment visitor to the site
55+
:param example: The text of the article to which visitor created a comment
56+
:param sender_ip: IP address of the visitor
57+
:param sender_email: Email IP of the visitor
58+
:param sender_nickname: Nickname of the visitor
59+
:param submit_time: The time taken to fill the comment form in seconds
60+
:param js_on: The presence of JavaScript for the site visitor, 0|1
61+
:param method_name:
62+
:return: dictionary, where:
63+
KEY VALUE
64+
----------- --------------------
65+
allow 0|1 - spam or not comment/registration
66+
id MD5_HEX - unique request ID
67+
comment string - description about request from server
68+
stop_queue 0|1 - should comment move to site's moderation queue or not
69+
inactive 0|1 - should registration move to inactive state or not
70+
"""
71+
if not method_name:
72+
method_name = self.__method_name
73+
74+
url = self.__server_url + self.__api_url
75+
headers = { 'User-Agent' : self.user_agent,
76+
'content-type' :'application/json; encoding=utf-8' }
77+
78+
values = {
79+
'auth_key' : self.__auth_key,
80+
'method_name' : method_name,
81+
'message' : message,
82+
'example' : example,
83+
'sender_ip' : sender_ip,
84+
'sender_email' : sender_email,
85+
'sender_nickname' : sender_nickname,
86+
'submit_time' : submit_time,
87+
'js_on' : js_on,
88+
'agent' : self.__agent
89+
}
90+
data = json.dumps(values, separators=(',',':'))
91+
print(data)
92+
request = Request(url, data.encode(CleanTalk.ENCODING), headers)
93+
response = urlopen(request, timeout=self.__connection_timeout)
94+
print(response.headers)
95+
response_bytes = response.read()
96+
response_str = response_bytes.decode(CleanTalk.ENCODING)
97+
response_parsed = json.loads(response_str)
98+
99+
if 'comment' in response_parsed:
100+
response_parsed['comment'] = response_parsed['comment'].encode('ISO-8859-1', 'ignore').decode('utf-8', 'ignore')
101+
102+
return response_parsed

Новая папка/tests.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/python
2+
#coding=utf-8
3+
4+
from __future__ import print_function
5+
from cleantalk import CleanTalk
6+
import unittest
7+
8+
9+
class TestCleanTalk(unittest.TestCase):
10+
11+
def setUp(self):
12+
self.ct = CleanTalk(auth_key='7emegy4e')
13+
14+
def test_blacklisted(self):
15+
response = self.ct.request(
16+
message = 'abc', # Comment visitor to the site
17+
sender_ip = '196.19.250.114', # IP address of the visitor
18+
sender_email = 'stop_email@example.com', # Email IP of the visitor
19+
sender_nickname = 'spam_bot', # Nickname of the visitor
20+
submit_time = 12, # The time taken to fill the comment form in seconds
21+
js_on = 1 # The presence of JavaScript for the site visitor, 0|1
22+
)
23+
print(response)
24+
#make sure that 'allow' is 0
25+
self.assertFalse(response['allow'])
26+
27+
def test_correct_ip(self):
28+
response = self.ct.request(
29+
message = 'abc', # Comment visitor to the site
30+
sender_ip = '109.188.126.23', # IP address of the visitor
31+
sender_email = '', # Email IP of the visitor
32+
sender_nickname = 'spam_bot', # Nickname of the visitor
33+
submit_time = 12, # The time taken to fill the comment form in seconds
34+
js_on = 1 # The presence of JavaScript for the site visitor, 0|1
35+
)
36+
print(response)
37+
self.assertTrue(response['allow'])
38+
39+
def test_correct_email(self):
40+
response = self.ct.request(
41+
message = 'abc', # Comment visitor to the site
42+
sender_ip = '', # IP address of the visitor
43+
sender_email = 'chelovek_cheloveku_volk@zombizombizombi.ru', # Email IP of the visitor
44+
sender_nickname = 'spam_bot', # Nickname of the visitor
45+
submit_time = 12, # The time taken to fill the comment form in seconds
46+
js_on = 1 # The presence of JavaScript for the site visitor, 0|1
47+
)
48+
print(response)
49+
self.assertTrue(response['allow'])
50+
51+
def test_incorrect_js_and_submit_time(self):
52+
response = self.ct.request(
53+
message = 'abc', # Comment visitor to the site
54+
sender_ip = '', # IP address of the visitor
55+
sender_email = 'aa-shi@yandex.ru', # Email IP of the visitor
56+
sender_nickname = 'aa-shi', # Nickname of the visitor
57+
submit_time = 1, # The time taken to fill the comment form in seconds
58+
js_on = 0 # The presence of JavaScript for the site visitor, 0|1
59+
)
60+
print(response)
61+
self.assertFalse(response['allow'])
62+
63+
def test_js_null(self):
64+
#bad user
65+
response = self.ct.request(
66+
message = 'abc', # Comment visitor to the site
67+
sender_ip = '196.19.250.114', # IP address of the visitor
68+
sender_email = 'stop_email@example.com', # Email IP of the visitor
69+
sender_nickname = 'spam_bot', # Nickname of the visitor
70+
submit_time = 12, # The time taken to fill the comment form in seconds
71+
js_on = None # The presence of JavaScript for the site visitor, 0|1
72+
)
73+
print(response)
74+
self.assertFalse(response['allow'])
75+
76+
#good user
77+
response = self.ct.request(
78+
message = 'abc', # Comment visitor to the site
79+
sender_ip = '', # IP address of the visitor
80+
sender_email = 'aa-shi@yandex.ru', # Email IP of the visitor
81+
sender_nickname = 'aa-shi', # Nickname of the visitor
82+
submit_time = 12, # The time taken to fill the comment form in seconds
83+
js_on = None # The presence of JavaScript for the site visitor, 0|1
84+
)
85+
print(response)
86+
self.assertTrue(response['allow'])
87+
88+
89+
if __name__ == '__main__':
90+
unittest.main()

0 commit comments

Comments
 (0)