2014-07-18 10:20:48 -04:00
|
|
|
require "active_support/core_ext/array/conversions"
|
|
|
|
|
2012-11-19 04:57:17 -05:00
|
|
|
module Pundit
|
|
|
|
module RSpec
|
|
|
|
module Matchers
|
|
|
|
extend ::RSpec::Matchers::DSL
|
|
|
|
|
|
|
|
matcher :permit do |user, record|
|
2014-06-27 16:39:59 -04:00
|
|
|
match_proc = lambda do |policy|
|
2016-01-14 09:15:30 -05:00
|
|
|
@violating_permissions = permissions.find_all do |permission|
|
|
|
|
not policy.new(user, record).public_send(permission)
|
|
|
|
end
|
2014-11-10 06:38:00 -05:00
|
|
|
@violating_permissions.empty?
|
2014-01-17 08:31:53 -05:00
|
|
|
end
|
|
|
|
|
2014-06-27 16:39:59 -04:00
|
|
|
match_when_negated_proc = lambda do |policy|
|
2016-01-14 09:15:30 -05:00
|
|
|
@violating_permissions = permissions.find_all do |permission|
|
|
|
|
policy.new(user, record).public_send(permission)
|
|
|
|
end
|
2014-11-10 06:38:00 -05:00
|
|
|
@violating_permissions.empty?
|
2012-11-19 04:57:17 -05:00
|
|
|
end
|
|
|
|
|
2014-06-27 16:39:59 -04:00
|
|
|
failure_message_proc = lambda do |policy|
|
2014-11-10 06:38:00 -05:00
|
|
|
was_were = @violating_permissions.count > 1 ? "were" : "was"
|
2016-01-14 09:15:30 -05:00
|
|
|
"Expected #{policy} to grant #{permissions.to_sentence} on \
|
|
|
|
#{record} but #{@violating_permissions.to_sentence} #{was_were} not granted"
|
2012-11-19 04:57:17 -05:00
|
|
|
end
|
|
|
|
|
2014-06-27 16:39:59 -04:00
|
|
|
failure_message_when_negated_proc = lambda do |policy|
|
2014-11-10 06:38:00 -05:00
|
|
|
was_were = @violating_permissions.count > 1 ? "were" : "was"
|
2016-01-14 09:15:30 -05:00
|
|
|
"Expected #{policy} not to grant #{permissions.to_sentence} on \
|
|
|
|
#{record} but #{@violating_permissions.to_sentence} #{was_were} granted"
|
2012-11-19 04:57:17 -05:00
|
|
|
end
|
|
|
|
|
2014-06-27 16:39:59 -04:00
|
|
|
if respond_to?(:match_when_negated)
|
|
|
|
match(&match_proc)
|
|
|
|
match_when_negated(&match_when_negated_proc)
|
|
|
|
failure_message(&failure_message_proc)
|
|
|
|
failure_message_when_negated(&failure_message_when_negated_proc)
|
|
|
|
else
|
|
|
|
match_for_should(&match_proc)
|
|
|
|
match_for_should_not(&match_when_negated_proc)
|
|
|
|
failure_message_for_should(&failure_message_proc)
|
|
|
|
failure_message_for_should_not(&failure_message_when_negated_proc)
|
|
|
|
end
|
|
|
|
|
2012-11-19 04:57:17 -05:00
|
|
|
def permissions
|
2014-03-13 15:19:43 -04:00
|
|
|
current_example = ::RSpec.respond_to?(:current_example) ? ::RSpec.current_example : example
|
|
|
|
current_example.metadata[:permissions]
|
2012-11-19 04:57:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module DSL
|
|
|
|
def permissions(*list, &block)
|
2016-01-14 09:15:30 -05:00
|
|
|
describe(list.to_sentence, permissions: list, caller: caller) { instance_eval(&block) }
|
2012-11-19 04:57:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module PolicyExampleGroup
|
|
|
|
include Pundit::RSpec::Matchers
|
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.metadata[:type] = :policy
|
|
|
|
base.extend Pundit::RSpec::DSL
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.configure do |config|
|
2014-07-18 10:20:48 -04:00
|
|
|
if RSpec::Core::Version::STRING.split(".").first.to_i >= 3
|
2016-01-14 09:15:30 -05:00
|
|
|
config.include(Pundit::RSpec::PolicyExampleGroup,
|
|
|
|
type: :policy,
|
|
|
|
file_path: %r{spec/policies}
|
|
|
|
)
|
2014-07-18 10:20:48 -04:00
|
|
|
else
|
2016-01-14 09:15:30 -05:00
|
|
|
config.include(Pundit::RSpec::PolicyExampleGroup,
|
|
|
|
type: :policy,
|
|
|
|
example_group: { file_path: %r{spec/policies} }
|
|
|
|
)
|
2014-07-18 10:20:48 -04:00
|
|
|
end
|
2012-11-19 04:57:17 -05:00
|
|
|
end
|