Skip to content

Commit

Permalink
Add created and updated to membership API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
seanh committed Dec 5, 2024
1 parent 032a781 commit 25ba3cb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
3 changes: 3 additions & 0 deletions h/presenters/group_membership_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
EditGroupMembershipContext,
GroupMembershipContext,
)
from h.util.datetime import utc_iso8601


class GroupMembershipJSONPresenter:
Expand All @@ -19,6 +20,8 @@ def asdict(self):
"display_name": self.membership.user.display_name,
"roles": self.membership.roles,
"actions": [],
"created": utc_iso8601(self.membership.created),
"updated": utc_iso8601(self.membership.updated),
}

if self.request.has_permission(
Expand Down
51 changes: 47 additions & 4 deletions tests/functional/api/groups/members_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64
import logging
from datetime import datetime

import pytest
from sqlalchemy import select
Expand All @@ -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)
]
)
Expand All @@ -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.000000+00:00",
"updated": "1970-01-01T00:00:01.000000+00:00",
}
for membership in sorted(
group.memberships, key=lambda membership: membership.user.username
Expand All @@ -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()

Expand All @@ -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.000000+00:00",
"updated": "1970-01-01T00:00:01.000000+00:00",
},
{
"authority": group.authority,
Expand All @@ -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.000000+00:00",
"updated": "1971-01-01T00:00:01.000000+00:00",
},
],
key=lambda membership: membership["username"],
Expand Down Expand Up @@ -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)
]
)
Expand All @@ -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.000000+00:00",
"updated": "1970-01-01T00:00:01.000000+00:00",
}
for membership in sorted(
group.memberships, key=lambda membership: membership.user.username
Expand All @@ -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()

Expand All @@ -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.000000+00:00",
"updated": "1970-01-01T00:00:01.000000+00:00",
},
{
"authority": group.authority,
Expand All @@ -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.000000+00:00",
"updated": "1971-01-01T00:00:01.000000+00:00",
},
],
key=lambda membership: membership["username"],
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/h/presenters/group_membership_json_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

import pytest

from h.models import GroupMembership
Expand All @@ -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.000000+00:00",
"updated": "1970-01-01T00:00:01.000000+00:00",
}

def test_it_with_permissive_securitypolicy(
Expand All @@ -39,8 +43,19 @@ def test_it_with_permissive_securitypolicy(
"updates.roles.admin",
"updates.roles.owner",
],
"created": "1970-01-01T00:00:00.000000+00:00",
"updated": "1970-01-01T00:00:01.000000+00:00",
}

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()
Expand All @@ -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),
)

0 comments on commit 25ba3cb

Please sign in to comment.