From 2d6a78ffb810cafa2e8aff8674618e8c18c9c3ac Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 21 Jul 2021 10:25:16 +1000 Subject: [PATCH 1/3] Add unit test for validation of reverse url lookup --- InvenTree/InvenTree/test_urls.py | 141 +++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 InvenTree/InvenTree/test_urls.py diff --git a/InvenTree/InvenTree/test_urls.py b/InvenTree/InvenTree/test_urls.py new file mode 100644 index 0000000000..90345aeeb4 --- /dev/null +++ b/InvenTree/InvenTree/test_urls.py @@ -0,0 +1,141 @@ +""" +Validate that all URLs specified in template files are correct. +""" + +from django import urls +from django.conf import settings + +from django.test import TestCase +from django.urls import reverse + +import os +import re + +from pathlib import Path + + +class URLTest(TestCase): + + # Need fixture data in the database + fixtures = [ + 'settings', + 'build', + 'company', + 'manufacturer_part', + 'price_breaks', + 'supplier_part', + 'order', + 'sales_order', + 'bom', + 'category', + 'params', + 'part_pricebreaks', + 'part', + 'test_templates', + 'location', + 'stock_tests', + 'stock', + 'users', + ] + + def find_files(self, suffix): + """ + Search for all files in the template directories, + which can have URLs rendered + """ + + template_dirs = [ + ('build', 'templates'), + ('common', 'templates'), + ('company', 'templates'), + ('label', 'templates'), + ('order', 'templates'), + ('part', 'templates'), + ('report', 'templates'), + ('stock', 'templates'), + ('templates', ), + ] + + template_files = [] + + here = os.path.abspath(os.path.dirname(__file__)) + tld = os.path.join(here, '..') + + for directory in template_dirs: + + template_dir = os.path.join(tld, *directory) + + for path in Path(template_dir).rglob(suffix): + + f = os.path.abspath(path) + + if f not in template_files: + template_files.append(f) + + return template_files + + def find_urls(self, input_file): + """ + Search for all instances of {% url %} in supplied template file + """ + + urls = [] + + pattern = "{% url ['\"]([^'\"]+)['\"]([^%]*)%}" + + with open(input_file, 'r') as f: + + data = f.read() + + results = re.findall(pattern, data) + + for result in results: + if len(result) == 2: + urls.append([ + result[0].strip(), + result[1].strip() + ]) + elif len(result) == 1: + urls.append([ + result[0].strip(), + '' + ]) + + return urls + + def reverse_url(self, url_pair): + """ + Perform lookup on the URL + """ + + url, pk = url_pair + + if pk: + # We will assume that there is at least one item in the database + reverse(url, kwargs={pk: 1}) + else: + reverse(url) + + def check_file(self, f): + """ + Run URL checks for the provided file + """ + + urls = self.find_urls(f) + + for url in urls: + self.reverse_url(url) + + def test_html_templates(self): + + template_files = self.find_files("*.html") + + for f in template_files: + self.check_file(f) + + def test_js_templates(self): + + template_files = self.find_files("*.js") + + for f in template_files: + self.check_file(f) From 8cb336f581052aa7c7cb8af0d57344e3bd14ab87 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 21 Jul 2021 10:42:24 +1000 Subject: [PATCH 2/3] PEP fixes --- InvenTree/InvenTree/test_urls.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/InvenTree/InvenTree/test_urls.py b/InvenTree/InvenTree/test_urls.py index 90345aeeb4..0723332d7d 100644 --- a/InvenTree/InvenTree/test_urls.py +++ b/InvenTree/InvenTree/test_urls.py @@ -2,9 +2,6 @@ Validate that all URLs specified in template files are correct. """ -from django import urls -from django.conf import settings - from django.test import TestCase from django.urls import reverse @@ -110,9 +107,13 @@ class URLTest(TestCase): url, pk = url_pair + # TODO: Handle reverse lookup of admin URLs! + if url.startswith("admin:"): + return + if pk: # We will assume that there is at least one item in the database - reverse(url, kwargs={pk: 1}) + reverse(url, kwargs={"pk": 1}) else: reverse(url) From 893628d1b8e905e0c8097ee759ab83e9c196a013 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 21 Jul 2021 10:52:14 +1000 Subject: [PATCH 3/3] URL fixes --- .../company/templates/company/manufacturer_part_navbar.html | 4 ++-- InvenTree/stock/templates/stock/item_base.html | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/InvenTree/company/templates/company/manufacturer_part_navbar.html b/InvenTree/company/templates/company/manufacturer_part_navbar.html index 893374858f..f0af203543 100644 --- a/InvenTree/company/templates/company/manufacturer_part_navbar.html +++ b/InvenTree/company/templates/company/manufacturer_part_navbar.html @@ -24,14 +24,14 @@ {% comment "for later" %}
  • - + {% trans "Stock" %}
  • - + {% trans "Orders" %} diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index 547806e256..b16d9b0b1a 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -272,7 +272,7 @@ {% trans "Customer" %} - {{ item.customer.name }} + {{ item.customer.name }} {% endif %} {% if item.belongs_to %}