debian-mirror-gitlab/rubocop/cop/rspec/httparty_basic_auth.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.2 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
2022-11-25 23:54:43 +05:30
require 'rubocop-rspec'
2021-02-22 17:27:13 +05:30
module RuboCop
module Cop
module RSpec
# This cop checks for invalid credentials passed to HTTParty
#
# @example
#
# # bad
# HTTParty.get(url, basic_auth: { user: 'foo' })
#
# # good
# HTTParty.get(url, basic_auth: { username: 'foo' })
2022-10-11 01:57:18 +05:30
class HTTPartyBasicAuth < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
2021-04-29 21:17:54 +05:30
MESSAGE = "`basic_auth: { user: ... }` does not work - replace `user:` with `username:`"
2021-02-22 17:27:13 +05:30
RESTRICT_ON_SEND = %i(get put post delete).freeze
def_node_matcher :httparty_basic_auth?, <<~PATTERN
(send
(const _ :HTTParty)
{#{RESTRICT_ON_SEND.map(&:inspect).join(' ')}}
<(hash
<(pair
(sym :basic_auth)
(hash
<(pair $(sym :user) _) ...>
)
) ...>
) ...>
)
PATTERN
def on_send(node)
return unless m = httparty_basic_auth?(node)
2022-10-11 01:57:18 +05:30
add_offense(m, message: MESSAGE) do |corrector|
corrector.replace(m, 'username')
2021-02-22 17:27:13 +05:30
end
end
end
end
end
end