2024-09-05 16:00:48 +00:00
|
|
|
|
from bx_django_utils.test_utils.html_assertion import HtmlAssertionMixin, assert_html_response_snapshot
|
2024-09-05 16:00:48 +00:00
|
|
|
|
from django.contrib.auth.models import User
|
2023-07-21 04:24:54 +00:00
|
|
|
|
from django.test import TestCase
|
2024-09-05 16:00:48 +00:00
|
|
|
|
from model_bakery import baker
|
2020-10-16 15:54:34 +00:00
|
|
|
|
|
2024-09-05 16:00:48 +00:00
|
|
|
|
from inventory import __version__
|
2020-10-16 15:54:34 +00:00
|
|
|
|
|
2024-09-05 16:00:48 +00:00
|
|
|
|
|
|
|
|
|
class AdminAnonymousTests(HtmlAssertionMixin, TestCase):
|
2020-10-16 15:54:34 +00:00
|
|
|
|
"""
|
|
|
|
|
Anonymous will be redirected to the login page.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def test_login_en(self):
|
2024-09-05 16:00:48 +00:00
|
|
|
|
response = self.client.get('/admin/', secure=True, HTTP_ACCEPT_LANGUAGE='en')
|
|
|
|
|
self.assertRedirects(response, expected_url='/admin/login/?next=/admin/', fetch_redirect_response=False)
|
2020-10-16 15:54:34 +00:00
|
|
|
|
|
|
|
|
|
def test_login_de(self):
|
2024-09-05 16:00:48 +00:00
|
|
|
|
response = self.client.get('/admin/', secure=True, HTTP_ACCEPT_LANGUAGE='de')
|
|
|
|
|
self.assertRedirects(response, expected_url='/admin/login/?next=/admin/', fetch_redirect_response=False)
|
2024-09-05 16:00:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AdminLoggedinTests(HtmlAssertionMixin, TestCase):
|
|
|
|
|
"""
|
|
|
|
|
Some basics test with the django admin
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpTestData(cls):
|
|
|
|
|
cls.superuser = baker.make(User, username='superuser', is_staff=True, is_active=True, is_superuser=True)
|
|
|
|
|
cls.staffuser = baker.make(User, username='staff_test_user', is_staff=True, is_active=True, is_superuser=False)
|
|
|
|
|
|
|
|
|
|
def test_staff_admin_index(self):
|
|
|
|
|
self.client.force_login(self.staffuser)
|
|
|
|
|
|
2024-09-05 16:00:48 +00:00
|
|
|
|
response = self.client.get("/admin/", secure=True, HTTP_ACCEPT_LANGUAGE="en")
|
2024-09-05 16:00:48 +00:00
|
|
|
|
self.assert_html_parts(
|
|
|
|
|
response,
|
|
|
|
|
parts=(
|
2024-09-05 16:00:48 +00:00
|
|
|
|
f"<title>Site administration | PyInventory v{__version__}</title>",
|
2024-09-05 16:00:48 +00:00
|
|
|
|
"<h1>Site administration</h1>",
|
|
|
|
|
"<strong>staff_test_user</strong>",
|
|
|
|
|
"<p>You don’t have permission to view or edit anything.</p>",
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
self.assertTemplateUsed(response, template_name="admin/index.html")
|
|
|
|
|
|
|
|
|
|
def test_superuser_admin_index(self):
|
|
|
|
|
self.client.force_login(self.superuser)
|
2024-09-05 16:00:48 +00:00
|
|
|
|
response = self.client.get("/admin/", secure=True, HTTP_ACCEPT_LANGUAGE="en")
|
2024-09-05 16:00:48 +00:00
|
|
|
|
self.assert_html_parts(
|
|
|
|
|
response,
|
|
|
|
|
parts=(
|
|
|
|
|
"inventory",
|
|
|
|
|
"<strong>superuser</strong>",
|
|
|
|
|
"Site administration",
|
|
|
|
|
"/admin/auth/group/add/",
|
|
|
|
|
"/admin/auth/user/add/",
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
self.assertTemplateUsed(response, template_name="admin/index.html")
|
2024-09-05 16:00:48 +00:00
|
|
|
|
assert_html_response_snapshot(response, validate=False)
|