Expose customer impersonation audit history in Django admin
This commit is contained in:
1 parent
bfe199aaff
commit
a91c403966
4 files changed
+137
-3
No files matched your search
@@ -1,8 +1,10 @@
|
||||
from django.contrib import admin
|
||||
from django.utils.html import format_html
|
||||
from django.utils.timezone import now
|
||||
|
||||
from .models import (
|
||||
Customer,
|
||||
CustomerImpersonation,
|
||||
HostingAccount,
|
||||
HostingPlan,
|
||||
Reseller,
|
||||
@@ -75,6 +77,45 @@ class CustomerAdmin(admin.ModelAdmin):
|
||||
search_fields = ("user__username",)
|
||||
|
||||
|
||||
@admin.register(CustomerImpersonation)
|
||||
class CustomerImpersonationAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
"operator_username",
|
||||
"customer_username",
|
||||
"started_at",
|
||||
"expires_at",
|
||||
"ended_at",
|
||||
"outcome",
|
||||
)
|
||||
search_fields = ("operator_username", "customer_username")
|
||||
fields = (
|
||||
"operator_username",
|
||||
"customer_username",
|
||||
"started_at",
|
||||
"expires_at",
|
||||
"ended_at",
|
||||
"outcome",
|
||||
)
|
||||
readonly_fields = fields
|
||||
|
||||
@admin.display(description="Outcome")
|
||||
def outcome(self, obj):
|
||||
if obj.ended_at is not None:
|
||||
return obj.get_end_reason_display()
|
||||
if obj.expires_at <= now():
|
||||
return CustomerImpersonation.EndReason.EXPIRED.label
|
||||
return "Open"
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return False
|
||||
|
||||
def has_change_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
def has_delete_permission(self, request, obj=None):
|
||||
return False
|
||||
|
||||
|
||||
@admin.register(WebProvisioningPlan)
|
||||
class WebProvisioningPlanAdmin(admin.ModelAdmin):
|
||||
list_display = (
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
from .models import Customer, CustomerImpersonation
|
||||
|
||||
|
||||
class CustomerImpersonationAuditTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
users = get_user_model().objects
|
||||
cls.operator = users.create_superuser("operator")
|
||||
cls.customer = Customer.objects.create(user=users.create_user("alice"))
|
||||
cutoff = timezone.now() - timedelta(minutes=1)
|
||||
cls.entry = CustomerImpersonation.objects.create(
|
||||
operator=cls.operator,
|
||||
customer=cls.customer,
|
||||
operator_username="operator",
|
||||
customer_username="alice",
|
||||
started_at=cutoff - timedelta(minutes=30),
|
||||
expires_at=cutoff,
|
||||
)
|
||||
|
||||
def test_audit_list_requires_the_separate_view_permission(self):
|
||||
auditor = get_user_model().objects.create_user(
|
||||
"auditor", is_staff=True
|
||||
)
|
||||
auditor.user_permissions.add(
|
||||
Permission.objects.get(
|
||||
content_type__app_label="hosting",
|
||||
codename="impersonate_customer",
|
||||
)
|
||||
)
|
||||
self.client.force_login(auditor)
|
||||
audit_url = reverse("admin:hosting_customerimpersonation_changelist")
|
||||
self.assertEqual(self.client.get(audit_url).status_code, 403)
|
||||
|
||||
auditor.user_permissions.add(
|
||||
Permission.objects.get(
|
||||
content_type__app_label="hosting",
|
||||
codename="view_customerimpersonation",
|
||||
)
|
||||
)
|
||||
response = self.client.get(audit_url)
|
||||
self.assertContains(response, "alice")
|
||||
self.assertContains(response, "Expired")
|
||||
|
||||
def test_audit_retains_usernames_and_disallows_manual_edits(self):
|
||||
self.customer.delete()
|
||||
self.client.force_login(self.operator)
|
||||
url = reverse(
|
||||
"admin:hosting_customerimpersonation_change",
|
||||
args=[self.entry.pk],
|
||||
)
|
||||
response = self.client.get(url)
|
||||
self.assertContains(response, "alice")
|
||||
self.assertNotContains(response, 'name="_save"')
|
||||
self.assertEqual(self.client.post(url).status_code, 403)
|
||||
for view, args in (
|
||||
("admin:hosting_customerimpersonation_add", ()),
|
||||
("admin:hosting_customerimpersonation_delete", (self.entry.pk,)),
|
||||
):
|
||||
with self.subTest(view=view):
|
||||
self.assertEqual(
|
||||
self.client.post(reverse(view, args=args)).status_code,
|
||||
403,
|
||||
)
|
||||
self.entry.refresh_from_db()
|
||||
self.assertIsNone(self.entry.customer)
|
||||
self.assertEqual(self.entry.customer_username, "alice")
|
||||
Reference in new issue
Block a user