0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-10 23:00:09 +08:00
discourse/plugins/discourse-calendar/spec/integration/allowed_custom_fields_setting_spec.rb
Martin Brennan 57779cc4be
FIX: can_create_discourse_post_event? not checking user groups correctly (#42284)
This commit fixes `can_create_discourse_post_event?` manually checking
a user's groups, which doesn't account for pseudogroups like
`logged_in_users`. We should always use `user.in_any_groups?` for this.

In addition, the opportunity is taken here for some cleanup, moving
all the guardian extensions from plugin.rb into a proper
`GuardianExtensions` file for calendar, adding extra specs along the
way.

Finally, I've added a `Group.refresh_automatic_groups_for_user!`
method as a single-user variant of `Group.refresh_automatic_groups!`
that can use in fabricators for testing, to make this more
thorough/reliable
than manually adding group user records in `after_create` in the
fabricators
2026-08-04 16:09:50 +10:00

56 lines
1.9 KiB
Ruby
Vendored
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
describe "discourse_post_event_allowed_custom_fields" do
let(:user_1) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
let(:topic_1) { Fabricate(:topic, user: user_1) }
let(:post_1) { Fabricate(:post, topic: topic_1) }
let(:post_event_1) { Fabricate(:event, post: post_1) }
before do
SiteSetting.discourse_post_event_allowed_custom_fields = "foo|bar|foo-bar|foo_baz"
post_event_1.update!(custom_fields: {})
SiteSetting.calendar_enabled = true
SiteSetting.discourse_post_event_enabled = true
Jobs.run_immediately!
end
it "removes the key on the custom fields when removing a key from site setting" do
post_event_1.update!(custom_fields: { foo: 1, bar: 2 })
expect(post_event_1.custom_fields["foo"]).to eq(1)
expect(post_event_1.custom_fields["bar"]).to eq(2)
DiscourseEvent.trigger(
:site_setting_changed,
:discourse_post_event_allowed_custom_fields,
"foo|bar",
"foo",
)
post_event_1.reload
expect(post_event_1.custom_fields["foo"]).to eq(1)
expect(post_event_1.custom_fields["bar"]).to eq(nil)
end
it "doesnt set a not allowed key from site setting" do
expect { post_event_1.update!(custom_fields: { baz: 3 }) }.to raise_error(
ActiveRecord::RecordInvalid,
)
end
it "works with a setting containing dash" do
post = create_post_with_event(user_1, "fooBar='1'")
expect(post.event.custom_fields["foo-bar"]).to eq("1")
expect(post.event.custom_fields["foo_bar"]).to eq(nil)
expect(post.event.custom_fields["fooBar"]).to eq(nil)
end
it "works with a setting containing underscore" do
post = create_post_with_event(user_1, "fooBaz='1'")
expect(post.event.custom_fields["foo_baz"]).to eq("1")
expect(post.event.custom_fields["foo-baz"]).to eq(nil)
expect(post.event.custom_fields["fooBaz"]).to eq(nil)
end
end