Skip to content

Commit

Permalink
[IMP] Add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edescalona committed Dec 6, 2024
1 parent b68a6f2 commit 959c4d8
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 25 deletions.
26 changes: 18 additions & 8 deletions website_recaptcha_v2_form/controllers/form.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import json

from odoo import http

from odoo.addons.website.controllers.form import WebsiteForm
from odoo.addons.website_recaptcha_v2_form.controllers.main import BinhexHome

from .main import BinhexHome


class WebsiteRecaptchaForm(WebsiteForm):
@http.route('/website/form/<string:model_name>', type='http', auth="public", methods=['POST'], website=True,
csrf=False)
@http.route(
"/website/form/<string:model_name>",
type="http",
auth="public",
methods=["POST"],
website=True,
csrf=False,
)
def website_form(self, model_name, **kwargs):
if kwargs.get('recaptcha_enabled', False):
if kwargs.get("recaptcha_enabled", False):
valid = BinhexHome.verify_recaptcha_v2(self, values=kwargs)
if not isinstance(valid, bool):
return json.dumps({
'error': valid,
})
return json.dumps(
{
"error": valid,
}
)
return super().website_form(model_name, **kwargs)

Check warning on line 30 in website_recaptcha_v2_form/controllers/form.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/form.py#L30

Added line #L30 was not covered by tests
16 changes: 8 additions & 8 deletions website_recaptcha_v2_form/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def verify_recaptcha_v2(self, args=None, kw=None, template="", values=None):
message_error = str(
e.args[0] if len(e.args) > 0 else _("Recaptcha is not valid.")
)
if template in ('web.login', 'auth_signup.reset_password', 'auth_signup.signup'):
values.update(
{
"error": message_error
}
)
if template in (
"web.login",
"auth_signup.reset_password",
"auth_signup.signup",
):
values.update({"error": message_error})
response = request.render(template, values)
response.headers["X-Frame-Options"] = "SAMEORIGIN"
response.headers["Content-Security-Policy"] = "frame-ancestors 'self'"
Expand All @@ -50,8 +50,8 @@ def web_login(self, redirect=None, **kw):
# that the recaptcha is not checked again to avoid errors.

if (
values.get("confirm_password", "") == ""
and request.httprequest.url.find("web/signup") == -1
values.get("confirm_password", "") == ""
and request.httprequest.url.find("web/signup") == -1
):
return self.verify_recaptcha_v2(

Check warning on line 56 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L56

Added line #L56 was not covered by tests
kw=kw, template="web.login", values=values
Expand Down
2 changes: 1 addition & 1 deletion website_recaptcha_v2_form/static/src/css/recaptcha.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion website_recaptcha_v2_form/static/src/css/recaptcha.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ odoo.define("website_recaptcha_v2_form.s_website_form", function (require) {
this.$target.data("model_name")),
form_values
)
.then(async function (result_data) {
.then(async function (data) {
// Restore send button behavior
self.$target
.find(".s_website_form_send, .o_website_form_send")
.removeAttr("disabled")
.removeClass("disabled");
result_data = JSON.parse(result_data);
var result_data = JSON.parse(data);
if (!result_data.id) {
// Failure, the server didn't return the created record ID
self.update_status(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ odoo.define("website_recaptcha_v2_form.form_editor", function (require) {
) || ""
);
}
return this._super(...arguments);
return this._super(methodName, params);
},
});
});
11 changes: 7 additions & 4 deletions website_recaptcha_v2_form/static/src/xml/website_form_editor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
window.reload();
}
</script>
<script src="https://www.recaptcha.net/recaptcha/api.js" async="1"/>
<div class="g-recaptcha" t-att-data-sitekey="recaptcha_site_key"
data-callback="callback_success_recaptcha"
data-expired-callback="callback_expired_recaptcha"/>
<script src="https://www.recaptcha.net/recaptcha/api.js" async="1" />
<div
class="g-recaptcha"
t-att-data-sitekey="recaptcha_site_key"
data-callback="callback_success_recaptcha"
data-expired-callback="callback_expired_recaptcha"
/>
</div>
</t>

Expand Down
1 change: 1 addition & 0 deletions website_recaptcha_v2_form/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_recaptcha
from . import test_controller_form
28 changes: 28 additions & 0 deletions website_recaptcha_v2_form/tests/test_controller_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json

from odoo.tests.common import HttpCase

imp_requests = "odoo.addons.website_recaptcha_v2.models.website.requests"


class TestControllerForm(HttpCase):
def open_url_form(self, data=None, url="/website/form/res.partner"):
if not data:
data = {
"recaptcha_enabled": True,
"g-recaptcha": "",
}
res = self.url_open(
url=url,
data=data,
)
return res

def test_recaptcha_enabled_form(self):
res = self.open_url_form()
recaptcha_not_response = json.loads(res.content.decode("utf-8"))
self.assertEqual(
recaptcha_not_response.get("error"),
"No response given.",
msg=recaptcha_not_response.get("error"),
)
20 changes: 20 additions & 0 deletions website_recaptcha_v2_form/tests/test_recaptcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,23 @@ def test_captcha_not_valid(self, requests_mock):
requests_mock.post().json.return_value = {"success": False}
with self.assertRaises(AccessDenied):
self.website.valid_recaptcha({"g-recaptcha-response": ""})

def test_valid_recaptcha_v2_site_key(self):
recaptcha_v2_site_key = self.website.recaptcha_v2_site_key
self.assertEqual(recaptcha_v2_site_key, "test-site")
recaptcha_v2_site_key = ""
self.assertNotEqual(
recaptcha_v2_site_key,
"test-site",
msg="The website key for recaptcha is empty.",
)

def test_valid_recaptcha_v2_recaptcha_v2_secret_key(self):
recaptcha_v2_secret_key = self.website.recaptcha_v2_secret_key
self.assertEqual(recaptcha_v2_secret_key, "test-secret")
recaptcha_v2_secret_key = ""
self.assertNotEqual(
recaptcha_v2_secret_key,
"test-site",
msg="The website key for recaptcha is empty.",
)

0 comments on commit 959c4d8

Please sign in to comment.