Skip to content

Commit

Permalink
Add back deleted unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zandercymatics committed Jan 10, 2024
1 parent cb7eec6 commit d49b42d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
77 changes: 76 additions & 1 deletion src/registrar/tests/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from registrar.models.public_contact import PublicContact
from registrar.models.user import User
from django.contrib.auth import get_user_model
from registrar.tests.common import MockEppLib
from registrar.utility.csv_export import (
write_header,
write_body,
Expand Down Expand Up @@ -222,7 +223,7 @@ def test_load_full_report(self):
self.assertEqual(expected_file_content, response.content)


class ExportDataTest(TestCase):
class ExportDataTest(MockEppLib):
def setUp(self):
super().setUp()
username = "test_user"
Expand Down Expand Up @@ -335,6 +336,80 @@ def tearDown(self):
User.objects.all().delete()
super().tearDown()

def test_export_domains_to_writer_security_emails(self):
"""Test that export_domains_to_writer returns the
expected security email"""

# Add security email information
self.domain_1.name = "defaultsecurity.gov"
self.domain_1.save()

# Invoke setter
self.domain_1.security_contact

# Invoke setter
self.domain_2.security_contact

# Invoke setter
self.domain_3.security_contact

# Create a CSV file in memory
csv_file = StringIO()
writer = csv.writer(csv_file)

# Define columns, sort fields, and filter condition
columns = [
"Domain name",
"Domain type",
"Agency",
"Organization name",
"City",
"State",
"AO",
"AO email",
"Security contact email",
"Status",
"Expiration date",
]
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_header(writer, columns)
write_body(writer, columns, sort_fields, filter_condition)

# 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,Domain type,Agency,Organization name,City,State,AO,"
"AO email,Security contact email,Status,Expiration date\n"
"adomain10.gov,Federal,Armed Forces Retirement Home,Ready\n"
"adomain2.gov,Interstate,(blank),Dns needed\n"
"ddomain3.gov,Federal,Armed Forces Retirement Home,[email protected],On hold,2023-05-25\n"
"defaultsecurity.gov,Federal - Executive,World War I Centennial Commission,[email protected],Ready"
)

print(csv_content)
# 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)

def test_write_body(self):
"""Test that write_body returns the
existing domain, test that sort by domain name works,
Expand Down
2 changes: 2 additions & 0 deletions src/registrar/utility/csv_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def get_domain_infos(filter_condition, sort_fields):

def write_row(writer, columns, domain_info):
security_contacts = domain_info.domain.contacts.filter(contact_type=PublicContact.ContactTypeChoices.SECURITY)

# For linter
ao = " "
if domain_info.authorizing_official:
Expand Down Expand Up @@ -61,6 +62,7 @@ def write_row(writer, columns, domain_info):
"First ready": domain_info.domain.first_ready,
"Deleted": domain_info.domain.deleted,
}

writer.writerow([FIELDS.get(column, "") for column in columns])


Expand Down

0 comments on commit d49b42d

Please sign in to comment.