linkding/bookmarks/tests/test_context_path.py

57 lines
1.9 KiB
Python
Raw Normal View History

import importlib
from django.test import TestCase, override_settings
from django.urls import reverse
class MockUrlConf:
def __init__(self, module):
self.urlpatterns = module.urlpatterns
class ContextPathTestCase(TestCase):
def setUp(self):
2024-01-27 10:29:16 +00:00
self.siteroot_urls = importlib.import_module("siteroot.urls")
@override_settings(LD_CONTEXT_PATH=None)
def tearDown(self):
importlib.reload(self.siteroot_urls)
2024-01-27 10:29:16 +00:00
@override_settings(LD_CONTEXT_PATH="linkding/")
def test_route_with_context_path(self):
module = importlib.reload(self.siteroot_urls)
# pass mock config instead of actual module to prevent caching the
# url config in django.urls.reverse
urlconf = MockUrlConf(module)
test_cases = [
2024-01-27 10:29:16 +00:00
("bookmarks:index", "/linkding/bookmarks"),
("bookmarks:bookmark-list", "/linkding/api/bookmarks/"),
("login", "/linkding/login/"),
(
"admin:bookmarks_bookmark_changelist",
"/linkding/admin/bookmarks/bookmark/",
),
]
for url_name, expected_url in test_cases:
url = reverse(url_name, urlconf=urlconf)
self.assertEqual(expected_url, url)
2024-01-27 10:29:16 +00:00
@override_settings(LD_CONTEXT_PATH="")
def test_route_without_context_path(self):
module = importlib.reload(self.siteroot_urls)
# pass mock config instead of actual module to prevent caching the
# url config in django.urls.reverse
urlconf = MockUrlConf(module)
test_cases = [
2024-01-27 10:29:16 +00:00
("bookmarks:index", "/bookmarks"),
("bookmarks:bookmark-list", "/api/bookmarks/"),
("login", "/login/"),
("admin:bookmarks_bookmark_changelist", "/admin/bookmarks/bookmark/"),
]
for url_name, expected_url in test_cases:
url = reverse(url_name, urlconf=urlconf)
2024-01-27 10:29:16 +00:00
self.assertEqual(expected_url, url)