discourse/plugins/automation/spec/scripts/banner_topic_spec.rb
Isaac Janzen 4f3613eefb
DEV: Improve banner topic permissions for read-restricted and unlisted topics (#38556)
## Summary

Follow-up to #38496. Addresses two issues with banner topic permissions:

**Read-restricted categories:** The "Make Banner" button was visible for
topics in read-restricted categories but silently failed with a 403.

- Serialize `can_banner_topic` from the Guardian to the client, so the
banner section in the Feature Topic modal is properly hidden for topics
in read-restricted categories
- Add a Guardian check to the automation `banner_topic` script to
prevent silently creating banners from read-restricted topics

**Unlisted topics in public categories:** The Feature Topic menu item
was completely hidden for unlisted topics, preventing admins from using
unlisted topics as banners. Banners use a separate display mechanism
(`application_layout_preloader`) that doesn't check topic visibility, so
this works fine. Pinning an unlisted topic is still prevented since
pinned topics wouldn't appear in topic lists for regular users.

- Add `can_banner_topic` to the visibility condition on the pin menu
item, so the Feature Topic modal is accessible for unlisted topics that
can be bannered
2026-03-16 11:45:07 -05:00

54 lines
1.5 KiB
Ruby

# frozen_string_literal: true
describe "BannerTopic" do
before { automation.upsert_field!("topic_id", "text", { value: topic.id }) }
fab!(:automation) { Fabricate(:automation, script: DiscourseAutomation::Scripts::BANNER_TOPIC) }
fab!(:topic)
context "when banner until is set" do
before do
freeze_time
automation.upsert_field!("banner_until", "date_time", { value: 10.days.from_now })
automation.upsert_field!("topic_id", "text", { value: topic.id })
end
it "banners the topic" do
expect(topic.bannered_until).to be_nil
expect(topic.archetype).to eq(Archetype.default)
automation.trigger!
topic.reload
expect(topic.bannered_until).to be_within_one_minute_of(10.days.from_now)
expect(topic.archetype).to eq(Archetype.banner)
end
end
context "when banner until is not set" do
it "banners the topic" do
expect(topic.bannered_until).to be_nil
expect(topic.archetype).to eq(Archetype.default)
automation.trigger!
topic.reload
expect(topic.bannered_until).to be_nil
expect(topic.archetype).to eq(Archetype.banner)
end
end
context "when topic is in a read-restricted category" do
fab!(:group)
fab!(:private_category) { Fabricate(:private_category, group: group) }
before { topic.update!(category: private_category) }
it "does not banner the topic" do
automation.trigger!
topic.reload
expect(topic.archetype).to eq(Archetype.default)
end
end
end