Skip to content

Commit

Permalink
feat: add rswag cop
Browse files Browse the repository at this point in the history
  • Loading branch information
andimrob committed Jun 8, 2024
1 parent 176bae2 commit e427f2f
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
7 changes: 7 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ Betterment/RenderStatus:
Include:
- app/controllers/**/*.rb

Betterment/NotUsingRswag:
Enabled: false
SafeAutoCorrect: false
Description: Detect API specs missing OpenAPI documentation using rswag
Include:
- spec/requests/**/*_spec.rb

FactoryBot/ConsistentParenthesesStyle:
Enabled: false

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/betterment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
require 'rubocop/cop/betterment/fetch_boolean'
require 'rubocop/cop/betterment/render_status'
require 'rubocop/cop/betterment/redirect_status'
require 'rubocop/cop/betterment/not_using_rswag'
34 changes: 34 additions & 0 deletions lib/rubocop/cop/betterment/not_using_rswag.rb
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
72 changes: 72 additions & 0 deletions spec/rubocop/cop/betterment/not_using_rswag_spec.rb
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

0 comments on commit e427f2f

Please sign in to comment.