pundit-matchers 1.2.2 → 1.2.3

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 +253 -10
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a89bba36d72696d1f5a0f85f8d999a09beff0d81
4
- data.tar.gz: 8b58079cdb9cf715ca168c05474070e4edef71df
3
+ metadata.gz: 57370d1383a4f4ae8bda97736537fc8fa39ad896
4
+ data.tar.gz: 397aebb87f9d1a2b3c31944fff1982f4b22ce7c7
5
5
  SHA512:
6
- metadata.gz: def002efd1b4c9092b823e641b144972528acf8e3ed30dc220245bea7130fd9e1a026c6b4b3c6d032a02d708b359d237ea8434017a58994d8078ed64b3b0a44f
7
- data.tar.gz: 5fc730fad6af6caaf65b1be2d14c7c89056e9f4b81f3224dfaa576b04483a33aa41dd682eac2f8790a1fb59c708ad8f8aa57758a3188ea3942232789d2b114c1
6
+ metadata.gz: ea4681e33c44a1f372c37c3f18995277cd0c2808c8d6458024e364cf285fb30d833702b4d9aad496166458072dd3225716fb16b1a3e7579cb76dd306abc964a7
7
+ data.tar.gz: 2e77a4a17c8dfbb546d81f4ed2b5f01aa4b207f9fa3e549deebeb1eca81cbe6a19c9f9898df777516e68fa7dd00a55454ef701f24ebba856c2eced6f3f80eb53
@@ -1,15 +1,258 @@
1
1
  require 'rspec/core'
2
2
 
3
- require 'pundit/matchers/forbid_action'
4
- require 'pundit/matchers/forbid_actions'
5
- require 'pundit/matchers/forbid_edit_and_update_actions'
6
- require 'pundit/matchers/forbid_mass_assignment_of'
7
- require 'pundit/matchers/forbid_new_and_create_actions'
8
- require 'pundit/matchers/permit_action'
9
- require 'pundit/matchers/permit_actions'
10
- require 'pundit/matchers/permit_edit_and_update_actions'
11
- require 'pundit/matchers/permit_mass_assignment_of'
12
- require 'pundit/matchers/permit_new_and_create_actions'
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|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pundit-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Alley