blob: 13955750f4f2fd078f391cc9825093792f9ba0e2 (
plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# frozen_string_literal: true
# AccessMatchersGeneric
#
# Matchers to test the access permissions for service classes or other generic pieces of business logic.
module AccessMatchersGeneric
extend RSpec::Matchers::DSL
include AccessMatchersHelpers
ERROR_CLASS = Gitlab::Access::AccessDeniedError
def error_message(error)
str = error.class.name
str += ": #{error.message}" if error.message != error.class.name
str
end
def error_expectation_message(allowed, error)
if allowed
"Expected to raise nothing but #{error_message(error)} was raised."
else
"Expected to raise #{ERROR_CLASS} but nothing was raised."
end
end
def description_for(role, type, error)
allowed = type == 'allowed'
"be #{type} for #{role} role. #{error_expectation_message(allowed, error)}"
end
matcher :be_allowed_for do |role|
match do |action|
# methods called in this and negated block are being run in context of ExampleGroup
# (not matcher) instance so we have to pass data via local vars
run_matcher(action, role, @membership, @owned_objects) do |action|
action.call
rescue => e
@error = e
raise unless e.is_a?(ERROR_CLASS)
end
@error.nil?
end
chain :of do |membership|
@membership = membership
end
chain :own do |*owned_objects|
@owned_objects = owned_objects
end
failure_message do
"expected this action to #{description_for(role, 'allowed', @error)}"
end
failure_message_when_negated do
"expected this action to #{description_for(role, 'denied', @error)}"
end
supports_block_expectations
end
RSpec::Matchers.define_negated_matcher :be_denied_for, :be_allowed_for
end
|