FIX: allow destination categories to be set if not at first

This commit is contained in:
Robin Ward 2018-03-23 11:33:02 -04:00
parent 38af67eb73
commit 5f19ad9507
2 changed files with 26 additions and 13 deletions

View File

@ -240,10 +240,14 @@ class TopicsController < ApplicationController
def update_shared_draft def update_shared_draft
topic = Topic.find_by(id: params[:id]) topic = Topic.find_by(id: params[:id])
guardian.ensure_can_edit!(topic) 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 render json: success_json
end end

View File

@ -465,31 +465,40 @@ RSpec.describe TopicsController do
end end
describe "#update_shared_draft" do describe "#update_shared_draft" do
let(:category) { Fabricate(:category) }
let(:other_cat) { Fabricate(:category) } let(:other_cat) { Fabricate(:category) }
let(:category) { Fabricate(:category) }
let(:topic) { Fabricate(:topic, category: shared_drafts_category, visible: false) } 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 context "anonymous" do
it "doesn't allow staff to update the shared draft" 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 } put "/t/#{topic.id}/shared-draft.json", params: { category_id: other_cat.id }
expect(response.code.to_i).to eq(403) expect(response.code.to_i).to eq(403)
topic.reload
expect(topic.shared_draft.category_id).to eq(category.id)
end end
end end
context "as a moderator" do context "as a moderator" do
let(:moderator) { Fabricate(:moderator) }
before do before do
sign_in(moderator) sign_in(moderator)
end end
it "allows staff to update the category id" do context "with a shared draft" do
put "/t/#{topic.id}/shared-draft.json", params: { category_id: other_cat.id } let!(:shared_draft) { Fabricate(:shared_draft, topic: topic, category: category) }
expect(response).to be_success it "allows staff to update the category id" do
topic.reload put "/t/#{topic.id}/shared-draft.json", params: { category_id: other_cat.id }
expect(topic.shared_draft.category_id).to eq(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 end
end end