2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-08 16:28:49 +00:00

Remove coupling of tests to cui ()

* remove coupling of tests to cui

* all testing for PUI absolute urls

* remove overlapping test

* refactor middleware check

* move static things into global

* factor out test target

* re-add api exception

* keep using settings.js for now
This commit is contained in:
Matthias Mair 2024-11-08 09:36:33 +01:00 committed by GitHub
parent 8f24119f29
commit cd9f491dc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 63 additions and 51 deletions

@ -36,6 +36,24 @@ def get_token_from_request(request):
return None
# List of target URL endpoints where *do not* want to redirect to
urls = [
reverse_lazy('account_login'),
reverse_lazy('account_logout'),
reverse_lazy('admin:login'),
reverse_lazy('admin:logout'),
]
# Do not redirect requests to any of these paths
paths_ignore = [
'/api/',
'/auth/',
'/js/', # TODO - remove when CUI is removed
settings.MEDIA_URL,
settings.STATIC_URL,
]
class AuthRequiredMiddleware:
"""Check for user to be authenticated."""
@ -108,23 +126,6 @@ class AuthRequiredMiddleware:
if not authorized:
path = request.path_info
# List of URL endpoints we *do not* want to redirect to
urls = [
reverse_lazy('account_login'),
reverse_lazy('account_logout'),
reverse_lazy('admin:login'),
reverse_lazy('admin:logout'),
]
# Do not redirect requests to any of these paths
paths_ignore = [
'/api/',
'/auth/',
'/js/',
settings.MEDIA_URL,
settings.STATIC_URL,
]
if path not in urls and not any(
path.startswith(p) for p in paths_ignore
):

@ -2,7 +2,6 @@
from django.conf import settings
from django.http import Http404
from django.test import tag
from django.urls import reverse
from error_report.models import Error
@ -11,8 +10,6 @@ from InvenTree.exceptions import log_error
from InvenTree.unit_test import InvenTreeTestCase
# TODO change test to not rely on CUI
@tag('cui')
class MiddlewareTests(InvenTreeTestCase):
"""Test for middleware functions."""
@ -32,25 +29,17 @@ class MiddlewareTests(InvenTreeTestCase):
# logout
self.client.logout()
# check that static files go through
# TODO @matmair re-enable this check
# self.check_path('/static/css/inventree.css', 302)
# check that account things go through
self.check_path(reverse('account_login'))
# logout goes directly to login
self.check_path(reverse('account_logout'))
# check that frontend code is redirected to login
response = self.check_path(reverse('stats'), 302)
self.assertEqual(response.url, '/accounts/login/?next=/stats/')
# check that a 401 is raised
self.check_path(reverse('settings.js'), 401)
response = self.check_path(reverse('index'), 302)
self.assertEqual(response.url, '/accounts/login/?next=/')
def test_token_auth(self):
"""Test auth with token auth."""
target = reverse('settings.js') # for PUI only use 'api-license'
# get token
response = self.client.get(reverse('api-token'), format='json', data={})
token = response.data['token']
@ -58,16 +47,17 @@ class MiddlewareTests(InvenTreeTestCase):
# logout
self.client.logout()
# this should raise a 401
self.check_path(reverse('settings.js'), 401)
# request with token
self.check_path(reverse('settings.js'), HTTP_Authorization=f'Token {token}')
self.check_path(target, 401)
# Request with broken token
self.check_path(reverse('settings.js'), 401, HTTP_Authorization='Token abcd123')
self.check_path(target, 401, HTTP_Authorization='Token abcd123')
# should still fail without token
self.check_path(reverse('settings.js'), 401)
self.check_path(target, 401)
# request with token
self.check_path(target, HTTP_Authorization=f'Token {token}')
def test_error_exceptions(self):
"""Test that ignored errors are not logged."""
@ -80,7 +70,7 @@ class MiddlewareTests(InvenTreeTestCase):
# Test normal setup
check()
response = self.client.get(reverse('part-detail', kwargs={'pk': 9999}))
response = self.client.get(reverse('api-part-detail', kwargs={'pk': 9999}))
self.assertEqual(response.status_code, 404)
check()
@ -92,13 +82,8 @@ class MiddlewareTests(InvenTreeTestCase):
# Test setup without ignored errors
settings.IGNORED_ERRORS = []
response = self.client.get(reverse('part-detail', kwargs={'pk': 9999}))
self.assertEqual(response.status_code, 404)
check(1)
# Test manual logging
try:
raise Http404
except Http404:
log_error('testpath')
check(2)
check(1)

@ -11,7 +11,7 @@ from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import mail
from django.core.exceptions import ValidationError
from django.test import TestCase, override_settings, tag
from django.test import TestCase, override_settings
from django.urls import reverse
from django.utils import timezone
@ -1493,8 +1493,6 @@ class MagicLoginTest(InvenTreeTestCase):
self.assertEqual(resp.wsgi_request.user, self.user)
# TODO - refactor to not use CUI
@tag('cui')
class MaintenanceModeTest(InvenTreeTestCase):
"""Unit tests for maintenance mode."""

@ -477,7 +477,9 @@ if settings.ENABLE_PLATFORM_FRONTEND:
frontendpatterns += [
path(
'accounts/login/',
RedirectView.as_view(url=settings.FRONTEND_URL_BASE, permanent=False),
RedirectView.as_view(
url=f'/{settings.FRONTEND_URL_BASE}', permanent=False
),
name='account_login',
)
]

@ -47,6 +47,8 @@ class BuildTestSimple(InvenTreeTestCase):
b1 = Build.objects.get(pk=1)
if settings.ENABLE_CLASSIC_FRONTEND:
self.assertEqual(b1.get_absolute_url(), '/build/1/')
else:
self.assertEqual(b1.get_absolute_url(), '/platform/manufacturing/build-order/1')
def test_is_complete(self):
"""Test build completion status"""

@ -62,6 +62,10 @@ class CompanySimpleTest(TestCase):
c = Company.objects.get(pk=1)
if settings.ENABLE_CLASSIC_FRONTEND:
self.assertEqual(c.get_absolute_url(), '/company/1/')
else:
self.assertEqual(
c.get_absolute_url(), '/platform/purchasing/manufacturer/1'
)
def test_image_renamer(self):
"""Test the company image upload functionality."""

@ -46,6 +46,11 @@ class OrderTest(TestCase):
self.assertEqual(
order.get_absolute_url(), f'/order/purchase-order/{pk}/'
)
else:
self.assertEqual(
order.get_absolute_url(),
f'/platform/purchasing/purchase-order/{pk}',
)
self.assertEqual(order.reference, f'PO-{pk:04d}')

@ -130,6 +130,10 @@ class CategoryTest(TestCase):
"""Test that the PartCategory URL works."""
if settings.ENABLE_CLASSIC_FRONTEND:
self.assertEqual(self.capacitors.get_absolute_url(), '/part/category/3/')
else:
self.assertEqual(
self.capacitors.get_absolute_url(), '/platform/part/category/3'
)
def test_part_count(self):
"""Test that the Category part count works."""

@ -246,6 +246,8 @@ class PartTest(TestCase):
self.assertEqual(self.r1.name, 'R_2K2_0805')
if settings.ENABLE_CLASSIC_FRONTEND:
self.assertEqual(self.r1.get_absolute_url(), '/part/3/')
else:
self.assertEqual(self.r1.get_absolute_url(), '/platform/part/3')
def test_category(self):
"""Test PartCategory path."""

@ -289,6 +289,10 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase):
self.assertEqual(
response.data['stocklocation']['web_url'], '/stock/location/5/'
)
else:
self.assertEqual(
response.data['stocklocation']['web_url'], '/platform/stock/location/5'
)
self.assertEqual(response.data['plugin'], 'InvenTreeBarcode')
# Scan a Part object
@ -332,6 +336,10 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase):
self.assertEqual(
response.data['stocklocation']['web_url'], '/stock/location/5/'
)
else:
self.assertEqual(
response.data['stocklocation']['web_url'], '/platform/stock/location/5'
)
self.assertEqual(response.data['plugin'], 'InvenTreeBarcode')
# Scan a Part object

@ -266,6 +266,9 @@ class StockTest(StockTestBase):
if settings.ENABLE_CLASSIC_FRONTEND:
self.assertEqual(it.get_absolute_url(), '/stock/item/2/')
self.assertEqual(self.home.get_absolute_url(), '/stock/location/1/')
else:
self.assertEqual(it.get_absolute_url(), '/platform/stock/item/2')
self.assertEqual(self.home.get_absolute_url(), '/platform/stock/location/1')
def test_strings(self):
"""Test str function."""

@ -2,7 +2,7 @@
from django.apps import apps
from django.contrib.auth.models import Group
from django.test import TestCase, tag
from django.test import TestCase
from django.urls import reverse
from common.settings import set_global_setting
@ -166,8 +166,6 @@ class OwnerModelTest(InvenTreeTestCase):
self.assertEqual(response.status_code, status_code)
return response.data
# TODO: Find out why this depends on CUI
@tag('cui')
def test_owner(self):
"""Tests for the 'owner' model."""
# Check that owner was created for user