From ac04f5e0cccf493844e1eb855565d5de857ebcab Mon Sep 17 00:00:00 2001 From: Eleanor Demis Date: Sat, 30 Sep 2017 07:31:32 -0700 Subject: [PATCH] update response error when deleting tags (#5213) --- app/controllers/tags_controller.rb | 5 ++++- spec/controllers/tags_controller_spec.rb | 26 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 75ce09efa7f..54115945e06 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -100,8 +100,11 @@ class TagsController < ::ApplicationController def destroy guardian.ensure_can_admin_tags! tag_name = params[:tag_id] + tag = Tag.find_by_name(tag_name) + raise Discourse::NotFound if tag.nil? + TopicCustomField.transaction do - Tag.find_by_name(tag_name).destroy + tag.destroy StaffActionLogger.new(current_user).log_custom('deleted_tag', subject: tag_name) end render json: success_json diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb index f0e296345de..62c8c55d5a9 100644 --- a/spec/controllers/tags_controller_spec.rb +++ b/spec/controllers/tags_controller_spec.rb @@ -190,4 +190,30 @@ describe TagsController do end end end + + describe 'destroy' do + context 'tagging enabled' do + before do + log_in(:admin) + SiteSetting.tagging_enabled = true + end + + context 'with an existent tag name' do + it 'deletes the tag' do + tag = Fabricate(:tag) + delete :destroy, params: { tag_id: tag.name }, format: :json + expect(response).to be_success + end + end + + context 'with a nonexistent tag name' do + it 'returns a tag not found message' do + delete :destroy, params: { tag_id: 'idontexist' }, format: :json + expect(response).not_to be_success + json = ::JSON.parse(response.body) + expect(json['error_type']).to eq('not_found') + end + end + end + end end