Skip to content

Commit

Permalink
Merge pull request #2364 from cisagov/bob/2274-renewal
Browse files Browse the repository at this point in the history
Issue #2274: removed data calculation from renew_domain DJA function
  • Loading branch information
dave-kennedy-ecs authored Jun 29, 2024
2 parents 98be06e + b92c3cb commit 694cfa5
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 121 deletions.
55 changes: 4 additions & 51 deletions src/registrar/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from dateutil.relativedelta import relativedelta # type: ignore
from epplibwrapper.errors import ErrorCode, RegistryError
from registrar.models.user_domain_role import UserDomainRole
from waffle.admin import FlagAdmin
Expand Down Expand Up @@ -2238,25 +2237,12 @@ def changeform_view(self, request, object_id=None, form_url="", extra_context=No

extra_context["state_help_message"] = Domain.State.get_admin_help_text(domain.state)
extra_context["domain_state"] = domain.get_state_display()

# Pass in what the an extended expiration date would be for the expiration date modal
self._set_expiration_date_context(domain, extra_context)
extra_context["curr_exp_date"] = (
domain.expiration_date if domain.expiration_date is not None else self._get_current_date()
)

return super().changeform_view(request, object_id, form_url, extra_context)

def _set_expiration_date_context(self, domain, extra_context):
"""Given a domain, calculate the an extended expiration date
from the current registry expiration date."""
years_to_extend_by = self._get_calculated_years_for_exp_date(domain)
try:
curr_exp_date = domain.registry_expiration_date
except KeyError:
# No expiration date was found. Return none.
extra_context["extended_expiration_date"] = None
else:
new_date = curr_exp_date + relativedelta(years=years_to_extend_by)
extra_context["extended_expiration_date"] = new_date

def response_change(self, request, obj):
# Create dictionary of action functions
ACTION_FUNCTIONS = {
Expand Down Expand Up @@ -2284,11 +2270,9 @@ def do_extend_expiration_date(self, request, obj):
self.message_user(request, "Object is not of type Domain.", messages.ERROR)
return None

years = self._get_calculated_years_for_exp_date(obj)

# Renew the domain.
try:
obj.renew_domain(length=years)
obj.renew_domain()
self.message_user(
request,
"Successfully extended the expiration date.",
Expand All @@ -2313,37 +2297,6 @@ def do_extend_expiration_date(self, request, obj):

return HttpResponseRedirect(".")

def _get_calculated_years_for_exp_date(self, obj, extension_period: int = 1):
"""Given the current date, an extension period, and a registry_expiration_date
on the domain object, calculate the number of years needed to extend the
current expiration date by the extension period.
"""
# Get the date we want to update to
desired_date = self._get_current_date() + relativedelta(years=extension_period)

# Grab the current expiration date
try:
exp_date = obj.registry_expiration_date
except KeyError:
# if no expiration date from registry, set it to today
logger.warning("current expiration date not set; setting to today")
exp_date = self._get_current_date()

# If the expiration date is super old (2020, for example), we need to
# "catch up" to the current year, so we add the difference.
# If both years match, then lets just proceed as normal.
calculated_exp_date = exp_date + relativedelta(years=extension_period)

year_difference = desired_date.year - exp_date.year

years = extension_period
if desired_date > calculated_exp_date:
# Max probably isn't needed here (no code flow), but it guards against negative and 0.
# In both of those cases, we just want to extend by the extension_period.
years = max(extension_period, year_difference)

return years

# Workaround for unit tests, as we cannot mock date directly.
# it is immutable. Rather than dealing with a convoluted workaround,
# lets wrap this in a function.
Expand Down
4 changes: 2 additions & 2 deletions src/registrar/templates/django/admin/domain_change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h2 class="usa-modal__heading" id="modal-1-heading">
</h2>
<div class="usa-prose">
<p>
This will extend the expiration date by one year.
This will extend the expiration date by one year from today.
{# Acts as a <br> #}
<div class="display-inline"></div>
This action cannot be undone.
Expand All @@ -78,7 +78,7 @@ <h2 class="usa-modal__heading" id="modal-1-heading">
Domain: <b>{{ original.name }}</b>
{# Acts as a <br> #}
<div class="display-inline"></div>
New expiration date: <b>{{ extended_expiration_date }}</b>
Current expiration date: <b>{{ curr_exp_date }}</b>
{{test}}
</p>
</div>
Expand Down
2 changes: 0 additions & 2 deletions src/registrar/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,6 @@ def mockRenewDomainCommand(self, _request, cleaned):
def mockInfoDomainCommands(self, _request, cleaned):
request_name = getattr(_request, "name", None).lower()

print(request_name)

# Define a dictionary to map request names to data and extension values
request_mappings = {
"security.gov": (self.infoDomainNoContact, None),
Expand Down
69 changes: 4 additions & 65 deletions src/registrar/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ def test_extend_expiration_date_button(self, mock_date_today):

# Create a ready domain with a preset expiration date
domain, _ = Domain.objects.get_or_create(name="fake.gov", state=Domain.State.READY)

response = self.app.get(reverse("admin:registrar_domain_change", args=[domain.pk]))

# load expiration date into cache and registrar with below command
domain.registry_expiration_date
# Make sure the ex date is what we expect it to be
domain_ex_date = Domain.objects.get(id=domain.id).expiration_date
self.assertEqual(domain_ex_date, date(2023, 5, 25))
Expand All @@ -400,7 +400,6 @@ def test_extend_expiration_date_button(self, mock_date_today):
self.assertEqual(response.status_code, 200)
self.assertContains(response, domain.name)
self.assertContains(response, "Extend expiration date")
self.assertContains(response, "New expiration date: <b>May 25, 2025</b>")

# Ensure the message we recieve is in line with what we expect
expected_message = "Successfully extended the expiration date."
Expand Down Expand Up @@ -519,70 +518,10 @@ def test_extend_expiration_date_button_epp(self, mock_date_today):
# Follow the response
response = response.follow()

# This value is based off of the current year - the expiration date.
# We "freeze" time to 2024, so 2024 - 2023 will always result in an
# "extension" of 2, as that will be one year of extension from that date.
extension_length = 2

# Assert that it is calling the function with the right extension length.
# We only need to test the value that EPP sends, as we can assume the other
# test cases cover the "renew" function.
renew_mock.assert_has_calls([call(length=extension_length)], any_order=False)

# We should not make duplicate calls
self.assertEqual(renew_mock.call_count, 1)

# Assert that everything on the page looks correct
self.assertEqual(response.status_code, 200)
self.assertContains(response, domain.name)
self.assertContains(response, "Extend expiration date")

# Ensure the message we recieve is in line with what we expect
expected_message = "Successfully extended the expiration date."
expected_call = call(
# The WGSI request doesn't need to be tested
ANY,
messages.INFO,
expected_message,
extra_tags="",
fail_silently=False,
)
mock_add_message.assert_has_calls([expected_call], 1)

@patch("registrar.admin.DomainAdmin._get_current_date", return_value=date(2023, 1, 1))
def test_extend_expiration_date_button_date_matches_epp(self, mock_date_today):
"""
Tests if extend_expiration_date button sends the right epp command
when the current year matches the expiration date
"""

# Create a ready domain with a preset expiration date
domain, _ = Domain.objects.get_or_create(name="fake.gov", state=Domain.State.READY)

response = self.app.get(reverse("admin:registrar_domain_change", args=[domain.pk]))

# Make sure that the page is loading as expected
self.assertEqual(response.status_code, 200)
self.assertContains(response, domain.name)
self.assertContains(response, "Extend expiration date")

# Grab the form to submit
form = response.forms["domain_form"]

with patch("django.contrib.messages.add_message") as mock_add_message:
with patch("registrar.models.Domain.renew_domain") as renew_mock:
# Submit the form
response = form.submit("_extend_expiration_date")

# Follow the response
response = response.follow()

extension_length = 1

# Assert that it is calling the function with the right extension length.
# Assert that it is calling the function with the default extension length.
# We only need to test the value that EPP sends, as we can assume the other
# test cases cover the "renew" function.
renew_mock.assert_has_calls([call(length=extension_length)], any_order=False)
renew_mock.assert_has_calls([call()], any_order=False)

# We should not make duplicate calls
self.assertEqual(renew_mock.call_count, 1)
Expand Down
1 change: 0 additions & 1 deletion src/registrar/tests/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,6 @@ def test_full_domain_request_report(self):
csv_file.seek(0)
# Read the content into a variable
csv_content = csv_file.read()
print(csv_content)
self.maxDiff = None
expected_content = (
# Header
Expand Down

0 comments on commit 694cfa5

Please sign in to comment.