pundit-matchers 1.2.2 → 1.2.3
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.rb +253 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57370d1383a4f4ae8bda97736537fc8fa39ad896
|
4
|
+
data.tar.gz: 397aebb87f9d1a2b3c31944fff1982f4b22ce7c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea4681e33c44a1f372c37c3f18995277cd0c2808c8d6458024e364cf285fb30d833702b4d9aad496166458072dd3225716fb16b1a3e7579cb76dd306abc964a7
|
7
|
+
data.tar.gz: 2e77a4a17c8dfbb546d81f4ed2b5f01aa4b207f9fa3e549deebeb1eca81cbe6a19c9f9898df777516e68fa7dd00a55454ef701f24ebba856c2eced6f3f80eb53
|
data/lib/pundit/matchers.rb
CHANGED
@@ -1,15 +1,258 @@
|
|
1
1
|
require 'rspec/core'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
module Pundit
|
4
|
+
module Matchers
|
5
|
+
RSpec::Matchers.define :forbid_action do |action|
|
6
|
+
match do |policy|
|
7
|
+
!policy.public_send("#{action}?")
|
8
|
+
end
|
9
|
+
|
10
|
+
failure_message do |policy|
|
11
|
+
"#{policy.class} does not forbid #{action} on #{policy.record} for " \
|
12
|
+
"#{policy.user.inspect}."
|
13
|
+
end
|
14
|
+
|
15
|
+
failure_message_when_negated do |policy|
|
16
|
+
"#{policy.class} does not permit #{action} on #{policy.record} for " \
|
17
|
+
"#{policy.user.inspect}."
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
RSpec::Matchers.define :forbid_actions do |actions|
|
23
|
+
match do |policy|
|
24
|
+
return false if actions.count < 2
|
25
|
+
actions.each do |action|
|
26
|
+
return false if policy.public_send("#{action}?")
|
27
|
+
end
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
zero_actions_failure_message = 'At least two actions must be ' \
|
32
|
+
'specified when using the forbid_actions matcher.'
|
33
|
+
|
34
|
+
one_action_failure_message = 'More than one action must be specified ' \
|
35
|
+
'when using the forbid_actions matcher. To test a single action, use ' \
|
36
|
+
'forbid_action instead.'
|
37
|
+
|
38
|
+
failure_message do |policy|
|
39
|
+
case actions.count
|
40
|
+
when 0
|
41
|
+
zero_actions_failure_message
|
42
|
+
when 1
|
43
|
+
one_action_failure_message
|
44
|
+
else
|
45
|
+
"#{policy.class} does not forbid #{actions} on #{policy.record} " \
|
46
|
+
"for #{policy.user.inspect}."
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
failure_message_when_negated do |policy|
|
51
|
+
case actions.count
|
52
|
+
when 0
|
53
|
+
zero_actions_failure_message
|
54
|
+
when 1
|
55
|
+
one_action_failure_message
|
56
|
+
else
|
57
|
+
"#{policy.class} does not permit #{actions} on #{policy.record} " \
|
58
|
+
"for #{policy.user.inspect}."
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
RSpec::Matchers.define :forbid_edit_and_update_actions do
|
64
|
+
match do |policy|
|
65
|
+
!policy.edit? && !policy.update?
|
66
|
+
end
|
67
|
+
|
68
|
+
failure_message do |policy|
|
69
|
+
"#{policy.class} does not forbid the edit or update action on " \
|
70
|
+
"#{policy.record} for #{policy.user.inspect}."
|
71
|
+
end
|
72
|
+
|
73
|
+
failure_message_when_negated do |policy|
|
74
|
+
"#{policy.class} does not permit the edit or update action on " \
|
75
|
+
"#{policy.record} for #{policy.user.inspect}."
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
RSpec::Matchers.define :forbid_mass_assignment_of do |attribute|
|
80
|
+
match do |policy|
|
81
|
+
if defined? @action
|
82
|
+
!policy.send("permitted_attributes_for_#{@action}").include? attribute
|
83
|
+
else
|
84
|
+
!policy.permitted_attributes.include? attribute
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
chain :for_action do |action|
|
89
|
+
@action = action
|
90
|
+
end
|
91
|
+
|
92
|
+
failure_message do |policy|
|
93
|
+
if defined? @action
|
94
|
+
"#{policy.class} does not forbid the mass assignment of the " \
|
95
|
+
"#{attribute} attribute, when authorising the #{@action} action, " \
|
96
|
+
"for #{policy.user.inspect}."
|
97
|
+
else
|
98
|
+
"#{policy.class} does not forbid the mass assignment of the " \
|
99
|
+
"#{attribute} attribute for #{policy.user.inspect}."
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
failure_message_when_negated do |policy|
|
104
|
+
if defined? @action
|
105
|
+
"#{policy.class} does not permit the mass assignment of the " \
|
106
|
+
"#{attribute} attribute, when authorising the #{@action} action, " \
|
107
|
+
"for #{policy.user.inspect}."
|
108
|
+
else
|
109
|
+
"#{policy.class} does not permit the mass assignment of the " \
|
110
|
+
"#{attribute} attribute for #{policy.user.inspect}."
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
RSpec::Matchers.define :forbid_new_and_create_actions do
|
116
|
+
match do |policy|
|
117
|
+
!policy.new? && !policy.create?
|
118
|
+
end
|
119
|
+
|
120
|
+
failure_message do |policy|
|
121
|
+
"#{policy.class} does not forbid the new or create action on " \
|
122
|
+
"#{policy.record} for #{policy.user.inspect}."
|
123
|
+
end
|
124
|
+
|
125
|
+
failure_message_when_negated do |policy|
|
126
|
+
"#{policy.class} does not permit the new or create action on " \
|
127
|
+
"#{policy.record} for #{policy.user.inspect}."
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
RSpec::Matchers.define :permit_action do |action|
|
132
|
+
match do |policy|
|
133
|
+
policy.public_send("#{action}?")
|
134
|
+
end
|
135
|
+
|
136
|
+
failure_message do |policy|
|
137
|
+
"#{policy.class} does not permit #{action} on #{policy.record} for " \
|
138
|
+
"#{policy.user.inspect}."
|
139
|
+
end
|
140
|
+
|
141
|
+
failure_message_when_negated do |policy|
|
142
|
+
"#{policy.class} does not forbid #{action} on #{policy.record} for " \
|
143
|
+
"#{policy.user.inspect}."
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
RSpec::Matchers.define :permit_actions do |actions|
|
148
|
+
match do |policy|
|
149
|
+
return false if actions.count < 2
|
150
|
+
actions.each do |action|
|
151
|
+
return false unless policy.public_send("#{action}?")
|
152
|
+
end
|
153
|
+
true
|
154
|
+
end
|
155
|
+
|
156
|
+
zero_actions_failure_message = 'At least two actions must be ' \
|
157
|
+
'specified when using the permit_actions matcher.'
|
158
|
+
|
159
|
+
one_action_failure_message = 'More than one action must be specified ' \
|
160
|
+
'when using the permit_actions matcher. To test a single action, use ' \
|
161
|
+
'permit_action instead.'
|
162
|
+
|
163
|
+
failure_message do |policy|
|
164
|
+
case actions.count
|
165
|
+
when 0
|
166
|
+
zero_actions_failure_message
|
167
|
+
when 1
|
168
|
+
one_action_failure_message
|
169
|
+
else
|
170
|
+
"#{policy.class} does not permit #{actions} on #{policy.record} " \
|
171
|
+
"for #{policy.user.inspect}."
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
failure_message_when_negated do |policy|
|
176
|
+
case actions.count
|
177
|
+
when 0
|
178
|
+
zero_actions_failure_message
|
179
|
+
when 1
|
180
|
+
one_action_failure_message
|
181
|
+
else
|
182
|
+
"#{policy.class} does not forbid #{actions} on #{policy.record} " \
|
183
|
+
"for #{policy.user.inspect}."
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
RSpec::Matchers.define :permit_edit_and_update_actions do
|
189
|
+
match do |policy|
|
190
|
+
policy.edit? && policy.update?
|
191
|
+
end
|
192
|
+
|
193
|
+
failure_message do |policy|
|
194
|
+
"#{policy.class} does not permit the edit or update action on " \
|
195
|
+
"#{policy.record} for #{policy.user.inspect}."
|
196
|
+
end
|
197
|
+
|
198
|
+
failure_message_when_negated do |policy|
|
199
|
+
"#{policy.class} does not forbid the edit or update action on " \
|
200
|
+
"#{policy.record} for #{policy.user.inspect}."
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
RSpec::Matchers.define :permit_mass_assignment_of do |attribute|
|
205
|
+
match do |policy|
|
206
|
+
if defined? @action
|
207
|
+
policy.send("permitted_attributes_for_#{@action}").include? attribute
|
208
|
+
else
|
209
|
+
policy.permitted_attributes.include? attribute
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
chain :for_action do |action|
|
214
|
+
@action = action
|
215
|
+
end
|
216
|
+
|
217
|
+
failure_message do |policy|
|
218
|
+
if defined? @action
|
219
|
+
"#{policy.class} does not permit the mass assignment of the " \
|
220
|
+
"#{attribute} attribute, when authorising the #{@action} action, " \
|
221
|
+
"for #{policy.user.inspect}."
|
222
|
+
else
|
223
|
+
"#{policy.class} does not permit the mass assignment of the " \
|
224
|
+
"#{attribute} attribute for #{policy.user.inspect}."
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
failure_message_when_negated do |policy|
|
229
|
+
if defined? @action
|
230
|
+
"#{policy.class} does not forbid the mass assignment of the " \
|
231
|
+
"#{attribute} attribute, when authorising the #{@action} action, " \
|
232
|
+
"for #{policy.user.inspect}."
|
233
|
+
else
|
234
|
+
"#{policy.class} does not forbid the mass assignment of the " \
|
235
|
+
"#{attribute} attribute for #{policy.user.inspect}."
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
RSpec::Matchers.define :permit_new_and_create_actions do
|
241
|
+
match do |policy|
|
242
|
+
policy.new? && policy.create?
|
243
|
+
end
|
244
|
+
|
245
|
+
failure_message do |policy|
|
246
|
+
"#{policy.class} does not permit the new or create action on " \
|
247
|
+
"#{policy.record} for #{policy.user.inspect}."
|
248
|
+
end
|
249
|
+
|
250
|
+
failure_message_when_negated do |policy|
|
251
|
+
"#{policy.class} does not forbid the new or create action on " \
|
252
|
+
"#{policy.record} for #{policy.user.inspect}."
|
253
|
+
end
|
254
|
+
end
|
255
|
+
end
|
13
256
|
|
14
257
|
if defined?(Pundit)
|
15
258
|
RSpec.configure do |config|
|