Opened 9 months ago

Closed 9 months ago

#35265 closed Cleanup/optimization (fixed)

Add test for AdminSite with custom headers.

Reported by: Kasun Herath Owned by: Kasun Herath
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Commit commit introduced attributes to AdminSite class that can control site header, site title and index title. AdminSite

But the corresponding unit test still checks for the default site titles not for the attributes on the site instance. https://github.com/django/django/blob/main/tests/admin_views/test_adminsite.py#L41

Change History (5)

comment:1 by Kasun Herath, 9 months ago

Version: 5.0dev

comment:2 by Kasun Herath, 9 months ago

Owner: changed from nobody to Kasun Herath
Status: newassigned

comment:3 by Mariusz Felisiak, 9 months ago

Summary: Adjust unit tests on admin custom site titlesAdd test for AdminSite with custom headers.
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Thanks for the ticket, there is no need to adjust existing tests, I'd rather add a new one, e.g.

  • tests/admin_views/test_adminsite.py

    diff --git a/tests/admin_views/test_adminsite.py b/tests/admin_views/test_adminsite.py
    index 68a32567d8..cb1da92003 100644
    a b  
    11from django.contrib import admin
    22from django.contrib.admin.actions import delete_selected
    3 from django.contrib.auth.models import User
     3from django.contrib.auth.models import Group, User
    44from django.test import SimpleTestCase, TestCase, override_settings
    55from django.test.client import RequestFactory
    66from django.urls import path, reverse
    site = admin.AdminSite(name="test_adminsite")  
    1111site.register(User)
    1212site.register(Article)
    1313
     14
     15class CustomAdminSite(admin.AdminSite):
     16    site_title = "My title"
     17    site_header = "My admin site"
     18
     19
     20custom_site = CustomAdminSite(name="test_custom_adminsite")
     21custom_site.register(Group)
     22
    1423urlpatterns = [
    1524    path("test_admin/admin/", site.urls),
     25    path("test_custom_admin/admin/", custom_site.urls),
    1626]
    1727
    1828
    class SiteEachContextTest(TestCase):  
    4353        self.assertEqual(ctx["site_url"], "/")
    4454        self.assertIs(ctx["has_permission"], True)
    4555
     56    def test_admin_custom_headers(self):
     57        request = self.request_factory.get(reverse("test_custom_adminsite:index"))
     58        request.user = self.u1
     59        ctx = custom_site.each_context(request)
     60        self.assertEqual(ctx["site_title"], "My title")
     61        self.assertEqual(ctx["site_header"], "My admin site")
     62
    4663    def test_each_context_site_url_with_script_name(self):
    4764        request = self.request_factory.get(
    4865            reverse("test_adminsite:index"), SCRIPT_NAME="/my-script-name/"

For the future, extra test coverage doesn't require a ticket.

comment:4 by Kasun Herath, 9 months ago

Has patch: set

Thanks for the quick feedback! Here is a PR with the suggested changes.

comment:5 by GitHub <noreply@…>, 9 months ago

Resolution: fixed
Status: assignedclosed

In f5ed4306:

Fixed #35265 -- Added AdminSite tests for changing titles.

Note: See TracTickets for help on using tickets.
Back to Top