Skip to content

Commit

Permalink
Add creators of new groups as owners
Browse files Browse the repository at this point in the history
  • Loading branch information
seanh committed Oct 18, 2024
1 parent c2a4fe7 commit 0005e13
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
6 changes: 4 additions & 2 deletions h/services/group_create.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions tests/unit/h/services/group_create_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 0005e13

Please sign in to comment.