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

[DOP-21415] Grant read-only rights to the previous group owner when ownership is transferred #134

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions docs/changelog/next_release/134.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provide read-only rights for the previous group owner when ownership is transferred
19 changes: 18 additions & 1 deletion syncmaster/db/repositories/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,11 @@ async def update(
description: str,
owner_id: int,
) -> Group:
group = await self.read_by_id(group_id=group_id)
previous_owner_id = group.owner_id
args = [Group.id == group_id, Group.is_deleted.is_(False)]
try:
return await self._update(
updated_group = await self._update(
*args,
name=name,
description=description,
Expand All @@ -199,6 +201,21 @@ async def update(
except IntegrityError as e:
self._raise_error(e)

if previous_owner_id != owner_id:
previous_user_group = await self._session.get(
UserGroup,
{
"group_id": group_id,
"user_id": previous_owner_id,
},
)
if previous_user_group:
await self.update_member_role(group_id=group_id, user_id=previous_owner_id, role=GroupMemberRole.Guest)
else:
await self.add_user(group_id=group_id, new_user_id=previous_owner_id, role=GroupMemberRole.Guest)

return updated_group

async def get_member_role(self, group_id: int, user_id: int) -> GroupMemberRole:
user_group = await self._session.get(
UserGroup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ async def group_connections(
)
elif conn_type in [
ConnectionType.POSTGRES,
ConnectionType.ORACLE,
ConnectionType.CLICKHOUSE,
ConnectionType.MSSQL,
ConnectionType.MYSQL,
Expand Down
71 changes: 67 additions & 4 deletions tests/test_unit/test_groups/test_update_group_by_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,87 @@ async def test_validation_on_update_group(


async def test_owner_change_group_owner(client: AsyncClient, empty_group: MockGroup, simple_user: MockUser):
# Arrange
previous_owner = empty_group.owner
user = empty_group.get_member_of_role(UserTestRoles.Owner)
# Act
result = await client.patch(
patch_result = await client.patch(
f"v1/groups/{empty_group.id}",
headers={"Authorization": f"Bearer {empty_group.get_member_of_role(UserTestRoles.Owner).token}"},
headers={"Authorization": f"Bearer {user.token}"},
json={
"name": empty_group.name,
"owner_id": simple_user.id,
"description": empty_group.description,
},
)
group_users_result = await client.get(
f"v1/groups/{empty_group.id}/users",
headers={"Authorization": f"Bearer {user.token}"},
)
# Assert
assert result.json() == {
assert patch_result.status_code == 200
assert patch_result.json() == {
"id": empty_group.id,
"name": empty_group.name,
"owner_id": simple_user.id,
"description": empty_group.description,
}
assert result.status_code == 200
assert group_users_result.status_code == 200
assert group_users_result.json()["items"] == [
{
"id": previous_owner.id,
"username": previous_owner.username,
"role": UserTestRoles.Guest,
},
]


async def test_owner_with_existing_role_change_group_owner(
client: AsyncClient,
empty_group: MockGroup,
simple_user: MockUser,
role_maintainer_or_below: UserTestRoles,
):
# Arrange
previous_owner = empty_group.owner
user = empty_group.get_member_of_role(UserTestRoles.Owner)
# Act
await client.post(
f"v1/groups/{empty_group.id}/users/{empty_group.owner_id}",
headers={"Authorization": f"Bearer {user.token}"},
json={
"role": role_maintainer_or_below,
},
)
patch_result = await client.patch(
f"v1/groups/{empty_group.id}",
headers={"Authorization": f"Bearer {user.token}"},
json={
"name": empty_group.name,
"owner_id": simple_user.id,
"description": empty_group.description,
},
)
group_users_result = await client.get(
f"v1/groups/{empty_group.id}/users",
headers={"Authorization": f"Bearer {user.token}"},
)
# Assert
assert patch_result.status_code == 200
assert patch_result.json() == {
"id": empty_group.id,
"name": empty_group.name,
"owner_id": simple_user.id,
"description": empty_group.description,
}
assert group_users_result.status_code == 200
assert group_users_result.json()["items"] == [
{
"id": previous_owner.id,
"username": previous_owner.username,
"role": UserTestRoles.Guest,
},
]


async def test_maintainer_or_below_cannot_change_group_owner(
Expand Down
Loading