Skip to content

Commit 27b4544

Browse files
committed
add settings score_thresold
1 parent 9107d78 commit 27b4544

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ INSTALLED_APPS = (
2525
)
2626
```
2727

28-
And add your reCaptcha private and public key to your django settings.py and the default action name:
28+
And add your reCaptcha private and public key to your django settings.py and the default action name, recaptcha score threshold:
2929

3030
```python
3131
RECAPTCHA_PRIVATE_KEY = 'your private key'
3232
RECAPTCHA_PUBLIC_KEY = 'your public key'
3333
RECAPTCHA_DEFAULT_ACTION = 'generic'
34+
RECAPTCHA_SCORE_THRESHOLD = 0.5
3435

3536
```
3637

snowpenguin/django/recaptcha3/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class ReCaptchaField(forms.CharField):
1717
def __init__(self, attrs=None, *args, **kwargs):
1818
self._private_key = kwargs.pop('private_key', settings.RECAPTCHA_PRIVATE_KEY)
19-
self._score_threshold = kwargs.pop('score_threshold', None)
19+
self._score_threshold = kwargs.pop('score_threshold', settings.RECAPTCHA_SCORE_THRESHOLD)
2020

2121
if 'widget' not in kwargs:
2222
kwargs['widget'] = ReCaptchaHiddenInput()

snowpenguin/django/recaptcha3/tests.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,58 @@ class RecaptchaTestForm(Form):
5656
form = RecaptchaTestForm({"g-recaptcha-response": "dummy token"})
5757
self.assertTrue(form.is_valid())
5858

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+
59111
@mock.patch('requests.post')
60112
def test_validate_success(self, requests_post):
61113

test_settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
RECAPTCHA_PRIVATE_KEY = 'your private key'
1313
RECAPTCHA_PUBLIC_KEY = 'your public key'
1414
RECAPTCHA_DEFAULT_ACTION = 'generic'
15+
RECAPTCHA_SCORE_THRESHOLD = 0.5

0 commit comments

Comments
 (0)