11import os
2+ import mock
23
34from django .forms import Form
45from django .test import TestCase
@@ -12,18 +13,109 @@ class RecaptchaTestForm(Form):
1213
1314
1415class TestRecaptchaForm (TestCase ):
15- def setUp (self ):
16- os .environ ['RECAPTCHA_DISABLE' ] = 'True'
17-
1816 def test_dummy_validation (self ):
17+ os .environ ['RECAPTCHA_DISABLE' ] = 'True'
1918 form = RecaptchaTestForm ({})
2019 self .assertTrue (form .is_valid ())
21-
22- def test_dummy_error (self ):
2320 del os .environ ['RECAPTCHA_DISABLE' ]
24- form = RecaptchaTestForm ({})
21+
22+ @mock .patch ('requests.post' )
23+ def test_validate_error_invalid_token (self , requests_post ):
24+
25+ recaptcha_response = {'success' : False }
26+ requests_post .return_value .json = lambda : recaptcha_response
27+
28+ form = RecaptchaTestForm ({"g-recaptcha-response" : "dummy token" })
2529 self .assertFalse (form .is_valid ())
2630
27- def tearDown (self ):
28- if 'RECAPTCHA_DISABLE' in os .environ .keys ():
29- del os .environ ['RECAPTCHA_DISABLE' ]
31+ @mock .patch ('requests.post' )
32+ def test_validate_error_lower_score (self , requests_post ):
33+
34+ recaptcha_response = {
35+ 'success' : True ,
36+ 'score' : 0.5
37+ }
38+ requests_post .return_value .json = lambda : recaptcha_response
39+
40+ class RecaptchaTestForm (Form ):
41+ recaptcha = ReCaptchaField (score_threshold = 0.7 )
42+ form = RecaptchaTestForm ({"g-recaptcha-response" : "dummy token" })
43+ self .assertFalse (form .is_valid ())
44+
45+ @mock .patch ('requests.post' )
46+ def test_validate_success_highter_score (self , requests_post ):
47+
48+ recaptcha_response = {
49+ 'success' : True ,
50+ 'score' : 0.7
51+ }
52+ requests_post .return_value .json = lambda : recaptcha_response
53+
54+ class RecaptchaTestForm (Form ):
55+ recaptcha = ReCaptchaField (score_threshold = 0.4 )
56+ form = RecaptchaTestForm ({"g-recaptcha-response" : "dummy token" })
57+ self .assertTrue (form .is_valid ())
58+
59+ @mock .patch ('requests.post' )
60+ def test_settings_score_threshold (self , requests_post ):
61+
62+ recaptcha_response = {
63+ 'success' : True ,
64+ 'score' : 0.6
65+ }
66+ requests_post .return_value .json = lambda : recaptcha_response
67+
68+ class RecaptchaTestForm (Form ):
69+ recaptcha = ReCaptchaField ()
70+ form = RecaptchaTestForm ({"g-recaptcha-response" : "dummy token" })
71+ self .assertTrue (form .is_valid ())
72+
73+ @mock .patch ('requests.post' )
74+ def test_settings_score_threshold_override_fields (self , requests_post ):
75+
76+ recaptcha_response = {
77+ 'success' : True ,
78+ 'score' : 0.6
79+ }
80+ requests_post .return_value .json = lambda : recaptcha_response
81+
82+ with self .settings (RECAPTCHA_SCORE_THRESHOLD = 0.7 ):
83+ class RecaptchaTestForm (Form ):
84+ recaptcha = ReCaptchaField ()
85+
86+ form = RecaptchaTestForm ({"g-recaptcha-response" : "dummy token" })
87+ self .assertFalse (form .is_valid ())
88+
89+ @mock .patch ('requests.post' )
90+ def test_settings_score_threshold_override_each_fields (self , requests_post ):
91+
92+ recaptcha_response = {
93+ 'success' : True ,
94+ 'score' : 0.4
95+ }
96+ requests_post .return_value .json = lambda : recaptcha_response
97+
98+ with self .settings (RECAPTCHA_SCORE_THRESHOLD = 0.7 ):
99+ class RecaptchaTestForm (Form ):
100+ recaptcha = ReCaptchaField ()
101+
102+ class RecaptchaOverrideTestForm (Form ):
103+ recaptcha = ReCaptchaField (score_threshold = 0.3 )
104+
105+ form1 = RecaptchaTestForm ({"g-recaptcha-response" : "dummy token" })
106+ self .assertFalse (form1 .is_valid ())
107+
108+ form2 = RecaptchaOverrideTestForm ({"g-recaptcha-response" : "dummy token" })
109+ self .assertTrue (form2 .is_valid ())
110+
111+ @mock .patch ('requests.post' )
112+ def test_validate_success (self , requests_post ):
113+
114+ recaptcha_response = {
115+ 'success' : True ,
116+ 'score' : 0.5
117+ }
118+ requests_post .return_value .json = lambda : recaptcha_response
119+
120+ form = RecaptchaTestForm ({"g-recaptcha-response" : "dummy token" })
121+ self .assertTrue (form .is_valid ())
0 commit comments