-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Betterment | ||
class NotUsingRswag < Base | ||
MSG = 'API tests should use documented using rswag and not the built in `get`, `post`, `put`, `patch`, `delete` methods' | ||
|
||
# @!method test?(node) | ||
def_node_matcher :test?, <<-PATTERN | ||
(block (send nil? :it _) ...) | ||
PATTERN | ||
|
||
# @!method shared_method?(node) | ||
def_node_matcher :shared_method?, <<-PATTERN | ||
(def ...) | ||
PATTERN | ||
|
||
# @!method before_block?(node) | ||
def_node_matcher :before_block?, <<-PATTERN | ||
(block (send nil? :before) ...) | ||
PATTERN | ||
|
||
RESTRICT_ON_SEND = %i(get put patch post delete).freeze | ||
|
||
def on_send(node) | ||
return unless node.ancestors.any? { |a| test?(a) || shared_method?(a) || before_block?(a) } | ||
|
||
add_offense(node) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::Betterment::NotUsingRswag, :config do | ||
let(:error_message) do | ||
'API tests should use documented using rswag and not the built in `get`, `post`, `put`, `patch`, `delete` methods' | ||
end | ||
|
||
it 'rejects using the built in get method in a test' do | ||
expect_offense(<<~RUBY) | ||
RSpec.describe MyApiController do | ||
it 'returns ok status with expected response' do | ||
get "/api/widgets/1" | ||
^^^^^^^^^^^^^^^^^^^^ #{error_message} | ||
expect(response).to have_http_status :ok | ||
expect(response_json).to eq accepted_response.as_json | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'rejects using the built in get method in a shared method' do | ||
expect_offense(<<~RUBY) | ||
RSpec.describe MyApiController do | ||
def make_get_request | ||
get "/api/widgets/1" | ||
^^^^^^^^^^^^^^^^^^^^ #{error_message} | ||
end | ||
it 'returns ok status with expected response' do | ||
make_get_request | ||
expect(response).to have_http_status :ok | ||
expect(response_json).to eq accepted_response.as_json | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'rejects using the built in get method in a before block' do | ||
expect_offense(<<~RUBY) | ||
RSpec.describe MyApiController do | ||
before do | ||
get "/api/widgets/1" | ||
^^^^^^^^^^^^^^^^^^^^ #{error_message} | ||
end | ||
it 'returns ok status with expected response' do | ||
expect(response).to have_http_status :ok | ||
expect(response_json).to eq accepted_response.as_json | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts using the rswag get method in the example setup' do | ||
expect_no_offenses(<<~RUBY) | ||
RSpec.describe MyApiController do | ||
path "/api/widgets/1" do | ||
get 'shows a foo widget' do | ||
it 'returns ok status with expected response' do | ||
expect(response).to have_http_status :ok | ||
expect(response_json).to eq accepted_response.as_json | ||
end | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end |