Add simple unit tests

This commit is contained in:
zandercymatics 2024-08-14 10:16:50 -06:00
parent fdc5236968
commit a847b07ef7
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
5 changed files with 90 additions and 4 deletions

View file

@ -2938,7 +2938,9 @@ class PortfolioAdmin(ListHeaderAdmin):
"""Returns a comma seperated list of links for each related domain request""" """Returns a comma seperated list of links for each related domain request"""
queryset = obj.get_domain_requests() queryset = obj.get_domain_requests()
sep = '<div class="display-block margin-top-1"></div>' sep = '<div class="display-block margin-top-1"></div>'
return self.get_field_links_as_csv(queryset, "domainrequest", link_info_attribute="get_status_display", seperator=sep) return self.get_field_links_as_csv(
queryset, "domainrequest", link_info_attribute="get_status_display", seperator=sep
)
domain_requests.short_description = "Domain requests" domain_requests.short_description = "Domain requests"

View file

@ -122,8 +122,9 @@ class Portfolio(TimeStampedModel):
@property @property
def portfolio_type(self): def portfolio_type(self):
"""Returns a combination of organization_type and federal_type, """
seperated by ' - '. If no federal_type is found, we just return the org type.""" Returns a combination of organization_type / federal_type, seperated by ' - '.
If no federal_type is found, we just return the org type."""
org_type = self.OrganizationChoices.get_org_label(self.organization_type) org_type = self.OrganizationChoices.get_org_label(self.organization_type)
if self.organization_type == self.OrganizationChoices.FEDERAL and self.federal_type: if self.organization_type == self.OrganizationChoices.FEDERAL and self.federal_type:
return " - ".join([org_type, self.federal_type]) return " - ".join([org_type, self.federal_type])

View file

@ -906,6 +906,7 @@ def completed_domain_request( # noqa
federal_agency=None, federal_agency=None,
federal_type=None, federal_type=None,
action_needed_reason=None, action_needed_reason=None,
portfolio=None,
): ):
"""A completed domain request.""" """A completed domain request."""
if not user: if not user:
@ -976,6 +977,9 @@ def completed_domain_request( # noqa
if action_needed_reason: if action_needed_reason:
domain_request_kwargs["action_needed_reason"] = action_needed_reason domain_request_kwargs["action_needed_reason"] = action_needed_reason
if portfolio:
domain_request_kwargs["portfolio"] = portfolio
domain_request, _ = DomainRequest.objects.get_or_create(**domain_request_kwargs) domain_request, _ = DomainRequest.objects.get_or_create(**domain_request_kwargs)
if has_other_contacts: if has_other_contacts:

View file

@ -23,6 +23,7 @@ from registrar.admin import (
PublicContactAdmin, PublicContactAdmin,
TransitionDomainAdmin, TransitionDomainAdmin,
UserGroupAdmin, UserGroupAdmin,
PortfolioAdmin,
) )
from registrar.models import ( from registrar.models import (
Domain, Domain,
@ -38,6 +39,8 @@ from registrar.models import (
FederalAgency, FederalAgency,
UserGroup, UserGroup,
TransitionDomain, TransitionDomain,
Portfolio,
Suborganization,
) )
from registrar.models.portfolio_invitation import PortfolioInvitation from registrar.models.portfolio_invitation import PortfolioInvitation
from registrar.models.senior_official import SeniorOfficial from registrar.models.senior_official import SeniorOfficial
@ -2042,3 +2045,79 @@ class TestUserGroup(TestCase):
response, "Groups are a way to bundle admin permissions so they can be easily assigned to multiple users." response, "Groups are a way to bundle admin permissions so they can be easily assigned to multiple users."
) )
self.assertContains(response, "Show more") self.assertContains(response, "Show more")
class TestPortfolioAdmin(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.site = AdminSite()
cls.superuser = create_superuser()
cls.admin = PortfolioAdmin(model=Portfolio, admin_site=cls.site)
cls.factory = RequestFactory()
def setUp(self):
self.client = Client(HTTP_HOST="localhost:8080")
self.portfolio = Portfolio.objects.create(organization_name="Test Portfolio", creator=self.superuser)
def tearDown(self):
Suborganization.objects.all().delete()
DomainInformation.objects.all().delete()
DomainRequest.objects.all().delete()
Domain.objects.all().delete()
Portfolio.objects.all().delete()
@less_console_noise_decorator
def test_created_on_display(self):
"""Tests the custom created on which is a reskin of the created_at field"""
created_on = self.admin.created_on(self.portfolio)
expected_date = self.portfolio.created_at.strftime("%b %d, %Y")
self.assertEqual(created_on, expected_date)
@less_console_noise_decorator
def test_suborganizations_display(self):
"""Tests the custom suborg field which displays all related suborgs"""
Suborganization.objects.create(name="Sub1", portfolio=self.portfolio)
Suborganization.objects.create(name="Sub2", portfolio=self.portfolio)
suborganizations = self.admin.suborganizations(self.portfolio)
self.assertIn("Sub1", suborganizations)
self.assertIn("Sub2", suborganizations)
self.assertIn('<div class="display-block margin-top-1"></div>', suborganizations)
@less_console_noise_decorator
def test_domains_display(self):
"""Tests the custom domains field which displays all related domains"""
request_1 = completed_domain_request(
name="request1.gov", portfolio=self.portfolio, status=DomainRequest.DomainRequestStatus.IN_REVIEW
)
request_2 = completed_domain_request(
name="request2.gov", portfolio=self.portfolio, status=DomainRequest.DomainRequestStatus.IN_REVIEW
)
# Create some domain objects
request_1.approve()
request_2.approve()
domain_1 = DomainInformation.objects.get(domain_request=request_1).domain
domain_1.name = "domain1.gov"
domain_1.save()
domain_2 = DomainInformation.objects.get(domain_request=request_2).domain
domain_2.name = "domain2.gov"
domain_2.save()
domains = self.admin.domains(self.portfolio)
self.assertIn("domain1.gov", domains)
self.assertIn("domain2.gov", domains)
self.assertIn('<div class="display-block margin-top-1"></div>', domains)
@less_console_noise_decorator
def test_domain_requests_display(self):
"""Tests the custom domains requests field which displays all related requests"""
completed_domain_request(name="request1.gov", portfolio=self.portfolio)
completed_domain_request(name="request2.gov", portfolio=self.portfolio)
domain_requests = self.admin.domain_requests(self.portfolio)
self.assertIn("request1.gov", domain_requests)
self.assertIn("request2.gov", domain_requests)
self.assertIn('<div class="display-block margin-top-1"></div>', domain_requests)