cleanup and linting

This commit is contained in:
rachidatecs 2023-06-30 12:08:31 -04:00
parent 15688b8fdb
commit 31a18bbd50
No known key found for this signature in database
GPG key ID: 3CEBBFA7325E5525
4 changed files with 178 additions and 84 deletions

View file

@ -1,8 +1,9 @@
from django.test import TestCase, RequestFactory, Client
from django.contrib.admin.sites import AdminSite
from registrar.admin import DomainApplicationAdmin, ListHeaderAdmin, AuditedAdmin
from registrar.admin import DomainApplicationAdmin, ListHeaderAdmin
from registrar.models import DomainApplication, User
from .common import completed_application
from django.contrib.auth import get_user_model
from django.conf import settings
from unittest.mock import MagicMock
@ -14,7 +15,20 @@ class TestDomainApplicationAdmin(TestCase):
self.site = AdminSite()
self.factory = RequestFactory()
self.admin = ListHeaderAdmin(model=DomainApplication, admin_site=None)
self.client = Client(HTTP_HOST='localhost:8080')
self.client = Client(HTTP_HOST="localhost:8080")
username = "admin"
first_name = "First"
last_name = "Last"
email = "info@example.com"
p = "adminpassword"
User = get_user_model()
self.superuser = User.objects.create_superuser(
username=username,
first_name=first_name,
last_name=last_name,
email=email,
password=p,
)
@boto3_mocking.patching
def test_save_model_sends_email_on_property_change(self):
@ -64,45 +78,54 @@ class TestDomainApplicationAdmin(TestCase):
# Cleanup
application.delete()
def test_changelist_view(self):
# Have to get creative to get past linter
p = "adminpassword"
self.client.login(username="admin", password=p)
# Make the request using the Client class
# which handles CSRF
# Follow=True handles the redirect
request = self.client.get('/admin/registrar/domainapplication/', {'param1': 'value1', 'param2': 'value2'}, follow=True, max_redirects=10)
print(f'request {request}')
# request = self.factory.get('/admin/registrar/domainapplication/')
# # Set the GET parameters for testing
# request.GET = {'param1': 'value1', 'param2': 'value2', 'q': 'search_value'}
# # Call the changelist_view method
response = self.admin.changelist_view(request, extra_context={'filters': [{'parameter_name': 'status', 'parameter_value': 'started'}], 'search_query': ''})
print(f'response {response}')
# Assert that the final response is a successful response (not a redirect)
# self.assertEqual(response.status_code, 200)
response = self.client.get(
"/admin/registrar/domainapplication/",
{"status__exact": "started", "investigator__id__exact": "4", "q": "Hello"},
follow=True,
)
# Assert that the filters and search_query are added to the extra_context
self.assertIn('filters', response.extra_context)
self.assertIn('search_query', response.extra_context)
self.assertIn("filters", response.context)
self.assertIn("search_query", response.context)
# Assert the content of filters and search_query
filters = response.extra_context['filters']
search_query = response.extra_context['search_query']
self.assertEqual(filters, [{'parameter_name': 'param1', 'parameter_value': 'value1'},
{'parameter_name': 'param2', 'parameter_value': 'value2'}])
self.assertEqual(search_query, 'value of q parameter if present in the request GET')
filters = response.context["filters"]
search_query = response.context["search_query"]
self.assertEqual(search_query, "Hello")
self.assertEqual(
filters,
[
{"parameter_name": "status", "parameter_value": "started"},
{"parameter_name": "investigator id", "parameter_value": "4"},
],
)
def test_get_filters(self):
# Create a mock request object
request = self.factory.get('/admin/yourmodel/')
request = self.factory.get("/admin/yourmodel/")
# Set the GET parameters for testing
request.GET = {'param1': 'value1', 'param2': 'value2', 'q': 'search_value'}
request.GET = {"status": "started", "investigator id": "4", "q": "search_value"}
# Call the get_filters method
filters = self.admin.get_filters(request)
# Assert the filters extracted from the request GET
self.assertEqual(filters, [{'parameter_name': 'param1', 'parameter_value': 'value1'},
{'parameter_name': 'param2', 'parameter_value': 'value2'}])
self.assertEqual(
filters,
[
{"parameter_name": "status", "parameter_value": "started"},
{"parameter_name": "investigator id", "parameter_value": "4"},
],
)
def tearDown(self):
# delete any applications too
DomainApplication.objects.all().delete()
self.superuser.delete()