2020-05-06 10:29:51 +02:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.admin.tests import AdminSeleniumTestCase
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.test import TestCase, override_settings
|
|
|
|
from django.urls import path, reverse
|
|
|
|
|
2021-08-26 17:43:40 +02:00
|
|
|
from .models import Héllo
|
|
|
|
|
2020-05-06 10:29:51 +02:00
|
|
|
|
|
|
|
class AdminSiteWithSidebar(admin.AdminSite):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class AdminSiteWithoutSidebar(admin.AdminSite):
|
|
|
|
enable_nav_sidebar = False
|
|
|
|
|
|
|
|
|
|
|
|
site_with_sidebar = AdminSiteWithSidebar(name="test_with_sidebar")
|
|
|
|
site_without_sidebar = AdminSiteWithoutSidebar(name="test_without_sidebar")
|
|
|
|
|
|
|
|
site_with_sidebar.register(User)
|
2021-08-26 17:43:40 +02:00
|
|
|
site_with_sidebar.register(Héllo)
|
2020-05-06 10:29:51 +02:00
|
|
|
|
|
|
|
urlpatterns = [
|
|
|
|
path("test_sidebar/admin/", site_with_sidebar.urls),
|
|
|
|
path("test_wihout_sidebar/admin/", site_without_sidebar.urls),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF="admin_views.test_nav_sidebar")
|
|
|
|
class AdminSidebarTests(TestCase):
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.superuser = User.objects.create_superuser(
|
|
|
|
username="super",
|
|
|
|
password="secret",
|
|
|
|
email="super@example.com",
|
|
|
|
)
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client.force_login(self.superuser)
|
|
|
|
|
|
|
|
def test_sidebar_not_on_index(self):
|
|
|
|
response = self.client.get(reverse("test_with_sidebar:index"))
|
2023-10-19 17:25:50 +02:00
|
|
|
self.assertContains(response, '<div class="main" id="main">')
|
2022-09-24 12:21:11 +02:00
|
|
|
self.assertNotContains(
|
|
|
|
response, '<nav class="sticky" id="nav-sidebar" aria-label="Sidebar">'
|
|
|
|
)
|
2020-05-06 10:29:51 +02:00
|
|
|
|
|
|
|
def test_sidebar_disabled(self):
|
|
|
|
response = self.client.get(reverse("test_without_sidebar:index"))
|
2022-09-24 12:21:11 +02:00
|
|
|
self.assertNotContains(
|
|
|
|
response, '<nav class="sticky" id="nav-sidebar" aria-label="Sidebar">'
|
|
|
|
)
|
2020-05-06 10:29:51 +02:00
|
|
|
|
|
|
|
def test_sidebar_unauthenticated(self):
|
|
|
|
self.client.logout()
|
|
|
|
response = self.client.get(reverse("test_with_sidebar:login"))
|
2022-09-24 12:21:11 +02:00
|
|
|
self.assertNotContains(
|
|
|
|
response, '<nav class="sticky" id="nav-sidebar" aria-label="Sidebar">'
|
|
|
|
)
|
2020-05-06 10:29:51 +02:00
|
|
|
|
|
|
|
def test_sidebar_aria_current_page(self):
|
2020-05-14 10:27:35 +02:00
|
|
|
url = reverse("test_with_sidebar:auth_user_changelist")
|
|
|
|
response = self.client.get(url)
|
2022-09-24 12:21:11 +02:00
|
|
|
self.assertContains(
|
|
|
|
response, '<nav class="sticky" id="nav-sidebar" aria-label="Sidebar">'
|
|
|
|
)
|
2020-05-14 10:27:35 +02:00
|
|
|
self.assertContains(
|
|
|
|
response, '<a href="%s" aria-current="page">Users</a>' % url
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
TEMPLATES=[
|
|
|
|
{
|
|
|
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
|
|
|
"DIRS": [],
|
|
|
|
"APP_DIRS": True,
|
|
|
|
"OPTIONS": {
|
|
|
|
"context_processors": [
|
|
|
|
"django.contrib.auth.context_processors.auth",
|
|
|
|
"django.contrib.messages.context_processors.messages",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_sidebar_aria_current_page_missing_without_request_context_processor(self):
|
|
|
|
url = reverse("test_with_sidebar:auth_user_changelist")
|
|
|
|
response = self.client.get(url)
|
2022-09-24 12:21:11 +02:00
|
|
|
self.assertContains(
|
|
|
|
response, '<nav class="sticky" id="nav-sidebar" aria-label="Sidebar">'
|
|
|
|
)
|
2020-05-14 10:27:35 +02:00
|
|
|
# Does not include aria-current attribute.
|
|
|
|
self.assertContains(response, '<a href="%s">Users</a>' % url)
|
|
|
|
self.assertNotContains(response, "aria-current")
|
2020-05-06 10:29:51 +02:00
|
|
|
|
2020-08-11 10:11:42 +02:00
|
|
|
@override_settings(DEBUG=True)
|
|
|
|
def test_included_app_list_template_context_fully_set(self):
|
|
|
|
# All context variables should be set when rendering the sidebar.
|
|
|
|
url = reverse("test_with_sidebar:auth_user_changelist")
|
2021-01-07 17:54:40 +01:00
|
|
|
with self.assertNoLogs("django.template", "DEBUG"):
|
|
|
|
self.client.get(url)
|
2020-08-11 10:11:42 +02:00
|
|
|
|
2021-08-26 17:43:40 +02:00
|
|
|
def test_sidebar_model_name_non_ascii(self):
|
|
|
|
url = reverse("test_with_sidebar:admin_views_héllo_changelist")
|
|
|
|
response = self.client.get(url)
|
|
|
|
self.assertContains(
|
|
|
|
response, '<div class="app-admin_views module current-app">'
|
|
|
|
)
|
|
|
|
self.assertContains(response, '<tr class="model-héllo current-model">')
|
|
|
|
self.assertContains(
|
|
|
|
response,
|
2023-10-20 15:42:30 +02:00
|
|
|
'<th scope="row" id="admin_views-héllo">'
|
2021-08-26 17:43:40 +02:00
|
|
|
'<a href="/test_sidebar/admin/admin_views/h%C3%A9llo/" aria-current="page">'
|
|
|
|
"Héllos</a></th>",
|
2023-10-20 15:42:30 +02:00
|
|
|
html=True,
|
2021-08-26 17:43:40 +02:00
|
|
|
)
|
|
|
|
|
2020-05-06 10:29:51 +02:00
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF="admin_views.test_nav_sidebar")
|
|
|
|
class SeleniumTests(AdminSeleniumTestCase):
|
2021-08-26 17:43:40 +02:00
|
|
|
available_apps = ["admin_views"] + AdminSeleniumTestCase.available_apps
|
|
|
|
|
2020-05-06 10:29:51 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.superuser = User.objects.create_superuser(
|
|
|
|
username="super",
|
|
|
|
password="secret",
|
|
|
|
email="super@example.com",
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2020-05-06 10:29:51 +02:00
|
|
|
self.admin_login(
|
|
|
|
username="super",
|
|
|
|
password="secret",
|
|
|
|
login_url=reverse("test_with_sidebar:index"),
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2020-05-06 10:29:51 +02:00
|
|
|
self.selenium.execute_script(
|
|
|
|
"localStorage.removeItem('django.admin.navSidebarIsOpen')"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_sidebar_starts_open(self):
|
2021-10-20 15:54:30 +02:00
|
|
|
from selenium.webdriver.common.by import By
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2020-05-06 10:29:51 +02:00
|
|
|
self.selenium.get(
|
|
|
|
self.live_server_url + reverse("test_with_sidebar:auth_user_changelist")
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2021-10-20 15:54:30 +02:00
|
|
|
main_element = self.selenium.find_element(By.CSS_SELECTOR, "#main")
|
2020-05-06 10:29:51 +02:00
|
|
|
self.assertIn("shifted", main_element.get_attribute("class").split())
|
|
|
|
|
|
|
|
def test_sidebar_can_be_closed(self):
|
2021-10-20 15:54:30 +02:00
|
|
|
from selenium.webdriver.common.by import By
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2020-05-06 10:29:51 +02:00
|
|
|
self.selenium.get(
|
|
|
|
self.live_server_url + reverse("test_with_sidebar:auth_user_changelist")
|
2021-10-20 15:54:30 +02:00
|
|
|
)
|
|
|
|
toggle_button = self.selenium.find_element(
|
|
|
|
By.CSS_SELECTOR, "#toggle-nav-sidebar"
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2020-05-16 14:16:56 +02:00
|
|
|
self.assertEqual(toggle_button.tag_name, "button")
|
|
|
|
self.assertEqual(toggle_button.get_attribute("aria-label"), "Toggle navigation")
|
2022-09-24 12:21:11 +02:00
|
|
|
nav_sidebar = self.selenium.find_element(By.ID, "nav-sidebar")
|
|
|
|
self.assertEqual(nav_sidebar.get_attribute("aria-expanded"), "true")
|
|
|
|
self.assertTrue(nav_sidebar.is_displayed())
|
2020-05-06 10:29:51 +02:00
|
|
|
toggle_button.click()
|
2022-09-24 12:21:11 +02:00
|
|
|
|
|
|
|
# Hidden sidebar is not visible.
|
|
|
|
nav_sidebar = self.selenium.find_element(By.ID, "nav-sidebar")
|
|
|
|
self.assertEqual(nav_sidebar.get_attribute("aria-expanded"), "false")
|
|
|
|
self.assertFalse(nav_sidebar.is_displayed())
|
2021-10-20 15:54:30 +02:00
|
|
|
main_element = self.selenium.find_element(By.CSS_SELECTOR, "#main")
|
2020-05-06 10:29:51 +02:00
|
|
|
self.assertNotIn("shifted", main_element.get_attribute("class").split())
|
|
|
|
|
|
|
|
def test_sidebar_state_persists(self):
|
2021-10-20 15:54:30 +02:00
|
|
|
from selenium.webdriver.common.by import By
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2020-05-06 10:29:51 +02:00
|
|
|
self.selenium.get(
|
|
|
|
self.live_server_url + reverse("test_with_sidebar:auth_user_changelist")
|
|
|
|
)
|
|
|
|
self.assertIsNone(
|
|
|
|
self.selenium.execute_script(
|
|
|
|
"return localStorage.getItem('django.admin.navSidebarIsOpen')"
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2021-10-20 15:54:30 +02:00
|
|
|
)
|
|
|
|
toggle_button = self.selenium.find_element(
|
|
|
|
By.CSS_SELECTOR, "#toggle-nav-sidebar"
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2020-05-06 10:29:51 +02:00
|
|
|
toggle_button.click()
|
|
|
|
self.assertEqual(
|
|
|
|
self.selenium.execute_script(
|
|
|
|
"return localStorage.getItem('django.admin.navSidebarIsOpen')"
|
|
|
|
),
|
|
|
|
"false",
|
|
|
|
)
|
|
|
|
self.selenium.get(
|
|
|
|
self.live_server_url + reverse("test_with_sidebar:auth_user_changelist")
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2021-10-20 15:54:30 +02:00
|
|
|
main_element = self.selenium.find_element(By.CSS_SELECTOR, "#main")
|
2020-05-06 10:29:51 +02:00
|
|
|
self.assertNotIn("shifted", main_element.get_attribute("class").split())
|
|
|
|
|
2021-10-20 15:54:30 +02:00
|
|
|
toggle_button = self.selenium.find_element(
|
|
|
|
By.CSS_SELECTOR, "#toggle-nav-sidebar"
|
|
|
|
)
|
2022-09-24 12:21:11 +02:00
|
|
|
# Hidden sidebar is not visible.
|
|
|
|
nav_sidebar = self.selenium.find_element(By.ID, "nav-sidebar")
|
|
|
|
self.assertEqual(nav_sidebar.get_attribute("aria-expanded"), "false")
|
|
|
|
self.assertFalse(nav_sidebar.is_displayed())
|
2020-05-06 10:29:51 +02:00
|
|
|
toggle_button.click()
|
2022-09-24 12:21:11 +02:00
|
|
|
nav_sidebar = self.selenium.find_element(By.ID, "nav-sidebar")
|
|
|
|
self.assertEqual(nav_sidebar.get_attribute("aria-expanded"), "true")
|
|
|
|
self.assertTrue(nav_sidebar.is_displayed())
|
2020-05-06 10:29:51 +02:00
|
|
|
self.assertEqual(
|
|
|
|
self.selenium.execute_script(
|
|
|
|
"return localStorage.getItem('django.admin.navSidebarIsOpen')"
|
|
|
|
),
|
|
|
|
"true",
|
|
|
|
)
|
|
|
|
self.selenium.get(
|
|
|
|
self.live_server_url + reverse("test_with_sidebar:auth_user_changelist")
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2021-10-20 15:54:30 +02:00
|
|
|
main_element = self.selenium.find_element(By.CSS_SELECTOR, "#main")
|
2020-05-06 10:29:51 +02:00
|
|
|
self.assertIn("shifted", main_element.get_attribute("class").split())
|
2020-12-04 12:50:11 +01:00
|
|
|
|
|
|
|
def test_sidebar_filter_persists(self):
|
2021-10-20 15:54:30 +02:00
|
|
|
from selenium.webdriver.common.by import By
|
2022-02-03 20:24:19 +01:00
|
|
|
|
2020-12-04 12:50:11 +01:00
|
|
|
self.selenium.get(
|
|
|
|
self.live_server_url + reverse("test_with_sidebar:auth_user_changelist")
|
|
|
|
)
|
|
|
|
filter_value_script = (
|
2021-10-06 09:01:31 +02:00
|
|
|
"return sessionStorage.getItem('django.admin.navSidebarFilterValue')"
|
2020-12-04 12:50:11 +01:00
|
|
|
)
|
|
|
|
self.assertIsNone(self.selenium.execute_script(filter_value_script))
|
2021-10-20 15:54:30 +02:00
|
|
|
filter_input = self.selenium.find_element(By.CSS_SELECTOR, "#nav-filter")
|
2020-12-04 12:50:11 +01:00
|
|
|
filter_input.send_keys("users")
|
|
|
|
self.assertEqual(self.selenium.execute_script(filter_value_script), "users")
|