Fix logic and additional unit tests

This commit is contained in:
Rebecca Hsieh 2025-02-10 12:47:53 -08:00
parent 95d912ecbc
commit 16e251c4fc
No known key found for this signature in database
3 changed files with 82 additions and 26 deletions

View file

@ -1519,8 +1519,7 @@ class DomainInvitationAdmin(BaseInvitationAdmin):
change_form_template = "django/admin/domain_invitation_change_form.html" change_form_template = "django/admin/domain_invitation_change_form.html"
def change_view(self, request, object_id, form_url="", extra_context=None): def change_view(self, request, object_id, form_url="", extra_context=None):
"""Override the change_view to add the invitation obj for the """Override the change_view to add the invitation obj for the change_form_object_tools template"""
change_form_object_tools template"""
if extra_context is None: if extra_context is None:
extra_context = {} extra_context = {}
@ -1529,6 +1528,15 @@ class DomainInvitationAdmin(BaseInvitationAdmin):
invitation = get_object_or_404(DomainInvitation, id=object_id) invitation = get_object_or_404(DomainInvitation, id=object_id)
extra_context["invitation"] = invitation extra_context["invitation"] = invitation
if request.method == "POST" and "cancel_invitation" in request.POST:
if invitation.status == DomainInvitation.DomainInvitationStatus.INVITED:
invitation.cancel_invitation()
invitation.save(update_fields=["status"])
messages.success(request, _("Invitation canceled successfully."))
# Redirect back to the change view
return redirect(reverse("admin:registrar_domaininvitation_change", args=[object_id]))
return super().change_view(request, object_id, form_url, extra_context) return super().change_view(request, object_id, form_url, extra_context)
def save_model(self, request, obj, form, change): def save_model(self, request, obj, form, change):

View file

@ -16,18 +16,22 @@
{% else %} {% else %}
<ul> <ul>
{% if opts.model_name == 'domaininvitation' %} {% if opts.model_name == 'domaininvitation' %}
{% if invitation.status == invitation.DomainInvitationStatus.INVITED %}
<li> <li>
<form method="post" action="{% url 'invitation-cancel' pk=invitation.id %}"> <form method="post">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="cancel_invitation" value="true">
<button type="submit" class="usa-button--dja"> <button type="submit" class="usa-button--dja">
<svg class="usa-icon"> <svg class="usa-icon">
<use xlink:href="{%static 'img/sprite.svg'%}#cancel"></use> <use xlink:href="{% static 'img/sprite.svg' %}#cancel"></use>
</svg> </svg>
<span>{% translate "Cancel invitation" %}</span> <span>{% translate "Cancel invitation" %}</span>
</button> </button>
</form> </form>
</li> </li>
{% endif %} {% endif %}
{% endif %}
<li> <li>
<a href="{% add_preserved_filters history_url %}">{% translate "History" %}</a> <a href="{% add_preserved_filters history_url %}">{% translate "History" %}</a>

View file

@ -514,7 +514,7 @@ class DomainInvitationAdminTest(TestCase):
self.client.force_login(self.staffuser) self.client.force_login(self.staffuser)
super().setUp() super().setUp()
def test_cancel_invitation_flow_in_admin(self): def test_successful_cancel_invitation_flow_in_admin(self):
"""Testing canceling a domain invitation in Django Admin.""" """Testing canceling a domain invitation in Django Admin."""
# 1. Create a domain and assign staff user role + domain manager # 1. Create a domain and assign staff user role + domain manager
@ -539,21 +539,65 @@ class DomainInvitationAdminTest(TestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertContains(response, "Cancel invitation") self.assertContains(response, "Cancel invitation")
# 5. Click the "Cancel invitation" button (a POST) # 5. Click the cancel invitation button
cancel_invitation_url = reverse("invitation-cancel", args=[invitation.id]) response = self.client.post(domain_invitation_change_url, {"cancel_invitation": "true"}, follow=True)
response = self.client.post(cancel_invitation_url, follow=True)
# 6.Confirm we're redirected to the domain managers page for the domain # 6. Make sure we're redirect back to the change view page in /admin
expected_redirect_url = reverse("domain-users", args=[domain.id]) self.assertRedirects(response, domain_invitation_change_url)
self.assertRedirects(response, expected_redirect_url)
# 7. Get the messages # 7. Confirm cancellation confirmation message appears
messages = list(get_messages(response.wsgi_request)) expected_message = f"Invitation for {invitation.email} on {domain.name} is canceled"
message_texts = [str(message) for message in messages] self.assertContains(response, expected_message)
# 8. Check that the success banner text is in the messages def test_no_cancel_invitation_button_in_retrieved_state(self):
expected_message = f"Canceled invitation to {invitation.email}." """Shouldn't be able to see the "Cancel invitation" button if invitation is RETRIEVED state"""
self.assertIn(expected_message, message_texts)
# 1. Create a domain and assign staff user role + domain manager
domain = Domain.objects.create(name="retrieved.gov")
UserDomainRole.objects.create(user=self.staffuser, domain=domain, role="manager")
# 2. Invite a domain manager to the above domain and NOT in invited state
invitation = DomainInvitation.objects.create(
email="retrievedinvitation@meoward.com",
domain=domain,
status=DomainInvitation.DomainInvitationStatus.RETRIEVED,
)
# 3. Go to the Domain Invitations list in /admin
domain_invitation_list_url = reverse("admin:registrar_domaininvitation_changelist")
response = self.client.get(domain_invitation_list_url)
self.assertEqual(response.status_code, 200)
# 4. Go to the change view of that invitation and make sure you CANNOT see the button
domain_invitation_change_url = reverse("admin:registrar_domaininvitation_change", args=[invitation.id])
response = self.client.get(domain_invitation_change_url)
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, "Cancel invitation")
def test_no_cancel_invitation_button_in_canceled_state(self):
"""Shouldn't be able to see the "Cancel invitation" button if invitation is CANCELED state"""
# 1. Create a domain and assign staff user role + domain manager
domain = Domain.objects.create(name="canceled.gov")
UserDomainRole.objects.create(user=self.staffuser, domain=domain, role="manager")
# 2. Invite a domain manager to the above domain and NOT in invited state
invitation = DomainInvitation.objects.create(
email="canceledinvitation@meoward.com",
domain=domain,
status=DomainInvitation.DomainInvitationStatus.CANCELED,
)
# 3. Go to the Domain Invitations list in /admin
domain_invitation_list_url = reverse("admin:registrar_domaininvitation_changelist")
response = self.client.get(domain_invitation_list_url)
self.assertEqual(response.status_code, 200)
# 4. Go to the change view of that invitation and make sure you CANNOT see the button
domain_invitation_change_url = reverse("admin:registrar_domaininvitation_change", args=[invitation.id])
response = self.client.get(domain_invitation_change_url)
self.assertEqual(response.status_code, 200)
self.assertNotContains(response, "Cancel invitation")
class TestDomainAdminWithClient(TestCase): class TestDomainAdminWithClient(TestCase):