pundit-matchers 2.0.0 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f3b354daf4a7619dc0fe63bc65984f0a8390b5fc9a3a2b1c7d2129cb5d12bf0
4
- data.tar.gz: c4a47b2387ddcf8541c7fe8f7e7b98067263d312db6a9d3718621d5fb446b115
3
+ metadata.gz: '013960778421a77229a9e50388f5cb7073cae3c60024b482f48c39431de063af'
4
+ data.tar.gz: 12a70d9a7ffdf1df5a2e584e555f3f934d36a971852605afa611c033a07c14dc
5
5
  SHA512:
6
- metadata.gz: adac619cd1badd3d0f83ccb535a45f4633f199badb12c6daba7d4b9a2be5f79db321f6268645b6a66d6776f4b048fb2096cb3dd74c850be64a9caf56d3d9bce2
7
- data.tar.gz: 25fb9deee8b3779156ea4d6ba26b71a5af47133bd2d4fdb594fe119e2803d85265027c7c8044a2c2ce364d04f5933330e752857617a4fd598c3596f1c2d8e2b2
6
+ metadata.gz: 4c00b1be800433a24203f2d964a810eaa7445ed98934598726e568b727db2ad6333886d1d7dffe56196a1bb445a982c6d17ea96cda737cca5a5ca7e801445ada
7
+ data.tar.gz: 0fb59915496d8b6807b01b84ce6f70c2c350e5dd6db00851c1e013211fb6ab41c6a1cc2f21c11f0a10107f116dadd666ff71228bcf06eee1ed706a90b56d9550
@@ -14,7 +14,7 @@ module Pundit
14
14
  module ErrorMessageFormatter
15
15
  def message
16
16
  "#{policy_name} expected to have all actions #{expected_kind}, " \
17
- "but #{mismatches_are(missed_expected_actions)} #{opposite_kind}"
17
+ "but #{mismatches_are(missed_expected_actions)} #{opposite_kind}"
18
18
  end
19
19
 
20
20
  private
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pundit
4
+ module Matchers
5
+ module Utils
6
+ module OnlyActions
7
+ # Parent class for specific only_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, :expected_actions
13
+
14
+ def initialize(policy, expected_actions)
15
+ @policy_info = PolicyInfo.new(policy)
16
+ @expected_actions = expected_actions
17
+ end
18
+
19
+ def match?
20
+ missed_expected_actions.empty? &&
21
+ actual_actions.sort == expected_actions.sort
22
+ end
23
+
24
+ def unexpected_actions
25
+ @unexpected_actions ||= actual_actions - expected_actions
26
+ end
27
+
28
+ def missed_expected_actions
29
+ @missed_expected_actions ||= expected_actions - actual_actions
30
+ end
31
+
32
+ def policy
33
+ policy_info.policy
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pundit
4
+ module Matchers
5
+ module Utils
6
+ module OnlyActions
7
+ # Adds #message method which generates failed assertion message
8
+ # for *_only_actions matchers.
9
+ #
10
+ # Expects methods to be defined:
11
+ # * matcher - instance which has `OnlyActions::ActionsMatcher` as a parent class
12
+ # * expected_kind - string with expected actions type (can be "forbidden" or "permitted")
13
+ # * opposite_kind - string with oposite then expected actions type (can be "permitted" or "forbidden")
14
+ module ErrorMessageFormatter
15
+ def message
16
+ "#{policy_name} expected to have only actions #{matcher.expected_actions} #{expected_kind}, but " \
17
+ "#{unless missed_expected_actions.empty?
18
+ "#{mismatches_are(missed_expected_actions)} #{opposite_kind} and "
19
+ end}" \
20
+ "#{mismatches_are(unexpected_actions)} #{expected_kind} too"
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :matcher, :expected_kind, :opposite_kind
26
+
27
+ def policy
28
+ matcher.policy
29
+ end
30
+
31
+ def unexpected_actions
32
+ matcher.unexpected_actions
33
+ end
34
+
35
+ def missed_expected_actions
36
+ matcher.missed_expected_actions
37
+ end
38
+
39
+ def policy_name
40
+ policy.class.name
41
+ end
42
+
43
+ def mismatches_are(mismatches)
44
+ if mismatches.count == 1
45
+ "#{mismatches} is"
46
+ else
47
+ "#{mismatches} are"
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'error_message_formatter'
4
+
5
+ module Pundit
6
+ module Matchers
7
+ module Utils
8
+ module OnlyActions
9
+ # Error message formatter for `forbid_only_actions` matcher.
10
+ class ForbiddenActionsErrorFormatter
11
+ include OnlyActions::ErrorMessageFormatter
12
+
13
+ def initialize(matcher)
14
+ @expected_kind = 'forbidden'
15
+ @opposite_kind = 'permitted'
16
+ @matcher = matcher
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :matcher, :expected_kind, :opposite_kind
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'actions_matcher'
4
+
5
+ module Pundit
6
+ module Matchers
7
+ module Utils
8
+ module OnlyActions
9
+ # Handles all the checks in `forbid_only_actions` matcher.
10
+ class ForbiddenActionsMatcher < OnlyActions::ActionsMatcher
11
+ private
12
+
13
+ def actual_actions
14
+ policy_info.forbidden_actions
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'error_message_formatter'
4
+
5
+ module Pundit
6
+ module Matchers
7
+ module Utils
8
+ module OnlyActions
9
+ # Error message formatter for `permit_only_actions` matcher.
10
+ class PermittedActionsErrorFormatter
11
+ include OnlyActions::ErrorMessageFormatter
12
+
13
+ def initialize(matcher)
14
+ @expected_kind = 'permitted'
15
+ @opposite_kind = 'forbidden'
16
+ @matcher = matcher
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :matcher, :expected_kind, :opposite_kind
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'actions_matcher'
4
+
5
+ module Pundit
6
+ module Matchers
7
+ module Utils
8
+ module OnlyActions
9
+ # Handles all the checks in `permit_only_actions` matcher.
10
+ class PermittedActionsMatcher < OnlyActions::ActionsMatcher
11
+ private
12
+
13
+ def actual_actions
14
+ policy_info.permitted_actions
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -14,7 +14,7 @@ module Pundit
14
14
  def actions
15
15
  @actions ||= begin
16
16
  policy_methods = @policy.public_methods - Object.instance_methods
17
- policy_methods.grep(/\?$/).map { |policy_method| policy_method.to_s.sub(/\?$/, '').to_sym }
17
+ policy_methods.grep(/\?$/).sort.map { |policy_method| policy_method.to_s.delete_suffix('?').to_sym }
18
18
  end
19
19
  end
20
20
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rspec/core'
2
4
 
3
5
  module Pundit
@@ -8,6 +10,11 @@ module Pundit
8
10
  require_relative 'matchers/utils/all_actions/permitted_actions_error_formatter'
9
11
  require_relative 'matchers/utils/all_actions/permitted_actions_matcher'
10
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'
17
+
11
18
  class Configuration
12
19
  attr_accessor :user_alias
13
20
 
@@ -25,27 +32,25 @@ module Pundit
25
32
  @configuration ||= Pundit::Matchers::Configuration.new
26
33
  end
27
34
  end
35
+ end
28
36
 
29
- RSpec::Matchers.define :forbid_action do |action, *args, **kwargs|
30
- match do |policy|
31
- if args.any?
32
- !policy.public_send("#{action}?", *args, **kwargs)
33
- else
34
- !policy.public_send("#{action}?", **kwargs)
35
- end
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)
36
43
  end
44
+ end
37
45
 
38
- failure_message do |policy|
39
- "#{policy.class} does not forbid #{action} for " +
40
- policy.public_send(Pundit::Matchers.configuration.user_alias)
41
- .inspect + '.'
42
- end
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
43
50
 
44
- failure_message_when_negated do |policy|
45
- "#{policy.class} does not permit #{action} for " +
46
- policy.public_send(Pundit::Matchers.configuration.user_alias)
47
- .inspect + '.'
48
- end
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}."
49
54
  end
50
55
  end
51
56
 
@@ -53,6 +58,7 @@ module Pundit
53
58
  actions.flatten!
54
59
  match do |policy|
55
60
  return false if actions.count < 1
61
+
56
62
  @allowed_actions = actions.select do |action|
57
63
  policy.public_send("#{action}?")
58
64
  end
@@ -62,16 +68,15 @@ module Pundit
62
68
  attr_reader :allowed_actions
63
69
 
64
70
  zero_actions_failure_message = 'At least one action must be ' \
65
- 'specified when using the forbid_actions matcher.'
71
+ 'specified when using the forbid_actions matcher.'
66
72
 
67
73
  failure_message do |policy|
68
74
  if actions.count.zero?
69
75
  zero_actions_failure_message
70
76
  else
71
- "#{policy.class} expected to forbid #{actions}, but allowed " \
72
- "#{allowed_actions} for " +
73
- policy.public_send(Pundit::Matchers.configuration.user_alias)
74
- .inspect + '.'
77
+ "#{policy.class} expected to forbid #{actions}, but permitted " \
78
+ "#{allowed_actions} for " \
79
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
75
80
  end
76
81
  end
77
82
 
@@ -80,9 +85,8 @@ module Pundit
80
85
  zero_actions_failure_message
81
86
  else
82
87
  "#{policy.class} expected to permit #{actions}, but forbade " \
83
- "#{allowed_actions} for " +
84
- policy.public_send(Pundit::Matchers.configuration.user_alias)
85
- .inspect + '.'
88
+ "#{allowed_actions} for " \
89
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
86
90
  end
87
91
  end
88
92
  end
@@ -93,21 +97,19 @@ module Pundit
93
97
  end
94
98
 
95
99
  failure_message do |policy|
96
- "#{policy.class} does not forbid the edit or update action for " +
97
- policy.public_send(Pundit::Matchers.configuration.user_alias)
98
- .inspect + '.'
100
+ "#{policy.class} does not forbid the edit or update action for " \
101
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
99
102
  end
100
103
 
101
104
  failure_message_when_negated do |policy|
102
- "#{policy.class} does not permit the edit or update action for " +
103
- policy.public_send(Pundit::Matchers.configuration.user_alias)
104
- .inspect + '.'
105
+ "#{policy.class} does not permit the edit or update action for " \
106
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
105
107
  end
106
108
  end
107
109
 
108
110
  RSpec::Matchers.define :forbid_mass_assignment_of do |attributes|
109
111
  # Map single object argument to an array, if necessary
110
- attributes = attributes.is_a?(Array) ? attributes : [attributes]
112
+ attributes = [attributes] unless attributes.is_a?(Array)
111
113
 
112
114
  match do |policy|
113
115
  return false if attributes.count < 1
@@ -130,7 +132,7 @@ module Pundit
130
132
  end
131
133
 
132
134
  zero_attributes_failure_message = 'At least one attribute must be ' \
133
- 'specified when using the forbid_mass_assignment_of matcher.'
135
+ 'specified when using the forbid_mass_assignment_of matcher.'
134
136
 
135
137
  failure_message do |policy|
136
138
  if attributes.count.zero?
@@ -138,16 +140,14 @@ module Pundit
138
140
  elsif defined? @action
139
141
  "#{policy.class} expected to forbid the mass assignment of the " \
140
142
  "attributes #{attributes} when authorising the #{@action} action, " \
141
- 'but allowed the mass assignment of the attributes ' \
142
- "#{allowed_attributes} for " +
143
- policy.public_send(Pundit::Matchers.configuration.user_alias)
144
- .inspect + '.'
143
+ 'but permitted the mass assignment of the attributes ' \
144
+ "#{allowed_attributes} for " \
145
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
145
146
  else
146
147
  "#{policy.class} expected to forbid the mass assignment of the " \
147
- "attributes #{attributes}, but allowed the mass assignment of " \
148
- "the attributes #{allowed_attributes} for " +
149
- policy.public_send(Pundit::Matchers.configuration.user_alias)
150
- .inspect + '.'
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
151
  end
152
152
  end
153
153
 
@@ -158,15 +158,13 @@ module Pundit
158
158
  "#{policy.class} expected to permit the mass assignment of the " \
159
159
  "attributes #{attributes} when authorising the #{@action} action, " \
160
160
  'but permitted the mass assignment of the attributes ' \
161
- "#{allowed_attributes} for " +
162
- policy.public_send(Pundit::Matchers.configuration.user_alias)
163
- .inspect + '.'
161
+ "#{allowed_attributes} for " \
162
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
164
163
  else
165
164
  "#{policy.class} expected to permit the mass assignment of the " \
166
165
  "attributes #{attributes}, but permitted the mass assignment of " \
167
- "the attributes #{allowed_attributes} for " +
168
- policy.public_send(Pundit::Matchers.configuration.user_alias)
169
- .inspect + '.'
166
+ "the attributes #{allowed_attributes} for " \
167
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
170
168
  end
171
169
  end
172
170
  end
@@ -177,15 +175,13 @@ module Pundit
177
175
  end
178
176
 
179
177
  failure_message do |policy|
180
- "#{policy.class} does not forbid the new or create action for " +
181
- policy.public_send(Pundit::Matchers.configuration.user_alias)
182
- .inspect + '.'
178
+ "#{policy.class} does not forbid the new or create action for " \
179
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
183
180
  end
184
181
 
185
182
  failure_message_when_negated do |policy|
186
- "#{policy.class} does not permit the new or create action for " +
187
- policy.public_send(Pundit::Matchers.configuration.user_alias)
188
- .inspect + '.'
183
+ "#{policy.class} does not permit the new or create action for " \
184
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
189
185
  end
190
186
  end
191
187
 
@@ -199,15 +195,13 @@ module Pundit
199
195
  end
200
196
 
201
197
  failure_message do |policy|
202
- "#{policy.class} does not permit #{action} for " +
203
- policy.public_send(Pundit::Matchers.configuration.user_alias)
204
- .inspect + '.'
198
+ "#{policy.class} does not permit #{action} for " \
199
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
205
200
  end
206
201
 
207
202
  failure_message_when_negated do |policy|
208
- "#{policy.class} does not forbid #{action} for " +
209
- policy.public_send(Pundit::Matchers.configuration.user_alias)
210
- .inspect + '.'
203
+ "#{policy.class} does not forbid #{action} for " \
204
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
211
205
  end
212
206
  end
213
207
 
@@ -215,6 +209,7 @@ module Pundit
215
209
  actions.flatten!
216
210
  match do |policy|
217
211
  return false if actions.count < 1
212
+
218
213
  @forbidden_actions = actions.reject do |action|
219
214
  policy.public_send("#{action}?")
220
215
  end
@@ -235,6 +230,7 @@ module Pundit
235
230
  tests would pass.'
236
231
 
237
232
  return true if actions.count < 1
233
+
238
234
  @forbidden_actions = actions.reject do |action|
239
235
  policy.public_send("#{action}?")
240
236
  end
@@ -244,16 +240,15 @@ module Pundit
244
240
  attr_reader :forbidden_actions
245
241
 
246
242
  zero_actions_failure_message = 'At least one action must be specified ' \
247
- 'when using the permit_actions matcher.'
243
+ 'when using the permit_actions matcher.'
248
244
 
249
245
  failure_message do |policy|
250
246
  if actions.count.zero?
251
247
  zero_actions_failure_message
252
248
  else
253
249
  "#{policy.class} expected to permit #{actions}, but forbade " \
254
- "#{forbidden_actions} for " +
255
- policy.public_send(Pundit::Matchers.configuration.user_alias)
256
- .inspect + '.'
250
+ "#{forbidden_actions} for " \
251
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
257
252
  end
258
253
  end
259
254
 
@@ -261,10 +256,9 @@ module Pundit
261
256
  if actions.count.zero?
262
257
  zero_actions_failure_message
263
258
  else
264
- "#{policy.class} expected to forbid #{actions}, but allowed " \
265
- "#{forbidden_actions} for " +
266
- policy.public_send(Pundit::Matchers.configuration.user_alias)
267
- .inspect + '.'
259
+ "#{policy.class} expected to forbid #{actions}, but permitted " \
260
+ "#{forbidden_actions} for " \
261
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
268
262
  end
269
263
  end
270
264
  end
@@ -275,21 +269,19 @@ module Pundit
275
269
  end
276
270
 
277
271
  failure_message do |policy|
278
- "#{policy.class} does not permit the edit or update action for " +
279
- policy.public_send(Pundit::Matchers.configuration.user_alias)
280
- .inspect + '.'
272
+ "#{policy.class} does not permit the edit or update action for " \
273
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
281
274
  end
282
275
 
283
276
  failure_message_when_negated do |policy|
284
- "#{policy.class} does not forbid the edit or update action for " +
285
- policy.public_send(Pundit::Matchers.configuration.user_alias)
286
- .inspect + '.'
277
+ "#{policy.class} does not forbid the edit or update action for " \
278
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
287
279
  end
288
280
  end
289
281
 
290
282
  RSpec::Matchers.define :permit_mass_assignment_of do |attributes|
291
283
  # Map single object argument to an array, if necessary
292
- attributes = attributes.is_a?(Array) ? attributes : [attributes]
284
+ attributes = [attributes] unless attributes.is_a?(Array)
293
285
 
294
286
  match do |policy|
295
287
  return false if attributes.count < 1
@@ -312,7 +304,7 @@ module Pundit
312
304
  end
313
305
 
314
306
  zero_attributes_failure_message = 'At least one attribute must be ' \
315
- 'specified when using the permit_mass_assignment_of matcher.'
307
+ 'specified when using the permit_mass_assignment_of matcher.'
316
308
 
317
309
  failure_message do |policy|
318
310
  if attributes.count.zero?
@@ -321,15 +313,13 @@ module Pundit
321
313
  "#{policy.class} expected to permit the mass assignment of the " \
322
314
  "attributes #{attributes} when authorising the #{@action} action, " \
323
315
  'but forbade the mass assignment of the attributes ' \
324
- "#{forbidden_attributes} for " +
325
- policy.public_send(Pundit::Matchers.configuration.user_alias)
326
- .inspect + '.'
316
+ "#{forbidden_attributes} for " \
317
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
327
318
  else
328
319
  "#{policy.class} expected to permit the mass assignment of the " \
329
320
  "attributes #{attributes}, but forbade the mass assignment of the " \
330
- "attributes #{forbidden_attributes} for " +
331
- policy.public_send(Pundit::Matchers.configuration.user_alias)
332
- .inspect + '.'
321
+ "attributes #{forbidden_attributes} for " \
322
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
333
323
  end
334
324
  end
335
325
 
@@ -340,15 +330,13 @@ module Pundit
340
330
  "#{policy.class} expected to forbid the mass assignment of the " \
341
331
  "attributes #{attributes} when authorising the #{@action} action, " \
342
332
  'but forbade the mass assignment of the attributes ' \
343
- "#{forbidden_attributes} for " +
344
- policy.public_send(Pundit::Matchers.configuration.user_alias)
345
- .inspect + '.'
333
+ "#{forbidden_attributes} for " \
334
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
346
335
  else
347
336
  "#{policy.class} expected to forbid the mass assignment of the " \
348
337
  "attributes #{attributes}, but forbade the mass assignment of the " \
349
- "attributes #{forbidden_attributes} for " +
350
- policy.public_send(Pundit::Matchers.configuration.user_alias)
351
- .inspect + '.'
338
+ "attributes #{forbidden_attributes} for " \
339
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
352
340
  end
353
341
  end
354
342
  end
@@ -359,15 +347,13 @@ module Pundit
359
347
  end
360
348
 
361
349
  failure_message do |policy|
362
- "#{policy.class} does not permit the new or create action for " +
363
- policy.public_send(Pundit::Matchers.configuration.user_alias)
364
- .inspect + '.'
350
+ "#{policy.class} does not permit the new or create action for " \
351
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
365
352
  end
366
353
 
367
354
  failure_message_when_negated do |policy|
368
- "#{policy.class} does not forbid the new or create action for " +
369
- policy.public_send(Pundit::Matchers.configuration.user_alias)
370
- .inspect + '.'
355
+ "#{policy.class} does not forbid the new or create action for " \
356
+ "#{policy.public_send(Pundit::Matchers.configuration.user_alias).inspect}."
371
357
  end
372
358
  end
373
359
 
@@ -383,6 +369,18 @@ module Pundit
383
369
  end
384
370
  end
385
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
+
386
384
  RSpec::Matchers.define :forbid_all_actions do
387
385
  match do |policy|
388
386
  @matcher = Pundit::Matchers::Utils::AllActions::ForbiddenActionsMatcher.new(policy)
@@ -394,6 +392,18 @@ module Pundit
394
392
  formatter.message
395
393
  end
396
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
406
+ end
397
407
  end
398
408
 
399
409
  if defined?(Pundit)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pundit-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.2.0
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-04-16 00:00:00.000000000 Z
11
+ date: 2023-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails
@@ -57,11 +57,18 @@ files:
57
57
  - lib/pundit/matchers/utils/all_actions/forbidden_actions_matcher.rb
58
58
  - lib/pundit/matchers/utils/all_actions/permitted_actions_error_formatter.rb
59
59
  - lib/pundit/matchers/utils/all_actions/permitted_actions_matcher.rb
60
+ - lib/pundit/matchers/utils/only_actions/actions_matcher.rb
61
+ - lib/pundit/matchers/utils/only_actions/error_message_formatter.rb
62
+ - lib/pundit/matchers/utils/only_actions/forbidden_actions_error_formatter.rb
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
60
66
  - lib/pundit/matchers/utils/policy_info.rb
61
- homepage: http://github.com/punditcommunity/pundit-matchers
67
+ homepage: https://github.com/punditcommunity/pundit-matchers
62
68
  licenses:
63
69
  - MIT
64
- metadata: {}
70
+ metadata:
71
+ rubygems_mfa_required: 'true'
65
72
  post_install_message:
66
73
  rdoc_options: []
67
74
  require_paths:
@@ -70,14 +77,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
70
77
  requirements:
71
78
  - - ">="
72
79
  - !ruby/object:Gem::Version
73
- version: '0'
80
+ version: '3.0'
74
81
  required_rubygems_version: !ruby/object:Gem::Requirement
75
82
  requirements:
76
83
  - - ">="
77
84
  - !ruby/object:Gem::Version
78
85
  version: '0'
79
86
  requirements: []
80
- rubygems_version: 3.3.7
87
+ rubygems_version: 3.4.12
81
88
  signing_key:
82
89
  specification_version: 4
83
90
  summary: RSpec matchers for Pundit policies