Skip to content

Commit

Permalink
[IMP] improve failure and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
huguesdk committed Dec 3, 2024
1 parent 5e0108b commit 071623e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
25 changes: 18 additions & 7 deletions website_recaptcha_v2/models/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
"""
Expand All @@ -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, "")
8 changes: 5 additions & 3 deletions website_recaptcha_v2/tests/test_recaptcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 071623e

Please sign in to comment.