2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-08-01 09:31:34 +00:00

Site access updates ()

* Throw error on invalid site access policy

- If ALLOWED_HOSTS is empty, exit
- If CSRF_TRUSTED_ORIGINS is empty, exit
- Closes https://github.com/inventree/InvenTree/issues/6730

* Set parameters for devcontainer

* Update documentation

* Ignore server exit in testing mode

* Add INVENTREE_SITE_URL to CI

* Further CI updates

* Update settings.py

* Unit test updates

* More unit test updates

* Bump API version

* Re-introduce checks in settings.py

- Allow more lenient behaviour in TESTING mode
This commit is contained in:
Oliver
2024-03-18 12:53:48 +11:00
committed by GitHub
parent 0c661f4f83
commit 23f0950a76
9 changed files with 32 additions and 4 deletions

@@ -1,11 +1,14 @@
"""InvenTree API version information."""
# InvenTree API version
INVENTREE_API_VERSION = 184
INVENTREE_API_VERSION = 185
"""Increment this API version number whenever there is a significant change to the API that any clients need to know about."""
INVENTREE_API_TEXT = """
v185 - 2024-03-18 : https://github.com/inventree/InvenTree/pull/6731
- Adds a default URL to the generated API schema (http://localhost:8000)
v184 - 2024-03-17 : https://github.com/inventree/InvenTree/pull/10464
- Add additional fields for tests (start/end datetime, test station)

@@ -1002,10 +1002,11 @@ if not ALLOWED_HOSTS:
'No ALLOWED_HOSTS specified. Defaulting to ["*"] for debug mode. This is not recommended for production use'
)
ALLOWED_HOSTS = ['*']
else:
elif not TESTING:
logger.error(
'No ALLOWED_HOSTS specified. Please provide a list of allowed hosts, or specify INVENTREE_SITE_URL'
)
sys.exit(-1)
# Ensure that the ALLOWED_HOSTS do not contain any scheme info
for i, host in enumerate(ALLOWED_HOSTS):
@@ -1025,6 +1026,12 @@ CSRF_TRUSTED_ORIGINS = get_setting(
if SITE_URL and SITE_URL not in CSRF_TRUSTED_ORIGINS:
CSRF_TRUSTED_ORIGINS.append(SITE_URL)
if not TESTING and len(CSRF_TRUSTED_ORIGINS) == 0:
logger.error(
'No CSRF_TRUSTED_ORIGINS specified. Please provide a list of trusted origins, or specify INVENTREE_SITE_URL'
)
sys.exit(-1)
USE_X_FORWARDED_HOST = get_boolean_setting(
'INVENTREE_USE_X_FORWARDED_HOST',
config_key='use_x_forwarded_host',
@@ -1265,5 +1272,5 @@ SPECTACULAR_SETTINGS = {
'SCHEMA_PATH_PREFIX': '/api/',
}
if SITE_URL:
if SITE_URL and not TESTING:
SPECTACULAR_SETTINGS['SERVERS'] = [{'url': SITE_URL}]

@@ -574,6 +574,7 @@ class FormatTest(TestCase):
class TestHelpers(TestCase):
"""Tests for InvenTree helper functions."""
@override_settings(SITE_URL=None)
def test_absolute_url(self):
"""Test helper function for generating an absolute URL."""
base = 'https://demo.inventree.org:12345'
@@ -1347,6 +1348,7 @@ class TestInstanceName(InvenTreeTestCase):
site_obj = Site.objects.all().order_by('id').first()
self.assertEqual(site_obj.name, 'Testing title')
@override_settings(SITE_URL=None)
def test_instance_url(self):
"""Test instance url settings."""
# Set up required setting

@@ -12,6 +12,7 @@ from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import Client, TestCase
from django.test.utils import override_settings
from django.urls import reverse
import PIL
@@ -271,6 +272,7 @@ class SettingsTest(InvenTreeTestCase):
print(f"run_settings_check failed for user setting '{key}'")
raise exc
@override_settings(SITE_URL=None)
def test_defaults(self):
"""Populate the settings with default values."""
for key in InvenTreeSetting.SETTINGS.keys():

@@ -6,6 +6,7 @@ from django.conf import settings
from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.test.utils import override_settings
from allauth.account.models import EmailAddress
@@ -63,6 +64,7 @@ class TemplateTagTest(InvenTreeTestCase):
"""Test the 'instance name' setting."""
self.assertEqual(inventree_extras.inventree_instance_name(), 'InvenTree')
@override_settings(SITE_URL=None)
def test_inventree_base_url(self):
"""Test that the base URL tag returns correctly."""
self.assertEqual(inventree_extras.inventree_base_url(), '')