pundit-matchers 2.3.0 → 3.0.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pundit/matchers/actions_matcher.rb +41 -0
- data/lib/pundit/matchers/attributes_matcher.rb +71 -0
- data/lib/pundit/matchers/base_matcher.rb +27 -0
- data/lib/pundit/matchers/forbid_all_actions_matcher.rb +43 -0
- data/lib/pundit/matchers/forbid_only_actions_matcher.rb +60 -0
- data/lib/pundit/matchers/permit_actions_matcher.rb +69 -0
- data/lib/pundit/matchers/permit_all_actions_matcher.rb +43 -0
- data/lib/pundit/matchers/permit_attributes_matcher.rb +65 -0
- data/lib/pundit/matchers/permit_only_actions_matcher.rb +60 -0
- data/lib/pundit/matchers/utils/policy_info.rb +67 -6
- data/lib/pundit/matchers.rb +120 -364
- metadata +16 -39
- data/lib/pundit/matchers/utils/all_actions/actions_matcher.rb +0 -39
- data/lib/pundit/matchers/utils/all_actions/error_message_formatter.rb +0 -47
- data/lib/pundit/matchers/utils/all_actions/forbidden_actions_error_formatter.rb +0 -26
- data/lib/pundit/matchers/utils/all_actions/forbidden_actions_matcher.rb +0 -20
- data/lib/pundit/matchers/utils/all_actions/permitted_actions_error_formatter.rb +0 -26
- data/lib/pundit/matchers/utils/all_actions/permitted_actions_matcher.rb +0 -20
- data/lib/pundit/matchers/utils/only_actions/actions_matcher.rb +0 -39
- data/lib/pundit/matchers/utils/only_actions/error_message_formatter.rb +0 -54
- data/lib/pundit/matchers/utils/only_actions/forbidden_actions_error_formatter.rb +0 -26
- data/lib/pundit/matchers/utils/only_actions/forbidden_actions_matcher.rb +0 -20
- data/lib/pundit/matchers/utils/only_actions/permitted_actions_error_formatter.rb +0 -26
- data/lib/pundit/matchers/utils/only_actions/permitted_actions_matcher.rb +0 -20
data/lib/pundit/matchers.rb
CHANGED
@@ -2,412 +2,168 @@
|
|
2
2
|
|
3
3
|
require 'rspec/core'
|
4
4
|
|
5
|
+
require_relative 'matchers/permit_actions_matcher'
|
6
|
+
|
7
|
+
require_relative 'matchers/permit_attributes_matcher'
|
8
|
+
|
9
|
+
require_relative 'matchers/forbid_all_actions_matcher'
|
10
|
+
require_relative 'matchers/forbid_only_actions_matcher'
|
11
|
+
|
12
|
+
require_relative 'matchers/permit_all_actions_matcher'
|
13
|
+
require_relative 'matchers/permit_only_actions_matcher'
|
14
|
+
|
5
15
|
module Pundit
|
16
|
+
# Matchers module provides a set of RSpec matchers for testing Pundit policies.
|
6
17
|
module Matchers
|
7
|
-
|
8
|
-
|
9
|
-
require_relative 'matchers/utils/all_actions/forbidden_actions_matcher'
|
10
|
-
require_relative 'matchers/utils/all_actions/permitted_actions_error_formatter'
|
11
|
-
require_relative 'matchers/utils/all_actions/permitted_actions_matcher'
|
12
|
-
|
13
|
-
require_relative 'matchers/utils/only_actions/forbidden_actions_error_formatter'
|
14
|
-
require_relative 'matchers/utils/only_actions/forbidden_actions_matcher'
|
15
|
-
require_relative 'matchers/utils/only_actions/permitted_actions_error_formatter'
|
16
|
-
require_relative 'matchers/utils/only_actions/permitted_actions_matcher'
|
18
|
+
# A Proc that negates the description of a matcher.
|
19
|
+
NEGATED_DESCRIPTION = ->(description) { description.gsub(/^permit/, 'forbid') }
|
17
20
|
|
21
|
+
# Configuration class for Pundit Matchers.
|
18
22
|
class Configuration
|
19
|
-
|
23
|
+
# The default user object value
|
24
|
+
DEFAULT_USER_ALIAS = :user
|
25
|
+
|
26
|
+
# The default user object in policies.
|
27
|
+
# @return [Symbol|String]
|
28
|
+
attr_accessor :default_user_alias
|
29
|
+
|
30
|
+
# Policy-specific user objects.
|
31
|
+
#
|
32
|
+
# @example Use +:client+ as user alias for class +Post+
|
33
|
+
# config.user_aliases = { 'Post' => :client }
|
34
|
+
#
|
35
|
+
# @return [Hash]
|
36
|
+
attr_accessor :user_aliases
|
20
37
|
|
21
38
|
def initialize
|
22
|
-
@
|
39
|
+
@default_user_alias = DEFAULT_USER_ALIAS
|
40
|
+
@user_aliases = {}
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns the user object for the given policy.
|
44
|
+
#
|
45
|
+
# @return [Symbol]
|
46
|
+
def user_alias(policy)
|
47
|
+
user_aliases.fetch(policy.class.name, default_user_alias)
|
23
48
|
end
|
24
49
|
end
|
25
50
|
|
26
51
|
class << self
|
52
|
+
# Configures Pundit Matchers.
|
53
|
+
#
|
54
|
+
# @yieldparam [Configuration] configuration the configuration object to be modified.
|
27
55
|
def configure
|
28
56
|
yield(configuration)
|
29
57
|
end
|
30
58
|
|
59
|
+
# Returns the configuration object for Pundit Matchers.
|
60
|
+
#
|
61
|
+
# @return [Configuration] the configuration object.
|
31
62
|
def configuration
|
32
63
|
@configuration ||= Pundit::Matchers::Configuration.new
|
33
64
|
end
|
34
65
|
end
|
35
|
-
end
|
36
|
-
|
37
|
-
RSpec::Matchers.define :forbid_action do |action, *args, **kwargs|
|
38
|
-
match do |policy|
|
39
|
-
if args.any?
|
40
|
-
!policy.public_send("#{action}?", *args, **kwargs)
|
41
|
-
else
|
42
|
-
!policy.public_send("#{action}?", **kwargs)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
failure_message do |policy|
|
47
|
-
"#{policy.class} does not forbid #{action} for " \
|
48
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
49
|
-
end
|
50
|
-
|
51
|
-
failure_message_when_negated do |policy|
|
52
|
-
"#{policy.class} does not permit #{action} for " \
|
53
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
RSpec::Matchers.define :forbid_actions do |*actions|
|
58
|
-
actions.flatten!
|
59
|
-
match do |policy|
|
60
|
-
return false if actions.count < 1
|
61
|
-
|
62
|
-
@allowed_actions = actions.select do |action|
|
63
|
-
policy.public_send("#{action}?")
|
64
|
-
end
|
65
|
-
@allowed_actions.empty?
|
66
|
-
end
|
67
|
-
|
68
|
-
attr_reader :allowed_actions
|
69
|
-
|
70
|
-
zero_actions_failure_message = 'At least one action must be ' \
|
71
|
-
'specified when using the forbid_actions matcher.'
|
72
|
-
|
73
|
-
failure_message do |policy|
|
74
|
-
if actions.count.zero?
|
75
|
-
zero_actions_failure_message
|
76
|
-
else
|
77
|
-
"#{policy.class} expected to forbid #{actions}, but permitted " \
|
78
|
-
"#{allowed_actions} for " \
|
79
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
failure_message_when_negated do |policy|
|
84
|
-
if actions.count.zero?
|
85
|
-
zero_actions_failure_message
|
86
|
-
else
|
87
|
-
"#{policy.class} expected to permit #{actions}, but forbade " \
|
88
|
-
"#{allowed_actions} for " \
|
89
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
RSpec::Matchers.define :forbid_edit_and_update_actions do
|
95
|
-
match do |policy|
|
96
|
-
!policy.edit? && !policy.update?
|
97
|
-
end
|
98
|
-
|
99
|
-
failure_message do |policy|
|
100
|
-
"#{policy.class} does not forbid the edit or update action for " \
|
101
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
102
|
-
end
|
103
|
-
|
104
|
-
failure_message_when_negated do |policy|
|
105
|
-
"#{policy.class} does not permit the edit or update action for " \
|
106
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
RSpec::Matchers.define :forbid_mass_assignment_of do |attributes|
|
111
|
-
# Map single object argument to an array, if necessary
|
112
|
-
attributes = [attributes] unless attributes.is_a?(Array)
|
113
|
-
|
114
|
-
match do |policy|
|
115
|
-
return false if attributes.count < 1
|
116
|
-
|
117
|
-
@allowed_attributes = attributes.select do |attribute|
|
118
|
-
if defined? @action
|
119
|
-
policy.send("permitted_attributes_for_#{@action}").include? attribute
|
120
|
-
else
|
121
|
-
policy.permitted_attributes.include? attribute
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
@allowed_attributes.empty?
|
126
|
-
end
|
127
|
-
|
128
|
-
attr_reader :allowed_attributes
|
129
|
-
|
130
|
-
chain :for_action do |action|
|
131
|
-
@action = action
|
132
|
-
end
|
133
|
-
|
134
|
-
zero_attributes_failure_message = 'At least one attribute must be ' \
|
135
|
-
'specified when using the forbid_mass_assignment_of matcher.'
|
136
|
-
|
137
|
-
failure_message do |policy|
|
138
|
-
if attributes.count.zero?
|
139
|
-
zero_attributes_failure_message
|
140
|
-
elsif defined? @action
|
141
|
-
"#{policy.class} expected to forbid the mass assignment of the " \
|
142
|
-
"attributes #{attributes} when authorising the #{@action} action, " \
|
143
|
-
'but permitted the mass assignment of the attributes ' \
|
144
|
-
"#{allowed_attributes} for " \
|
145
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
146
|
-
else
|
147
|
-
"#{policy.class} expected to forbid the mass assignment of the " \
|
148
|
-
"attributes #{attributes}, but permitted the mass assignment of " \
|
149
|
-
"the attributes #{allowed_attributes} for " \
|
150
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
failure_message_when_negated do |policy|
|
155
|
-
if attributes.count.zero?
|
156
|
-
zero_attributes_failure_message
|
157
|
-
elsif defined? @action
|
158
|
-
"#{policy.class} expected to permit the mass assignment of the " \
|
159
|
-
"attributes #{attributes} when authorising the #{@action} action, " \
|
160
|
-
'but permitted the mass assignment of the attributes ' \
|
161
|
-
"#{allowed_attributes} for " \
|
162
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
163
|
-
else
|
164
|
-
"#{policy.class} expected to permit the mass assignment of the " \
|
165
|
-
"attributes #{attributes}, but permitted the mass assignment of " \
|
166
|
-
"the attributes #{allowed_attributes} for " \
|
167
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
RSpec::Matchers.define :forbid_new_and_create_actions do
|
173
|
-
match do |policy|
|
174
|
-
!policy.new? && !policy.create?
|
175
|
-
end
|
176
|
-
|
177
|
-
failure_message do |policy|
|
178
|
-
"#{policy.class} does not forbid the new or create action for " \
|
179
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
180
|
-
end
|
181
|
-
|
182
|
-
failure_message_when_negated do |policy|
|
183
|
-
"#{policy.class} does not permit the new or create action for " \
|
184
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
RSpec::Matchers.define :permit_action do |action, *args, **kwargs|
|
189
|
-
match do |policy|
|
190
|
-
if args.any?
|
191
|
-
policy.public_send("#{action}?", *args, **kwargs)
|
192
|
-
else
|
193
|
-
policy.public_send("#{action}?", **kwargs)
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
failure_message do |policy|
|
198
|
-
"#{policy.class} does not permit #{action} for " \
|
199
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
200
|
-
end
|
201
66
|
|
202
|
-
|
203
|
-
|
204
|
-
|
67
|
+
# Creates a matcher that tests if the policy permits a given action.
|
68
|
+
#
|
69
|
+
# @param [Symbol] action the action to be tested.
|
70
|
+
# @return [PermitActionsMatcher] the matcher object.
|
71
|
+
def permit_action(action)
|
72
|
+
PermitActionsMatcher.new(action)
|
205
73
|
end
|
206
|
-
end
|
207
|
-
|
208
|
-
RSpec::Matchers.define :permit_actions do |*actions|
|
209
|
-
actions.flatten!
|
210
|
-
match do |policy|
|
211
|
-
return false if actions.count < 1
|
212
|
-
|
213
|
-
@forbidden_actions = actions.reject do |action|
|
214
|
-
policy.public_send("#{action}?")
|
215
|
-
end
|
216
|
-
@forbidden_actions.empty?
|
217
|
-
end
|
218
|
-
|
219
|
-
match_when_negated do |policy|
|
220
|
-
::Kernel.warn 'Using expect { }.not_to permit_actions could produce \
|
221
|
-
confusing results. Please use `.to forbid_actions` instead. To \
|
222
|
-
clarify, `.not_to permit_actions` will look at all of the actions and \
|
223
|
-
checks if ANY actions fail, not if all actions fail. Therefore, you \
|
224
|
-
could result in something like this: \
|
225
|
-
|
226
|
-
it { is_expected.to permit_actions([:new, :create, :edit]) } \
|
227
|
-
it { is_expected.not_to permit_actions([:edit, :destroy]) } \
|
228
74
|
|
229
|
-
|
230
|
-
|
75
|
+
# @!macro [attach] RSpec::Matchers.define_negated_matcher
|
76
|
+
# @!method $1
|
77
|
+
#
|
78
|
+
# The negated matcher of {$2}.
|
79
|
+
#
|
80
|
+
# Same as +expect(policy).not_to $2(*args)+.
|
81
|
+
RSpec::Matchers.define_negated_matcher :forbid_action, :permit_action, &NEGATED_DESCRIPTION
|
231
82
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
83
|
+
# Creates a matcher that tests if the policy permits a set of actions.
|
84
|
+
#
|
85
|
+
# @param [Array<Symbol>] actions the actions to be tested.
|
86
|
+
# @return [PermitActionsMatcher] the matcher object.
|
87
|
+
def permit_actions(*actions)
|
88
|
+
PermitActionsMatcher.new(*actions)
|
238
89
|
end
|
239
90
|
|
240
|
-
|
91
|
+
RSpec::Matchers.define_negated_matcher :forbid_actions, :permit_actions, &NEGATED_DESCRIPTION
|
241
92
|
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
"#{forbidden_actions} for " \
|
251
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
252
|
-
end
|
93
|
+
# Creates a matcher that tests if the policy permits all actions.
|
94
|
+
#
|
95
|
+
# @note The negative form +not_to permit_all_actions+ is not supported
|
96
|
+
# since it creates ambiguity. Instead use +to forbid_all_actions+.
|
97
|
+
#
|
98
|
+
# @return [PermitAllActionsMatcher] the matcher object.
|
99
|
+
def permit_all_actions
|
100
|
+
PermitAllActionsMatcher.new
|
253
101
|
end
|
254
102
|
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
103
|
+
# Creates a matcher that tests if the policy forbids all actions.
|
104
|
+
#
|
105
|
+
# @note The negative form +not_to forbid_all_actions+ is not supported
|
106
|
+
# since it creates ambiguity. Instead use +to permit_all_actions+.
|
107
|
+
#
|
108
|
+
# @return [ForbidAllActionsMatcher] the matcher object.
|
109
|
+
def forbid_all_actions
|
110
|
+
ForbidAllActionsMatcher.new
|
263
111
|
end
|
264
|
-
end
|
265
112
|
|
266
|
-
|
267
|
-
|
268
|
-
|
113
|
+
# Creates a matcher that tests if the policy permits the edit and update actions.
|
114
|
+
#
|
115
|
+
# @return [PermitActionsMatcher] the matcher object.
|
116
|
+
def permit_edit_and_update_actions
|
117
|
+
PermitActionsMatcher.new(:edit, :update)
|
269
118
|
end
|
270
119
|
|
271
|
-
|
272
|
-
|
273
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
274
|
-
end
|
120
|
+
RSpec::Matchers.define_negated_matcher :forbid_edit_and_update_actions, :permit_edit_and_update_actions,
|
121
|
+
&NEGATED_DESCRIPTION
|
275
122
|
|
276
|
-
|
277
|
-
|
278
|
-
|
123
|
+
# Creates a matcher that tests if the policy permits the new and create actions.
|
124
|
+
#
|
125
|
+
# @return [PermitActionsMatcher] the matcher object.
|
126
|
+
def permit_new_and_create_actions
|
127
|
+
PermitActionsMatcher.new(:new, :create)
|
279
128
|
end
|
280
|
-
end
|
281
129
|
|
282
|
-
|
283
|
-
|
284
|
-
attributes = [attributes] unless attributes.is_a?(Array)
|
130
|
+
RSpec::Matchers.define_negated_matcher :forbid_new_and_create_actions, :permit_new_and_create_actions,
|
131
|
+
&NEGATED_DESCRIPTION
|
285
132
|
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
end
|
296
|
-
|
297
|
-
@forbidden_attributes.empty?
|
133
|
+
# Creates a matcher that tests if the policy permits only a set of actions.
|
134
|
+
#
|
135
|
+
# @note The negative form +not_to permit_only_actions+ is not supported
|
136
|
+
# since it creates ambiguity. Instead use +to forbid_only_actions+.
|
137
|
+
#
|
138
|
+
# @param [Array<Symbol>] actions the actions to be tested.
|
139
|
+
# @return [PermitOnlyActionsMatcher] the matcher object.
|
140
|
+
def permit_only_actions(*actions)
|
141
|
+
PermitOnlyActionsMatcher.new(*actions)
|
298
142
|
end
|
299
143
|
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
144
|
+
# Creates a matcher that tests if the policy forbids only a set of actions.
|
145
|
+
#
|
146
|
+
# @note The negative form +not_to forbid_only_actions+ is not supported
|
147
|
+
# since it creates ambiguity. Instead use +to permit_only_actions+.
|
148
|
+
#
|
149
|
+
# @param [Array<Symbol>] actions the actions to be tested.
|
150
|
+
# @return [ForbidOnlyActionsMatcher] the matcher object.
|
151
|
+
def forbid_only_actions(*actions)
|
152
|
+
ForbidOnlyActionsMatcher.new(*actions)
|
304
153
|
end
|
305
154
|
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
elsif defined? @action
|
313
|
-
"#{policy.class} expected to permit the mass assignment of the " \
|
314
|
-
"attributes #{attributes} when authorising the #{@action} action, " \
|
315
|
-
'but forbade the mass assignment of the attributes ' \
|
316
|
-
"#{forbidden_attributes} for " \
|
317
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
318
|
-
else
|
319
|
-
"#{policy.class} expected to permit the mass assignment of the " \
|
320
|
-
"attributes #{attributes}, but forbade the mass assignment of the " \
|
321
|
-
"attributes #{forbidden_attributes} for " \
|
322
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
323
|
-
end
|
155
|
+
# Creates a matcher that tests if the policy permits mass assignment of a set of attributes.
|
156
|
+
#
|
157
|
+
# @param [Array<Symbol>] attributes the attributes to be tested.
|
158
|
+
# @return [PermitAttributesMatcher] the matcher object.
|
159
|
+
def permit_mass_assignment_of(*attributes)
|
160
|
+
PermitAttributesMatcher.new(*attributes)
|
324
161
|
end
|
325
162
|
|
326
|
-
|
327
|
-
if attributes.count.zero?
|
328
|
-
zero_attributes_failure_message
|
329
|
-
elsif defined? @action
|
330
|
-
"#{policy.class} expected to forbid the mass assignment of the " \
|
331
|
-
"attributes #{attributes} when authorising the #{@action} action, " \
|
332
|
-
'but forbade the mass assignment of the attributes ' \
|
333
|
-
"#{forbidden_attributes} for " \
|
334
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
335
|
-
else
|
336
|
-
"#{policy.class} expected to forbid the mass assignment of the " \
|
337
|
-
"attributes #{attributes}, but forbade the mass assignment of the " \
|
338
|
-
"attributes #{forbidden_attributes} for " \
|
339
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
340
|
-
end
|
341
|
-
end
|
342
|
-
end
|
343
|
-
|
344
|
-
RSpec::Matchers.define :permit_new_and_create_actions do
|
345
|
-
match do |policy|
|
346
|
-
policy.new? && policy.create?
|
347
|
-
end
|
348
|
-
|
349
|
-
failure_message do |policy|
|
350
|
-
"#{policy.class} does not permit the new or create action for " \
|
351
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
352
|
-
end
|
353
|
-
|
354
|
-
failure_message_when_negated do |policy|
|
355
|
-
"#{policy.class} does not forbid the new or create action for " \
|
356
|
-
"#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
|
357
|
-
end
|
358
|
-
end
|
359
|
-
|
360
|
-
RSpec::Matchers.define :permit_all_actions do
|
361
|
-
match do |policy|
|
362
|
-
@matcher = Pundit::Matchers::Utils::AllActions::PermittedActionsMatcher.new(policy)
|
363
|
-
@matcher.match?
|
364
|
-
end
|
365
|
-
|
366
|
-
failure_message do
|
367
|
-
formatter = Pundit::Matchers::Utils::AllActions::PermittedActionsErrorFormatter.new(@matcher)
|
368
|
-
formatter.message
|
369
|
-
end
|
370
|
-
end
|
371
|
-
|
372
|
-
RSpec::Matchers.define :permit_only_actions do |actions|
|
373
|
-
match do |policy|
|
374
|
-
@matcher = Pundit::Matchers::Utils::OnlyActions::PermittedActionsMatcher.new(policy, actions)
|
375
|
-
@matcher.match?
|
376
|
-
end
|
377
|
-
|
378
|
-
failure_message do
|
379
|
-
formatter = Pundit::Matchers::Utils::OnlyActions::PermittedActionsErrorFormatter.new(@matcher)
|
380
|
-
formatter.message
|
381
|
-
end
|
382
|
-
end
|
383
|
-
|
384
|
-
RSpec::Matchers.define :forbid_all_actions do
|
385
|
-
match do |policy|
|
386
|
-
@matcher = Pundit::Matchers::Utils::AllActions::ForbiddenActionsMatcher.new(policy)
|
387
|
-
@matcher.match?
|
388
|
-
end
|
389
|
-
|
390
|
-
failure_message do
|
391
|
-
formatter = Pundit::Matchers::Utils::AllActions::ForbiddenActionsErrorFormatter.new(@matcher)
|
392
|
-
formatter.message
|
393
|
-
end
|
394
|
-
end
|
395
|
-
|
396
|
-
RSpec::Matchers.define :forbid_only_actions do |actions|
|
397
|
-
match do |policy|
|
398
|
-
@matcher = Pundit::Matchers::Utils::OnlyActions::ForbiddenActionsMatcher.new(policy, actions)
|
399
|
-
@matcher.match?
|
400
|
-
end
|
401
|
-
|
402
|
-
failure_message do
|
403
|
-
formatter = Pundit::Matchers::Utils::OnlyActions::ForbiddenActionsErrorFormatter.new(@matcher)
|
404
|
-
formatter.message
|
405
|
-
end
|
163
|
+
RSpec::Matchers.define_negated_matcher :forbid_mass_assignment_of, :permit_mass_assignment_of, &NEGATED_DESCRIPTION
|
406
164
|
end
|
407
165
|
end
|
408
166
|
|
409
|
-
|
410
|
-
|
411
|
-
config.include Pundit::Matchers
|
412
|
-
end
|
167
|
+
RSpec.configure do |config|
|
168
|
+
config.include Pundit::Matchers
|
413
169
|
end
|
metadata
CHANGED
@@ -1,49 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pundit-matchers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Alley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name: rspec
|
14
|
+
name: rspec
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 3.
|
19
|
+
version: '3.12'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 3.
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: pundit
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '1.1'
|
34
|
-
- - ">="
|
35
|
-
- !ruby/object:Gem::Version
|
36
|
-
version: 1.1.0
|
37
|
-
type: :development
|
38
|
-
prerelease: false
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
40
|
-
requirements:
|
41
|
-
- - "~>"
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '1.1'
|
44
|
-
- - ">="
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version: 1.1.0
|
26
|
+
version: '3.12'
|
47
27
|
description: A set of RSpec matchers for testing Pundit authorisation policies
|
48
28
|
email: chris@chrisalley.info
|
49
29
|
executables: []
|
@@ -51,18 +31,15 @@ extensions: []
|
|
51
31
|
extra_rdoc_files: []
|
52
32
|
files:
|
53
33
|
- lib/pundit/matchers.rb
|
54
|
-
- lib/pundit/matchers/
|
55
|
-
- lib/pundit/matchers/
|
56
|
-
- lib/pundit/matchers/
|
57
|
-
- lib/pundit/matchers/
|
58
|
-
- lib/pundit/matchers/
|
59
|
-
- lib/pundit/matchers/
|
60
|
-
- lib/pundit/matchers/
|
61
|
-
- lib/pundit/matchers/
|
62
|
-
- lib/pundit/matchers/
|
63
|
-
- lib/pundit/matchers/utils/only_actions/forbidden_actions_matcher.rb
|
64
|
-
- lib/pundit/matchers/utils/only_actions/permitted_actions_error_formatter.rb
|
65
|
-
- lib/pundit/matchers/utils/only_actions/permitted_actions_matcher.rb
|
34
|
+
- lib/pundit/matchers/actions_matcher.rb
|
35
|
+
- lib/pundit/matchers/attributes_matcher.rb
|
36
|
+
- lib/pundit/matchers/base_matcher.rb
|
37
|
+
- lib/pundit/matchers/forbid_all_actions_matcher.rb
|
38
|
+
- lib/pundit/matchers/forbid_only_actions_matcher.rb
|
39
|
+
- lib/pundit/matchers/permit_actions_matcher.rb
|
40
|
+
- lib/pundit/matchers/permit_all_actions_matcher.rb
|
41
|
+
- lib/pundit/matchers/permit_attributes_matcher.rb
|
42
|
+
- lib/pundit/matchers/permit_only_actions_matcher.rb
|
66
43
|
- lib/pundit/matchers/utils/policy_info.rb
|
67
44
|
homepage: https://github.com/punditcommunity/pundit-matchers
|
68
45
|
licenses:
|
@@ -80,9 +57,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
80
57
|
version: '3.0'
|
81
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
59
|
requirements:
|
83
|
-
- - "
|
60
|
+
- - ">"
|
84
61
|
- !ruby/object:Gem::Version
|
85
|
-
version:
|
62
|
+
version: 1.3.1
|
86
63
|
requirements: []
|
87
64
|
rubygems_version: 3.4.12
|
88
65
|
signing_key:
|
@@ -1,39 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Pundit
|
4
|
-
module Matchers
|
5
|
-
module Utils
|
6
|
-
module AllActions
|
7
|
-
# Parent class for specific all_action matcher. Should not be used directly.
|
8
|
-
#
|
9
|
-
# Expects methods in child class:
|
10
|
-
# * actual_actions - list of actions which actually matches expected type.
|
11
|
-
class ActionsMatcher
|
12
|
-
attr_reader :policy_info
|
13
|
-
|
14
|
-
def initialize(policy)
|
15
|
-
@policy_info = PolicyInfo.new(policy)
|
16
|
-
end
|
17
|
-
|
18
|
-
def match?
|
19
|
-
missed_expected_actions.empty?
|
20
|
-
end
|
21
|
-
|
22
|
-
def missed_expected_actions
|
23
|
-
@missed_expected_actions ||= expected_actions - actual_actions
|
24
|
-
end
|
25
|
-
|
26
|
-
def policy
|
27
|
-
policy_info.policy
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def expected_actions
|
33
|
-
policy_info.actions
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|