2022-08-07 10:41:11 +00:00
|
|
|
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")
|
2022-08-07 10:41:11 +00:00
|
|
|
|
|
|
|
@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/")
|
2022-08-07 10:41:11 +00:00
|
|
|
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/",
|
|
|
|
),
|
2022-08-07 10:41:11 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
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="")
|
2022-08-07 10:41:11 +00:00
|
|
|
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/"),
|
2022-08-07 10:41:11 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
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)
|