-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1633 from cisagov/za/1484-domain-manager-delete
(On getgov-za) Ticket #1484: Domain manager delete
- Loading branch information
Showing
12 changed files
with
444 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2662,6 +2662,7 @@ def tearDown(self): | |
super().tearDown() | ||
self.user.is_staff = False | ||
self.user.save() | ||
User.objects.all().delete() | ||
|
||
def test_domain_managers(self): | ||
response = self.client.get(reverse("domain-users", kwargs={"pk": self.domain.id})) | ||
|
@@ -2677,6 +2678,183 @@ def test_domain_user_add(self): | |
response = self.client.get(reverse("domain-users-add", kwargs={"pk": self.domain.id})) | ||
self.assertContains(response, "Add a domain manager") | ||
|
||
def test_domain_user_delete(self): | ||
"""Tests if deleting a domain manager works""" | ||
|
||
# Add additional users | ||
dummy_user_1 = User.objects.create( | ||
username="macncheese", | ||
email="[email protected]", | ||
) | ||
dummy_user_2 = User.objects.create( | ||
username="pastapizza", | ||
email="[email protected]", | ||
) | ||
|
||
role_1 = UserDomainRole.objects.create(user=dummy_user_1, domain=self.domain, role=UserDomainRole.Roles.MANAGER) | ||
role_2 = UserDomainRole.objects.create(user=dummy_user_2, domain=self.domain, role=UserDomainRole.Roles.MANAGER) | ||
|
||
response = self.client.get(reverse("domain-users", kwargs={"pk": self.domain.id})) | ||
|
||
# Make sure we're on the right page | ||
self.assertContains(response, "Domain managers") | ||
|
||
# Make sure the desired user exists | ||
self.assertContains(response, "[email protected]") | ||
|
||
# Delete dummy_user_1 | ||
response = self.client.post( | ||
reverse("domain-user-delete", kwargs={"pk": self.domain.id, "user_pk": dummy_user_1.id}), follow=True | ||
) | ||
|
||
# Grab the displayed messages | ||
messages = list(response.context["messages"]) | ||
self.assertEqual(len(messages), 1) | ||
|
||
# Ensure the error we recieve is in line with what we expect | ||
message = messages[0] | ||
self.assertEqual(message.message, "Removed [email protected] as a manager for this domain.") | ||
self.assertEqual(message.tags, "success") | ||
|
||
# Check that role_1 deleted in the DB after the post | ||
deleted_user_exists = UserDomainRole.objects.filter(id=role_1.id).exists() | ||
self.assertFalse(deleted_user_exists) | ||
|
||
# Ensure that the current user wasn't deleted | ||
current_user_exists = UserDomainRole.objects.filter(user=self.user.id, domain=self.domain).exists() | ||
self.assertTrue(current_user_exists) | ||
|
||
# Ensure that the other userdomainrole was not deleted | ||
role_2_exists = UserDomainRole.objects.filter(id=role_2.id).exists() | ||
self.assertTrue(role_2_exists) | ||
|
||
def test_domain_user_delete_denied_if_no_permission(self): | ||
"""Deleting a domain manager is denied if the user has no permission to do so""" | ||
|
||
# Create a domain object | ||
vip_domain = Domain.objects.create(name="freeman.gov") | ||
|
||
# Add users | ||
dummy_user_1 = User.objects.create( | ||
username="bagel", | ||
email="[email protected]", | ||
) | ||
dummy_user_2 = User.objects.create( | ||
username="pastapizza", | ||
email="[email protected]", | ||
) | ||
|
||
role_1 = UserDomainRole.objects.create(user=dummy_user_1, domain=vip_domain, role=UserDomainRole.Roles.MANAGER) | ||
role_2 = UserDomainRole.objects.create(user=dummy_user_2, domain=vip_domain, role=UserDomainRole.Roles.MANAGER) | ||
|
||
response = self.client.get(reverse("domain-users", kwargs={"pk": vip_domain.id})) | ||
|
||
# Make sure that we can't access the domain manager page normally | ||
self.assertEqual(response.status_code, 403) | ||
|
||
# Try to delete dummy_user_1 | ||
response = self.client.post( | ||
reverse("domain-user-delete", kwargs={"pk": vip_domain.id, "user_pk": dummy_user_1.id}), follow=True | ||
) | ||
|
||
# Ensure that we are denied access | ||
self.assertEqual(response.status_code, 403) | ||
|
||
# Ensure that the user wasn't deleted | ||
role_1_exists = UserDomainRole.objects.filter(id=role_1.id).exists() | ||
self.assertTrue(role_1_exists) | ||
|
||
# Ensure that the other userdomainrole was not deleted | ||
role_2_exists = UserDomainRole.objects.filter(id=role_2.id).exists() | ||
self.assertTrue(role_2_exists) | ||
|
||
# Make sure that the current user wasn't deleted for some reason | ||
current_user_exists = UserDomainRole.objects.filter(user=dummy_user_1.id, domain=vip_domain.id).exists() | ||
self.assertTrue(current_user_exists) | ||
|
||
def test_domain_user_delete_denied_if_last_man_standing(self): | ||
"""Deleting a domain manager is denied if the user is the only manager""" | ||
|
||
# Create a domain object | ||
vip_domain = Domain.objects.create(name="olive-oil.gov") | ||
|
||
# Add the requesting user as the only manager on the domain | ||
UserDomainRole.objects.create(user=self.user, domain=vip_domain, role=UserDomainRole.Roles.MANAGER) | ||
|
||
response = self.client.get(reverse("domain-users", kwargs={"pk": vip_domain.id})) | ||
|
||
# Make sure that we can still access the domain manager page normally | ||
self.assertContains(response, "Domain managers") | ||
|
||
# Make sure that the logged in user exists | ||
self.assertContains(response, "[email protected]") | ||
|
||
# Try to delete the current user | ||
response = self.client.post( | ||
reverse("domain-user-delete", kwargs={"pk": vip_domain.id, "user_pk": self.user.id}), follow=True | ||
) | ||
|
||
# Ensure that we are denied access | ||
self.assertEqual(response.status_code, 403) | ||
|
||
# Make sure that the current user wasn't deleted | ||
current_user_exists = UserDomainRole.objects.filter(user=self.user.id, domain=vip_domain.id).exists() | ||
self.assertTrue(current_user_exists) | ||
|
||
def test_domain_user_delete_self_redirects_home(self): | ||
"""Tests if deleting yourself redirects to home""" | ||
# Add additional users | ||
dummy_user_1 = User.objects.create( | ||
username="macncheese", | ||
email="[email protected]", | ||
) | ||
dummy_user_2 = User.objects.create( | ||
username="pastapizza", | ||
email="[email protected]", | ||
) | ||
|
||
role_1 = UserDomainRole.objects.create(user=dummy_user_1, domain=self.domain, role=UserDomainRole.Roles.MANAGER) | ||
role_2 = UserDomainRole.objects.create(user=dummy_user_2, domain=self.domain, role=UserDomainRole.Roles.MANAGER) | ||
|
||
response = self.client.get(reverse("domain-users", kwargs={"pk": self.domain.id})) | ||
|
||
# Make sure we're on the right page | ||
self.assertContains(response, "Domain managers") | ||
|
||
# Make sure the desired user exists | ||
self.assertContains(response, "[email protected]") | ||
|
||
# Make sure more than one UserDomainRole exists on this object | ||
self.assertContains(response, "[email protected]") | ||
|
||
# Delete the current user | ||
response = self.client.post( | ||
reverse("domain-user-delete", kwargs={"pk": self.domain.id, "user_pk": self.user.id}), follow=True | ||
) | ||
|
||
# Check if we've been redirected to the home page | ||
self.assertContains(response, "Manage your domains") | ||
|
||
# Grab the displayed messages | ||
messages = list(response.context["messages"]) | ||
self.assertEqual(len(messages), 1) | ||
|
||
# Ensure the error we recieve is in line with what we expect | ||
message = messages[0] | ||
self.assertEqual(message.message, "You are no longer managing the domain igorville.gov.") | ||
self.assertEqual(message.tags, "success") | ||
|
||
# Ensure that the current user was deleted | ||
current_user_exists = UserDomainRole.objects.filter(user=self.user.id, domain=self.domain).exists() | ||
self.assertFalse(current_user_exists) | ||
|
||
# Ensure that the other userdomainroles are not deleted | ||
role_1_exists = UserDomainRole.objects.filter(id=role_1.id).exists() | ||
self.assertTrue(role_1_exists) | ||
|
||
role_2_exists = UserDomainRole.objects.filter(id=role_2.id).exists() | ||
self.assertTrue(role_2_exists) | ||
|
||
@boto3_mocking.patching | ||
def test_domain_user_add_form(self): | ||
"""Adding an existing user works.""" | ||
|
Oops, something went wrong.