mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-03 00:12:16 +02:00
Fix logic and additional unit tests
This commit is contained in:
parent
95d912ecbc
commit
16e251c4fc
3 changed files with 82 additions and 26 deletions
|
@ -1519,8 +1519,7 @@ class DomainInvitationAdmin(BaseInvitationAdmin):
|
|||
change_form_template = "django/admin/domain_invitation_change_form.html"
|
||||
|
||||
def change_view(self, request, object_id, form_url="", extra_context=None):
|
||||
"""Override the change_view to add the invitation obj for the
|
||||
change_form_object_tools template"""
|
||||
"""Override the change_view to add the invitation obj for the change_form_object_tools template"""
|
||||
|
||||
if extra_context is None:
|
||||
extra_context = {}
|
||||
|
@ -1529,6 +1528,15 @@ class DomainInvitationAdmin(BaseInvitationAdmin):
|
|||
invitation = get_object_or_404(DomainInvitation, id=object_id)
|
||||
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)
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
|
|
|
@ -16,18 +16,22 @@
|
|||
{% else %}
|
||||
<ul>
|
||||
{% if opts.model_name == 'domaininvitation' %}
|
||||
<li>
|
||||
<form method="post" action="{% url 'invitation-cancel' pk=invitation.id %}">
|
||||
{% csrf_token %}
|
||||
<button type="submit" class="usa-button--dja">
|
||||
<svg class="usa-icon">
|
||||
<use xlink:href="{%static 'img/sprite.svg'%}#cancel"></use>
|
||||
</svg>
|
||||
<span>{% translate "Cancel invitation" %}</span>
|
||||
</button>
|
||||
</form>
|
||||
</li>
|
||||
{% if invitation.status == invitation.DomainInvitationStatus.INVITED %}
|
||||
<li>
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="cancel_invitation" value="true">
|
||||
<button type="submit" class="usa-button--dja">
|
||||
<svg class="usa-icon">
|
||||
<use xlink:href="{% static 'img/sprite.svg' %}#cancel"></use>
|
||||
</svg>
|
||||
<span>{% translate "Cancel invitation" %}</span>
|
||||
</button>
|
||||
</form>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
|
||||
|
||||
<li>
|
||||
<a href="{% add_preserved_filters history_url %}">{% translate "History" %}</a>
|
||||
|
|
|
@ -514,7 +514,7 @@ class DomainInvitationAdminTest(TestCase):
|
|||
self.client.force_login(self.staffuser)
|
||||
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."""
|
||||
|
||||
# 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.assertContains(response, "Cancel invitation")
|
||||
|
||||
# 5. Click the "Cancel invitation" button (a POST)
|
||||
cancel_invitation_url = reverse("invitation-cancel", args=[invitation.id])
|
||||
response = self.client.post(cancel_invitation_url, follow=True)
|
||||
# 5. Click the cancel invitation button
|
||||
response = self.client.post(domain_invitation_change_url, {"cancel_invitation": "true"}, follow=True)
|
||||
|
||||
# 6.Confirm we're redirected to the domain managers page for the domain
|
||||
expected_redirect_url = reverse("domain-users", args=[domain.id])
|
||||
self.assertRedirects(response, expected_redirect_url)
|
||||
# 6. Make sure we're redirect back to the change view page in /admin
|
||||
self.assertRedirects(response, domain_invitation_change_url)
|
||||
|
||||
# 7. Get the messages
|
||||
messages = list(get_messages(response.wsgi_request))
|
||||
message_texts = [str(message) for message in messages]
|
||||
# 7. Confirm cancellation confirmation message appears
|
||||
expected_message = f"Invitation for {invitation.email} on {domain.name} is canceled"
|
||||
self.assertContains(response, expected_message)
|
||||
|
||||
# 8. Check that the success banner text is in the messages
|
||||
expected_message = f"Canceled invitation to {invitation.email}."
|
||||
self.assertIn(expected_message, message_texts)
|
||||
def test_no_cancel_invitation_button_in_retrieved_state(self):
|
||||
"""Shouldn't be able to see the "Cancel invitation" button if invitation is RETRIEVED state"""
|
||||
|
||||
# 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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue