Skip to content

A platform for community discussion. Free, open, simple.

FIX: Accept booleans for whisper, is_warning, no_bump, shared_draft when creating a post (#37551)

Natalie Taybc570b37517b
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index a15ba79f549..f25b8e51ee7 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -937,23 +937,23 @@ class PostsController < ApplicationController
# Staff are allowed to pass `is_warning`
if current_user.staff?
params.permit(:is_warning)
- result[:is_warning] = (params[:is_warning] == "true")
+ result[:is_warning] = ActiveModel::Type::Boolean.new.cast(params[:is_warning])
else
result[:is_warning] = false
end
- if params[:no_bump] == "true"
+ if ActiveModel::Type::Boolean.new.cast(params[:no_bump])
raise Discourse::InvalidParameters.new(:no_bump) unless guardian.can_skip_bump?
result[:no_bump] = true
end
- if params[:shared_draft] == "true"
+ if ActiveModel::Type::Boolean.new.cast(params[:shared_draft])
raise Discourse::InvalidParameters.new(:shared_draft) unless guardian.can_create_shared_draft?
result[:shared_draft] = true
end
- if params[:whisper] == "true"
+ if ActiveModel::Type::Boolean.new.cast(params[:whisper])
unless guardian.can_create_whisper?
raise Discourse::InvalidAccess.new(
"invalid_whisper_access",
diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rb
index 3e95564c7cd..5d841ef9ab7 100644
--- a/spec/requests/posts_controller_spec.rb
+++ b/spec/requests/posts_controller_spec.rb
@@ -1150,6 +1150,47 @@ RSpec.describe PostsController do
expect(response.status).to eq(403)
end
+ it "allows staff to create whispers with boolean parameter" do
+ post_1 = Fabricate(:post)
+ staff_key = ApiKey.create!(user: admin).key
+
+ post "/posts.json",
+ params: {
+ raw: "this is a staff whisper",
+ topic_id: post_1.topic.id,
+ reply_to_post_number: 1,
+ whisper: true,
+ }.to_json,
+ headers: {
+ "CONTENT_TYPE" => "application/json",
+ "HTTP_API_USERNAME" => admin.username,
+ "HTTP_API_KEY" => staff_key,
+ }
+
+ expect(response.status).to eq(200)
+ expect(Post.last.post_type).to eq(Post.types[:whisper])
+ end
+
+ it "allows staff to create whispers with string 'true' parameter" do
+ post_1 = Fabricate(:post)
+ staff_key = ApiKey.create!(user: admin).key
+
+ post "/posts.json",
+ params: {
+ raw: "this is a staff whisper with string param",
+ topic_id: post_1.topic.id,
+ reply_to_post_number: 1,
+ whisper: "true",
+ },
+ headers: {
+ HTTP_API_USERNAME: admin.username,
+ HTTP_API_KEY: staff_key,
+ }
+
+ expect(response.status).to eq(200)
+ expect(Post.last.post_type).to eq(Post.types[:whisper])
+ end
+
it "does not advance draft" do
Draft.set(user, Draft::NEW_TOPIC, 0, "test")
user_key = ApiKey.create!(user: user).key
@@ -2040,6 +2081,24 @@ RSpec.describe PostsController do
expect(topic.category_id).to eq(shared_category.id)
expect(topic.shared_draft.category_id).to eq(destination_category.id)
end
+
+ it "accepts boolean true for shared_draft parameter" do
+ post "/posts.json",
+ params: {
+ raw: "this is the shared draft content",
+ title: "this is the shared draft title with boolean",
+ category: destination_category.id,
+ shared_draft: true,
+ }.to_json,
+ headers: {
+ "CONTENT_TYPE" => "application/json",
+ }
+ expect(response.status).to eq(200)
+ result = response.parsed_body
+ topic = Topic.find(result["topic_id"])
+ expect(topic.category_id).to eq(shared_category.id)
+ expect(topic.shared_draft.category_id).to eq(destination_category.id)
+ end
end
end
end
@@ -2068,6 +2127,20 @@ RSpec.describe PostsController do
expect(new_topic.is_official_warning?).to eq(true)
end
+ it "accepts string 'true' for is_warning parameter" do
+ post "/posts.json",
+ params: {
+ raw: "this is the test content",
+ archetype: "private_message",
+ title: "this is some post with string param",
+ target_recipients: user_2.username,
+ is_warning: "true",
+ }
+
+ expect(response.status).to eq(200)
+ expect(Topic.last.is_official_warning?).to eq(true)
+ end
+
it "should be able to mark a topic as not a warning" do
post "/posts.json",
params: {
@@ -2126,6 +2199,21 @@ RSpec.describe PostsController do
expect(topic.reload.bumped_at).to eq_time(original_bumped_at)
end
+ it "accepts string 'true' for no_bump parameter" do
+ original_bumped_at = 1.day.ago
+ topic = Fabricate(:topic, bumped_at: original_bumped_at)
+
+ post "/posts.json",
+ params: {
+ raw: "this is the test content",
+ topic_id: topic.id,
+ no_bump: "true",
+ }
+
+ expect(response.status).to eq(200)
+ expect(topic.reload.bumped_at).to eq_time(original_bumped_at)
+ end
+
it "should be able to post with topic bumping" do
post "/posts.json", params: { raw: "this is the test content", topic_id: topic.id }