Skip to content

Commit

Permalink
Set group.pubid before returning open groups
Browse files Browse the repository at this point in the history
`GroupCreateService.create_private_group()` and
`GroupCreateService.create_restricted_group()` both return
`models.Group` objects whose `pubid` attribute has already been
generated, but `GroupCreateService.create_open_group()` returns groups
with `pubid=None` because the value for `pubid` won't be generated until
the group is flushed to the DB.

This is a likely source of errors (for example the API returning
`id: null` in the JSON response when creating an open group) so fix
`GroupCreateService` to generate `pubid`'s also for open groups before
returning the group to the caller.
  • Loading branch information
seanh committed Sep 5, 2024
1 parent 464c20a commit a0d3045
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
7 changes: 4 additions & 3 deletions h/services/group_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ def _create( # pylint: disable=too-many-arguments
)
self.db.add(group)

# Flush the DB to generate `group.pubid` before passing `group` to
# self.publish() or `return group`.
self.db.flush()

if add_creator_as_member:
group.members.append(group.creator)

# Flush the DB to generate group.pubid before publish()ing it.
self.db.flush()

self.publish("group-join", group.pubid, group.creator.userid)

return group
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/h/services/group_create_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def test_it_returns_group_model(self, creator, svc):
group = svc.create_private_group("Anteater fans", creator.userid)

assert isinstance(group, Group)
assert group.pubid

def test_it_sets_group_name(self, creator, svc):
group = svc.create_private_group("Anteater fans", creator.userid)
Expand Down Expand Up @@ -118,6 +119,7 @@ def test_it_returns_group_model(self, creator, svc, origins):
group = svc.create_open_group("Anteater fans", creator.userid, scopes=origins)

assert isinstance(group, Group)
assert group.pubid

@pytest.mark.parametrize(
"group_attr,expected_value",
Expand Down Expand Up @@ -258,6 +260,7 @@ def test_it_returns_group_model(self, creator, svc, origins):
)

assert isinstance(group, Group)
assert group.pubid

@pytest.mark.parametrize(
"group_attr,expected_value",
Expand Down

0 comments on commit a0d3045

Please sign in to comment.