PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/rails/actionpack/test/controller/filter_params_test.rb

https://bitbucket.org/hfwang/good_shepherd
Ruby | 50 lines | 40 code | 10 blank | 0 comment | 1 complexity | 3674e62645c8e917dd22b824023ff9ad MD5 | raw file
  1. require 'abstract_unit'
  2. class FilterParamController < ActionController::Base
  3. end
  4. class FilterParamTest < Test::Unit::TestCase
  5. def setup
  6. @controller = FilterParamController.new
  7. end
  8. def test_filter_parameters
  9. assert FilterParamController.respond_to?(:filter_parameter_logging)
  10. assert !@controller.respond_to?(:filter_parameters)
  11. FilterParamController.filter_parameter_logging
  12. assert @controller.respond_to?(:filter_parameters)
  13. test_hashes = [[{},{},[]],
  14. [{'foo'=>nil},{'foo'=>nil},[]],
  15. [{'foo'=>'bar'},{'foo'=>'bar'},[]],
  16. [{'foo'=>'bar'},{'foo'=>'bar'},%w'food'],
  17. [{'foo'=>'bar'},{'foo'=>'[FILTERED]'},%w'foo'],
  18. [{'foo'=>'bar', 'bar'=>'foo'},{'foo'=>'[FILTERED]', 'bar'=>'foo'},%w'foo baz'],
  19. [{'foo'=>'bar', 'baz'=>'foo'},{'foo'=>'[FILTERED]', 'baz'=>'[FILTERED]'},%w'foo baz'],
  20. [{'bar'=>{'foo'=>'bar','bar'=>'foo'}},{'bar'=>{'foo'=>'[FILTERED]','bar'=>'foo'}},%w'fo'],
  21. [{'foo'=>{'foo'=>'bar','bar'=>'foo'}},{'foo'=>'[FILTERED]'},%w'f banana'],
  22. [{'baz'=>[{'foo'=>'baz'}]}, {'baz'=>[{'foo'=>'[FILTERED]'}]}, %w(foo)]]
  23. test_hashes.each do |before_filter, after_filter, filter_words|
  24. FilterParamController.filter_parameter_logging(*filter_words)
  25. assert_equal after_filter, @controller.__send__(:filter_parameters, before_filter)
  26. filter_words.push('blah')
  27. FilterParamController.filter_parameter_logging(*filter_words) do |key, value|
  28. value.reverse! if key =~ /bargain/
  29. end
  30. before_filter['barg'] = {'bargain'=>'gain', 'blah'=>'bar', 'bar'=>{'bargain'=>{'blah'=>'foo'}}}
  31. after_filter['barg'] = {'bargain'=>'niag', 'blah'=>'[FILTERED]', 'bar'=>{'bargain'=>{'blah'=>'[FILTERED]'}}}
  32. assert_equal after_filter, @controller.__send__(:filter_parameters, before_filter)
  33. end
  34. end
  35. def test_filter_parameters_is_protected
  36. FilterParamController.filter_parameter_logging(:foo)
  37. assert !FilterParamController.action_methods.include?('filter_parameters')
  38. assert_raise(NoMethodError) { @controller.filter_parameters([{'password' => '[FILTERED]'}]) }
  39. end
  40. end