mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-15 17:17:02 +02:00
Finish test cases
This commit is contained in:
parent
7ea5503c91
commit
ec43865f70
2 changed files with 62 additions and 4 deletions
|
@ -553,7 +553,8 @@ class DomainApplicationAdmin(ListHeaderAdmin):
|
||||||
"""Custom investigator filter that only displays users with the manager role"""
|
"""Custom investigator filter that only displays users with the manager role"""
|
||||||
|
|
||||||
title = "investigator"
|
title = "investigator"
|
||||||
parameter_name = "investigator"
|
# Match the old param name to avoid unnecessary refactoring
|
||||||
|
parameter_name = "investigator__id__exact"
|
||||||
|
|
||||||
def lookups(self, request, model_admin):
|
def lookups(self, request, model_admin):
|
||||||
"""Lookup reimplementation, gets users of is_staff.
|
"""Lookup reimplementation, gets users of is_staff.
|
||||||
|
@ -564,7 +565,7 @@ class DomainApplicationAdmin(ListHeaderAdmin):
|
||||||
|
|
||||||
def queryset(self, request, queryset):
|
def queryset(self, request, queryset):
|
||||||
"""Custom queryset implementation, filters by investigator"""
|
"""Custom queryset implementation, filters by investigator"""
|
||||||
return queryset.filter(investigator=self.value())
|
return queryset.filter(investigator__id__exact=self.value())
|
||||||
|
|
||||||
# Columns
|
# Columns
|
||||||
list_display = [
|
list_display = [
|
||||||
|
|
|
@ -877,17 +877,74 @@ class TestDomainApplicationAdmin(MockEppLib):
|
||||||
self.assertIn("filters", response.context)
|
self.assertIn("filters", response.context)
|
||||||
# Assert the content of filters and search_query
|
# Assert the content of filters and search_query
|
||||||
filters = response.context["filters"]
|
filters = response.context["filters"]
|
||||||
print(response.context.__dict__)
|
|
||||||
|
# Ensure that the format is correct. We will test the value later in the test.
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
filters,
|
filters,
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"parameter_name": "investigator",
|
"parameter_name": "investigator",
|
||||||
"parameter_value": "4",
|
"parameter_value": str(investigator_user.id),
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Manually test the returned values
|
||||||
|
request = self.factory.get("/admin/registrar/domainapplication/")
|
||||||
|
# Set the GET parameters for testing
|
||||||
|
request.GET = {
|
||||||
|
"investigator__id__exact": investigator_user.id,
|
||||||
|
}
|
||||||
|
# Call the get_filters method
|
||||||
|
filters = self.admin.get_filters(request)
|
||||||
|
|
||||||
|
# Assert the filters extracted from the request GET
|
||||||
|
self.assertEqual(
|
||||||
|
filters,
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"parameter_name": "investigator",
|
||||||
|
# We intentionally test a weird value, to see what happens
|
||||||
|
"parameter_value": "SomeGuy first_name:investigator SomeGuy last_name:investigator",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_investigator_filter_filters_correctly(self):
|
||||||
|
"""Tests the investigator filter"""
|
||||||
|
|
||||||
|
# Create a mock DomainApplication object, with a fake investigator
|
||||||
|
application: DomainApplication = generic_domain_object("application", "SomeGuy")
|
||||||
|
investigator_user = User.objects.filter(username=application.investigator.username).get()
|
||||||
|
investigator_user.is_staff = True
|
||||||
|
investigator_user.save()
|
||||||
|
|
||||||
|
# Create a second mock DomainApplication object, to test filtering
|
||||||
|
application: DomainApplication = generic_domain_object("application", "BadGuy")
|
||||||
|
another_user = User.objects.filter(username=application.investigator.username).get()
|
||||||
|
another_user.is_staff = True
|
||||||
|
another_user.save()
|
||||||
|
|
||||||
|
p = "userpass"
|
||||||
|
self.client.login(username="staffuser", password=p)
|
||||||
|
response = self.client.get(
|
||||||
|
"/admin/registrar/domainapplication/",
|
||||||
|
{
|
||||||
|
"investigator__id__exact": investigator_user.id,
|
||||||
|
},
|
||||||
|
follow=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
expected_name = "SomeGuy first_name:investigator SomeGuy last_name:investigator"
|
||||||
|
# We expect to see this four times, two of them are from the html for the filter,
|
||||||
|
# the other two are the html from the list entry in the table.
|
||||||
|
self.assertContains(response, expected_name, count=4)
|
||||||
|
|
||||||
|
# Check that we don't also get the thing we aren't filtering for.
|
||||||
|
# We expect to see this two times, two of them are from the html for the filter.
|
||||||
|
unexpected_name = "BadGuy first_name:investigator BadGuy last_name:investigator"
|
||||||
|
self.assertContains(response, unexpected_name, count=2)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
super().tearDown()
|
super().tearDown()
|
||||||
Domain.objects.all().delete()
|
Domain.objects.all().delete()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue