pundit-matchers 1.5.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pundit/matchers.rb +78 -34
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a107f9850cec3d77bce20866a01785d61e2504e
4
- data.tar.gz: 1e9e2002a82f622e3355f8c138ee0c35fe4af121
3
+ metadata.gz: ebd49115f1fcd116573587c08ba1e87701d69ab1
4
+ data.tar.gz: 20875c30d34d115d91918ad6031bb3b7b5bf899e
5
5
  SHA512:
6
- metadata.gz: 19fac269273c7411d7c0bd13e02ac8cbb97993b86f8783f7e5336c0edd4533529a970500b3182f430e00094dd24804c59db907eeb0af10af10058eb88bb7eff1
7
- data.tar.gz: 3ca539cd281d580845c05b4d41c3b3ec0cdcbd218b5aa72dba52379b049329e6e38360f1014feba0f162e96c4a8c2e38f98e70b85d1df51fe0b8b34256153bc2
6
+ metadata.gz: c4634c52a265faacfcf42971b28994d585b1c33233f691c21a4499afa8d4ceb36170b2563702477d9caa1eff5560999c3ca05444f9db8496045d3b97981967c5
7
+ data.tar.gz: f301a9c3f2cc38fcb4dc5c71f46ac07462ad0022c8b5bc5f7822687274b27204e7a66e22245aeb9e24a0ddb9c40e741ea0918131246f3f9888e616d42424f1b0
@@ -98,44 +98,66 @@ module Pundit
98
98
  end
99
99
  end
100
100
 
101
- RSpec::Matchers.define :forbid_mass_assignment_of do |attribute|
101
+ RSpec::Matchers.define :forbid_mass_assignment_of do |attributes|
102
+ # Map single object argument to an array, if necessary
103
+ attributes = attributes.is_a?(Array) ? attributes : [attributes]
104
+
102
105
  match do |policy|
103
- if defined? @action
104
- !policy.send("permitted_attributes_for_#{@action}").include? attribute
105
- else
106
- !policy.permitted_attributes.include? attribute
106
+ return false if attributes.count < 1
107
+
108
+ @allowed_attributes = attributes.select do |attribute|
109
+ if defined? @action
110
+ policy.send("permitted_attributes_for_#{@action}").include? attribute
111
+ else
112
+ policy.permitted_attributes.include? attribute
113
+ end
107
114
  end
115
+
116
+ @allowed_attributes.empty?
108
117
  end
109
118
 
119
+ attr_reader :allowed_attributes
120
+
110
121
  chain :for_action do |action|
111
122
  @action = action
112
123
  end
113
124
 
125
+ zero_attributes_failure_message = 'At least one attribute must be ' \
126
+ 'specified when using the forbid_mass_assignment_of matcher.'
127
+
114
128
  failure_message do |policy|
115
- if defined? @action
116
- "#{policy.class} does not forbid the mass assignment of the " \
117
- "#{attribute} attribute, when authorising the #{@action} action, " \
118
- 'for ' +
129
+ if attributes.count.zero?
130
+ zero_attributes_failure_message
131
+ elsif defined? @action
132
+ "#{policy.class} expected to forbid the mass assignment of the " \
133
+ "attributes #{attributes} when authorising the #{@action} action, " \
134
+ 'but allowed the mass assignment of the attributes ' \
135
+ "#{allowed_attributes} for " +
119
136
  policy.public_send(Pundit::Matchers.configuration.user_alias)
120
137
  .inspect + '.'
121
138
  else
122
- "#{policy.class} does not forbid the mass assignment of the " \
123
- "#{attribute} attribute for " +
139
+ "#{policy.class} expected to forbid the mass assignment of the " \
140
+ "attributes #{attributes}, but allowed the mass assignment of " \
141
+ "the attributes #{allowed_attributes} for " +
124
142
  policy.public_send(Pundit::Matchers.configuration.user_alias)
125
143
  .inspect + '.'
126
144
  end
127
145
  end
128
146
 
129
147
  failure_message_when_negated do |policy|
130
- if defined? @action
131
- "#{policy.class} does not permit the mass assignment of the " \
132
- "#{attribute} attribute, when authorising the #{@action} action, " \
133
- 'for ' +
148
+ if attributes.count.zero?
149
+ zero_attributes_failure_message
150
+ elsif defined? @action
151
+ "#{policy.class} expected to permit the mass assignment of the " \
152
+ "attributes #{attributes} when authorising the #{@action} action, " \
153
+ 'but permitted the mass assignment of the attributes ' \
154
+ "#{allowed_attributes} for " +
134
155
  policy.public_send(Pundit::Matchers.configuration.user_alias)
135
156
  .inspect + '.'
136
157
  else
137
- "#{policy.class} does not permit the mass assignment of the " \
138
- "#{attribute} attribute for " +
158
+ "#{policy.class} expected to permit the mass assignment of the " \
159
+ "attributes #{attributes}, but permitted the mass assignment of " \
160
+ "the attributes #{allowed_attributes} for " +
139
161
  policy.public_send(Pundit::Matchers.configuration.user_alias)
140
162
  .inspect + '.'
141
163
  end
@@ -237,44 +259,66 @@ module Pundit
237
259
  end
238
260
  end
239
261
 
240
- RSpec::Matchers.define :permit_mass_assignment_of do |attribute|
262
+ RSpec::Matchers.define :permit_mass_assignment_of do |attributes|
263
+ # Map single object argument to an array, if necessary
264
+ attributes = attributes.is_a?(Array) ? attributes : [attributes]
265
+
241
266
  match do |policy|
242
- if defined? @action
243
- policy.send("permitted_attributes_for_#{@action}").include? attribute
244
- else
245
- policy.permitted_attributes.include? attribute
267
+ return false if attributes.count < 1
268
+
269
+ @forbidden_attributes = attributes.select do |attribute|
270
+ if defined? @action
271
+ !policy.send("permitted_attributes_for_#{@action}").include? attribute
272
+ else
273
+ !policy.permitted_attributes.include? attribute
274
+ end
246
275
  end
276
+
277
+ @forbidden_attributes.empty?
247
278
  end
248
279
 
280
+ attr_reader :forbidden_attributes
281
+
249
282
  chain :for_action do |action|
250
283
  @action = action
251
284
  end
252
285
 
286
+ zero_attributes_failure_message = 'At least one attribute must be ' \
287
+ 'specified when using the permit_mass_assignment_of matcher.'
288
+
253
289
  failure_message do |policy|
254
- if defined? @action
255
- "#{policy.class} does not permit the mass assignment of the " \
256
- "#{attribute} attribute, when authorising the #{@action} action, " \
257
- 'for ' +
290
+ if attributes.count.zero?
291
+ zero_attributes_failure_message
292
+ elsif defined? @action
293
+ "#{policy.class} expected to permit the mass assignment of the " \
294
+ "attributes #{attributes} when authorising the #{@action} action, " \
295
+ 'but forbade the mass assignment of the attributes ' \
296
+ "#{forbidden_attributes} for " +
258
297
  policy.public_send(Pundit::Matchers.configuration.user_alias)
259
298
  .inspect + '.'
260
299
  else
261
- "#{policy.class} does not permit the mass assignment of the " \
262
- "#{attribute} attribute for " +
300
+ "#{policy.class} expected to permit the mass assignment of the " \
301
+ "attributes #{attributes}, but forbade the mass assignment of the " \
302
+ "attributes #{forbidden_attributes} for " +
263
303
  policy.public_send(Pundit::Matchers.configuration.user_alias)
264
304
  .inspect + '.'
265
305
  end
266
306
  end
267
307
 
268
308
  failure_message_when_negated do |policy|
269
- if defined? @action
270
- "#{policy.class} does not forbid the mass assignment of the " \
271
- "#{attribute} attribute, when authorising the #{@action} action, " \
272
- 'for ' +
309
+ if attributes.count.zero?
310
+ zero_attributes_failure_message
311
+ elsif defined? @action
312
+ "#{policy.class} expected to forbid the mass assignment of the " \
313
+ "attributes #{attributes} when authorising the #{@action} action, " \
314
+ 'but forbade the mass assignment of the attributes ' \
315
+ "#{forbidden_attributes} for " +
273
316
  policy.public_send(Pundit::Matchers.configuration.user_alias)
274
317
  .inspect + '.'
275
318
  else
276
- "#{policy.class} does not forbid the mass assignment of the " \
277
- "#{attribute} attribute for " +
319
+ "#{policy.class} expected to forbid the mass assignment of the " \
320
+ "attributes #{attributes}, but forbade the mass assignment of the " \
321
+ "attributes #{forbidden_attributes} for " +
278
322
  policy.public_send(Pundit::Matchers.configuration.user_alias)
279
323
  .inspect + '.'
280
324
  end
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: 1.5.1
4
+ version: 1.6.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: 2018-05-12 00:00:00.000000000 Z
11
+ date: 2018-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails