From 559b469114195abc21176f911c0e60782fe272ea Mon Sep 17 00:00:00 2001 From: Sean Hammond Date: Thu, 5 Dec 2024 16:01:50 +0000 Subject: [PATCH] Add created and updated to membership API responses --- h/models/group.py | 11 +--- h/presenters/group_membership_json.py | 6 +++ tests/functional/api/groups/members_test.py | 51 +++++++++++++++++-- .../presenters/group_membership_json_test.py | 22 +++++++- 4 files changed, 76 insertions(+), 14 deletions(-) diff --git a/h/models/group.py b/h/models/group.py index f13799ae584..65c94175aaf 100644 --- a/h/models/group.py +++ b/h/models/group.py @@ -79,17 +79,10 @@ class GroupMembership(Base): nullable=False, ) - created = sa.Column( - sa.DateTime, - server_default=sa.func.now(), # pylint:disable=not-callable - default=datetime.datetime.utcnow, - ) + created = sa.Column(sa.DateTime, default=datetime.datetime.utcnow) updated = sa.Column( - sa.DateTime, - server_default=sa.func.now(), # pylint:disable=not-callable - default=datetime.datetime.utcnow, - onupdate=datetime.datetime.utcnow, + sa.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow ) def __repr__(self): diff --git a/h/presenters/group_membership_json.py b/h/presenters/group_membership_json.py index 22ac99a508e..e4c0d003484 100644 --- a/h/presenters/group_membership_json.py +++ b/h/presenters/group_membership_json.py @@ -19,6 +19,12 @@ def asdict(self): "display_name": self.membership.user.display_name, "roles": self.membership.roles, "actions": [], + "created": ( + self.membership.created.isoformat() if self.membership.created else None + ), + "updated": ( + self.membership.updated.isoformat() if self.membership.updated else None + ), } if self.request.has_permission( diff --git a/tests/functional/api/groups/members_test.py b/tests/functional/api/groups/members_test.py index 3071b483a27..075e0735203 100644 --- a/tests/functional/api/groups/members_test.py +++ b/tests/functional/api/groups/members_test.py @@ -1,5 +1,6 @@ import base64 import logging +from datetime import datetime import pytest from sqlalchemy import select @@ -14,7 +15,11 @@ def test_it_returns_list_of_members_for_restricted_group_without_authn( ): group = factories.RestrictedGroup( memberships=[ - GroupMembership(user=user) + GroupMembership( + user=user, + created=datetime(1970, 1, 1, 0, 0, 0), + updated=datetime(1970, 1, 1, 0, 0, 1), + ) for user in factories.User.create_batch(size=3) ] ) @@ -37,6 +42,8 @@ def test_it_returns_list_of_members_for_restricted_group_without_authn( "display_name": membership.user.display_name, "roles": membership.roles, "actions": [], + "created": "1970-01-01T00:00:00", + "updated": "1970-01-01T00:00:01", } for membership in sorted( group.memberships, key=lambda membership: membership.user.username @@ -50,7 +57,18 @@ def test_it_returns_list_of_members_if_user_has_access_to_private_group( user, other_user = factories.User.create_batch(size=2) token = factories.DeveloperToken(user=user) group.memberships.extend( - [GroupMembership(user=user), GroupMembership(user=other_user)] + [ + GroupMembership( + user=user, + created=datetime(1970, 1, 1, 0, 0, 0), + updated=datetime(1970, 1, 1, 0, 0, 1), + ), + GroupMembership( + user=other_user, + created=datetime(1971, 1, 1, 0, 0, 0), + updated=datetime(1971, 1, 1, 0, 0, 1), + ), + ] ) db_session.commit() @@ -69,6 +87,8 @@ def test_it_returns_list_of_members_if_user_has_access_to_private_group( "display_name": user.display_name, "roles": [GroupMembershipRoles.MEMBER], "actions": ["delete"], + "created": "1970-01-01T00:00:00", + "updated": "1970-01-01T00:00:01", }, { "authority": group.authority, @@ -77,6 +97,8 @@ def test_it_returns_list_of_members_if_user_has_access_to_private_group( "display_name": other_user.display_name, "roles": [GroupMembershipRoles.MEMBER], "actions": [], + "created": "1971-01-01T00:00:00", + "updated": "1971-01-01T00:00:01", }, ], key=lambda membership: membership["username"], @@ -108,7 +130,11 @@ def test_it_returns_list_of_members_for_restricted_group_without_auth( ): group = factories.RestrictedGroup( memberships=[ - GroupMembership(user=user) + GroupMembership( + user=user, + created=datetime(1970, 1, 1, 0, 0, 0), + updated=datetime(1970, 1, 1, 0, 0, 1), + ) for user in factories.User.create_batch(size=4) ] ) @@ -131,6 +157,8 @@ def test_it_returns_list_of_members_for_restricted_group_without_auth( "display_name": membership.user.display_name, "roles": membership.roles, "actions": [], + "created": "1970-01-01T00:00:00", + "updated": "1970-01-01T00:00:01", } for membership in sorted( group.memberships, key=lambda membership: membership.user.username @@ -145,7 +173,18 @@ def test_it_returns_list_of_members_if_user_has_access_to_private_group( user, other_user = factories.User.create_batch(size=2) token = factories.DeveloperToken(user=user) group.memberships.extend( - [GroupMembership(user=user), GroupMembership(user=other_user)] + [ + GroupMembership( + user=user, + created=datetime(1970, 1, 1, 0, 0, 0), + updated=datetime(1970, 1, 1, 0, 0, 1), + ), + GroupMembership( + user=other_user, + created=datetime(1971, 1, 1, 0, 0, 0), + updated=datetime(1971, 1, 1, 0, 0, 1), + ), + ] ) db_session.commit() @@ -167,6 +206,8 @@ def test_it_returns_list_of_members_if_user_has_access_to_private_group( "display_name": user.display_name, "roles": [GroupMembershipRoles.MEMBER], "actions": ["delete"], + "created": "1970-01-01T00:00:00", + "updated": "1970-01-01T00:00:01", }, { "authority": group.authority, @@ -175,6 +216,8 @@ def test_it_returns_list_of_members_if_user_has_access_to_private_group( "display_name": other_user.display_name, "roles": [GroupMembershipRoles.MEMBER], "actions": [], + "created": "1971-01-01T00:00:00", + "updated": "1971-01-01T00:00:01", }, ], key=lambda membership: membership["username"], diff --git a/tests/unit/h/presenters/group_membership_json_test.py b/tests/unit/h/presenters/group_membership_json_test.py index 1dc8985ecdb..b4aae04aecf 100644 --- a/tests/unit/h/presenters/group_membership_json_test.py +++ b/tests/unit/h/presenters/group_membership_json_test.py @@ -1,3 +1,5 @@ +from datetime import datetime + import pytest from h.models import GroupMembership @@ -17,6 +19,8 @@ def test_it(self, user, group, membership, pyramid_request, pyramid_config): "display_name": user.display_name, "roles": membership.roles, "actions": [], + "created": "1970-01-01T00:00:00", + "updated": "1970-01-01T00:00:01", } def test_it_with_permissive_securitypolicy( @@ -39,8 +43,19 @@ def test_it_with_permissive_securitypolicy( "updates.roles.admin", "updates.roles.owner", ], + "created": "1970-01-01T00:00:00", + "updated": "1970-01-01T00:00:01", } + def test_it_with_no_created_or_updated_times(self, membership, pyramid_request): + membership.created = None + membership.updated = None + + json = GroupMembershipJSONPresenter(pyramid_request, membership).asdict() + + assert json["created"] is None + assert json["updated"] is None + @pytest.fixture def user(self, factories): return factories.User.build() @@ -51,4 +66,9 @@ def group(self, factories): @pytest.fixture def membership(self, user, group): - return GroupMembership(user=user, group=group) + return GroupMembership( + user=user, + group=group, + created=datetime(1970, 1, 1, 0, 0, 0), + updated=datetime(1970, 1, 1, 0, 0, 1), + )