mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-19 07:54:16 +02:00
Update unit tests
This commit is contained in:
parent
3c48fb9f7e
commit
8ea135b5a4
2 changed files with 75 additions and 23 deletions
|
@ -7,13 +7,14 @@ from registrar.models.domain import Domain
|
||||||
from registrar.models.public_contact import PublicContact
|
from registrar.models.public_contact import PublicContact
|
||||||
from registrar.models.user import User
|
from registrar.models.user import User
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
from registrar.models.user_domain_role import UserDomainRole
|
||||||
from registrar.tests.common import MockEppLib
|
from registrar.tests.common import MockEppLib
|
||||||
from registrar.utility.csv_export import (
|
from registrar.utility.csv_export import (
|
||||||
write_header,
|
|
||||||
write_body,
|
write_body,
|
||||||
get_default_start_date,
|
get_default_start_date,
|
||||||
get_default_end_date,
|
get_default_end_date,
|
||||||
)
|
)
|
||||||
|
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from unittest.mock import MagicMock, call, mock_open, patch
|
from unittest.mock import MagicMock, call, mock_open, patch
|
||||||
from api.views import get_current_federal, get_current_full
|
from api.views import get_current_federal, get_current_full
|
||||||
|
@ -336,11 +337,28 @@ class ExportDataTest(MockEppLib):
|
||||||
federal_agency="Armed Forces Retirement Home",
|
federal_agency="Armed Forces Retirement Home",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
meoward_user = get_user_model().objects.create(
|
||||||
|
username="meoward_username", first_name="first_meoward", last_name="last_meoward", email="meoward@rocks.com"
|
||||||
|
)
|
||||||
|
|
||||||
|
_, created = UserDomainRole.objects.get_or_create(
|
||||||
|
user=meoward_user, domain=self.domain_1, role=UserDomainRole.Roles.MANAGER
|
||||||
|
)
|
||||||
|
|
||||||
|
_, created = UserDomainRole.objects.get_or_create(
|
||||||
|
user=self.user, domain=self.domain_1, role=UserDomainRole.Roles.MANAGER
|
||||||
|
)
|
||||||
|
|
||||||
|
_, created = UserDomainRole.objects.get_or_create(
|
||||||
|
user=meoward_user, domain=self.domain_2, role=UserDomainRole.Roles.MANAGER
|
||||||
|
)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
PublicContact.objects.all().delete()
|
PublicContact.objects.all().delete()
|
||||||
Domain.objects.all().delete()
|
Domain.objects.all().delete()
|
||||||
DomainInformation.objects.all().delete()
|
DomainInformation.objects.all().delete()
|
||||||
User.objects.all().delete()
|
User.objects.all().delete()
|
||||||
|
UserDomainRole.objects.all().delete()
|
||||||
super().tearDown()
|
super().tearDown()
|
||||||
|
|
||||||
def test_export_domains_to_writer_security_emails(self):
|
def test_export_domains_to_writer_security_emails(self):
|
||||||
|
@ -383,7 +401,6 @@ class ExportDataTest(MockEppLib):
|
||||||
}
|
}
|
||||||
self.maxDiff = None
|
self.maxDiff = None
|
||||||
# Call the export functions
|
# Call the export functions
|
||||||
write_header(writer, columns)
|
|
||||||
write_body(writer, columns, sort_fields, filter_condition)
|
write_body(writer, columns, sort_fields, filter_condition)
|
||||||
# Reset the CSV file's position to the beginning
|
# Reset the CSV file's position to the beginning
|
||||||
csv_file.seek(0)
|
csv_file.seek(0)
|
||||||
|
@ -440,7 +457,6 @@ class ExportDataTest(MockEppLib):
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
# Call the export functions
|
# Call the export functions
|
||||||
write_header(writer, columns)
|
|
||||||
write_body(writer, columns, sort_fields, filter_condition)
|
write_body(writer, columns, sort_fields, filter_condition)
|
||||||
# Reset the CSV file's position to the beginning
|
# Reset the CSV file's position to the beginning
|
||||||
csv_file.seek(0)
|
csv_file.seek(0)
|
||||||
|
@ -489,7 +505,6 @@ class ExportDataTest(MockEppLib):
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
# Call the export functions
|
# Call the export functions
|
||||||
write_header(writer, columns)
|
|
||||||
write_body(writer, columns, sort_fields, filter_condition)
|
write_body(writer, columns, sort_fields, filter_condition)
|
||||||
# Reset the CSV file's position to the beginning
|
# Reset the CSV file's position to the beginning
|
||||||
csv_file.seek(0)
|
csv_file.seek(0)
|
||||||
|
@ -567,7 +582,6 @@ class ExportDataTest(MockEppLib):
|
||||||
}
|
}
|
||||||
|
|
||||||
# Call the export functions
|
# Call the export functions
|
||||||
write_header(writer, columns)
|
|
||||||
write_body(
|
write_body(
|
||||||
writer,
|
writer,
|
||||||
columns,
|
columns,
|
||||||
|
@ -575,10 +589,7 @@ class ExportDataTest(MockEppLib):
|
||||||
filter_condition,
|
filter_condition,
|
||||||
)
|
)
|
||||||
write_body(
|
write_body(
|
||||||
writer,
|
writer, columns, sort_fields_for_deleted_domains, filter_conditions_for_deleted_domains, False, False
|
||||||
columns,
|
|
||||||
sort_fields_for_deleted_domains,
|
|
||||||
filter_conditions_for_deleted_domains,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Reset the CSV file's position to the beginning
|
# Reset the CSV file's position to the beginning
|
||||||
|
@ -606,6 +617,61 @@ class ExportDataTest(MockEppLib):
|
||||||
|
|
||||||
self.assertEqual(csv_content, expected_content)
|
self.assertEqual(csv_content, expected_content)
|
||||||
|
|
||||||
|
def test_export_domains_to_writer_domain_managers(self):
|
||||||
|
"""Test that export_domains_to_writer returns the
|
||||||
|
expected domain managers"""
|
||||||
|
with less_console_noise():
|
||||||
|
# Create a CSV file in memory
|
||||||
|
csv_file = StringIO()
|
||||||
|
writer = csv.writer(csv_file)
|
||||||
|
# Define columns, sort fields, and filter condition
|
||||||
|
|
||||||
|
columns = [
|
||||||
|
"Domain name",
|
||||||
|
"Status",
|
||||||
|
"Expiration date",
|
||||||
|
"Domain type",
|
||||||
|
"Agency",
|
||||||
|
"Organization name",
|
||||||
|
"City",
|
||||||
|
"State",
|
||||||
|
"AO",
|
||||||
|
"AO email",
|
||||||
|
"Security contact email",
|
||||||
|
]
|
||||||
|
sort_fields = ["domain__name"]
|
||||||
|
filter_condition = {
|
||||||
|
"domain__state__in": [
|
||||||
|
Domain.State.READY,
|
||||||
|
Domain.State.DNS_NEEDED,
|
||||||
|
Domain.State.ON_HOLD,
|
||||||
|
],
|
||||||
|
}
|
||||||
|
self.maxDiff = None
|
||||||
|
# Call the export functions
|
||||||
|
write_body(writer, columns, sort_fields, filter_condition, True, True)
|
||||||
|
# Reset the CSV file's position to the beginning
|
||||||
|
csv_file.seek(0)
|
||||||
|
# Read the content into a variable
|
||||||
|
csv_content = csv_file.read()
|
||||||
|
# We expect READY domains,
|
||||||
|
# sorted alphabetially by domain name
|
||||||
|
expected_content = (
|
||||||
|
"Domain name,Status,Expiration date,Domain type,Agency,"
|
||||||
|
"Organization name,City,State,AO,AO email,"
|
||||||
|
"Security contact email,Domain manager email 1,Domain manager email 2,\n"
|
||||||
|
"adomain10.gov,Ready,,Federal,Armed Forces Retirement Home,,,, , ,\n"
|
||||||
|
"adomain2.gov,Dns needed,,Interstate,,,,, , , ,meoward@rocks.com\n"
|
||||||
|
"cdomain1.gov,Ready,,Federal - Executive,World War I Centennial Commission,,,"
|
||||||
|
", , , ,meoward@rocks.com,info@example.com\n"
|
||||||
|
"ddomain3.gov,On hold,,Federal,Armed Forces Retirement Home,,,, , , ,,\n"
|
||||||
|
)
|
||||||
|
# Normalize line endings and remove commas,
|
||||||
|
# spaces and leading/trailing whitespace
|
||||||
|
csv_content = csv_content.replace(",,", "").replace(",", "").replace(" ", "").replace("\r\n", "\n").strip()
|
||||||
|
expected_content = expected_content.replace(",,", "").replace(",", "").replace(" ", "").strip()
|
||||||
|
self.assertEqual(csv_content, expected_content)
|
||||||
|
|
||||||
|
|
||||||
class HelperFunctions(TestCase):
|
class HelperFunctions(TestCase):
|
||||||
"""This asserts that 1=1. Its limited usefulness lies in making sure the helper methods stay healthy."""
|
"""This asserts that 1=1. Its limited usefulness lies in making sure the helper methods stay healthy."""
|
||||||
|
|
|
@ -105,19 +105,6 @@ def parse_row(columns, domain_info: DomainInformation, security_emails_dict=None
|
||||||
return row
|
return row
|
||||||
|
|
||||||
|
|
||||||
# def _check_domain_managers(domain_info, columns):
|
|
||||||
# max_dm_count = 0
|
|
||||||
|
|
||||||
# dm_count = len(domain_info.domain.permissions.all())
|
|
||||||
# if dm_count > max_dm_count:
|
|
||||||
# max_dm_count = dm_count
|
|
||||||
# for i in range(1, max_dm_count + 1):
|
|
||||||
# if f"Domain manager email {i}" not in columns:
|
|
||||||
# columns.append(f"Domain manager email {i}")
|
|
||||||
|
|
||||||
# return columns
|
|
||||||
|
|
||||||
|
|
||||||
def _get_security_emails(sec_contact_ids):
|
def _get_security_emails(sec_contact_ids):
|
||||||
"""
|
"""
|
||||||
Retrieve security contact emails for the given security contact IDs.
|
Retrieve security contact emails for the given security contact IDs.
|
||||||
|
@ -186,7 +173,6 @@ def write_body(
|
||||||
rows = []
|
rows = []
|
||||||
for domain_info in page.object_list:
|
for domain_info in page.object_list:
|
||||||
if get_domain_managers:
|
if get_domain_managers:
|
||||||
# _check_domain_managers(domain_info, columns)
|
|
||||||
dm_count = len(domain_info.domain.permissions.all())
|
dm_count = len(domain_info.domain.permissions.all())
|
||||||
if dm_count > max_dm_count:
|
if dm_count > max_dm_count:
|
||||||
max_dm_count = dm_count
|
max_dm_count = dm_count
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue