diff --git a/Makefile b/Makefile index f175ec6..703404a 100644 --- a/Makefile +++ b/Makefile @@ -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 . diff --git a/adminsec/tests/test_views_api.py b/adminsec/tests/test_views_api.py index 89dd955..bbb070c 100644 --- a/adminsec/tests/test_views_api.py +++ b/adminsec/tests/test_views_api.py @@ -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): diff --git a/hpcaccess/users/tests/factories.py b/hpcaccess/users/tests/factories.py index 819ed72..be6b382 100644 --- a/hpcaccess/users/tests/factories.py +++ b/hpcaccess/users/tests/factories.py @@ -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): diff --git a/usersec/models.py b/usersec/models.py index 308a2f9..58f4840 100644 --- a/usersec/models.py +++ b/usersec/models.py @@ -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) @@ -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): @@ -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): diff --git a/usersec/serializers.py b/usersec/serializers.py index 26add41..7d53442 100644 --- a/usersec/serializers.py +++ b/usersec/serializers.py @@ -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 @@ -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", @@ -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 + [ @@ -148,6 +171,7 @@ class Meta: "name", "folder", "expiration", + "members", ] diff --git a/usersec/tests/factories.py b/usersec/tests/factories.py index ebe0468..1440550 100644 --- a/usersec/tests/factories.py +++ b/usersec/tests/factories.py @@ -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, @@ -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" @@ -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) @@ -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 diff --git a/usersec/tests/snapshots/snap_test_serializers.py b/usersec/tests/snapshots/snap_test_serializers.py index e411c9b..65b9b34 100644 --- a/usersec/tests/snapshots/snap_test_serializers.py +++ b/usersec/tests/snapshots/snap_test_serializers.py @@ -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", @@ -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", @@ -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"}, diff --git a/usersec/tests/test_models.py b/usersec/tests/test_models.py index e877818..c9c9f21 100644 --- a/usersec/tests/test_models.py +++ b/usersec/tests/test_models.py @@ -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 ( @@ -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() diff --git a/usersec/tests/test_serializers.py b/usersec/tests/test_serializers.py index e1fe7fb..d62056c 100644 --- a/usersec/tests/test_serializers.py +++ b/usersec/tests/test_serializers.py @@ -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) @@ -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) @@ -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)