0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/spec/lib/theme_settings_group_resolver/object_setting_spec.rb
Martin Brennan bee1be8599
DEV: Add resolve_group_membership for theme object settings of group type (#41756)
Followup 7e77ce4bd3

We need to automatically resolve group membership into a boolean
for theme object type settings which are of the group type, similar
to what we did in the original commit above for group list type
settings.

This behaves in the same way -- for an object setting schema like this:

```
menu_sections:
  type: objects
  default:
    - name: section 1
      groups:
        - 1
        - 3
  schema:
    name: menu section
    properties:
      name:
        type: string
      groups:
        type: groups
        resolve_group_membership: true
```

We replace `groups` with a boolean `user_in_groups` (groups is just
the property name, it could be foo_bar etc.) and then you can
do this on the client:

```
for (const section of settings.menu_sections) {
  if (section.user_in_groups) {
    // User is in at least one selected group for this section.
  }
}
```

Rather than inspecting the `currentUser.groups`, which only includes
visible groups, not all groups the user is a member of. This allows
for more accurate permission checks for theme settings that are group
based.

Also c.f.
https://meta.discourse.org/t/granular-group-based-permissions-for-anonymous-and-logged-in-users/402273/18?u=martin
2026-07-17 10:19:29 +10:00

176 lines
4.7 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe ThemeSettingsGroupResolver::ObjectSetting do
fab!(:user)
fab!(:group)
fab!(:other_group, :group)
before { group.add(user) }
describe ".applies?" do
it "applies only to object settings" do
expect(described_class.applies?(type: "objects")).to eq(true)
expect(described_class.applies?(type: "list")).to eq(false)
end
end
describe "#resolve!" do
it "replaces opted-in group properties with user_in booleans" do
settings = {
menu_sections: [
{
"name" => "member section",
"groups" => [group.id],
"visible_groups" => [other_group.id],
},
{
"name" => "other section",
"groups" => [other_group.id],
"visible_groups" => [group.id],
},
],
}
resolve!(settings, schema: section_schema)
expect(settings[:menu_sections]).to eq(
[
{
"name" => "member section",
"user_in_groups" => true,
"visible_groups" => [other_group.id],
},
{ "name" => "other section", "user_in_groups" => false, "visible_groups" => [group.id] },
],
)
end
it "resolves nested object group properties" do
settings = {
menu_sections: [
{
"name" => "section",
"links" => [
{ "name" => "member link", "groups" => [group.id] },
{ "name" => "other link", "groups" => [other_group.id] },
],
},
],
}
resolve!(settings, schema: nested_groups_schema)
expect(settings[:menu_sections]).to eq(
[
{
"name" => "section",
"links" => [
{ "name" => "member link", "user_in_groups" => true },
{ "name" => "other link", "user_in_groups" => false },
],
},
],
)
end
it "does not mutate the original object value" do
original_objects = [{ "name" => "section", "groups" => [group.id] }]
settings = { menu_sections: original_objects }
resolve!(settings, schema: section_schema)
expect(original_objects).to eq([{ "name" => "section", "groups" => [group.id] }])
expect(settings[:menu_sections]).not_to equal(original_objects)
end
it "preserves symbol keys when resolving symbol-keyed objects" do
settings = { menu_sections: [{ name: "section", groups: [group.id] }] }
resolve!(settings, schema: section_schema)
expect(settings[:menu_sections]).to eq([{ name: "section", user_in_groups: true }])
end
it "leaves settings unchanged when no group properties opt in" do
original_objects = [{ "name" => "section", "groups" => [group.id] }]
settings = { menu_sections: original_objects }
schema = { name: "section", properties: { groups: { type: "groups" } } }
resolve!(settings, schema:)
expect(settings[:menu_sections]).to equal(original_objects)
end
it "handles anonymous users, auto-groups, empty arrays, and multiple groups" do
settings = {
menu_sections: [
{ "name" => "logged in", "groups" => [Group::AUTO_GROUPS[:logged_in_users]] },
{ "name" => "anonymous", "groups" => [Group::AUTO_GROUPS[:anonymous_users]] },
{ "name" => "empty", "groups" => [] },
{ "name" => "multiple", "groups" => [group.id, other_group.id] },
],
}
resolve!(settings, schema: section_schema, guardian: Guardian.new)
expect(settings[:menu_sections].pluck("user_in_groups")).to eq([false, true, false, false])
end
end
def resolve!(settings, schema:, guardian: user.guardian)
described_class.new(
setting_name: :menu_sections,
setting_info: {
schema:,
},
guardian:,
).resolve!(settings)
end
def section_schema
{
name: "section",
properties: {
groups: {
type: "groups",
resolve_group_membership: true,
},
visible_groups: {
type: "groups",
},
links: {
type: "objects",
schema: {
name: "link",
properties: {
groups: {
type: "groups",
resolve_group_membership: true,
},
},
},
},
},
}
end
def nested_groups_schema
{
name: "section",
properties: {
links: {
type: "objects",
schema: {
name: "link",
properties: {
groups: {
type: "groups",
resolve_group_membership: true,
},
},
},
},
},
}
end
end