A platform for community discussion. Free, open, simple.
FIX: Return 422 when creating topics with tags w/out permission (#10400)
Blake Erickson2032c11f781b
diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex bef10efd4cd..cfa0a449f58 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -530,6 +530,7 @@ en:reply_by_email_disabled: "Reply by email has been disabled."target_user_not_found: "One of the users you are sending this message to could not be found."unable_to_update: "There was an error updating that topic."+ unable_to_tag: "There was an error tagging the topic."featured_link:invalid: "is invalid. URL should include http:// or https://."invalid_category: "can't be edited in this category."diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rbindex 5cd3ad716eb..7b47a52456c 100644--- a/lib/discourse_tagging.rb+++ b/lib/discourse_tagging.rb@@ -78,7 +78,7 @@ module DiscourseTaggingparent_tags_map = DB.query("SELECT tgm.tag_id, tg.parent_tag_idFROM tag_groups tg- INNER JOIN tag_group_memberships tgm+ INNER JOIN tag_group_memberships tgmON tgm.tag_group_id = tg.idWHERE tg.parent_tag_id IS NOT NULLAND tgm.tag_id IN (?)@@ -112,8 +112,9 @@ module DiscourseTaggingtopic.tags = []endtopic.tags_changed = true+ return trueend- true+ falseenddef self.validate_min_required_tags_for_category(guardian, topic, category, tags = [])diff --git a/lib/topic_creator.rb b/lib/topic_creator.rbindex c52396b8f64..e9a5ad14ccc 100644--- a/lib/topic_creator.rb+++ b/lib/topic_creator.rb@@ -165,7 +165,10 @@ class TopicCreatorendelsevalid_tags = DiscourseTagging.tag_topic_by_names(topic, @guardian, @opts[:tags])- rollback_from_errors!(topic) unless valid_tags+ unless valid_tags+ topic.errors.add(:base, :unable_to_tag)+ rollback_from_errors!(topic)+ endendenddiff --git a/spec/components/new_post_manager_spec.rb b/spec/components/new_post_manager_spec.rbindex 983a626e458..e8f6fa16342 100644--- a/spec/components/new_post_manager_spec.rb+++ b/spec/components/new_post_manager_spec.rb@@ -289,6 +289,9 @@ describe NewPostManager doit "calls custom enqueuing handlers" doReviewable.set_priorities(high: 20.5)SiteSetting.reviewable_default_visibility = 'high'+ SiteSetting.tagging_enabled = true+ SiteSetting.min_trust_to_create_tag = 0+ SiteSetting.min_trust_level_to_tag_topics = 0manager = NewPostManager.new(topic.user,diff --git a/spec/components/post_creator_spec.rb b/spec/components/post_creator_spec.rbindex e013559003e..2838e86072e 100644--- a/spec/components/post_creator_spec.rb+++ b/spec/components/post_creator_spec.rb@@ -406,7 +406,7 @@ describe PostCreator doit "doesn't create tags" doexpect { @post = creator_with_tags.create }.to change { Tag.count }.by(0)- expect(@post.topic.tags.size).to eq(0)+ expect(@post.topic&.tags&.size).to eq(nil)endenddiff --git a/spec/models/reviewable_queued_post_spec.rb b/spec/models/reviewable_queued_post_spec.rbindex 70842e39a16..73125443b97 100644--- a/spec/models/reviewable_queued_post_spec.rb+++ b/spec/models/reviewable_queued_post_spec.rb@@ -143,6 +143,12 @@ RSpec.describe ReviewableQueuedPost, type: :model docontext "creating a topic" dolet(:reviewable) { Fabricate(:reviewable_queued_post_topic, category: category) }+ before do+ SiteSetting.tagging_enabled = true+ SiteSetting.min_trust_to_create_tag = 0+ SiteSetting.min_trust_level_to_tag_topics = 0+ end+context "editing" doit "is editable and returns the fields" dodiff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex 30ec063b8c0..500ba1e8148 100644--- a/spec/requests/posts_controller_spec.rb+++ b/spec/requests/posts_controller_spec.rb@@ -939,7 +939,7 @@ describe PostsController doexpect(response.status).to eq(422)end- it 'can not create a post in a disallowed category' do+ it 'cannot create a post in a disallowed category' docategory.set_permissions(staff: :full)category.save!@@ -953,7 +953,7 @@ describe PostsController doexpect(response.status).to eq(403)end- it 'can not create a post with a tag that is restricted' do+ it 'cannot create a post with a tag that is restricted' doSiteSetting.tagging_enabled = truetag = Fabricate(:tag)category.allowed_tags = [tag.name]@@ -970,6 +970,51 @@ describe PostsController doexpect(json['errors']).to be_presentend+ it 'cannot create a post with a tag when tagging is disabled' do+ SiteSetting.tagging_enabled = false+ tag = Fabricate(:tag)++ post "/posts.json", params: {+ raw: 'this is the test content',+ title: 'this is the test title for the topic',+ tags: [tag.name],+ }++ expect(response.status).to eq(422)+ json = response.parsed_body+ expect(json['errors']).to be_present+ end++ it 'cannot create a post with a tag without tagging permission' do+ SiteSetting.tagging_enabled = true+ SiteSetting.min_trust_level_to_tag_topics = 4+ tag = Fabricate(:tag)++ post "/posts.json", params: {+ raw: 'this is the test content',+ title: 'this is the test title for the topic',+ tags: [tag.name],+ }++ expect(response.status).to eq(422)+ json = response.parsed_body+ expect(json['errors']).to be_present+ end++ it 'can create a post with a tag when tagging is enabled' do+ SiteSetting.tagging_enabled = true+ tag = Fabricate(:tag)++ post "/posts.json", params: {+ raw: 'this is the test content',+ title: 'this is the test title for the topic',+ tags: [tag.name],+ }++ expect(response.status).to eq(200)+ expect(Post.last.topic.tags.count).to eq(1)+ end+it 'creates the post' dopost "/posts.json", params: {raw: 'this is the test content',