/
to_forbid_over_not_to_permit.rb
48 lines (40 loc) · 1.51 KB
/
to_forbid_over_not_to_permit.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
require 'capybara/rspec/matchers'
module RuboCop
module Cop
module Zammad
# Policy specs using ".not_to permit_actions" will lead to presumably false results,
# as that matches when only one of the listed actions is forbidden. Instead,
# use the simpler ".to forbid_actions" which ensures all actions are forbidden.
#
# @example
# # bad
# expect(page).not_to permit(:action)
#
# # good
# expect(page).to forbid(:action)
class ToForbidOverNotToPermit < Base
extend AutoCorrector
MSG = 'Prefer `.to forbid_action[s]` over `.not_to permit_action[s]`.'.freeze
def_node_matcher :on_not_to_permit, '(send (send nil? {:expect :is_expected} {_ ...}) :not_to (send nil? {:permit_action :permit_actions} ...))'
def on_send(node)
on_not_to_permit(node) do
add_offense(node, message: MSG) do |corrector|
corrector.replace(original_matcher_node(node).loc.selector, alternative_matcher(node))
corrector.replace(node.loc.selector, 'to')
end
end
end
def original_matcher_node(node)
node.children[2]
end
def original_matcher(node)
original_matcher_node(node).method_name
end
def alternative_matcher(node)
original_matcher(node).to_s.sub 'permit_action', 'forbid_action'
end
end
end
end
end