diff --git a/website_recaptcha_v2/models/website.py b/website_recaptcha_v2/models/website.py index 7b6ddc77b9..f0b43094a7 100644 --- a/website_recaptcha_v2/models/website.py +++ b/website_recaptcha_v2/models/website.py @@ -10,7 +10,7 @@ from odoo import _, api, fields, models -URL = "https://www.recaptcha.net/recaptcha/api/siteverify" +RECAPTCHA_API_URL = "https://www.recaptcha.net/recaptcha/api/siteverify" RECAPTCHA_API_TIMEOUT = 30 @@ -30,8 +30,20 @@ def _get_error_message(self, errorcode=None): "invalid-input-response": _( "The response parameter is invalid or malformed." ), + "bad-request": _("The request is invalid or malformed."), + "timeout-or-duplicate": _( + "The response is no longer valid: either is too old or has " + "been used previously." + ), } - return mapping.get(errorcode, _("There was a problem with the captcha entry.")) + return mapping.get( + errorcode, + _( + "Unknown reCAPTCHA error (error code: {errorcode}).".format( + errorcode=errorcode + ) + ), + ) def is_recaptcha_v2_valid(self, form_values): """ @@ -52,15 +64,14 @@ def is_recaptcha_v2_valid(self, form_values): if not response: return (False, _("No response given.")) get_res = {"secret": self.recaptcha_v2_secret_key, "response": response} - - res = requests.post(URL, data=get_res, timeout=RECAPTCHA_API_TIMEOUT).json() - + res = requests.post( + RECAPTCHA_API_URL, data=get_res, timeout=RECAPTCHA_API_TIMEOUT + ).json() error_msg = "\n".join( self._get_error_message(error) for error in res.get("error-codes", []) ) if error_msg: return (False, error_msg) - if not res.get("success"): - return (False, self._get_error_message()) + return (False, _("The challenge was not successfully completed.")) return (True, "") diff --git a/website_recaptcha_v2/tests/test_recaptcha.py b/website_recaptcha_v2/tests/test_recaptcha.py index ae10506bbf..4af83acdc1 100644 --- a/website_recaptcha_v2/tests/test_recaptcha.py +++ b/website_recaptcha_v2/tests/test_recaptcha.py @@ -76,7 +76,7 @@ def test_captcha_false_success(self, requests_mock): {"g-recaptcha-response": "dummy_response"} ) self.assertFalse(result) - self.assertEqual(error_msg, "There was a problem with the captcha entry.") + self.assertEqual(error_msg, "The challenge was not successfully completed.") @mock.patch(imp_requests) def test_captcha_empty_response(self, requests_mock): @@ -85,7 +85,7 @@ def test_captcha_empty_response(self, requests_mock): {"g-recaptcha-response": "dummy_response"} ) self.assertFalse(result) - self.assertEqual(error_msg, "There was a problem with the captcha entry.") + self.assertEqual(error_msg, "The challenge was not successfully completed.") @mock.patch(imp_requests) def test_captcha_unknown_error(self, requests_mock): @@ -94,7 +94,9 @@ def test_captcha_unknown_error(self, requests_mock): {"g-recaptcha-response": "dummy_response"} ) self.assertFalse(result) - self.assertEqual(error_msg, "There was a problem with the captcha entry.") + self.assertEqual( + error_msg, "Unknown reCAPTCHA error (error code: unknown-error)." + ) @mock.patch(imp_requests) def test_captcha_no_errors_and_success(self, requests_mock):