Skip to content

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

Don't swallow errors when failing to create message for post action.

Guo Xiang Tan30ddc1f2226d
diff --git a/app/models/post_action.rb b/app/models/post_action.rb
index adb1cf09142..617d0c72b3b 100644
--- a/app/models/post_action.rb
+++ b/app/models/post_action.rb
@@ -255,7 +255,7 @@ SQL
end
end
- PostCreator.new(user, opts).create.try(:id)
+ PostCreator.new(user, opts).create!&.id
end
def self.limit_action!(user, post, post_action_type_id)
diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb
index 7c2a13a3b69..9c742835dfe 100644
--- a/spec/models/post_action_spec.rb
+++ b/spec/models/post_action_spec.rb
@@ -515,7 +515,7 @@ describe PostAction do
end
end
- describe "#create_message_for_post_action" do
+ describe ".create_message_for_post_action" do
it "does not create a message when there is no message" do
message_id = PostAction.create_message_for_post_action(Discourse.system_user, post, PostActionType.types[:spam], {})
expect(message_id).to be_nil
@@ -528,6 +528,25 @@ describe PostAction do
end
end
+ it "should raise the right errors when it fails to create a post" do
+ begin
+ group = Group[:moderators]
+ messageable_level = group.messageable_level
+ group.update!(messageable_level: Group::ALIAS_LEVELS[:nobody])
+
+ expect do
+ PostAction.create_message_for_post_action(
+ Fabricate(:user),
+ post,
+ PostActionType.types[:notify_moderators],
+ message: 'testing',
+ )
+ end.to raise_error(ActiveRecord::RecordNotSaved)
+ ensure
+ group.update!(messageable_level: messageable_level)
+ end
+ end
+
end
describe ".lookup_for" do