Skip to content

Commit

Permalink
feat: add function to remove user from a group (#353) (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
stolpeo committed Dec 18, 2024
1 parent 8e0aa43 commit 8ab3c58
Show file tree
Hide file tree
Showing 24 changed files with 1,957 additions and 89 deletions.
14 changes: 6 additions & 8 deletions adminsec/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,9 @@
{footer}
""".lstrip()

#:
SUBJECT_MANAGER_CHANGE_REQUEST_APPROVED = (
SUBJECT_PREFIX + "Your {request_type} request has been approved"
)
NOTIFICATION_MANAGER_CHANGE_REQUEST_APPROVED = """
#: Notification text for managers for a request approval
SUBJECT_MANAGER_REQUEST_APPROVED = SUBJECT_PREFIX + "Your {request_type} request has been approved"
NOTIFICATION_MANAGER_REQUEST_APPROVED = """
{greeting}
your {request_type} request has been approved. Please see the active changes
Expand Down Expand Up @@ -521,11 +519,11 @@ def send_notification_manager_project_created(request, project):
return send_mail(subject, message, [request.requester.email])


def send_notification_manager_change_request_approved(request):
subject = SUBJECT_MANAGER_CHANGE_REQUEST_APPROVED.format(
def send_notification_manager_request_approved(request):
subject = SUBJECT_MANAGER_REQUEST_APPROVED.format(
request_type=request.get_request_type(),
)
message = NOTIFICATION_MANAGER_CHANGE_REQUEST_APPROVED.format(
message = NOTIFICATION_MANAGER_REQUEST_APPROVED.format(
greeting=USER_GREETING.format(user=request.requester.name),
request_type=request.get_request_type(),
hpc_access_link=HPC_ACCESS_LINK + request.get_detail_path(),
Expand Down
4 changes: 2 additions & 2 deletions adminsec/tests/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from adminsec.email import (
send_mail,
send_notification_admin_request,
send_notification_manager_change_request_approved,
send_notification_manager_group_created,
send_notification_manager_group_request,
send_notification_manager_project_created,
send_notification_manager_project_request,
send_notification_manager_request_approved,
send_notification_manager_request_denied,
send_notification_manager_revision_required,
send_notification_manager_user_decided_invitation,
Expand Down Expand Up @@ -71,7 +71,7 @@ def test_send_notification_manager_project_created(self):
self.assertEqual(len(mail.outbox), 1)

def test_send_notification_manager_change_request_approved(self):
ret = send_notification_manager_change_request_approved(self.hpc_user_change_request)
ret = send_notification_manager_request_approved(self.hpc_user_change_request)
self.assertEqual(ret, 1)
self.assertEqual(len(mail.outbox), 1)

Expand Down
233 changes: 233 additions & 0 deletions adminsec/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
HpcProjectCreateRequestFactory,
HpcUserChangeRequestFactory,
HpcUserCreateRequestFactory,
HpcUserDeleteRequestFactory,
HpcUserFactory,
TermsAndConditionsFactory,
)
Expand Down Expand Up @@ -1284,6 +1285,238 @@ def test_post(self):
self.assertEqual(len(mail.outbox), 1)


class TestHpcUserDeleteRequestDetailView(TestViewBase):
"""Tests for HpcUserDeleteRequestDetailView."""

def test_get(self):
request = HpcUserDeleteRequestFactory(
requester=self.user_owner, user=self.hpc_member, status=REQUEST_STATUS_ACTIVE
)

with self.login(self.user_hpcadmin):
response = self.client.get(
reverse(
"adminsec:hpcuserdeleterequest-detail",
kwargs={"hpcuserdeleterequest": request.uuid},
)
)

self.assertEqual(response.status_code, 200)
self.assertFalse(response.context["is_decided"])
self.assertFalse(response.context["is_denied"])
self.assertFalse(response.context["is_retracted"])
self.assertFalse(response.context["is_approved"])
self.assertTrue(response.context["is_active"])
self.assertFalse(response.context["is_revision"])
self.assertTrue(response.context["admin"])

def test_get_retracted(self):
request = HpcUserDeleteRequestFactory(
requester=self.user_owner, user=self.hpc_member, status=REQUEST_STATUS_RETRACTED
)

with self.login(self.user_hpcadmin):
response = self.client.get(
reverse(
"adminsec:hpcuserdeleterequest-detail",
kwargs={"hpcuserdeleterequest": request.uuid},
)
)

self.assertEqual(response.status_code, 200)
self.assertFalse(response.context["is_decided"])
self.assertFalse(response.context["is_denied"])
self.assertTrue(response.context["is_retracted"])
self.assertFalse(response.context["is_approved"])
self.assertFalse(response.context["is_active"])
self.assertFalse(response.context["is_revision"])

def test_get_denied(self):
request = HpcUserDeleteRequestFactory(
requester=self.user_owner, user=self.hpc_member, status=REQUEST_STATUS_DENIED
)

with self.login(self.user_hpcadmin):
response = self.client.get(
reverse(
"adminsec:hpcuserdeleterequest-detail",
kwargs={"hpcuserdeleterequest": request.uuid},
)
)

self.assertEqual(response.status_code, 200)
self.assertTrue(response.context["is_decided"])
self.assertTrue(response.context["is_denied"])
self.assertFalse(response.context["is_retracted"])
self.assertFalse(response.context["is_approved"])
self.assertFalse(response.context["is_active"])
self.assertFalse(response.context["is_revision"])

def test_get_approved(self):
request = HpcUserDeleteRequestFactory(
requester=self.user_owner, user=self.hpc_member, status=REQUEST_STATUS_APPROVED
)

with self.login(self.user_hpcadmin):
response = self.client.get(
reverse(
"adminsec:hpcuserdeleterequest-detail",
kwargs={"hpcuserdeleterequest": request.uuid},
)
)

self.assertEqual(response.status_code, 200)
self.assertTrue(response.context["is_decided"])
self.assertFalse(response.context["is_denied"])
self.assertFalse(response.context["is_retracted"])
self.assertTrue(response.context["is_approved"])
self.assertFalse(response.context["is_active"])
self.assertFalse(response.context["is_revision"])


class TestHpcUserDeleteRequestApproveView(TestViewBase):
"""Tests for HpcUserDeleteRequestApproveView."""

def setUp(self):
super().setUp()
self.obj = HpcUserDeleteRequestFactory(
requester=self.user_owner, user=self.hpc_member, status=REQUEST_STATUS_ACTIVE
)

def test_get(self):
with self.login(self.user_hpcadmin):
response = self.client.get(
reverse(
"adminsec:hpcuserdeleterequest-approve",
kwargs={"hpcuserdeleterequest": self.obj.uuid},
)
)
self.assertEqual(response.status_code, 200)
self.assertIsNotNone(response.context["form"])

def test_post(self):
with self.login(self.user_hpcadmin):
response = self.client.post(
reverse(
"adminsec:hpcuserdeleterequest-approve",
kwargs={"hpcuserdeleterequest": self.obj.uuid},
),
)

self.assertRedirects(
response,
reverse("adminsec:overview"),
)

messages = list(get_messages(response.wsgi_request))
self.assertEqual(len(messages), 1)
self.assertEqual(str(messages[0]), "Successfully approved request for user deletion.")

self.assertEqual(self.obj.status, REQUEST_STATUS_ACTIVE)
self.obj.refresh_from_db()
self.assertEqual(self.obj.status, REQUEST_STATUS_APPROVED)

self.assertEqual(len(mail.outbox), 1)


class TestHpcUserDeleteRequestRevisionView(TestViewBase):
"""Tests for HpcUserDeleteRequestRevisionView."""

def setUp(self):
super().setUp()
self.obj = HpcUserDeleteRequestFactory(
requester=self.user_owner, user=self.hpc_member, status=REQUEST_STATUS_ACTIVE
)

def test_get(self):
with self.login(self.user_hpcadmin):
response = self.client.get(
reverse(
"adminsec:hpcuserdeleterequest-revision",
kwargs={"hpcuserdeleterequest": self.obj.uuid},
)
)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["form"]["comment"].value(), "")
self.assertTrue(response.context["update"])

def test_post(self):
update = {
"comment": "I made a comment!",
}

with self.login(self.user_hpcadmin):
response = self.client.post(
reverse(
"adminsec:hpcuserdeleterequest-revision",
kwargs={"hpcuserdeleterequest": self.obj.uuid},
),
update,
)
self.assertRedirects(
response,
reverse("adminsec:overview"),
)

self.obj.refresh_from_db()

self.assertEqual(self.obj.comment, update["comment"])

messages = list(get_messages(response.wsgi_request))
self.assertEqual(len(messages), 1)
self.assertEqual(str(messages[0]), "Successfully requested revision for user deletion.")

self.assertEqual(len(mail.outbox), 1)


class TestHpcUserDeleteRequestDenyView(TestViewBase):
"""Tests for HpcUserDeleteRequestDenyView."""

def setUp(self):
super().setUp()
self.obj = HpcUserDeleteRequestFactory(
requester=self.user_owner, user=self.hpc_member, status=REQUEST_STATUS_ACTIVE
)

def test_get(self):
with self.login(self.user_hpcadmin):
response = self.client.get(
reverse(
"adminsec:hpcuserdeleterequest-deny",
kwargs={"hpcuserdeleterequest": self.obj.uuid},
)
)
self.assertEqual(response.status_code, 200)
self.assertIsNotNone(response.context["form"])

def test_post(self):
with self.login(self.user_hpcadmin):
response = self.client.post(
reverse(
"adminsec:hpcuserdeleterequest-deny",
kwargs={"hpcuserdeleterequest": self.obj.uuid},
),
data={"comment": "Denied"},
)

self.assertRedirects(
response,
reverse("adminsec:overview"),
)

messages = list(get_messages(response.wsgi_request))
self.assertEqual(len(messages), 1)
self.assertEqual(str(messages[0]), "Successfully denied request for user deletion.")

self.assertEqual(self.obj.status, REQUEST_STATUS_ACTIVE)
self.obj.refresh_from_db()
self.assertEqual(self.obj.comment, "Denied")
self.assertEqual(self.obj.status, REQUEST_STATUS_DENIED)

self.assertEqual(len(mail.outbox), 1)


class TestHpcProjectCreateRequestDetailView(TestViewBase):
"""Tests for HpcProjectCreateRequestDetailView."""

Expand Down
Loading

0 comments on commit 8ab3c58

Please sign in to comment.