<commit_before># frozen_string_literal: true

require "rack"

RSpec.describe Hanami::Action::Params do
  xit "is frozen"

  # This is temporary suspended.
  # We need to get the dependency Hanami::Validations, more stable before to enable this back.
  #
  # it 'is frozen' do
  #   params = Hanami::Action::Params.new({id: '23'})
  #   params.must_be :frozen?
  # end

  describe "#raw" do
    let(:params) { Class.new(Hanami::Action::Params) }

    context "when this feature isn't enabled" do
      let(:action) { ParamsAction.new }

      it "raw gets all params" do
        File.open("spec/support/fixtures/multipart-upload.png", "rb") do |upload|
          response = action.call("id" => "1", "unknown" => "2", "upload" => upload, "_csrf_token" => "3")

          expect(response[:params][:id]).to eq("1")
          expect(response[:params][:unknown]).to eq("2")
          expect(FileUtils.cmp(response[:params][:upload], upload)).to be(true)
          expect(response[:params][:_csrf_token]).to eq("3")

          expect(response[:params].raw.fetch("id")).to eq("1")
          expect(response[:params].raw.fetch("unknown")).to eq("2")
          expect(response[:params].raw.fetch("upload")).to eq(upload)
          expect(response[:params].raw.fetch("_csrf_token")).to eq("3")
        end
      end
    end

    context "when this feature is enabled" do
      let(:action) { WhitelistedUploadDslAction.new }

      it "raw gets all params" do
        Tempfile.create("multipart-upload") do |upload|
          response = action.call("id" => "1", "unknown" => "2", "upload" => upload, "_csrf_token" => "3")

          expect(response[:params][:id]).to          eq(1)
          expect(response[:params][:unknown]).to     be(nil)
          expect(response[:params][:upload]).to      eq(upload)
          expect(response[:params][:_csrf_token]).to eq("3")

          expect(response[:params].raw.fetch("id")).to          eq("1")
          expect(response[:params].raw.fetch("unknown")).to     eq("2")
          expect(response[:params].raw.fetch("upload")).to      eq(upload)
          expect(response[:params].raw.fetch("_csrf_token")).to eq("3")
        end
      end
    end
  end

  describe "whitelisting" do
    let(:params) { Class.new(Hanami::Action::Params) }

    context "when this feature isn't enabled" do
      let(:action) { ParamsAction.new }

      it "creates a Params innerclass" do
        expect(defined?(ParamsAction::Params)).to eq("constant")
        expect(ParamsAction::Params.ancestors).to include(Hanami::Action::Params)
      end

      context "in testing mode" do
        it "returns all the params as they are" do
          # For unit tests in Hanami projects, developers may want to define
          # params with symbolized keys.
          response = action.call(a: "1", b: "2", c: "3")
          expect(response.body).to eq([%({:a=>"1", :b=>"2", :c=>"3"})])
        end
      end

      context "in a Rack context" do
        it "returns all the params as they are" do
          # Rack params are always stringified
          response = Rack::MockRequest.new(action).request("PATCH", "?id=23", params: {"x" => {"foo" => "bar"}})
          expect(response.body).to match(%({:id=>"23", :x=>{:foo=>"bar"}}))
        end
      end

      context "with Hanami::Router" do
        it "returns all the params as they are" do
          # Hanami::Router params are always symbolized
          response = action.call("router.params" => {id: "23"})
          expect(response.body).to eq([%({:id=>"23"})])
        end
      end
    end

    context "when this feature is enabled" do
      context "with an explicit class" do
        let(:action) { WhitelistedParamsAction.new }

        # For unit tests in Hanami projects, developers may want to define
        # params with symbolized keys.
        context "in testing mode" do
          it "returns only the listed params" do
            response = action.call(id: 23, unknown: 4, article: {foo: "bar", tags: [:cool]})
            expect(response.body).to eq([%({:id=>23, :article=>{:tags=>[:cool]}})])
          end

          it "doesn't filter _csrf_token" do
            response = action.call(_csrf_token: "abc")
            expect(response.body).to eq([%({:_csrf_token=>"abc"})])
          end
        end

        context "in a Rack context" do
          it "returns only the listed params" do
            response = Rack::MockRequest.new(action).request("PATCH", "?id=23", params: {x: {foo: "bar"}})
            expect(response.body).to match(%({:id=>23}))
          end

          it "doesn't filter _csrf_token" do
            response = Rack::MockRequest.new(action).request("PATCH", "?id=1", params: {_csrf_token: "def", x: {foo: "bar"}})
            expect(response.body).to match(%(:_csrf_token=>"def"))
            expect(response.body).to match(%(:id=>1))
          end
        end

        context "with Hanami::Router" do
          it "returns only the listed params" do
            response = action.call('router.params' => { id: 23, another: 'x' })
            expect(response.body).to eq([%({:id=>23})])
          end
        end
      end

      context "with an anoymous class" do
        let(:action) { WhitelistedDslAction.new }

        it "creates a Params innerclass" do
          expect(defined?(WhitelistedDslAction::Params)).to eq("constant")
          expect(WhitelistedDslAction::Params.ancestors).to include(Hanami::Action::Params)
        end

        context "in testing mode" do
          it "returns only the listed params" do
            response = action.call(username: "jodosha", unknown: "field")
            expect(response.body).to eq([%({:username=>"jodosha"})])
          end
        end

        context "in a Rack context" do
          it "returns only the listed params" do
            response = Rack::MockRequest.new(action).request("PATCH", "?username=jodosha", params: {x: {foo: "bar"}})
            expect(response.body).to match(%({:username=>"jodosha"}))
          end
        end

        context "with Hanami::Router" do
          it "returns only the listed params" do
            response = action.call('router.params' => { username: 'jodosha', y: 'x' })
            expect(response.body).to eq([%({:username=>"jodosha"})])
          end
        end
      end
    end
  end<commit_msg>Code cleanup https://github.com/hanami/controller/pull/375<commit_after># frozen_string_literal: true

require "rack"

RSpec.describe Hanami::Action::Params do
  xit "is frozen"

  # This is temporary suspended.
  # We need to get the dependency Hanami::Validations, more stable before to enable this back.
  #
  # it 'is frozen' do
  #   params = Hanami::Action::Params.new({id: '23'})
  #   params.must_be :frozen?
  # end

  describe "#raw" do
    let(:params) { Class.new(Hanami::Action::Params) }

    context "when this feature isn't enabled" do
      let(:action) { ParamsAction.new }

      it "raw gets all params" do
        File.open("spec/support/fixtures/multipart-upload.png", "rb") do |upload|
          response = action.call("id" => "1", "unknown" => "2", "upload" => upload, "_csrf_token" => "3")

          expect(response[:params][:id]).to eq("1")
          expect(response[:params][:unknown]).to eq("2")
          expect(FileUtils.cmp(response[:params][:upload], upload)).to be(true)
          expect(response[:params][:_csrf_token]).to eq("3")

          expect(response[:params].raw.fetch("id")).to eq("1")
          expect(response[:params].raw.fetch("unknown")).to eq("2")
          expect(response[:params].raw.fetch("upload")).to eq(upload)
          expect(response[:params].raw.fetch("_csrf_token")).to eq("3")
        end
      end
    end

    context "when this feature is enabled" do
      let(:action) { WhitelistedUploadDslAction.new }

      it "raw gets all params" do
        Tempfile.create("multipart-upload") do |upload|
          response = action.call("id" => "1", "unknown" => "2", "upload" => upload, "_csrf_token" => "3")

          expect(response[:params][:id]).to          eq(1)
          expect(response[:params][:unknown]).to     be(nil)
          expect(response[:params][:upload]).to      eq(upload)
          expect(response[:params][:_csrf_token]).to eq("3")

          expect(response[:params].raw.fetch("id")).to          eq("1")
          expect(response[:params].raw.fetch("unknown")).to     eq("2")
          expect(response[:params].raw.fetch("upload")).to      eq(upload)
          expect(response[:params].raw.fetch("_csrf_token")).to eq("3")
        end
      end
    end
  end

  describe "whitelisting" do
    let(:params) { Class.new(Hanami::Action::Params) }

    context "when this feature isn't enabled" do
      let(:action) { ParamsAction.new }

      it "creates a Params innerclass" do
        expect(defined?(ParamsAction::Params)).to eq("constant")
        expect(ParamsAction::Params.ancestors).to include(Hanami::Action::Params)
      end

      context "in testing mode" do
        it "returns all the params as they are" do
          # For unit tests in Hanami projects, developers may want to define
          # params with symbolized keys.
          response = action.call(a: "1", b: "2", c: "3")
          expect(response.body).to eq([%({:a=>"1", :b=>"2", :c=>"3"})])
        end
      end

      context "in a Rack context" do
        it "returns all the params as they are" do
          # Rack params are always stringified
          response = Rack::MockRequest.new(action).request("PATCH", "?id=23", params: {"x" => {"foo" => "bar"}})
          expect(response.body).to match(%({:id=>"23", :x=>{:foo=>"bar"}}))
        end
      end

      context "with Hanami::Router" do
        it "returns all the params as they are" do
          # Hanami::Router params are always symbolized
          response = action.call("router.params" => {id: "23"})
          expect(response.body).to eq([%({:id=>"23"})])
        end
      end
    end

    context "when this feature is enabled" do
      context "with an explicit class" do
        let(:action) { WhitelistedParamsAction.new }

        # For unit tests in Hanami projects, developers may want to define
        # params with symbolized keys.
        context "in testing mode" do
          it "returns only the listed params" do
            response = action.call(id: 23, unknown: 4, article: {foo: "bar", tags: [:cool]})
            expect(response.body).to eq([%({:id=>23, :article=>{:tags=>[:cool]}})])
          end

          it "doesn't filter _csrf_token" do
            response = action.call(_csrf_token: "abc")
            expect(response.body).to eq([%({:_csrf_token=>"abc"})])
          end
        end

        context "in a Rack context" do
          it "returns only the listed params" do
            response = Rack::MockRequest.new(action).request("PATCH", "?id=23", params: {x: {foo: "bar"}})
            expect(response.body).to match(%({:id=>23}))
          end

          it "doesn't filter _csrf_token" do
            response = Rack::MockRequest.new(action).request("PATCH", "?id=1", params: {_csrf_token: "def", x: {foo: "bar"}})
            expect(response.body).to match(%(:_csrf_token=>"def"))
            expect(response.body).to match(%(:id=>1))
          end
        end

        context "with Hanami::Router" do
          it "returns only the listed params" do
            response = action.call("router.params" => {id: 23, another: "x"})
            expect(response.body).to eq([%({:id=>23})])
          end
        end
      end

      context "with an anoymous class" do
        let(:action) { WhitelistedDslAction.new }

        it "creates a Params innerclass" do
          expect(defined?(WhitelistedDslAction::Params)).to eq("constant")
          expect(WhitelistedDslAction::Params.ancestors).to include(Hanami::Action::Params)
        end

        context "in testing mode" do
          it "returns only the listed params" do
            response = action.call(username: "jodosha", unknown: "field")
            expect(response.body).to eq([%({:username=>"jodosha"})])
          end
        end

        context "in a Rack context" do
          it "returns only the listed params" do
            response = Rack::MockRequest.new(action).request("PATCH", "?username=jodosha", params: {x: {foo: "bar"}})
            expect(response.body).to match(%({:username=>"jodosha"}))
          end
        end

        context "with Hanami::Router" do
          it "returns only the listed params" do
            response = action.call("router.params" => {username: "jodosha", y: "x"})
            expect(response.body).to eq([%({:username=>"jodosha"})])
          end
        end
      end
    end
  end
{'fraction_non_alphanumeric': 0.15530153724871895, 'fraction_numerical': 0.00780449349625542, 'mean_word_length': 2.519977802441731, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 65, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2350803', 'n_tokens_mistral': 3912, 'n_tokens_neox': 3785, 'n_words': 973}