forked from thoughtbot/shoulda-matchers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permit_matcher.rb
394 lines (341 loc) · 10.6 KB
/
permit_matcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
require 'delegate'
begin
require 'strong_parameters'
rescue LoadError
end
require 'active_support/hash_with_indifferent_access'
module Shoulda
module Matchers
module ActionController
# The `permit` matcher tests that an action in your controller receives a
# whitelist of parameters using Rails' Strong Parameters feature
# (specifically that `permit` was called with the correct arguments).
#
# Here's an example:
#
# class UsersController < ApplicationController
# def create
# user = User.create(user_params)
# # ...
# end
#
# private
#
# def user_params
# params.require(:user).permit(
# :first_name,
# :last_name,
# :email,
# :password
# )
# end
# end
#
# # RSpec
# describe UsersController do
# it do
# should permit(:first_name, :last_name, :email, :password).
# for(:create).
# on(:user)
# end
# end
#
# # Test::Unit
# class UsersControllerTest < ActionController::TestCase
# should permit(:first_name, :last_name, :email, :password).
# for(:create).
# on(:user)
# end
#
# If your action requires query parameters in order to work, then you'll
# need to supply them:
#
# class UsersController < ApplicationController
# def update
# user = User.find(params[:id])
#
# if user.update_attributes(user_params)
# # ...
# else
# # ...
# end
# end
#
# private
#
# def user_params
# params.require(:user).permit(
# :first_name,
# :last_name,
# :email,
# :password
# )
# end
# end
#
# # RSpec
# describe UsersController do
# before do
# create(:user, id: 1)
# end
#
# it do
# should permit(:first_name, :last_name, :email, :password).
# for(:update, params: { id: 1 }).
# on(:user)
# end
# end
#
# # Test::Unit
# class UsersControllerTest < ActionController::TestCase
# setup do
# create(:user, id: 1)
# end
#
# should permit(:first_name, :last_name, :email, :password).
# for(:update, params: { id: 1 }).
# on(:user)
# end
#
# Finally, if you have an action that isn't one of the seven resourceful
# actions, then you'll need to provide the HTTP verb that it responds to:
#
# Rails.application.routes.draw do
# resources :users do
# member do
# put :toggle
# end
# end
# end
#
# class UsersController < ApplicationController
# def toggle
# user = User.find(params[:id])
#
# if user.update_attributes(user_params)
# # ...
# else
# # ...
# end
# end
#
# private
#
# def user_params
# params.require(:user).permit(:activated)
# end
# end
#
# # RSpec
# describe UsersController do
# before do
# create(:user, id: 1)
# end
#
# it do
# should permit(:activated).
# for(:toggle, params: { id: 1 }, verb: :put).
# on(:user)
# end
# end
#
# # Test::Unit
# class UsersControllerTest < ActionController::TestCase
# setup do
# create(:user, id: 1)
# end
#
# should permit(:activated).
# for(:toggle, params: { id: 1 }, verb: :put).
# on(:user)
# end
#
# @return [PermitMatcher]
#
def permit(*params)
PermitMatcher.new(params).in_context(self)
end
# @private
class PermitMatcher
attr_writer :stubbed_params
def initialize(expected_permitted_params)
@expected_permitted_params = expected_permitted_params
@action = nil
@verb = nil
@request_params = {}
@subparameter = nil
@parameters_doubles = ParametersDoubles.new
end
def for(action, options = {})
@action = action
@verb = options.fetch(:verb, default_verb)
@request_params = options.fetch(:params, {})
self
end
def add_params(params)
request_params.merge!(params)
self
end
def on(subparameter)
@subparameter = subparameter
@parameters_doubles = SliceOfParametersDoubles.new(subparameter)
self
end
def in_context(context)
@context = context
self
end
def description
"(on #{verb.upcase} ##{action}) " + expectation
end
def matches?(controller)
@controller = controller
ensure_action_and_verb_present!
parameters_doubles.register
Doublespeak.with_doubles_activated do
context.__send__(verb, action, request_params)
end
unpermitted_params.empty?
end
def failure_message
"Expected #{verb.upcase} ##{action} to #{expectation},\nbut #{reality}."
end
alias failure_message_for_should failure_message
def failure_message_when_negated
"Expected #{verb.upcase} ##{action} not to #{expectation},\nbut it did."
end
alias failure_message_for_should_not failure_message_when_negated
protected
attr_reader :controller, :double_collections_by_param, :action, :verb,
:request_params, :expected_permitted_params, :context, :subparameter,
:parameters_doubles
def expectation
message = 'restrict parameters '
if subparameter
message << " for #{subparameter.inspect}"
end
message << 'to ' + format_param_names(expected_permitted_params)
message
end
def reality
if actual_permitted_params.empty?
'it did not restrict any parameters'
else
'the restricted parameters were ' +
format_param_names(actual_permitted_params) +
' instead'
end
end
def format_param_names(param_names)
param_names.map(&:inspect).to_sentence
end
def actual_permitted_params
parameters_doubles.permitted_params
end
def permit_called?
actual_permitted_params.any?
end
def unpermitted_params
expected_permitted_params - actual_permitted_params
end
def verified_permitted_params
expected_permitted_params & actual_permitted_params
end
def ensure_action_and_verb_present!
if action.blank?
raise ActionNotDefinedError
end
if verb.blank?
raise VerbNotDefinedError
end
end
def default_verb
case action
when :create then :post
when :update then RailsShim.verb_for_update
end
end
def param_names_as_sentence
expected_permitted_params.map(&:inspect).to_sentence
end
# @private
class ParametersDoubles
def self.permitted_params_within(double_collection)
double_collection.calls_to(:permit).map(&:args).flatten
end
def initialize
klass = ::ActionController::Parameters
@double_collection = Doublespeak.double_collection_for(klass)
end
def register
double_collection.register_proxy(:permit)
end
def permitted_params
ParametersDoubles.permitted_params_within(double_collection)
end
protected
attr_reader :double_collection
end
# @private
class SliceOfParametersDoubles
TOP_LEVEL = Object.new
def initialize(subparameter)
klass = ::ActionController::Parameters
@subparameter = subparameter
@double_collections_by_param = {
TOP_LEVEL => Doublespeak.double_collection_for(klass)
}
end
def register
top_level_collection = double_collections_by_param[TOP_LEVEL]
double_permit_on(top_level_collection)
double_require_on(top_level_collection)
end
def permitted_params
if double_collections_by_param.key?(subparameter)
ParametersDoubles.permitted_params_within(
double_collections_by_param[subparameter]
)
else
[]
end
end
protected
attr_reader :subparameter, :double_collections_by_param
private
def double_permit_on(double_collection)
double_collection.register_proxy(:permit)
end
def double_require_on(double_collection)
double_collections_by_param = @double_collections_by_param
require_double = double_collection.register_proxy(:require)
require_double.to_return do |call|
param_name = call.args.first
params = call.return_value
double_collections_by_param[param_name] ||=
double_permit_against(params)
end
end
def double_permit_against(params)
klass = params.singleton_class
Doublespeak.double_collection_for(klass).tap do |double_collection|
double_permit_on(double_collection)
end
end
end
# @private
class ActionNotDefinedError < StandardError
def message
'You must specify the controller action using the #for method.'
end
end
# @private
class VerbNotDefinedError < StandardError
def message
'You must specify an HTTP verb when using a non-RESTful action. For example: for(:authorize, verb: :post)'
end
end
end
end
end
end