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.
- checksums.yaml +4 -4
- data/lib/pundit/matchers.rb +78 -34
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebd49115f1fcd116573587c08ba1e87701d69ab1
|
4
|
+
data.tar.gz: 20875c30d34d115d91918ad6031bb3b7b5bf899e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4634c52a265faacfcf42971b28994d585b1c33233f691c21a4499afa8d4ceb36170b2563702477d9caa1eff5560999c3ca05444f9db8496045d3b97981967c5
|
7
|
+
data.tar.gz: f301a9c3f2cc38fcb4dc5c71f46ac07462ad0022c8b5bc5f7822687274b27204e7a66e22245aeb9e24a0ddb9c40e741ea0918131246f3f9888e616d42424f1b0
|
data/lib/pundit/matchers.rb
CHANGED
@@ -98,44 +98,66 @@ module Pundit
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
RSpec::Matchers.define :forbid_mass_assignment_of do |
|
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
|
104
|
-
|
105
|
-
|
106
|
-
|
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
|
116
|
-
|
117
|
-
|
118
|
-
|
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}
|
123
|
-
"#{
|
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
|
131
|
-
|
132
|
-
|
133
|
-
|
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}
|
138
|
-
"#{
|
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 |
|
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
|
243
|
-
|
244
|
-
|
245
|
-
|
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
|
255
|
-
|
256
|
-
|
257
|
-
|
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}
|
262
|
-
"#{
|
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
|
270
|
-
|
271
|
-
|
272
|
-
|
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}
|
277
|
-
"#{
|
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.
|
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-
|
11
|
+
date: 2018-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|