diff --git a/InvenTree/InvenTree/test_urls.py b/InvenTree/InvenTree/test_urls.py new file mode 100644 index 0000000000..0723332d7d --- /dev/null +++ b/InvenTree/InvenTree/test_urls.py @@ -0,0 +1,142 @@ +""" +Validate that all URLs specified in template files are correct. +""" + +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 + + # 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}) + 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) 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" %}