mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 15:15:42 +00:00 
			
		
		
		
	Merge pull request #1854 from SchrodingersGat/url-unit-test
Add unit test for validation of reverse url lookup
This commit is contained in:
		
							
								
								
									
										142
									
								
								InvenTree/InvenTree/test_urls.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										142
									
								
								InvenTree/InvenTree/test_urls.py
									
									
									
									
									
										Normal file
									
								
							@@ -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)
 | 
				
			||||||
@@ -24,14 +24,14 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    {% comment "for later" %}
 | 
					    {% comment "for later" %}
 | 
				
			||||||
    <li class='list-group-item {% if tab == "stock" %}active{% endif %}' title='{% trans "Manufacturer Part Stock" %}'>
 | 
					    <li class='list-group-item {% if tab == "stock" %}active{% endif %}' title='{% trans "Manufacturer Part Stock" %}'>
 | 
				
			||||||
        <a href='{% url "manufacturer-part-stock" part.id %}'>
 | 
					        <a href='#'>
 | 
				
			||||||
            <span class='fas fa-boxes sidebar-icon'></span>
 | 
					            <span class='fas fa-boxes sidebar-icon'></span>
 | 
				
			||||||
            {% trans "Stock" %}
 | 
					            {% trans "Stock" %}
 | 
				
			||||||
        </a>
 | 
					        </a>
 | 
				
			||||||
    </li>
 | 
					    </li>
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    <li class='list-group-item {% if tab == "orders" %}active{% endif %}' title='{% trans "Manufacturer Part Orders" %}'>
 | 
					    <li class='list-group-item {% if tab == "orders" %}active{% endif %}' title='{% trans "Manufacturer Part Orders" %}'>
 | 
				
			||||||
        <a href='{% url "manufacturer-part-orders" part.id %}'>
 | 
					        <a href='#'>
 | 
				
			||||||
            <span class='fas fa-shopping-cart sidebar-icon'></span>
 | 
					            <span class='fas fa-shopping-cart sidebar-icon'></span>
 | 
				
			||||||
            {% trans "Orders" %}
 | 
					            {% trans "Orders" %}
 | 
				
			||||||
        </a>
 | 
					        </a>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -272,7 +272,7 @@
 | 
				
			|||||||
    <tr>
 | 
					    <tr>
 | 
				
			||||||
        <td><span class='fas fa-user-tie'></span></td>
 | 
					        <td><span class='fas fa-user-tie'></span></td>
 | 
				
			||||||
        <td>{% trans "Customer" %}</td>
 | 
					        <td>{% trans "Customer" %}</td>
 | 
				
			||||||
        <td><a href="{% url 'company-detail-assigned-stock' item.customer.id %}">{{ item.customer.name }}</a></td>
 | 
					        <td><a href="{% url 'company-detail' item.customer.id %}?display=assigned-stock">{{ item.customer.name }}</a></td>
 | 
				
			||||||
    </tr>
 | 
					    </tr>
 | 
				
			||||||
    {% endif %}
 | 
					    {% endif %}
 | 
				
			||||||
    {% if item.belongs_to %}
 | 
					    {% if item.belongs_to %}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user