From 959c4d8cff54a1863391ed04190d00344e518f12 Mon Sep 17 00:00:00 2001 From: edescalona Date: Fri, 6 Dec 2024 09:30:39 -0500 Subject: [PATCH] [IMP] Add new tests --- website_recaptcha_v2_form/controllers/form.py | 26 +++++++++++------ website_recaptcha_v2_form/controllers/main.py | 16 +++++------ .../static/src/css/recaptcha.css | 2 +- .../static/src/css/recaptcha.css.map | 2 +- .../static/src/snippets/s_website_form/000.js | 4 +-- .../src/snippets/s_website_form/options.js | 2 +- .../static/src/xml/website_form_editor.xml | 11 +++++--- website_recaptcha_v2_form/tests/__init__.py | 1 + .../tests/test_controller_form.py | 28 +++++++++++++++++++ .../tests/test_recaptcha.py | 20 +++++++++++++ 10 files changed, 87 insertions(+), 25 deletions(-) create mode 100644 website_recaptcha_v2_form/tests/test_controller_form.py diff --git a/website_recaptcha_v2_form/controllers/form.py b/website_recaptcha_v2_form/controllers/form.py index 93cf9de18b..477881d3d7 100644 --- a/website_recaptcha_v2_form/controllers/form.py +++ b/website_recaptcha_v2_form/controllers/form.py @@ -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/', type='http', auth="public", methods=['POST'], website=True, - csrf=False) + @http.route( + "/website/form/", + 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) diff --git a/website_recaptcha_v2_form/controllers/main.py b/website_recaptcha_v2_form/controllers/main.py index a0b6aa89fb..0aac83a320 100644 --- a/website_recaptcha_v2_form/controllers/main.py +++ b/website_recaptcha_v2_form/controllers/main.py @@ -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'" @@ -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( kw=kw, template="web.login", values=values diff --git a/website_recaptcha_v2_form/static/src/css/recaptcha.css b/website_recaptcha_v2_form/static/src/css/recaptcha.css index 8a6bfd26f2..2a4de6cb60 100644 --- a/website_recaptcha_v2_form/static/src/css/recaptcha.css +++ b/website_recaptcha_v2_form/static/src/css/recaptcha.css @@ -1,5 +1,5 @@ div.s_website_form_recaptcha_v2 > div.g-recaptcha { - margin-left: 18% !important; + margin-left: 18% !important; } /*# sourceMappingURL=recaptcha.css.map */ diff --git a/website_recaptcha_v2_form/static/src/css/recaptcha.css.map b/website_recaptcha_v2_form/static/src/css/recaptcha.css.map index 86d6983513..5b27d9f714 100644 --- a/website_recaptcha_v2_form/static/src/css/recaptcha.css.map +++ b/website_recaptcha_v2_form/static/src/css/recaptcha.css.map @@ -1 +1 @@ -{"version":3,"sourceRoot":"","sources":["../scss/recaptcha.scss"],"names":[],"mappings":"AACI;EACI","file":"recaptcha.css"} \ No newline at end of file +{"version":3,"sourceRoot":"","sources":["../scss/recaptcha.scss"],"names":[],"mappings":"AACI;EACI","file":"recaptcha.css"} diff --git a/website_recaptcha_v2_form/static/src/snippets/s_website_form/000.js b/website_recaptcha_v2_form/static/src/snippets/s_website_form/000.js index b0a1665d53..42322bc7fd 100644 --- a/website_recaptcha_v2_form/static/src/snippets/s_website_form/000.js +++ b/website_recaptcha_v2_form/static/src/snippets/s_website_form/000.js @@ -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( diff --git a/website_recaptcha_v2_form/static/src/snippets/s_website_form/options.js b/website_recaptcha_v2_form/static/src/snippets/s_website_form/options.js index 045c59efeb..e9cffdefaa 100644 --- a/website_recaptcha_v2_form/static/src/snippets/s_website_form/options.js +++ b/website_recaptcha_v2_form/static/src/snippets/s_website_form/options.js @@ -38,7 +38,7 @@ odoo.define("website_recaptcha_v2_form.form_editor", function (require) { ) || "" ); } - return this._super(...arguments); + return this._super(methodName, params); }, }); }); diff --git a/website_recaptcha_v2_form/static/src/xml/website_form_editor.xml b/website_recaptcha_v2_form/static/src/xml/website_form_editor.xml index 2e45dcb2a6..02150a753f 100644 --- a/website_recaptcha_v2_form/static/src/xml/website_form_editor.xml +++ b/website_recaptcha_v2_form/static/src/xml/website_form_editor.xml @@ -11,10 +11,13 @@ window.reload(); } -