Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Rails/HttpStatus aware of Rails specific response assertions #1267

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1267](https://github.com/rubocop/rubocop-rails/pull/1267): Make `Rails/HttpStatus` aware of Rails-specific response assertions. ([@tldn0718][])
14 changes: 12 additions & 2 deletions lib/rubocop/cop/rails/http_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ module Rails
# render plain: 'foo/bar', status: 304
# redirect_to root_url, status: 301
# head 200
# assert_response 200
# assert_redirected_to '/some/path', status: 301
#
# # good
# render :foo, status: :ok
# render json: { foo: 'bar' }, status: :ok
# render plain: 'foo/bar', status: :not_modified
# redirect_to root_url, status: :moved_permanently
# head :ok
# assert_response :ok
# assert_redirected_to '/some/path', status: :moved_permanently
#
# @example EnforcedStyle: numeric
# # bad
Expand All @@ -28,25 +32,31 @@ module Rails
# render plain: 'foo/bar', status: :not_modified
# redirect_to root_url, status: :moved_permanently
# head :ok
# assert_response :ok
# assert_redirected_to '/some/path', status: :moved_permanently
#
# # good
# render :foo, status: 200
# render json: { foo: 'bar' }, status: 404
# render plain: 'foo/bar', status: 304
# redirect_to root_url, status: 301
# head 200
# assert_response 200
# assert_redirected_to '/some/path', status: 301
#
class HttpStatus < Base
include ConfigurableEnforcedStyle
extend AutoCorrector

RESTRICT_ON_SEND = %i[render redirect_to head].freeze
RESTRICT_ON_SEND = %i[render redirect_to head assert_response assert_redirected_to].freeze

def_node_matcher :http_status, <<~PATTERN
{
(send nil? {:render :redirect_to} _ $hash)
(send nil? {:render :redirect_to} $hash)
(send nil? :head ${int sym} ...)
(send nil? {:head :assert_response} ${int sym} ...)
(send nil? :assert_redirected_to _ $hash ...)
(send nil? :assert_redirected_to $hash ...)
}
PATTERN

Expand Down
46 changes: 46 additions & 0 deletions spec/rubocop/cop/rails/http_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
^^^ Prefer `:ok` over `200` to define HTTP status code.
head 200, location: 'accounts'
^^^ Prefer `:ok` over `200` to define HTTP status code.
assert_response 200
^^^ Prefer `:ok` over `200` to define HTTP status code.
assert_response 404, 'message'
^^^ Prefer `:not_found` over `404` to define HTTP status code.
assert_redirected_to '/some/path', status: 301
^^^ Prefer `:moved_permanently` over `301` to define HTTP status code.
assert_redirected_to action: 'index', status: 301
^^^ Prefer `:moved_permanently` over `301` to define HTTP status code.
assert_redirected_to '/some/path', { status: 301 }, 'message'
^^^ Prefer `:moved_permanently` over `301` to define HTTP status code.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -36,6 +46,11 @@
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: :moved_permanently
head :ok
head :ok, location: 'accounts'
assert_response :ok
assert_response :not_found, 'message'
assert_redirected_to '/some/path', status: :moved_permanently
assert_redirected_to action: 'index', status: :moved_permanently
assert_redirected_to '/some/path', { status: :moved_permanently }, 'message'
RUBY
end

Expand All @@ -58,6 +73,10 @@
redirect_to root_url, status: :moved_permanently
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: :moved_permanently
head :ok
assert_response :ok
assert_response :not_found, 'message'
assert_redirected_to '/some/path', status: :moved_permanently
assert_redirected_to action: 'index', status: :moved_permanently
RUBY
end

Expand All @@ -69,6 +88,10 @@
redirect_to root_url, status: 550
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 550
head 550
assert_response 550
assert_response 550, 'message'
assert_redirected_to '/some/path', status: 550
assert_redirected_to action: 'index', status: 550
RUBY
end

Expand Down Expand Up @@ -102,6 +125,16 @@
^^^ Prefer `200` over `:ok` to define HTTP status code.
head :ok, location: 'accounts'
^^^ Prefer `200` over `:ok` to define HTTP status code.
assert_response :ok
^^^ Prefer `200` over `:ok` to define HTTP status code.
assert_response :not_found, 'message'
^^^^^^^^^^ Prefer `404` over `:not_found` to define HTTP status code.
assert_redirected_to '/some/path', status: :moved_permanently
^^^^^^^^^^^^^^^^^^ Prefer `301` over `:moved_permanently` to define HTTP status code.
assert_redirected_to action: 'index', status: :moved_permanently
^^^^^^^^^^^^^^^^^^ Prefer `301` over `:moved_permanently` to define HTTP status code.
assert_redirected_to '/some/path', { status: :moved_permanently }, 'message'
^^^^^^^^^^^^^^^^^^ Prefer `301` over `:moved_permanently` to define HTTP status code.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -114,6 +147,11 @@
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 301
head 200
head 200, location: 'accounts'
assert_response 200
assert_response 404, 'message'
assert_redirected_to '/some/path', status: 301
assert_redirected_to action: 'index', status: 301
assert_redirected_to '/some/path', { status: 301 }, 'message'
RUBY
end

Expand All @@ -125,6 +163,10 @@
redirect_to root_url, status: 301
redirect_to root_path(utm_source: :pr, utm_medium: :web), status: 301
head 200
assert_response 200
assert_response 404, 'message'
assert_redirected_to '/some/path', status: 301
assert_redirected_to action: 'index', status: 301
RUBY
end

Expand All @@ -134,6 +176,10 @@
render :foo, status: :success
render :foo, status: :missing
render :foo, status: :redirect
assert_response :error
assert_response :success
assert_response :missing
assert_response :redirect
RUBY
end

Expand Down
Loading