Skip to content

Commit

Permalink
When cleaning params to save them as yaml, also clean each element of…
Browse files Browse the repository at this point in the history
… arrays.
  • Loading branch information
pupeno committed Sep 5, 2014
1 parent b3bf5f2 commit 6180601
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
24 changes: 13 additions & 11 deletions lib/validation_auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ def self.request
end

# Clean parameters before storing them in the database.
def self.clean_params(params)
cleaned_params = {}
params.each do |k, v|
cleaned_params[k] = if v.is_a? Hash
clean_params(v) # clean params recursively.
elsif v.is_a? ActionDispatch::Http::UploadedFile
v.inspect # UploadedFiles cannot be yaml-serialized, so, we replace them with a string.
else
v
end
def self.clean_params(param)
if param.is_a? Hash
cleaned_params = {}
param.each do |k, v|
cleaned_params[k] = clean_params(v) # clean each value
end
cleaned_params
elsif param.is_a? Array
param.map { |v| clean_params(v) } # clean each value
elsif param.is_a? ActionDispatch::Http::UploadedFile
param.inspect # UploadedFiles cannot be yaml-serialized, so, we replace them with a string.
else
param
end
cleaned_params
end
end

Expand Down
6 changes: 4 additions & 2 deletions test/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class ControllerTest < ActionController::TestCase
end

should "clean params" do
cleaned_params = ValidationAuditor::Controller.clean_params({name: "John Doe", deep: {structure: {with: {file: ActionDispatch::Http::UploadedFile.new(tempfile: Tempfile.new("test.txt"))}}}})
cleaned_params = ValidationAuditor::Controller.clean_params({name: "John Doe", deep: {structure: {with: {file: ActionDispatch::Http::UploadedFile.new(tempfile: Tempfile.new("test.txt")), and_files: [ActionDispatch::Http::UploadedFile.new(tempfile: Tempfile.new("test.txt"))]}}}})
assert_equal "John Doe", cleaned_params[:name]
assert cleaned_params[:deep][:structure][:with][:file].is_a? String
assert cleaned_params[:deep][:structure][:with][:and_files].is_a? Array
assert cleaned_params[:deep][:structure][:with][:and_files].first.is_a? String
end

should "not create a validation audit due to no validation failing when creating a new record" do
Expand All @@ -65,7 +67,7 @@ class ControllerTest < ActionController::TestCase

should "create a validation audit even when an uploaded file is in the params" do
assert_difference "ValidationAuditor::ValidationAudit.count" => +1 do
post :create, audited_record: {name: "John Doe"}, deep: {structure: {with: {file: ActionDispatch::Http::UploadedFile.new(tempfile: Tempfile.new("test.txt"))}}} # Missing email and a deep structure with a file in it.
post :create, audited_record: {name: "John Doe"}, deep: {structure: {with: {file: ActionDispatch::Http::UploadedFile.new(tempfile: Tempfile.new("test.txt")), and_files: [ActionDispatch::Http::UploadedFile.new(tempfile: Tempfile.new("test.txt"))]}}} # Missing email and a deep structure with a file in it.
end
audit = ValidationAuditor::ValidationAudit.order(:id).last
assert_nil audit.record # New records cannot be referenced because they don't exist...
Expand Down

0 comments on commit 6180601

Please sign in to comment.