mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: enforce tagging on categories
This commit is contained in:
@@ -75,4 +75,49 @@ describe DiscourseTagging do
|
||||
expect(tags).to contain_exactly(tag1, tag2, tag3)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'tag_topic_by_names' do
|
||||
context 'staff-only tags' do
|
||||
let(:topic) { Fabricate(:topic) }
|
||||
|
||||
before do
|
||||
SiteSetting.staff_tags = "alpha"
|
||||
end
|
||||
|
||||
it "regular users can't add staff-only tags" do
|
||||
valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(user), ['alpha'])
|
||||
expect(valid).to eq(false)
|
||||
expect(topic.errors[:base]&.first).to eq(I18n.t("tags.staff_tag_disallowed", tag: 'alpha'))
|
||||
end
|
||||
|
||||
it 'staff can add staff-only tags' do
|
||||
valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), ['alpha'])
|
||||
expect(valid).to eq(true)
|
||||
expect(topic.errors[:base]).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
context 'respects category minimum_required_tags setting' do
|
||||
let(:category) { Fabricate(:category, minimum_required_tags: 2) }
|
||||
let(:topic) { Fabricate(:topic, category: category) }
|
||||
|
||||
it 'when tags are less than minimum_required_tags' do
|
||||
valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(user), [tag1.name])
|
||||
expect(valid).to eq(false)
|
||||
expect(topic.errors[:base]&.first).to eq(I18n.t("tags.minimum_required_tags", count: category.minimum_required_tags))
|
||||
end
|
||||
|
||||
it 'when tags are equal to minimum_required_tags' do
|
||||
valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(user), [tag1.name, tag2.name])
|
||||
expect(valid).to eq(true)
|
||||
expect(topic.errors[:base]).to be_empty
|
||||
end
|
||||
|
||||
it 'lets admin tag a topic regardless of minimum_required_tags' do
|
||||
valid = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [tag1.name])
|
||||
expect(valid).to eq(true)
|
||||
expect(topic.errors[:base]).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -60,6 +60,65 @@ describe TopicCreator do
|
||||
end
|
||||
end
|
||||
|
||||
context 'tags' do
|
||||
let!(:tag1) { Fabricate(:tag, name: "fun") }
|
||||
let!(:tag2) { Fabricate(:tag, name: "fun2") }
|
||||
|
||||
before do
|
||||
SiteSetting.tagging_enabled = true
|
||||
SiteSetting.min_trust_to_create_tag = 0
|
||||
SiteSetting.min_trust_level_to_tag_topics = 0
|
||||
end
|
||||
|
||||
context 'regular tags' do
|
||||
it "user can add tags to topic" do
|
||||
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(tags: [tag1.name]))
|
||||
expect(topic).to be_valid
|
||||
expect(topic.tags.length).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'staff-only tags' do
|
||||
before do
|
||||
SiteSetting.staff_tags = "alpha"
|
||||
end
|
||||
|
||||
it "regular users can't add staff-only tags" do
|
||||
expect do
|
||||
TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(tags: ['alpha']))
|
||||
end.to raise_error(ActiveRecord::Rollback)
|
||||
end
|
||||
|
||||
it 'staff can add staff-only tags' do
|
||||
topic = TopicCreator.create(admin, Guardian.new(admin), valid_attrs.merge(tags: ['alpha']))
|
||||
expect(topic).to be_valid
|
||||
expect(topic.tags.length).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'minimum_required_tags is present' do
|
||||
let!(:category) { Fabricate(:category, name: "beta", minimum_required_tags: 2) }
|
||||
|
||||
it "fails for regular user if minimum_required_tags is not satisfied" do
|
||||
expect do
|
||||
TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(category: "beta"))
|
||||
end.to raise_error(ActiveRecord::Rollback)
|
||||
end
|
||||
|
||||
it "lets admin create a topic regardless of minimum_required_tags" do
|
||||
topic = TopicCreator.create(admin, Guardian.new(admin), valid_attrs.merge(tags: [tag1.name], category: "beta"))
|
||||
expect(topic).to be_valid
|
||||
expect(topic.tags.length).to eq(1)
|
||||
end
|
||||
|
||||
it "works for regular user if minimum_required_tags is satisfied" do
|
||||
topic = TopicCreator.create(user, Guardian.new(user), valid_attrs.merge(tags: [tag1.name, tag2.name], category: "beta"))
|
||||
expect(topic).to be_valid
|
||||
expect(topic.tags.length).to eq(2)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'private message' do
|
||||
|
||||
context 'success cases' do
|
||||
|
||||
Reference in New Issue
Block a user