Uncommited changes for tests

Test cases still a WIP - unsure as to why these two querysets are ordered differently. Otherwise the sorting logic is there
This commit is contained in:
zandercymatics 2023-08-11 12:41:45 -06:00
parent d7d4090054
commit d99260f70a
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 123 additions and 7 deletions

View file

@ -14,8 +14,7 @@ logger = logging.getLogger(__name__)
foreignkey_orderby_dict: [SortingDictInterface] = [
#foreign_key - order_by
SortingDictInterface(["submitter", "authorizing_official", "investigator", "creator", "user"], ['first_name', 'last_name']).sorting_dict,
SortingDictInterface(["domain", "requested_domain"], ["name"]).sorting_dict,
SortingDictInterface(["domain_application"], ['id']).sorting_dict
SortingDictInterface(["domain", "requested_domain"], ["name"]).sorting_dict
]
class AuditedAdmin(admin.ModelAdmin):

View file

@ -2,6 +2,8 @@ import os
import logging
from contextlib import contextmanager
import random
from string import ascii_uppercase
from unittest.mock import Mock
from typing import List, Dict
@ -149,8 +151,8 @@ def completed_application(
phone="(555) 555 5556",
)
other, _ = Contact.objects.get_or_create(
first_name="Testy2",
last_name="Tester2",
first_name="Testy",
last_name="Tester",
title="Another Tester",
email="testy2@town.com",
phone="(555) 555 5557",
@ -188,3 +190,75 @@ def completed_application(
application.alternative_domains.add(alt)
return application
def multiple_completed_applications(has_other_contacts=True,
has_current_website=True,
has_alternative_gov_domain=True,
has_type_of_work=True,
has_anything_else=True,
status=DomainApplication.STARTED,
user=False,):
applications = []
list_of_letters = list(ascii_uppercase)
random.shuffle(list_of_letters)
for x in list_of_letters:
if not user:
user = get_user_model().objects.create(username="username{}".format(x))
ao, _ = Contact.objects.get_or_create(
first_name="{} Testy".format(x),
last_name="{} Tester".format(x),
title="{} Chief Tester".format(x),
email="testy@town.com",
phone="(555) 555 5555",
)
domain, _ = DraftDomain.objects.get_or_create(name="city{}.gov".format(x))
alt, _ = Website.objects.get_or_create(website="cityalt{}.gov".format(x))
current, _ = Website.objects.get_or_create(website="city{}.com".format(x))
you, _ = Contact.objects.get_or_create(
first_name="{} Testy you".format(x),
last_name="{} Tester you".format(x),
title="{} Admin Tester".format(x),
email="mayor@igorville.gov",
phone="(555) 555 5556",
)
other, _ = Contact.objects.get_or_create(
first_name="{} Testy".format(x),
last_name="{} Tester".format(x),
title="{} Another Tester".format(x),
email="{}testy2@town.com".format(x),
phone="(555) 555 5557",
)
domain_application_kwargs = dict(
organization_type="federal",
federal_type="executive",
purpose="Purpose of the site",
is_policy_acknowledged=True,
organization_name="{}Testorg".format(x),
address_line1="address 1",
address_line2="address 2",
state_territory="NY",
zipcode="10002",
authorizing_official=ao,
requested_domain=domain,
submitter=you,
creator=user,
status=status,
)
if has_type_of_work:
domain_application_kwargs["type_of_work"] = "e-Government"
if has_anything_else:
domain_application_kwargs["anything_else"] = "There is more"
application, _ = DomainApplication.objects.get_or_create(
**domain_application_kwargs
)
if has_other_contacts:
application.other_contacts.add(other)
if has_current_website:
application.current_websites.add(current)
if has_alternative_gov_domain:
application.alternative_domains.add(alt)
applications.append(application)
return applications

View file

@ -1,15 +1,17 @@
from django.test import TestCase, RequestFactory, Client
from django.contrib.admin.sites import AdminSite
from registrar.admin import DomainApplicationAdmin, ListHeaderAdmin, MyUserAdmin
from registrar.admin import DomainApplicationAdmin, ListHeaderAdmin, MyUserAdmin, AuditedAdmin
from registrar.models import DomainApplication, DomainInformation, User
from .common import completed_application, mock_user, create_superuser, create_user
from registrar.models.contact import Contact
from .common import completed_application, mock_user, create_superuser, create_user, multiple_completed_applications
from django.contrib.auth import get_user_model
from django.conf import settings
from unittest.mock import MagicMock
import boto3_mocking # type: ignore
import logging
logger = logging.getLogger(__name__)
class TestDomainApplicationAdmin(TestCase):
def setUp(self):
self.site = AdminSite()
@ -367,3 +369,44 @@ class MyUserAdminTest(TestCase):
def tearDown(self):
User.objects.all().delete()
class AuditedAdminTest(TestCase):
def setUp(self):
self.site = AdminSite()
self.factory = RequestFactory()
self.client = Client(HTTP_HOST="localhost:8080")
self.superuser = create_superuser()
self.factory.post
def test_alphabetically_sorted_fk_fields(self):
mock_client = MagicMock()
#tested_fields = [{"name": "submitter"}, {"name": "authorizing_official"}, {"name": "investigator"}, {"name": "creator"}, {"name": "user"}]
tested_fields = [DomainApplication.authorizing_official.field, DomainApplication.submitter.field, DomainApplication.investigator.field, DomainApplication.creator.field]
with boto3_mocking.clients.handler_for("sesv2", mock_client):
# Create a sample application - review status does not matter
applications = multiple_completed_applications(status=DomainApplication.IN_REVIEW)
# Create a mock request
request = self.factory.post(
"/admin/registrar/domainapplication/{}/change/".format(applications[0].pk)
)
model_admin = AuditedAdmin(DomainApplication, self.site)
for field in tested_fields:
desired_order = model_admin.get_queryset(request).order_by("{}__first_name".format(field.name))
current_sort_order = model_admin.formfield_for_foreignkey(field, request).queryset
self.assertEqual(desired_order, current_sort_order, "{} is not ordered alphabetically".format(field.name))
# Is initalized in alphabetical order
for x in model_admin.get_queryset(request).all():
logger.debug(x.authorizing_official)
def tearDown(self):
DomainInformation.objects.all().delete()
DomainApplication.objects.all().delete()
User.objects.all().delete()
self.superuser.delete()