Skip to content

Commit

Permalink
feat: indicate when hpcuser has been removed from group (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
stolpeo committed Dec 5, 2024
1 parent c943c94 commit 3bcf8af
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 13 deletions.
59 changes: 57 additions & 2 deletions adminsec/tests/test_views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from test_plus import TestCase

from usersec.models import REQUEST_STATUS_ACTIVE
from usersec.serializers import HPC_ALUMNI_GROUP
from usersec.tests.factories import (
HpcGroupCreateRequestFactory,
HpcGroupFactory,
Expand Down Expand Up @@ -634,8 +635,6 @@ class TestHpcAccessStatusApiView(ApiTestCase):
def test_get_succeed(self):
"""Test the GET method (staff users can do)."""

self.maxDiff = None

expected = {
"hpc_users": [
{
Expand Down Expand Up @@ -689,6 +688,62 @@ def test_get_succeed(self):
self.response_200()
self.assertEqual(self.last_response.json(), expected)

def test_get_succeed_alumni(self):
self.hpcuser_user.primary_group = None
self.hpcuser_user.save()
expected = {
"hpc_users": [
{
"uid": self.hpcuser_user.user.uid,
"email": self.hpcuser_user.user.email,
"full_name": "User Name",
"first_name": self.hpcuser_user.user.first_name,
"last_name": self.hpcuser_user.user.last_name,
"phone_number": None,
"primary_group": HPC_ALUMNI_GROUP,
"resources_requested": self.hpcuser_user.resources_requested,
"status": "INITIAL",
"description": self.hpcuser_user.description,
"username": self.hpcuser_user.username,
"expiration": self.hpcuser_user.expiration.strftime("%Y-%m-%dT%H:%M:%SZ"),
"home_directory": self.hpcuser_user.home_directory,
"login_shell": self.hpcuser_user.login_shell,
}
],
"hpc_groups": [
{
"owner": None,
"delegate": None,
"resources_requested": self.hpcuser_group.resources_requested,
"status": "INITIAL",
"description": self.hpcuser_group.description,
"name": self.hpcuser_group.name,
"folders": self.hpcuser_group.folders,
"expiration": self.hpcuser_group.expiration.strftime("%Y-%m-%dT%H:%M:%SZ"),
"gid": self.hpcuser_group.gid,
}
],
"hpc_projects": [
{
"gid": self.hpcuser_project.gid,
"group": self.hpcuser_group.name,
"delegate": None,
"resources_requested": self.hpcuser_project.resources_requested,
"status": "INITIAL",
"description": self.hpcuser_project.description,
"name": self.hpcuser_project.name,
"folders": self.hpcuser_project.folders,
"expiration": self.hpcuser_project.expiration.strftime("%Y-%m-%dT%H:%M:%SZ"),
"members": [],
}
],
}
for user in [self.user_staff, self.user_admin, self.user_hpcadmin]:
with self.login(user):
self.get("adminsec:api-hpcaccess-status")
self.response_200()
self.assertEqual(self.last_response.json(), expected)

def test_get_fail(self):
"""Test the GET method (non-staff cannot do)."""
for user in [self.user_user]:
Expand Down
11 changes: 10 additions & 1 deletion usersec/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
HpcUserVersion,
)

HPC_ALUMNI_GROUP = "hpc-alumnis"


class HpcObjectAbstractSerializer(serializers.Serializer):
"""Common base class for HPC object serializers."""
Expand Down Expand Up @@ -46,6 +48,7 @@ class HpcUserAbstractSerializer(HpcObjectAbstractSerializer):
phone_number = serializers.SerializerMethodField()
home_directory = serializers.CharField()
login_shell = serializers.CharField()
removed = serializers.BooleanField(read_only=True)

def get_email(self, obj) -> Optional[str]:
return obj.user.email
Expand Down Expand Up @@ -82,6 +85,7 @@ class Meta:
"expiration",
"home_directory",
"login_shell",
"removed",
]


Expand All @@ -100,7 +104,12 @@ class Meta:
class HpcUserStatusSerializer(HpcUserAbstractSerializer, serializers.ModelSerializer):
"""Serializer for HpcUser model."""

primary_group = serializers.SlugRelatedField(slug_field="name", read_only=True)
primary_group = serializers.SerializerMethodField()

def get_primary_group(self, obj):
if obj.primary_group is None:
return HPC_ALUMNI_GROUP
return obj.primary_group.name

class Meta:
model = HpcUser
Expand Down
140 changes: 135 additions & 5 deletions usersec/tests/snapshots/snap_test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,75 @@

snapshots = Snapshot()

snapshots["TestHpcGroupCreateRequestSerializer::testSerializeExisting 1"] = {
snapshots["TestHcpaccessStatus::testSerializerExisting 1"] = {
"hpc_groups": [
{
"delegate": None,
"description": "this is a group",
"expiration": "2050-01-01T00:00:00Z",
"folders": {
"tier1_scratch": "/data/scratch/group",
"tier1_work": "/data/work/group",
"tier2_mirrored": "/data/mirrored/group",
"tier2_unmirrored": "/data/unmirrored/group",
},
"gid": 2000,
"name": "group0",
"owner": None,
"resources_requested": {
"tier1_scratch": 1,
"tier1_work": 1,
"tier2_mirrored": 0,
"tier2_unmirrored": 0,
},
"status": "INITIAL",
}
],
"hpc_projects": [
{
"delegate": None,
"description": "this is a project",
"expiration": "2050-01-01T00:00:00Z",
"folders": {
"tier1_scratch": "/data/scratch/project",
"tier1_work": "/data/work/project",
"tier2_mirrored": "/data/mirrored/project",
"tier2_unmirrored": "/data/unmirrored/project",
},
"gid": 5000,
"group": "group0",
"members": [],
"name": "hpc-project0",
"resources_requested": {
"tier1_scratch": 1,
"tier1_work": 1,
"tier2_mirrored": 0,
"tier2_unmirrored": 0,
},
"status": "INITIAL",
}
],
"hpc_users": [
{
"description": "this is a user",
"email": "[email protected]",
"expiration": "2050-01-01T00:00:00Z",
"first_name": "",
"full_name": "Kari Reed",
"home_directory": "/data/cephfs-1/home/users/user0_c",
"last_name": "",
"login_shell": "/usr/bin/bash",
"phone_number": "(791)316-3049",
"primary_group": "group0",
"resources_requested": {"tier1_home": 1.0},
"status": "INITIAL",
"uid": 3632,
"username": "user0_c",
}
],
}

snapshots["TestHpcGroupCreateRequestSerializer::testSerializerExisting 1"] = {
"current_version": 1,
"date_created": "2019-01-01T00:00:00Z",
"description": "some group create request",
Expand All @@ -17,7 +85,7 @@
"uuid": "uuid_placeholder",
}

snapshots["TestHpcGroupSerializer::testSerializeExisting 1"] = {
snapshots["TestHpcGroupSerializer::testSerializerExisting 1"] = {
"current_version": 1,
"date_created": "2019-01-01T00:00:00Z",
"delegate": None,
Expand Down Expand Up @@ -48,7 +116,29 @@
"uuid": "uuid_placeholder",
}

snapshots["TestHpcProjectCreateRequestSerializer::testSerializeExisting 1"] = {
snapshots["TestHpcGroupStatusSerializer::testSerializerExisting 1"] = {
"delegate": None,
"description": "this is a group",
"expiration": "2050-01-01T00:00:00Z",
"folders": {
"tier1_scratch": "/data/scratch/group",
"tier1_work": "/data/work/group",
"tier2_mirrored": "/data/mirrored/group",
"tier2_unmirrored": "/data/unmirrored/group",
},
"gid": 2000,
"name": "group0",
"owner": None,
"resources_requested": {
"tier1_scratch": 1,
"tier1_work": 1,
"tier2_mirrored": 0,
"tier2_unmirrored": 0,
},
"status": "INITIAL",
}

snapshots["TestHpcProjectCreateRequestSerializer::testSerializerExisting 1"] = {
"current_version": 1,
"date_created": "2019-01-01T00:00:00Z",
"description": "some description",
Expand All @@ -62,7 +152,7 @@
"uuid": "uuid_placeholder",
}

snapshots["TestHpcProjectSerializer::testSerializeExisting 1"] = {
snapshots["TestHpcProjectSerializer::testSerializerExisting 1"] = {
"current_version": 1,
"date_created": "2019-01-01T00:00:00Z",
"delegate": None,
Expand Down Expand Up @@ -94,7 +184,30 @@
"uuid": "uuid_placeholder",
}

snapshots["TestHpcUserSerializer::testSerializeExisting 1"] = {
snapshots["TestHpcProjectStatusSerializer::testSerializerExisting 1"] = {
"delegate": None,
"description": "this is a project",
"expiration": "2050-01-01T00:00:00Z",
"folders": {
"tier1_scratch": "/data/scratch/project",
"tier1_work": "/data/work/project",
"tier2_mirrored": "/data/mirrored/project",
"tier2_unmirrored": "/data/unmirrored/project",
},
"gid": 5000,
"group": "group_name_placeholder",
"members": [],
"name": "hpc-project0",
"resources_requested": {
"tier1_scratch": 1,
"tier1_work": 1,
"tier2_mirrored": 0,
"tier2_unmirrored": 0,
},
"status": "INITIAL",
}

snapshots["TestHpcUserSerializer::testSerializerExisting 1"] = {
"current_version": 1,
"date_created": "2019-01-01T00:00:00Z",
"description": "this is a user",
Expand All @@ -114,3 +227,20 @@
"username": "user0_c",
"uuid": "uuid_placeholder",
}

snapshots["TestHpcUserStatusSerializer::testSerializerExisting 1"] = {
"description": "this is a user",
"email": "email_placeholder",
"expiration": "2050-01-01T00:00:00Z",
"first_name": "first_name_placeholder",
"full_name": "name_placeholder",
"home_directory": "/data/cephfs-1/home/users/user0_c",
"last_name": "last_name_placeholder",
"login_shell": "/usr/bin/bash",
"phone_number": "phone_number_placeholder",
"primary_group": "primary_group_name_placeholder",
"resources_requested": {"tier1_home": 1.0},
"status": "INITIAL",
"uid": 2000,
"username": "user0_c",
}
Loading

0 comments on commit 3bcf8af

Please sign in to comment.