From 9e29a944d777c78608ab0a57e2ddf18a8a6b6639 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 19 Oct 2022 17:05:55 +0530 Subject: [PATCH] fix: validate website settings (#18446) (#18469) (cherry picked from commit c2f43c4b26c857dd9cd238ec17958b8008b503e3) Co-authored-by: Ankush Menat --- .../website_settings/test_website_settings.py | 31 +++++++++++++++++-- .../website_settings/website_settings.py | 12 +++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/frappe/website/doctype/website_settings/test_website_settings.py b/frappe/website/doctype/website_settings/test_website_settings.py index 1e8410fb6e..5f75e46d5e 100644 --- a/frappe/website/doctype/website_settings/test_website_settings.py +++ b/frappe/website/doctype/website_settings/test_website_settings.py @@ -1,8 +1,35 @@ # Copyright (c) 2020, Frappe Technologies and Contributors # License: MIT. See LICENSE -# import frappe + +import frappe from frappe.tests.utils import FrappeTestCase +from frappe.website.doctype.website_settings.website_settings import get_website_settings class TestWebsiteSettings(FrappeTestCase): - pass + def test_child_items_in_top_bar(self): + ws = frappe.get_doc("Website Settings") + ws.append( + "top_bar_items", + {"label": "Parent Item"}, + ) + ws.append( + "top_bar_items", + {"parent_label": "Parent Item", "label": "Child Item"}, + ) + ws.save() + + context = get_website_settings() + + for item in context.top_bar_items: + if item.label == "Parent Item": + self.assertEqual(item.child_items[0].label, "Child Item") + break + else: + self.fail("Child items not found") + + def test_redirect_setups(self): + ws = frappe.get_doc("Website Settings") + + ws.append("route_redirects", {"source": "/engineering/(*.)", "target": "/development/(*.)"}) + self.assertRaises(frappe.ValidationError, ws.validate) diff --git a/frappe/website/doctype/website_settings/website_settings.py b/frappe/website/doctype/website_settings/website_settings.py index 0d6e8d2d1f..ef48203cda 100644 --- a/frappe/website/doctype/website_settings/website_settings.py +++ b/frappe/website/doctype/website_settings/website_settings.py @@ -1,5 +1,6 @@ # Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # License: MIT. See LICENSE +import re from urllib.parse import quote import frappe @@ -16,6 +17,7 @@ class WebsiteSettings(Document): self.validate_footer_items() self.validate_home_page() self.validate_google_settings() + self.validate_redirects() def validate_home_page(self): if frappe.flags.in_install: @@ -72,6 +74,16 @@ class WebsiteSettings(Document): if self.enable_google_indexing and not frappe.db.get_single_value("Google Settings", "enable"): frappe.throw(_("Enable Google API in Google Settings.")) + def validate_redirects(self): + for idx, row in enumerate(self.route_redirects): + try: + source = row.source.strip("/ ") + "$" + re.compile(source) + re.sub(source, row.target, "") + except Exception as e: + if not frappe.flags.in_migrate: + frappe.throw(_("Invalid redirect regex in row #{}: {}").format(idx, str(e))) + def on_update(self): self.clear_cache()