Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: writing out more data in API (#170, #172) #174

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ test-keepdb:
ENABLE_LDAP=0 ENABLE_LDAP_SECONDARY=0 $(manage) test -v2 --settings=config.settings.test --keepdb


.PHONY: test-snap
test-snap:
ENABLE_LDAP=0 ENABLE_LDAP_SECONDARY=0 $(manage) test -v2 --settings=config.settings.test --keepdb usersec.tests.test_serializers adminsec.tests.test_views_api


.PHONY: isort
isort:
isort --force-sort-within-sections --profile=black .
Expand Down
5 changes: 5 additions & 0 deletions adminsec/tests/test_views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def setUp(self):
self.user_admin.is_staff = True
self.user_admin.is_superuser = True
self.user_admin.save()
# Connect user to HpcUser.
self.user_user.name = "User Name"
self.user_user.save()
self.hpcuser_user.user = self.user_user
self.hpcuser_user.save()


class TestHpcUserListApiView(ApiTestCase):
Expand Down
2 changes: 1 addition & 1 deletion hpcaccess/users/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UserFactory(DjangoModelFactory):
username = Faker("user_name")
email = Faker("email")
name = Faker("name")
phone = Faker("+123456")
phone = Faker("phone_number")

@post_generation
def password(self, create: bool, extracted: Sequence[Any], **kwargs):
Expand Down
25 changes: 20 additions & 5 deletions usersec/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ def __repr__(self):
)

def __str__(self):
return "{} ({})".format(self.user.name, self.username)
self_user_name = None
if self.user:
self_user_name = self.user.username
return "{} ({})".format(self_user_name, self.username)

def get_pending_invitations(self):
return self.hpcprojectinvitations.filter(status=INVITATION_STATUS_PENDING)
Expand Down Expand Up @@ -501,10 +504,16 @@ def __repr__(self):
)

def __str__(self):
self_owner_username = None
if self.owner:
self_owner_username = self.owner.username
self_delegate_username = None
if self.delegate:
self_delegate_username = self.delegate.username
return "{} ({}, {})".format(
self.name,
self.owner.username,
self.delegate.username if self.delegate else None,
self_owner_username,
self_delegate_username,
)

def get_manager_emails(self):
Expand Down Expand Up @@ -661,10 +670,16 @@ def __repr__(self):
)

def __str__(self):
self_group_owner_username = None
if self.group and self.group.owner:
self_group_owner_username = self.group.owner.username
self_delegate_username = None
if self.delegate:
self_delegate_username = self.delegate.username
return "{} (owner: {}, delegate: {})".format(
self.name,
self.group.owner.username,
self.delegate.username if self.delegate else None,
self_group_owner_username,
self_delegate_username,
)

def get_manager_emails(self):
Expand Down
24 changes: 24 additions & 0 deletions usersec/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""DRF serializers for the usersec app."""

from typing import Optional

from rest_framework import serializers

from usersec.models import HpcGroupVersion, HpcUser, HpcUserVersion
Expand Down Expand Up @@ -29,9 +31,29 @@ class HpcUserAbstractSerializer(HpcObjectAbstractSerializer):
uid = serializers.IntegerField()
username = serializers.CharField(read_only=True)
expiration = serializers.DateTimeField(read_only=True)
full_name = serializers.SerializerMethodField()
first_name = serializers.SerializerMethodField()
last_name = serializers.SerializerMethodField()
phone_number = serializers.SerializerMethodField()

def get_full_name(self, obj) -> str:
return obj.user.name

def get_last_name(self, obj) -> Optional[str]:
return obj.user.last_name

def get_first_name(self, obj) -> Optional[str]:
return obj.user.first_name

def get_phone_number(self, obj) -> Optional[str]:
return obj.user.phone

class Meta:
fields = HpcObjectAbstractSerializer.Meta.fields + [
"full_name",
"first_name",
"last_name",
"phone_number",
"primary_group",
"resources_requested",
"resources_used",
Expand Down Expand Up @@ -135,6 +157,7 @@ class HpcProjectAbstractSerializer(HpcObjectAbstractSerializer):
name = serializers.CharField(read_only=True)
folder = serializers.CharField()
expiration = serializers.DateTimeField(read_only=True)
members = serializers.SlugRelatedField(slug_field="uuid", many=True, read_only=True)

class Meta:
fields = HpcObjectAbstractSerializer.Meta.fields + [
Expand All @@ -148,6 +171,7 @@ class Meta:
"name",
"folder",
"expiration",
"members",
]


Expand Down
9 changes: 5 additions & 4 deletions usersec/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.timezone import utc
import factory

from hpcaccess.users.tests.factories import UserFactory
from usersec.models import (
HpcGroup,
HpcGroupChangeRequest,
Expand Down Expand Up @@ -158,7 +159,7 @@ class Meta:
resources_requested = {"tier1": 1, "tier2_mirrored": 0, "tier2_unmirrored": 0}
resources_used = {"tier1": 0.5, "tier2_mirrored": 0, "tier2_unmirrored": 0}
description = "this is a group"
creator = None # User
creator = factory.SubFactory(UserFactory) # User
gid = 2000
name = factory.Sequence(lambda n: f"hpc-group{n}")
folder = "/data/group"
Expand Down Expand Up @@ -213,11 +214,11 @@ class HpcUserFactory(HpcObjectFactoryBase):
class Meta:
model = HpcUser

user = None # User
user = factory.SubFactory(UserFactory) # User
primary_group = factory.SubFactory(HpcGroupFactory)
resources_requested = {"null": "null"}
resources_used = {"null": "null"}
creator = None # User
creator = factory.SubFactory(UserFactory) # User
description = "this is a user"
uid = 2000
username = factory.Sequence(lambda n: f"user{n}_" + settings.INSTITUTE_USERNAME_SUFFIX)
Expand Down Expand Up @@ -274,7 +275,7 @@ class Meta:
group = factory.SubFactory(HpcGroupFactory)
resources_requested = {"null": "null"}
resources_used = {"null": "null"}
creator = None # User
creator = factory.SubFactory(UserFactory) # User
delegate = None # HpcUser
description = "this is a project"
gid = 5000
Expand Down
9 changes: 9 additions & 0 deletions usersec/tests/snapshots/snap_test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
"description": "this is a group",
"expiration": "2050-01-01T00:00:00Z",
"folder": "/data/group",
"full_name": "name_placeholder",
"gid": 2000,
"name": "hpc-group0",
"owner": None,
"phone_number": "phone_number_placeholder",
"resources_requested": {"tier1": 1, "tier2_mirrored": 0, "tier2_unmirrored": 0},
"resources_used": {"tier1": 0.5, "tier2_mirrored": 0, "tier2_unmirrored": 0},
"status": "INITIAL",
Expand All @@ -29,9 +31,12 @@
"description": "this is a project",
"expiration": "2050-01-01T00:00:00Z",
"folder": "/data/project",
"full_name": "name_placeholder",
"gid": 5000,
"group": "group_uuid_placeholder",
"members": [],
"name": "hpc-project0",
"phone_number": "phone_number_placeholder",
"resources_requested": {"null": "null"},
"resources_used": {"null": "null"},
"status": "INITIAL",
Expand All @@ -43,6 +48,10 @@
"date_created": "2019-01-01T00:00:00Z",
"description": "this is a user",
"expiration": "2050-01-01T00:00:00Z",
"first_name": None,
"full_name": "name_placeholder",
"last_name": None,
"phone_number": "phone_number_placeholder",
"primary_group": "primary_group_uuid_placeholder",
"resources_requested": {"null": "null"},
"resources_used": {"null": "null"},
Expand Down
4 changes: 3 additions & 1 deletion usersec/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.conf import settings
from django.urls import reverse
from factory import SubFactory
from test_plus.test import TestCase

from usersec.models import (
Expand Down Expand Up @@ -317,7 +318,8 @@ def _test_save_with_version_new(self, **supplementaries):
data.update(supplementaries)

for k, v in data.items():
setattr(obj, k, v)
if not isinstance(v, SubFactory):
setattr(obj, k, v)

obj.save_with_version()

Expand Down
6 changes: 6 additions & 0 deletions usersec/tests/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def testSerializeExisting(self):
result = dict(serializer.data)
result["uuid"] = "uuid_placeholder"
result["primary_group"] = "primary_group_uuid_placeholder"
result["phone_number"] = "phone_number_placeholder"
result["full_name"] = "name_placeholder"
self.assertMatchSnapshot(result)


Expand All @@ -43,6 +45,8 @@ def testSerializeExisting(self):
serializer = HpcGroupSerializer(self.hpc_group)
result = dict(serializer.data)
result["uuid"] = "uuid_placeholder"
result["phone_number"] = "phone_number_placeholder"
result["full_name"] = "name_placeholder"
self.assertMatchSnapshot(result)


Expand All @@ -57,4 +61,6 @@ def testSerializeExisting(self):
result = dict(serializer.data)
result["uuid"] = "uuid_placeholder"
result["group"] = "group_uuid_placeholder"
result["phone_number"] = "phone_number_placeholder"
result["full_name"] = "name_placeholder"
self.assertMatchSnapshot(result)
Loading