From 0005e13a8ce6091d2b607a28307993cce2e0d5c2 Mon Sep 17 00:00:00 2001 From: Sean Hammond Date: Fri, 18 Oct 2024 17:17:12 +0100 Subject: [PATCH] Add creators of new groups as owners --- h/services/group_create.py | 6 ++++-- tests/unit/h/services/group_create_test.py | 25 ++++++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/h/services/group_create.py b/h/services/group_create.py index fbf9305ba6f..5a53a793680 100644 --- a/h/services/group_create.py +++ b/h/services/group_create.py @@ -1,7 +1,7 @@ from functools import partial from h import session -from h.models import Group, GroupScope +from h.models import Group, GroupMembership, GroupScope from h.models.group import GROUP_TYPE_FLAGS @@ -126,7 +126,9 @@ def _create(self, name, userid, type_flags, scopes, **kwargs): # self.publish() or `return group`. self.db.flush() - group.members.append(group.creator) + self.db.add( + GroupMembership(user_id=creator.id, group_id=group.id, roles=["owner"]) + ) self.publish("group-join", group.pubid, group.creator.userid) return group diff --git a/tests/unit/h/services/group_create_test.py b/tests/unit/h/services/group_create_test.py index e6e2c5c8733..3cccd7d9a76 100644 --- a/tests/unit/h/services/group_create_test.py +++ b/tests/unit/h/services/group_create_test.py @@ -2,8 +2,9 @@ import pytest from h_matchers import Any +from sqlalchemy import select -from h.models import Group, GroupScope, User +from h.models import Group, GroupMembership, GroupScope, User from h.models.group import JoinableBy, ReadableBy, WriteableBy from h.services.group_create import GroupCreateService, group_create_factory from tests.common.matchers import Matcher @@ -51,10 +52,12 @@ def test_it_skips_setting_description_when_missing(self, svc, creator): assert group.description is None - def test_it_adds_group_creator_to_members(self, svc, creator): + def test_it_adds_group_creator_to_members_as_owner(self, svc, creator, db_session): group = svc.create_private_group("Anteater fans", creator.userid) - assert creator in group.members + assert db_session.scalar( + select(GroupMembership).where(GroupMembership.group_id == group.id) + ).roles == ["owner"] @pytest.mark.parametrize( "flag,expected_value", @@ -167,10 +170,14 @@ def test_it_sets_group_creator(self, svc, creator, origins): assert group.creator == creator - def test_it_adds_group_creator_to_members(self, svc, creator, origins): + def test_it_adds_group_creator_to_members_as_owner( + self, svc, creator, origins, db_session + ): group = svc.create_open_group("Anteater fans", creator.userid, scopes=origins) - assert creator in group.members + assert db_session.scalar( + select(GroupMembership).where(GroupMembership.group_id == group.id) + ).roles == ["owner"] @pytest.mark.parametrize( "flag,expected_value", @@ -327,12 +334,16 @@ def test_it_sets_group_creator(self, svc, creator, origins): assert group.creator == creator - def test_it_adds_group_creator_to_members(self, svc, creator, origins): + def test_it_adds_group_creator_to_members_as_owner( + self, svc, creator, origins, db_session + ): group = svc.create_restricted_group( "Anteater fans", creator.userid, scopes=origins ) - assert creator in group.members + assert db_session.scalar( + select(GroupMembership).where(GroupMembership.group_id == group.id) + ).roles == ["owner"] @pytest.mark.parametrize( "flag,expected_value",