This commit is contained in:
zandercymatics 2024-05-08 14:41:19 -06:00
parent f7208a80ea
commit 5c685a3fd6
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 121 additions and 122 deletions

View file

@ -746,6 +746,10 @@ class MockDb(TestCase):
DomainInvitation.objects.all().delete() DomainInvitation.objects.all().delete()
FederalAgency.objects.all().delete() FederalAgency.objects.all().delete()
def get_time_aware_date(self, date=datetime(2023, 11, 1)):
"""Returns a time aware date"""
return timezone.make_aware(date)
def mock_user(): def mock_user():
"""A simple user.""" """A simple user."""

View file

@ -25,56 +25,6 @@ from registrar.utility.s3_bucket import S3ClientError, S3ClientErrorCodes # typ
from datetime import datetime from datetime import datetime
from django.utils import timezone from django.utils import timezone
from .common import MockDb, MockEppLib, less_console_noise from .common import MockDb, MockEppLib, less_console_noise
from api.tests.common import less_console_noise_decorator
class HelperFunctions(MockDb):
"""This asserts that 1=1. Its limited usefulness lies in making sure the helper methods stay healthy."""
def get_time_aware_date(self, date=datetime(2023, 11, 1)):
"""Returns a time aware date"""
return timezone.make_aware(date)
def test_get_default_start_date(self):
expected_date = self.get_time_aware_date()
actual_date = get_default_start_date()
self.assertEqual(actual_date, expected_date)
def test_get_default_end_date(self):
# Note: You may need to mock timezone.now() for accurate testing
expected_date = timezone.now()
actual_date = get_default_end_date()
self.assertEqual(actual_date.date(), expected_date.date())
def test_get_sliced_domains(self):
"""Should get fitered domains counts sliced by org type and election office."""
with less_console_noise():
filter_condition = {
"domain__permissions__isnull": False,
"domain__first_ready__lte": self.end_date,
}
# Test with distinct
managed_domains_sliced_at_end_date = get_sliced_domains(filter_condition)
expected_content = [3, 2, 1, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(managed_domains_sliced_at_end_date, expected_content)
# Test without distinct
managed_domains_sliced_at_end_date = get_sliced_domains(filter_condition)
expected_content = [3, 2, 1, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(managed_domains_sliced_at_end_date, expected_content)
def test_get_sliced_requests(self):
"""Should get fitered requests counts sliced by org type and election office."""
with less_console_noise():
filter_condition = {
"status": DomainRequest.DomainRequestStatus.SUBMITTED,
"submission_date__lte": self.end_date,
}
submitted_requests_sliced_at_end_date = get_sliced_requests(filter_condition)
expected_content = [2, 2, 0, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(submitted_requests_sliced_at_end_date, expected_content)
class CsvReportsTest(MockDb): class CsvReportsTest(MockDb):
@ -244,89 +194,89 @@ class CsvReportsTest(MockDb):
self.assertEqual(expected_file_content, response.content) self.assertEqual(expected_file_content, response.content)
class ExportDataTest(HelperFunctions, MockEppLib): class ExportDataTest(MockDb, MockEppLib):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
def tearDown(self): def tearDown(self):
super().tearDown() super().tearDown()
@less_console_noise_decorator
def test_export_domains_to_writer_security_emails_and_first_ready(self): def test_export_domains_to_writer_security_emails_and_first_ready(self):
"""Test that export_domains_to_writer returns the """Test that export_domains_to_writer returns the
expected security email and first_ready value""" expected security email and first_ready value"""
# Add security email information with less_console_noise:
self.domain_1.name = "defaultsecurity.gov" # Add security email information
self.domain_1.save() self.domain_1.name = "defaultsecurity.gov"
# Invoke setter self.domain_1.save()
self.domain_1.security_contact # Invoke setter
# Invoke setter self.domain_1.security_contact
self.domain_2.security_contact # Invoke setter
# Invoke setter self.domain_2.security_contact
self.domain_3.security_contact # Invoke setter
self.domain_3.security_contact
# Add a first ready date on the first domain. Leaving the others blank. # Add a first ready date on the first domain. Leaving the others blank.
self.domain_1.first_ready = get_default_start_date() self.domain_1.first_ready = get_default_start_date()
self.domain_1.save() self.domain_1.save()
# Create a CSV file in memory # Create a CSV file in memory
csv_file = StringIO() csv_file = StringIO()
writer = csv.writer(csv_file) writer = csv.writer(csv_file)
# Define columns, sort fields, and filter condition # Define columns, sort fields, and filter condition
columns = [ columns = [
"Domain name", "Domain name",
"Domain type", "Domain type",
"Agency", "Agency",
"Organization name", "Organization name",
"City", "City",
"State", "State",
"AO", "AO",
"AO email", "AO email",
"Security contact email", "Security contact email",
"Status", "Status",
"Expiration date", "Expiration date",
"First ready on", "First ready on",
] ]
sort_fields = ["domain__name"] sort_fields = ["domain__name"]
filter_condition = { filter_condition = {
"domain__state__in": [ "domain__state__in": [
Domain.State.READY, Domain.State.READY,
Domain.State.DNS_NEEDED, Domain.State.DNS_NEEDED,
Domain.State.ON_HOLD, Domain.State.ON_HOLD,
], ],
} }
self.maxDiff = None self.maxDiff = None
# Call the export functions # Call the export functions
write_csv_for_domains( write_csv_for_domains(
writer, writer,
columns, columns,
sort_fields, sort_fields,
filter_condition, filter_condition,
should_get_domain_managers=False, should_get_domain_managers=False,
should_write_header=True, should_write_header=True,
) )
# 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)
# Read the content into a variable # Read the content into a variable
csv_content = csv_file.read() csv_content = csv_file.read()
# We expect READY domains, # We expect READY domains,
# sorted alphabetially by domain name # sorted alphabetially by domain name
expected_content = ( expected_content = (
"Domain name,Domain type,Agency,Organization name,City,State,AO," "Domain name,Domain type,Agency,Organization name,City,State,AO,"
"AO email,Security contact email,Status,Expiration date, First ready on\n" "AO email,Security contact email,Status,Expiration date, First ready on\n"
"adomain10.gov,Federal,Armed Forces Retirement Home,Ready,2024-05-09\n" "adomain10.gov,Federal,Armed Forces Retirement Home,Ready,2024-05-09\n"
"adomain2.gov,Interstate,(blank),Dns needed,(blank)\n" "adomain2.gov,Interstate,(blank),Dns needed,(blank)\n"
"cdomain11.govFederal-ExecutiveWorldWarICentennialCommissionReady,2024-05-08\n" "cdomain11.govFederal-ExecutiveWorldWarICentennialCommissionReady,2024-05-08\n"
"ddomain3.gov,Federal,Armed Forces Retirement Home,security@mail.gov,On hold,2023-11-15,(blank)\n" "ddomain3.gov,Federal,Armed Forces Retirement Home,security@mail.gov,On hold,2023-11-15,(blank)\n"
"defaultsecurity.gov,Federal - Executive,World War I Centennial Commission,(blank),Ready,2023-11-01\n" "defaultsecurity.gov,Federal - Executive,World War I Centennial Commission,(blank),Ready,2023-11-01\n"
"zdomain12.govInterstateReady,2024-05-08\n" "zdomain12.govInterstateReady,2024-05-08\n"
) )
# Normalize line endings and remove commas, # Normalize line endings and remove commas,
# spaces and leading/trailing whitespace # spaces and leading/trailing whitespace
csv_content = csv_content.replace(",,", "").replace(",", "").replace(" ", "").replace("\r\n", "\n").strip() csv_content = csv_content.replace(",,", "").replace(",", "").replace(" ", "").replace("\r\n", "\n").strip()
expected_content = expected_content.replace(",,", "").replace(",", "").replace(" ", "").strip() expected_content = expected_content.replace(",,", "").replace(",", "").replace(" ", "").strip()
self.assertEqual(csv_content, expected_content) self.assertEqual(csv_content, expected_content)
def test_write_csv_for_domains(self): def test_write_csv_for_domains(self):
"""Test that write_body returns the """Test that write_body returns the
@ -747,3 +697,48 @@ class ExportDataTest(HelperFunctions, MockEppLib):
expected_content = expected_content.replace(",,", "").replace(",", "").replace(" ", "").strip() expected_content = expected_content.replace(",,", "").replace(",", "").replace(" ", "").strip()
self.assertEqual(csv_content, expected_content) self.assertEqual(csv_content, expected_content)
class HelperFunctions(MockDb):
"""This asserts that 1=1. Its limited usefulness lies in making sure the helper methods stay healthy."""
def test_get_default_start_date(self):
expected_date = self.get_time_aware_date()
actual_date = get_default_start_date()
self.assertEqual(actual_date, expected_date)
def test_get_default_end_date(self):
# Note: You may need to mock timezone.now() for accurate testing
expected_date = timezone.now()
actual_date = get_default_end_date()
self.assertEqual(actual_date.date(), expected_date.date())
def test_get_sliced_domains(self):
"""Should get fitered domains counts sliced by org type and election office."""
with less_console_noise():
filter_condition = {
"domain__permissions__isnull": False,
"domain__first_ready__lte": self.end_date,
}
# Test with distinct
managed_domains_sliced_at_end_date = get_sliced_domains(filter_condition)
expected_content = [3, 2, 1, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(managed_domains_sliced_at_end_date, expected_content)
# Test without distinct
managed_domains_sliced_at_end_date = get_sliced_domains(filter_condition)
expected_content = [3, 2, 1, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(managed_domains_sliced_at_end_date, expected_content)
def test_get_sliced_requests(self):
"""Should get fitered requests counts sliced by org type and election office."""
with less_console_noise():
filter_condition = {
"status": DomainRequest.DomainRequestStatus.SUBMITTED,
"submission_date__lte": self.end_date,
}
submitted_requests_sliced_at_end_date = get_sliced_requests(filter_condition)
expected_content = [2, 2, 0, 0, 0, 0, 0, 0, 0, 0]
self.assertEqual(submitted_requests_sliced_at_end_date, expected_content)