From ce15f085c6737ae17e577f14388a7ba44481efeb Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 10 May 2019 07:33:02 +0800 Subject: [PATCH] DEV: Return the right response code and error message. Follow up to d1dbafebbc4b21c2a6714cd19c0ba05fac7717c4. Also remove test that is a duplicate of 867c1a5ac9355aadef35d60fd9c7aa4ebd0eda25. --- lib/topic_creator.rb | 4 +++- spec/requests/posts_controller_spec.rb | 17 ++++------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb index 396dabb4f3c..66c3d09520e 100644 --- a/lib/topic_creator.rb +++ b/lib/topic_creator.rb @@ -121,7 +121,9 @@ class TopicCreator category = find_category @guardian.ensure_can_create!(Topic, category) unless (@opts[:skip_validations] || @opts[:archetype] == Archetype.private_message) - raise Discourse::NotFound if @opts[:category].present? && category.nil? + if @opts[:category].present? && category.nil? + raise Discourse::InvalidParameters.new(:category) + end topic_params[:category_id] = category.id if category.present? diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb index 73c863b63c3..dea1680497e 100644 --- a/spec/requests/posts_controller_spec.rb +++ b/spec/requests/posts_controller_spec.rb @@ -787,21 +787,12 @@ describe PostsController do raw: 'this is test body', category: 'invalid' } - expect(response.status).to eq(404) - end - it 'can create topics with an empty category param' do - user = Fabricate(:admin) - master_key = ApiKey.create_master_key.key + expect(response.status).to eq(400) - post "/posts.json", params: { - api_username: user.username, - api_key: master_key, - title: 'title for a topic without a category', - raw: 'body for my topic without a category', - category: '' - } - expect(response.status).to eq(200) + expect(JSON.parse(response.body)["errors"]).to include( + I18n.t("invalid_params", message: "category") + ) end end