Skip to content

Commit f0c1bec

Browse files
committed
abstracted rate limit checks in CommentsController into the check_create_access action callback & added tests for it
1 parent 8b273dc commit f0c1bec

2 files changed

Lines changed: 35 additions & 14 deletions

File tree

app/controllers/comments_controller.rb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class CommentsController < ApplicationController
1010

1111
before_action :check_post_access, only: [:create_thread, :create]
1212
before_action :check_privilege, only: [:update, :destroy, :undelete]
13+
before_action :check_create_access, only: [:create_thread, :create]
1314
before_action :check_reply_access, only: [:create]
1415
before_action :check_restrict_access, only: [:thread_restrict]
1516
before_action :check_thread_access, only: [:thread, :thread_content, :thread_followers]
@@ -34,13 +35,6 @@ def create_thread
3435

3536
pings = check_for_pings @comment_thread, body
3637

37-
rate_limited, limit_message = helpers.comment_rate_limited?(current_user, @post)
38-
if rate_limited
39-
flash[:danger] = limit_message
40-
redirect_to helpers.generic_share_link(@post)
41-
return
42-
end
43-
4438
success = ActiveRecord::Base.transaction do
4539
@comment_thread.save!
4640
@comment.save!
@@ -74,13 +68,6 @@ def create
7468
@comment = Comment.new(post: @post, content: body, user: current_user,
7569
comment_thread: @comment_thread, has_reference: false)
7670

77-
rate_limited, limit_message = helpers.comment_rate_limited?(current_user, @post)
78-
if rate_limited
79-
flash[:danger] = limit_message
80-
redirect_to helpers.generic_share_link(@post)
81-
return
82-
end
83-
8471
status = @comment.save
8572

8673
if status
@@ -338,6 +325,14 @@ def check_privilege
338325
end
339326
end
340327

328+
def check_create_access
329+
rate_limited, limit_message = helpers.comment_rate_limited?(current_user, @post)
330+
if rate_limited
331+
flash[:danger] = limit_message
332+
redirect_to helpers.generic_share_link(@post)
333+
end
334+
end
335+
341336
def check_reply_access
342337
if @comment_thread.read_only? && current_user&.standard?
343338
respond_to do |format|

test/controllers/comments/create_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ class CommentsControllerTest < ActionController::TestCase
4646
assert_redirected_to_sign_in
4747
end
4848

49+
test 'should not create threads on posts of others without the unrestricted ability when rate-limited' do
50+
sign_in users(:basic_user)
51+
52+
SiteSetting['RL_NewUserComments'] = 0
53+
54+
post = posts(:question_one)
55+
56+
try_create_thread(post)
57+
58+
assert_not_nil flash[:danger]
59+
assert_redirected_to @controller.helpers.generic_share_link(post)
60+
end
61+
4962
test 'should not create thread if the target post is inaccessible' do
5063
sign_in users(:editor)
5164
try_create_thread(posts(:high_trust))
@@ -134,6 +147,19 @@ class CommentsControllerTest < ActionController::TestCase
134147
assert_redirected_to_sign_in
135148
end
136149

150+
test 'should not create comments on threads on posts of others without the unrestricted ability when rate-limited' do
151+
sign_in users(:basic_user)
152+
153+
SiteSetting['RL_NewUserComments'] = 0
154+
155+
thread = comment_threads(:normal)
156+
157+
try_create_comment(thread)
158+
159+
assert_not_nil flash[:danger]
160+
assert_redirected_to @controller.helpers.generic_share_link(thread.post)
161+
end
162+
137163
test 'should not create comment if the target post is inaccessible' do
138164
sign_in users(:editor)
139165
try_create_comment(comment_threads(:high_trust))

0 commit comments

Comments
 (0)