11#!/usr/bin/python
2- #coding=utf-8
2+ # coding=utf-8
33
44from __future__ import print_function
55from cleantalk import CleanTalk
6+ import os
67import unittest
78
89
910class TestCleanTalk (unittest .TestCase ):
1011
1112 def setUp (self ):
12- self .ct = CleanTalk (auth_key = '7emegy4e' )
13+ # Make sure you have defined system environment variable 'CLEANTALK_TEST_API_KEY'.
14+ # Use your operating system tools or IDE tools to set this.
15+ self .ct = CleanTalk (auth_key = os .getenv ('CLEANTALK_TEST_API_KEY' ))
1316
1417 def test_blacklisted (self ):
1518 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
19+ message = 'abc' , # Comment visitor to the site
20+ sender_ip = '196.19.250.114' , # IP address of the visitor
21+ sender_email = 's@cleantalk.org' , # Email IP of the visitor
22+ sender_nickname = 'spam_bot' , # Nickname of the visitor
23+ submit_time = 12 , # The time taken to fill the comment form in seconds
24+ js_on = 1 # The presence of JavaScript for the site visitor, 0|1
2225 )
2326 print (response )
24- #make sure that 'allow' is 0
27+ # make sure that 'allow' is 0
2528 self .assertFalse (response ['allow' ])
2629
2730 def test_correct_ip (self ):
2831 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
32+ message = 'Good text.' , # Comment visitor to the site
33+ sender_ip = '127.0.0.1' , # IP address of the visitor
34+ sender_email = 'support@cleantalk.org' , # Email IP of the visitor
35+ sender_nickname = 'real human' , # Nickname of the visitor
36+ submit_time = 12 , # The time taken to fill the comment form in seconds
37+ js_on = 1 # The presence of JavaScript for the site visitor, 0|1
3538 )
3639 print (response )
3740 self .assertTrue (response ['allow' ])
3841
39- def test_correct_email (self ):
42+ def test_incorrect_email (self ):
4043 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
44+ message = 'Good text.' , # Comment visitor to the site
45+ sender_ip = '127.0.0.1' , # IP address of the visitor
46+ sender_email = 'sadasdas@cleaasdasdntalk.org' , # Email IP of the visitor
47+ sender_nickname = 'real human' , # Nickname of the visitor
48+ submit_time = 12 , # The time taken to fill the comment form in seconds
49+ js_on = 1 # The presence of JavaScript for the site visitor, 0|1
4750 )
4851 print (response )
49- self .assertTrue (response ['allow' ])
52+ self .assertTrue (bool (response ['codes' ].find ('EMAIL_DOMAIN_NOT_EXISTS' )))
53+ self .assertFalse (response ['allow' ])
5054
5155 def test_incorrect_js_and_submit_time (self ):
5256 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
57+ message = 'Good text.' , # Comment visitor to the site
58+ sender_ip = '127.0.0.1' , # IP address of the visitor
59+ sender_email = 'good@cleantalk.org' , # Email IP of the visitor
60+ sender_nickname = 'aa-shi' , # Nickname of the visitor
61+ submit_time = 1 , # The time taken to fill the comment form in seconds
62+ js_on = 0 # The presence of JavaScript for the site visitor, 0|1
5963 )
6064 print (response )
61- self .assertFalse (response ['allow' ])
65+ self .assertTrue (bool (response ['codes' ].find ('JS DISABLED' )))
66+ self .assertTrue (bool (response ['codes' ].find ('FAST_SUBMIT' )))
6267
6368 def test_js_null (self ):
64- #bad user
69+ # bad user
6570 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
71+ message = 'abc' , # Comment visitor to the site
72+ sender_ip = '196.19.250.114' , # IP address of the visitor
73+ sender_email = 'stop_email@example.com' , # Email IP of the visitor
74+ sender_nickname = 'spam_bot' , # Nickname of the visitor
75+ submit_time = 12 , # The time taken to fill the comment form in seconds
76+ js_on = None # The presence of JavaScript for the site visitor, 0|1
7277 )
7378 print (response )
74- self .assertFalse ( response ['allow' ] )
79+ self .assertTrue ( bool ( response ['codes' ]. find ( 'JS DISABLED' )) )
7580
76- #good user
81+ # good user
7782 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
83+ message = 'abc' , # Comment visitor to the site
84+ sender_ip = '127.0.0.1' , # IP address of the visitor
85+ sender_email = 's@cleantalk.org' , # Email IP of the visitor
86+ sender_nickname = 'aa-shi' , # Nickname of the visitor
87+ submit_time = 12 , # The time taken to fill the comment form in seconds
88+ js_on = None # The presence of JavaScript for the site visitor, 0|1
8489 )
8590 print (response )
86- self .assertTrue (response ['allow' ])
91+ self .assertTrue (bool (response ['codes' ].find ('JS DISABLED' )))
92+ self .assertFalse (response ['allow' ])
8793
8894
8995if __name__ == '__main__' :
90- unittest .main ()
96+ unittest .main ()
0 commit comments