Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zandercymatics committed Oct 29, 2024
1 parent aeb496c commit c2a07a8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/registrar/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3193,9 +3193,6 @@ def get_fieldsets(self, request, obj=None):
return self.add_fieldsets
return super().get_fieldsets(request, obj)

# TODO - I think the solution to this may just be to set this as editable.
# Then in the underlying page override, we need a fake readonly display that
# is toggled on or off.
def get_readonly_fields(self, request, obj=None):
"""Set the read-only state on form elements.
We have 2 conditions that determine which fields are read-only:
Expand Down Expand Up @@ -3241,7 +3238,6 @@ def change_view(self, request, object_id, form_url="", extra_context=None):
return super().change_view(request, object_id, form_url, extra_context)

def save_model(self, request, obj: Portfolio, form, change):

if hasattr(obj, "creator") is False:
# ---- update creator ----
# Set the creator field to the current admin user
Expand All @@ -3255,8 +3251,8 @@ def save_model(self, request, obj: Portfolio, form, change):
if is_federal and obj.organization_name is None:
obj.organization_name = obj.federal_agency.agency

# TODO - this should be handled almost entirely in javascript
# Handle the federal agency and senior official fields
# Set the senior official field to the senior official on the federal agency
# when federal - otherwise, clear the field.
if obj.organization_type == obj.OrganizationChoices.FEDERAL:
if obj.federal_agency:
if obj.federal_agency.so_federal_agency.exists():
Expand All @@ -3269,7 +3265,6 @@ def save_model(self, request, obj: Portfolio, form, change):
obj.senior_official = None
obj.federal_agency = FederalAgency.objects.filter(agency="Non-Federal Agency").first()


super().save_model(request, obj, form, change)


Expand Down
60 changes: 60 additions & 0 deletions src/registrar/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,66 @@ def test_portfolio_members_display(self):
display_members = self.admin.display_members(self.portfolio)
self.assertIn(f'<a href="{url}">2 members</a>', display_members)

@less_console_noise_decorator
def test_senior_official_readonly_for_federal_org(self):
"""Test that senior_official field is readonly for federal organizations"""
request = self.factory.get("/")
request.user = self.superuser

# Create a federal portfolio
portfolio = Portfolio.objects.create(
organization_name="Test Federal Org",
organization_type=DomainRequest.OrganizationChoices.FEDERAL,
creator=self.superuser,
)

readonly_fields = self.admin.get_readonly_fields(request, portfolio)
self.assertIn("senior_official", readonly_fields)

# Change to non-federal org
portfolio.organization_type = DomainRequest.OrganizationChoices.CITY
readonly_fields = self.admin.get_readonly_fields(request, portfolio)
self.assertNotIn("senior_official", readonly_fields)

@less_console_noise_decorator
def test_senior_official_auto_assignment(self):
"""Test automatic senior official assignment based on organization type and federal agency"""
request = self.factory.get("/")
request.user = self.superuser

# Create a federal agency with a senior official
federal_agency = FederalAgency.objects.create(agency="Test Agency")
senior_official = SeniorOfficial.objects.create(
first_name="Test",
last_name="Official",
title="Some guy",
email="[email protected]",
federal_agency=federal_agency,
)

# Create a federal portfolio
portfolio = Portfolio.objects.create(
organization_name="Test Federal Org",
organization_type=DomainRequest.OrganizationChoices.FEDERAL,
creator=self.superuser,
)

# Test that the federal org gets senior official from agency when federal
portfolio.federal_agency = federal_agency
self.admin.save_model(request, portfolio, form=None, change=False)
self.assertEqual(portfolio.senior_official, senior_official)

# Test non-federal org clears senior official when not city
portfolio.organization_type = DomainRequest.OrganizationChoices.CITY
self.admin.save_model(request, portfolio, form=None, change=True)
self.assertIsNone(portfolio.senior_official)
self.assertEqual(portfolio.federal_agency.agency, "Non-Federal Agency")

# Cleanup
senior_official.delete()
federal_agency.delete()
portfolio.delete()


class TestTransferUser(WebTest):
"""User transfer custom admin page"""
Expand Down

0 comments on commit c2a07a8

Please sign in to comment.