From 5f19ad95074329bea3328a796f061de79db9c227 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 23 Mar 2018 11:33:02 -0400 Subject: [PATCH] FIX: allow destination categories to be set if not at first --- app/controllers/topics_controller.rb | 10 ++++++--- spec/requests/topics_controller_spec.rb | 29 ++++++++++++++++--------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index af3f12c5483..9f6467212d2 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -240,10 +240,14 @@ class TopicsController < ApplicationController def update_shared_draft topic = Topic.find_by(id: params[:id]) guardian.ensure_can_edit!(topic) - guardian.ensure_can_create_shared_draft! - raise Discourse::NotFound unless topic.shared_draft.present? - SharedDraft.where(topic_id: topic.id).update_all(category_id: params[:category_id].to_i) + category = Category.where(id: params[:category_id].to_i).first + guardian.ensure_can_publish_topic!(topic, category) + + row_count = SharedDraft.where(topic_id: topic.id).update_all(category_id: category.id) + if row_count == 0 + SharedDraft.create(topic_id: topic.id, category_id: category.id) + end render json: success_json end diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb index b70c38aeed8..d5573e23fd5 100644 --- a/spec/requests/topics_controller_spec.rb +++ b/spec/requests/topics_controller_spec.rb @@ -465,31 +465,40 @@ RSpec.describe TopicsController do end describe "#update_shared_draft" do - let(:category) { Fabricate(:category) } let(:other_cat) { Fabricate(:category) } + let(:category) { Fabricate(:category) } let(:topic) { Fabricate(:topic, category: shared_drafts_category, visible: false) } - let!(:shared_draft) { Fabricate(:shared_draft, topic: topic, category: category) } - let(:moderator) { Fabricate(:moderator) } context "anonymous" do it "doesn't allow staff to update the shared draft" do put "/t/#{topic.id}/shared-draft.json", params: { category_id: other_cat.id } expect(response.code.to_i).to eq(403) - topic.reload - expect(topic.shared_draft.category_id).to eq(category.id) end end context "as a moderator" do + let(:moderator) { Fabricate(:moderator) } before do sign_in(moderator) end - it "allows staff to update the category id" do - put "/t/#{topic.id}/shared-draft.json", params: { category_id: other_cat.id } - expect(response).to be_success - topic.reload - expect(topic.shared_draft.category_id).to eq(other_cat.id) + context "with a shared draft" do + let!(:shared_draft) { Fabricate(:shared_draft, topic: topic, category: category) } + it "allows staff to update the category id" do + put "/t/#{topic.id}/shared-draft.json", params: { category_id: other_cat.id } + expect(response).to be_success + topic.reload + expect(topic.shared_draft.category_id).to eq(other_cat.id) + end + end + + context "without a shared draft" do + it "allows staff to update the category id" do + put "/t/#{topic.id}/shared-draft.json", params: { category_id: other_cat.id } + expect(response).to be_success + topic.reload + expect(topic.shared_draft.category_id).to eq(other_cat.id) + end end end end