FIX: Fail if none of our tags could be updated

For example, if a category has a tag restriction and the API tries to
attempt to update it but cannot.

See:
https://meta.discourse.org/t/unallowed-tag-in-conversation-returns-200/122170
This commit is contained in:
Robin Ward 2019-07-05 11:39:29 -04:00
parent 5494e17c71
commit c2c169f5b7
2 changed files with 18 additions and 2 deletions

View File

@ -83,6 +83,7 @@ module DiscourseTagging
return false
end
return false if tags.size == 0
topic.tags = tags
else
# validate minimum required tags for a category

View File

@ -116,14 +116,29 @@ describe DiscourseTagging do
other_category = Fabricate(:category, allowed_tags: [other_tag.name])
topic = Fabricate(:topic, category: category)
DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [tag.name, other_tag.name, 'hello'])
result = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [tag.name, other_tag.name, 'hello'])
expect(result).to eq(true)
expect(topic.tags.pluck(:name)).to contain_exactly(tag.name)
category.update!(allow_global_tags: true)
DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [tag.name, other_tag.name, 'hello'])
result = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [tag.name, other_tag.name, 'hello'])
expect(result).to eq(true)
expect(topic.tags.pluck(:name)).to contain_exactly(tag.name, 'hello')
end
it 'raises an error if no tags could be updated' do
tag = Fabricate(:tag)
other_tag = Fabricate(:tag)
tag_group = Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [tag.name])
category = Fabricate(:category, allowed_tag_groups: [tag_group.name])
other_category = Fabricate(:category, allowed_tags: [other_tag.name])
topic = Fabricate(:topic, category: category)
result = DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [other_tag.name])
expect(result).to eq(false)
expect(topic.tags.pluck(:name)).to be_blank
end
context 'respects category minimum_required_tags setting' do
fab!(:category) { Fabricate(:category, minimum_required_tags: 2) }
fab!(:topic) { Fabricate(:topic, category: category) }