mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-14 16:47:02 +02:00
Add unit tests
This commit is contained in:
parent
93b5cee23d
commit
ae6cea7100
3 changed files with 105 additions and 56 deletions
|
@ -9,6 +9,7 @@ from epplibwrapper.errors import RegistryError
|
|||
from registrar.models import Domain
|
||||
from registrar.management.commands.utility.terminal_helper import TerminalColors, TerminalHelper
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
try:
|
||||
from epplib.exceptions import TransportError
|
||||
except ImportError:
|
||||
|
@ -42,15 +43,9 @@ class Command(BaseCommand):
|
|||
help="Sets a cap on the number of records to parse",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--disableIdempotentCheck",
|
||||
action=argparse.BooleanOptionalAction,
|
||||
help="Disable script idempotence"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--debug",
|
||||
action=argparse.BooleanOptionalAction,
|
||||
help="Increases log chattiness"
|
||||
"--disableIdempotentCheck", action=argparse.BooleanOptionalAction, help="Disable script idempotence"
|
||||
)
|
||||
parser.add_argument("--debug", action=argparse.BooleanOptionalAction, help="Increases log chattiness")
|
||||
|
||||
def handle(self, **options):
|
||||
"""
|
||||
|
@ -80,8 +75,7 @@ class Command(BaseCommand):
|
|||
self.check_if_positive_int(limit_parse, "limitParse")
|
||||
|
||||
valid_domains = Domain.objects.filter(
|
||||
expiration_date__gte=date(2023, 11, 15),
|
||||
state=Domain.State.READY
|
||||
expiration_date__gte=date(2023, 11, 15), state=Domain.State.READY
|
||||
).order_by("name")
|
||||
|
||||
domains_to_change_count = valid_domains.count()
|
||||
|
@ -113,9 +107,7 @@ class Command(BaseCommand):
|
|||
domain.renew_domain(extension_amount)
|
||||
except (RegistryError, TransportError) as err:
|
||||
logger.error(
|
||||
f"{TerminalColors.FAIL}"
|
||||
f"Failed to update expiration date for {domain}"
|
||||
f"{TerminalColors.ENDC}"
|
||||
f"{TerminalColors.FAIL}" f"Failed to update expiration date for {domain}" f"{TerminalColors.ENDC}"
|
||||
)
|
||||
logger.error(err)
|
||||
except Exception as err:
|
||||
|
@ -124,9 +116,7 @@ class Command(BaseCommand):
|
|||
else:
|
||||
self.update_success.append(domain.name)
|
||||
logger.info(
|
||||
f"{TerminalColors.OKCYAN}"
|
||||
f"Successfully updated expiration date for {domain}"
|
||||
f"{TerminalColors.ENDC}"
|
||||
f"{TerminalColors.OKCYAN}" f"Successfully updated expiration date for {domain}" f"{TerminalColors.ENDC}"
|
||||
)
|
||||
|
||||
# == Helper functions == #
|
||||
|
@ -139,7 +129,7 @@ class Command(BaseCommand):
|
|||
# CAVEAT: This check stops working after a year has elapsed between when this script
|
||||
# was ran, and when it was ran again. This is good enough for now, but a more robust
|
||||
# solution would be a DB flag.
|
||||
is_idempotent = proposed_date < (date.today() + relativedelta(years=extension_amount+1))
|
||||
is_idempotent = proposed_date < (date.today() + relativedelta(years=extension_amount + 1))
|
||||
return is_idempotent
|
||||
|
||||
def prompt_user_to_proceed(self, extension_amount, domains_to_change_count):
|
||||
|
@ -156,11 +146,7 @@ class Command(BaseCommand):
|
|||
prompt_title="Do you wish to modify Expiration Dates for the given Domains?",
|
||||
)
|
||||
|
||||
logger.info(
|
||||
f"{TerminalColors.MAGENTA}"
|
||||
"Preparing to extend expiration dates..."
|
||||
f"{TerminalColors.ENDC}"
|
||||
)
|
||||
logger.info(f"{TerminalColors.MAGENTA}" "Preparing to extend expiration dates..." f"{TerminalColors.ENDC}")
|
||||
|
||||
def check_if_positive_int(self, value: int, var_name: str):
|
||||
"""
|
||||
|
@ -169,8 +155,7 @@ class Command(BaseCommand):
|
|||
"""
|
||||
if value < 0:
|
||||
raise argparse.ArgumentTypeError(
|
||||
f"{value} is an invalid integer value for {var_name}. "
|
||||
"Must be positive."
|
||||
f"{value} is an invalid integer value for {var_name}. " "Must be positive."
|
||||
)
|
||||
|
||||
return value
|
||||
|
|
|
@ -819,6 +819,16 @@ class MockEppLib(TestCase):
|
|||
ex_date=datetime.date(2023, 5, 25),
|
||||
)
|
||||
|
||||
mockDnsNeededRenewedDomainExpDate = fakedEppObject(
|
||||
"fakeneeded.gov",
|
||||
ex_date=datetime.date(2023, 2, 15),
|
||||
)
|
||||
|
||||
mockRecentRenewedDomainExpDate = fakedEppObject(
|
||||
"waterbutpurple.gov",
|
||||
ex_date=datetime.date(2025, 1, 10),
|
||||
)
|
||||
|
||||
def _mockDomainName(self, _name, _avail=False):
|
||||
return MagicMock(
|
||||
res_data=[
|
||||
|
@ -919,6 +929,16 @@ class MockEppLib(TestCase):
|
|||
def mockRenewDomainCommand(self, _request, cleaned):
|
||||
if getattr(_request, "name", None) == "fake-error.gov":
|
||||
raise RegistryError(code=ErrorCode.PARAMETER_VALUE_RANGE_ERROR)
|
||||
elif getattr(_request, "name", None) == "waterbutpurple.gov":
|
||||
return MagicMock(
|
||||
res_data=[self.mockRecentRenewedDomainExpDate],
|
||||
code=ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY,
|
||||
)
|
||||
elif getattr(_request, "name", None) == "fakeneeded.gov":
|
||||
return MagicMock(
|
||||
res_data=[self.mockDnsNeededRenewedDomainExpDate],
|
||||
code=ErrorCode.COMMAND_COMPLETED_SUCCESSFULLY,
|
||||
)
|
||||
else:
|
||||
return MagicMock(
|
||||
res_data=[self.mockRenewedDomainExpDate],
|
||||
|
|
|
@ -26,10 +26,14 @@ class TestExtendExpirationDates(MockEppLib):
|
|||
def setUp(self):
|
||||
"""Defines the file name of migration_json and the folder its contained in"""
|
||||
super().setUp()
|
||||
self.domain, _ = Domain.objects.get_or_create(
|
||||
name="fake.gov",
|
||||
state=Domain.State.READY,
|
||||
expiration_date=datetime.date(2023, 5, 25)
|
||||
Domain.objects.get_or_create(
|
||||
name="waterbutpurple.gov", state=Domain.State.READY, expiration_date=datetime.date(2023, 11, 15)
|
||||
)
|
||||
Domain.objects.get_or_create(
|
||||
name="fake.gov", state=Domain.State.READY, expiration_date=datetime.date(2022, 5, 25)
|
||||
)
|
||||
Domain.objects.get_or_create(
|
||||
name="fakeneeded.gov", state=Domain.State.DNS_NEEDED, expiration_date=datetime.date(2023, 11, 15)
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
|
@ -59,45 +63,85 @@ class TestExtendExpirationDates(MockEppLib):
|
|||
call_command("extend_expiration_dates")
|
||||
|
||||
def test_extends_expiration_date_correctly(self):
|
||||
desired_domain = Domain.objects.filter(name="fake.gov").get()
|
||||
"""
|
||||
Tests that the extend_expiration_dates method extends dates as expected
|
||||
"""
|
||||
desired_domain = Domain.objects.filter(name="waterbutpurple.gov").get()
|
||||
desired_domain.expiration_date = desired_domain.expiration_date + relativedelta(years=1)
|
||||
|
||||
# Run the expiration date script
|
||||
self.run_extend_expiration_dates()
|
||||
|
||||
self.assertEqual(desired_domain, self.domain)
|
||||
current_domain = Domain.objects.filter(name="waterbutpurple.gov").get()
|
||||
self.assertEqual(desired_domain, current_domain)
|
||||
|
||||
# Explicitly test the expiration date
|
||||
self.assertEqual(self.domain.expiration_date, datetime.date(2024, 5, 25))
|
||||
self.assertEqual(current_domain.expiration_date, datetime.date(2025, 1, 10))
|
||||
|
||||
# TODO ALSO NEED A TEST FOR NON READY DOMAINS
|
||||
def test_extends_expiration_date_skips_non_current(self):
|
||||
"""
|
||||
Tests that the extend_expiration_dates method correctly skips domains
|
||||
with an expiration date less than a certain threshold.
|
||||
"""
|
||||
desired_domain = Domain.objects.filter(name="fake.gov").get()
|
||||
desired_domain.expiration_date = desired_domain.expiration_date + relativedelta(years=1)
|
||||
|
||||
# Run the expiration date script
|
||||
self.run_extend_expiration_dates()
|
||||
|
||||
current_domain = Domain.objects.filter(name="FakeWebsite3.gov").get()
|
||||
current_domain = Domain.objects.filter(name="fake.gov").get()
|
||||
self.assertEqual(desired_domain, current_domain)
|
||||
|
||||
# Explicitly test the expiration date. The extend_expiration_dates script
|
||||
# will skip all dates less than date(2023, 11, 15), meaning that this domain
|
||||
# should not be affected by the change.
|
||||
self.assertEqual(current_domain.expiration_date, datetime.date(2023, 5, 25))
|
||||
self.assertEqual(current_domain.expiration_date, datetime.date(2022, 5, 25))
|
||||
|
||||
def test_extends_expiration_date_idempotent(self):
|
||||
desired_domain = Domain.objects.filter(name="FakeWebsite3.gov").get()
|
||||
def test_extends_expiration_date_skips_non_ready(self):
|
||||
"""
|
||||
Tests that the extend_expiration_dates method correctly skips domains not in the state "ready"
|
||||
"""
|
||||
desired_domain = Domain.objects.filter(name="fakeneeded.gov").get()
|
||||
desired_domain.expiration_date = desired_domain.expiration_date + relativedelta(years=1)
|
||||
|
||||
# Run the expiration date script
|
||||
self.run_extend_expiration_dates()
|
||||
|
||||
current_domain = Domain.objects.filter(name="FakeWebsite3.gov").get()
|
||||
current_domain = Domain.objects.filter(name="fake.gov").get()
|
||||
self.assertEqual(desired_domain, current_domain)
|
||||
|
||||
# Explicitly test the expiration date. The extend_expiration_dates script
|
||||
# will skip all dates less than date(2023, 11, 15), meaning that this domain
|
||||
# should not be affected by the change.
|
||||
self.assertEqual(current_domain.expiration_date, datetime.date(2023, 11, 15))
|
||||
|
||||
def test_extends_expiration_date_idempotent(self):
|
||||
"""
|
||||
Tests the idempotency of the extend_expiration_dates command.
|
||||
|
||||
Verifies that running the method multiple times does not change the expiration date
|
||||
of a domain beyond the initial extension.
|
||||
"""
|
||||
desired_domain = Domain.objects.filter(name="waterbutpurple.gov").get()
|
||||
desired_domain.expiration_date = desired_domain.expiration_date + relativedelta(years=1)
|
||||
|
||||
# Run the expiration date script
|
||||
self.run_extend_expiration_dates()
|
||||
|
||||
current_domain = Domain.objects.filter(name="waterbutpurple.gov").get()
|
||||
self.assertEqual(desired_domain, current_domain)
|
||||
|
||||
# Explicitly test the expiration date
|
||||
self.assertEqual(desired_domain.expiration_date, datetime.date(2023, 9, 30))
|
||||
self.assertEqual(desired_domain.expiration_date, datetime.date(2024, 11, 15))
|
||||
|
||||
# Run the expiration date script again
|
||||
self.run_extend_expiration_dates()
|
||||
|
||||
# The old domain shouldn't have changed
|
||||
self.assertEqual(desired_domain, current_domain)
|
||||
|
||||
# Explicitly test the expiration date - should be the same
|
||||
self.assertEqual(desired_domain.expiration_date, datetime.date(2024, 11, 15))
|
||||
|
||||
|
||||
class TestOrganizationMigration(TestCase):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue