params_wrapper_test.rb 6.2 KB
Newer Older
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
require 'abstract_unit'

module Admin; class User; end; end

class ParamsWrapperTest < ActionController::TestCase
  class UsersController < ActionController::Base
    def test
      render :json => params.except(:controller, :action)
    end
  end

  class User; end
  class Person; end

  tests UsersController

  def test_derivered_name_from_controller
    with_default_wrapper_options do
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'username' => 'sikachu' }
      assert_equal '{"username":"sikachu","user":{"username":"sikachu"}}', @response.body
    end
  end

  def test_specify_wrapper_name
    with_default_wrapper_options do
      UsersController.wrap_parameters :person

      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'username' => 'sikachu' }
      assert_equal '{"username":"sikachu","person":{"username":"sikachu"}}', @response.body
    end
  end

  def test_specify_wrapper_model
    with_default_wrapper_options do
      UsersController.wrap_parameters Person

      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'username' => 'sikachu' }
      assert_equal '{"username":"sikachu","person":{"username":"sikachu"}}', @response.body
    end
  end

  def test_specify_only_option
    with_default_wrapper_options do
      UsersController.wrap_parameters :only => :username

      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
      assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body
    end
  end

  def test_specify_except_option
    with_default_wrapper_options do
      UsersController.wrap_parameters :except => :title

      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
      assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body
    end
  end

  def test_specify_both_wrapper_name_and_only_option
    with_default_wrapper_options do
      UsersController.wrap_parameters :person, :only => :username

      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
      assert_equal '{"username":"sikachu","title":"Developer","person":{"username":"sikachu"}}', @response.body
    end
  end

  def test_not_enabled_format
    with_default_wrapper_options do
      @request.env['CONTENT_TYPE'] = 'application/xml'
      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
      assert_equal '{"username":"sikachu","title":"Developer"}', @response.body
    end
  end

  def test_specify_format
    with_default_wrapper_options do
      UsersController.wrap_parameters :format => :xml

      @request.env['CONTENT_TYPE'] = 'application/xml'
      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
      assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu","title":"Developer"}}', @response.body
    end
  end

  def test_not_wrap_reserved_parameters
    with_default_wrapper_options do
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '&#9731;', 'username' => 'sikachu' }
      assert_equal '{"authenticity_token":"pwned","_method":"put","utf8":"&#9731;","username":"sikachu","user":{"username":"sikachu"}}', @response.body
    end
  end

  def test_no_double_wrap_if_key_exists
    with_default_wrapper_options do
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'user' => { 'username' => 'sikachu' }}
      assert_equal '{"user":{"username":"sikachu"}}', @response.body
    end
  end

  def test_nested_params
    with_default_wrapper_options do
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'person' => { 'username' => 'sikachu' }}
      assert_equal '{"person":{"username":"sikachu"},"user":{"person":{"username":"sikachu"}}}', @response.body
    end
  end

  def test_derived_wrapped_keys_from_matching_model
    with_default_wrapper_options do
      User.expects(:respond_to?).with(:column_names).returns(true)
      User.expects(:column_names).returns(["username"])

      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
      assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body
    end
  end

  def test_derived_wrapped_keys_from_specified_model
    with_default_wrapper_options do
      Person.expects(:respond_to?).with(:column_names).returns(true)
      Person.expects(:column_names).returns(["username"])

      UsersController.wrap_parameters Person

      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'username' => 'sikachu', 'title' => 'Developer' }
      assert_equal '{"username":"sikachu","title":"Developer","person":{"username":"sikachu"}}', @response.body
    end
  end

  private
    def with_default_wrapper_options(&block)
      @controller.class._wrapper_options = {:format => [:json]}
      @controller.class.inherited(@controller.class)
      yield
    end
end

class NamespacedParamsWrapperTest < ActionController::TestCase
  module Admin
    class UsersController < ActionController::Base
      def test
        render :json => params.except(:controller, :action)
      end
    end

    class User; end
  end
  class User; end
  class Person; end

  tests Admin::UsersController

  def test_derivered_name_from_controller
    with_default_wrapper_options do
      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'username' => 'sikachu' }
      assert_equal '{"username":"sikachu","user":{"username":"sikachu"}}', @response.body
    end
  end

  def test_namespace_lookup_when_namespaced_model_available
    with_default_wrapper_options do
      Admin::User.expects(:respond_to?).with(:column_names).returns(false)

      @request.env['CONTENT_TYPE'] = 'application/json'
      post :test, { 'username' => 'sikachu' }
    end
  end

  private
    def with_default_wrapper_options(&block)
      @controller.class._wrapper_options = {:format => [:json]}
      @controller.class.inherited(@controller.class)
      yield
    end
end