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 b5f80ab commit 559b469
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
11 changes: 2 additions & 9 deletions h/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions h/presenters/group_membership_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
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",
"updated": "1970-01-01T00:00:01",
}
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",
"updated": "1970-01-01T00:00:01",
},
{
"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",
"updated": "1971-01-01T00:00:01",
},
],
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",
"updated": "1970-01-01T00:00:01",
}
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",
"updated": "1970-01-01T00:00:01",
},
{
"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",
"updated": "1971-01-01T00:00:01",
},
],
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",
"updated": "1970-01-01T00:00:01",
}

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",
"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()
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 559b469

Please sign in to comment.