-
-
Notifications
You must be signed in to change notification settings - Fork 910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Permit matcher now supports subparameters #675
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,14 +38,16 @@ module ActionController | |
# describe UsersController do | ||
# it do | ||
# should permit(:first_name, :last_name, :email, :password). | ||
# for(:create) | ||
# for(:create). | ||
# on(:user) | ||
# end | ||
# end | ||
# | ||
# # Test::Unit | ||
# class UsersControllerTest < ActionController::TestCase | ||
# should permit(:first_name, :last_name, :email, :password). | ||
# for(:create) | ||
# for(:create). | ||
# on(:user) | ||
# end | ||
# | ||
# If your action requires query parameters in order to work, then you'll | ||
|
@@ -82,7 +84,8 @@ module ActionController | |
# | ||
# it do | ||
# should permit(:first_name, :last_name, :email, :password). | ||
# for(:update, params: { id: 1 }) | ||
# for(:update, params: { id: 1 }). | ||
# on(:user) | ||
# end | ||
# end | ||
# | ||
|
@@ -93,7 +96,8 @@ module ActionController | |
# end | ||
# | ||
# should permit(:first_name, :last_name, :email, :password). | ||
# for(:update, params: { id: 1 }) | ||
# for(:update, params: { id: 1 }). | ||
# on(:user) | ||
# end | ||
# | ||
# Finally, if you have an action that isn't one of the seven resourceful | ||
|
@@ -132,10 +136,9 @@ module ActionController | |
# end | ||
# | ||
# it do | ||
# should permit(:activated).for(:toggle, | ||
# params: { id: 1 }, | ||
# verb: :put | ||
# ) | ||
# should permit(:activated). | ||
# for(:toggle, params: { id: 1 }, verb: :put). | ||
# on(:user) | ||
# end | ||
# end | ||
# | ||
|
@@ -145,10 +148,9 @@ module ActionController | |
# create(:user, id: 1) | ||
# end | ||
# | ||
# should permit(:activated).for(:toggle, | ||
# params: { id: 1 }, | ||
# verb: :put | ||
# ) | ||
# should permit(:activated). | ||
# for(:toggle, params: { id: 1 }, verb: :put). | ||
# on(:user) | ||
# end | ||
# | ||
# @return [PermitMatcher] | ||
|
@@ -162,11 +164,12 @@ class PermitMatcher | |
attr_writer :stubbed_params | ||
|
||
def initialize(expected_permitted_params) | ||
@expected_permitted_params = expected_permitted_params | ||
@action = nil | ||
@verb = nil | ||
@request_params = {} | ||
@expected_permitted_params = expected_permitted_params | ||
set_double_collection | ||
@subparameter = nil | ||
@parameters_doubles = ParametersDoubles.new | ||
end | ||
|
||
def for(action, options = {}) | ||
|
@@ -176,19 +179,32 @@ def for(action, options = {}) | |
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 | ||
"permit #{verb.upcase} ##{action} to receive parameters #{param_names_as_sentence}" | ||
"(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 | ||
|
@@ -197,32 +213,49 @@ def matches?(controller) | |
end | ||
|
||
def failure_message | ||
"Expected controller to permit #{unpermitted_params.to_sentence}, but it did not." | ||
"Expected #{verb.upcase} ##{action} to #{expectation},\nbut #{reality}." | ||
end | ||
alias failure_message_for_should failure_message | ||
|
||
def failure_message_when_negated | ||
"Expected controller not to permit #{verified_permitted_params.to_sentence}, but it did." | ||
"Expected #{verb.upcase} ##{action} not to #{expectation},\nbut it did." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [82/80] |
||
end | ||
alias failure_message_for_should_not failure_message_when_negated | ||
|
||
protected | ||
|
||
attr_reader :controller, :double_collection, :action, :verb, | ||
:request_params, :expected_permitted_params, :context | ||
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) | ||
|
||
def set_double_collection | ||
@double_collection = | ||
Doublespeak.double_collection_for(::ActionController::Parameters) | ||
message | ||
end | ||
|
||
@double_collection.register_stub(:require).to_return(&:object) | ||
@double_collection.register_proxy(:permit) | ||
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 | ||
double_collection.calls_to(:permit).inject([]) do |all_param_names, call| | ||
all_param_names + call.args | ||
end.flatten | ||
parameters_doubles.permitted_params | ||
end | ||
|
||
def permit_called? | ||
|
@@ -258,6 +291,90 @@ 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,16 @@ class << self | |
def world | ||
@_world ||= World.new | ||
end | ||
|
||
def debugging_enabled? | ||
ENV['DEBUG_DOUBLESPEAK'] == '1' | ||
end | ||
|
||
def debug(&block) | ||
if debugging_enabled? | ||
puts block.call | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not write to stdout. Use Rails' logger if you want to log. |
||
end | ||
end | ||
end | ||
end | ||
end | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long. [82/80]