#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/network/http'
require 'webrick'
require 'puppet/network/http/webrick/rest'

describe Puppet::Network::HTTP::WEBrickREST do
  it "should include the Puppet::Network::HTTP::Handler module" do
    Puppet::Network::HTTP::WEBrickREST.ancestors.should be_include(Puppet::Network::HTTP::Handler)
  end

  describe "when initializing" do
    it "should call the Handler's initialization hook with its provided arguments as the server and handler" do
      server = WEBrick::HTTPServer.new(:BindAddress => '127.0.0.1',
                                       # Probablistically going to succeed
                                       # even if we run more than one test
                                       # instance at once.
                                       :Port        => 40000 + rand(10000),
                                       # Just discard any log output, thanks.
                                       :Logger      => stub_everything('logger'))

      Puppet::Network::HTTP::WEBrickREST.any_instance.
        expects(:initialize_for_puppet).with(:server => server, :handler => "arguments")

      Puppet::Network::HTTP::WEBrickREST.new(server, "arguments")
    end
  end

  describe "when receiving a request" do
    before do
      @request     = stub('webrick http request', :query => {}, :peeraddr => %w{eh boo host ip}, :client_cert => nil)
      @response    = stub('webrick http response', :status= => true, :body= => true)
      @model_class = stub('indirected model class')
      @webrick     = stub('webrick http server', :mount => true, :[] => {})
      Puppet::Indirector::Indirection.stubs(:model).with(:foo).returns(@model_class)
      @handler = Puppet::Network::HTTP::WEBrickREST.new(@webrick, :foo)
    end

    it "should delegate its :service method to its :process method" do
      @handler.expects(:process).with(@request, @response).returns "stuff"
      @handler.service(@request, @response).should == "stuff"
    end

    describe "#headers" do
      let(:fake_request) { {"Foo" => "bar", "BAZ" => "bam" } }

      it "should iterate over the request object using #each" do
        fake_request.expects(:each)
        @handler.headers(fake_request)
      end

      it "should return a hash with downcased header names" do
        result = @handler.headers(fake_request)
        result.should == fake_request.inject({}) { |m,(k,v)| m[k.downcase] = v; m }
      end
    end

    describe "when using the Handler interface" do
      it "should use the 'accept' request parameter as the Accept header" do
        @request.expects(:[]).with("accept").returns "foobar"
        @handler.accept_header(@request).should == "foobar"
      end

      it "should use the 'content-type' request header as the Content-Type header" do
        @request.expects(:[]).with("content-type").returns "foobar"
        @handler.content_type_header(@request).should == "foobar"
      end

      it "should use the request method as the http method" do
        @request.expects(:request_method).returns "FOO"
        @handler.http_method(@request).should == "FOO"
      end

      it "should return the request path as the path" do
        @request.expects(:path).returns "/foo/bar"
        @handler.path(@request).should == "/foo/bar"
      end

      it "should return the request body as the body" do
        @request.expects(:body).returns "my body"
        @handler.body(@request).should == "my body"
      end

      it "should set the response's 'content-type' header when setting the content type" do
        @response.expects(:[]=).with("content-type", "text/html")
        @handler.set_content_type(@response, "text/html")
      end

      it "should set the status and body on the response when setting the response for a successful query" do
        @response.expects(:status=).with 200
        @response.expects(:body=).with "mybody"

        @handler.set_response(@response, "mybody", 200)
      end

      describe "when the result is a File" do
        before(:each) do
          stat = stub 'stat', :size => 100
          @file = stub 'file', :stat => stat, :path => "/tmp/path"
          @file.stubs(:is_a?).with(File).returns(true)
        end

        it "should serve it" do
          @response.stubs(:[]=)

          @response.expects(:status=).with 200
          @response.expects(:body=).with @file

          @handler.set_response(@response, @file, 200)
        end

        it "should set the Content-Length header" do
          @response.expects(:[]=).with('content-length', 100)

          @handler.set_response(@response, @file, 200)
        end
      end

      it "should set the status and message on the response when setting the response for a failed query" do
        @response.expects(:status=).with 400
        @response.expects(:reason_phrase=).with "mybody"

        @handler.set_response(@response, "mybody", 400)
      end
    end

    describe "and determining the request parameters" do
      def query_of(options)
        request = Puppet::Indirector::Request.new(:myind, :find, "my key", nil, options)
        WEBrick::HTTPUtils.parse_query(request.query_string.sub(/^\?/, ''))
      end

      def a_request_querying(query_data)
        @request.expects(:query).returns(query_of(query_data))
        @request
      end

      it "has no parameters when there is no query string" do
        only_server_side_information = [:authenticated, :ip, :node]
        @request.stubs(:query).returns(nil)

        result = @handler.params(@request)

        result.keys.sort.should == only_server_side_information
      end

      it "should include the HTTP request parameters, with the keys as symbols" do
        request = a_request_querying("foo" => "baz", "bar" => "xyzzy")
        result = @handler.params(request)

        result[:foo].should == "baz"
        result[:bar].should == "xyzzy"
      end

      it "should handle parameters with no value" do
        request = a_request_querying('foo' => "")

        result = @handler.params(request)

        result[:foo].should == ""
      end

      it "should convert the string 'true' to the boolean" do
        request = a_request_querying('foo' => "true")

        result = @handler.params(request)

        result[:foo].should == true
      end

      it "should convert the string 'false' to the boolean" do
        request = a_request_querying('foo' => "false")

        result = @handler.params(request)

        result[:foo].should == false
      end

      it "should reconstruct arrays" do
        request = a_request_querying('foo' => ["a", "b", "c"])

        result = @handler.params(request)

        result[:foo].should == ["a", "b", "c"]
      end

      it "should convert values inside arrays into primitive types" do
        request = a_request_querying('foo' => ["true", "false", "1", "1.2"])

        result = @handler.params(request)

        result[:foo].should == [true, false, 1, 1.2]
      end

      it "should YAML-load values that are YAML-encoded" do
        request = a_request_querying('foo' => YAML.dump(%w{one two}))

        result = @handler.params(request)

        result[:foo].should == %w{one two}
      end

      it "should YAML-load that are YAML-encoded" do
        request = a_request_querying('foo' => YAML.dump(%w{one two}))

        result = @handler.params(request)

        result[:foo].should == %w{one two}
      end

      it "should not allow clients to set the node via the request parameters" do
        request = a_request_querying("node" => "foo")
        @handler.stubs(:resolve_node)

        @handler.params(request)[:node].should be_nil
      end

      it "should not allow clients to set the IP via the request parameters" do
        request = a_request_querying("ip" => "foo")

        @handler.params(request)[:ip].should_not == "foo"
      end

      it "should pass the client's ip address to model find" do
        @request.stubs(:peeraddr).returns(%w{noidea dunno hostname ipaddress})
        @handler.params(@request)[:ip].should == "ipaddress"
      end

      it "should set 'authenticated' to true if a certificate is present" do
        cert = stub 'cert', :subject => [%w{CN host.domain.com}]
        @request.stubs(:client_cert).returns cert
        @handler.params(@request)[:authenticated].should be_true
      end

      it "should set 'authenticated' to false if no certificate is present" do
        @request.stubs(:client_cert).returns nil
        @handler.params(@request)[:authenticated].should be_false
      end

      it "should pass the client's certificate name to model method if a certificate is present" do
        subj = stub 'subj'
        cert = stub 'cert', :subject => subj
        @request.stubs(:client_cert).returns cert
        Puppet::Util::SSL.expects(:cn_from_subject).with(subj).returns 'host.domain.com'
        @handler.params(@request)[:node].should == "host.domain.com"
      end

      it "should resolve the node name with an ip address look-up if no certificate is present" do
        @request.stubs(:client_cert).returns nil

        @handler.expects(:resolve_node).returns(:resolved_node)

        @handler.params(@request)[:node].should == :resolved_node
      end

      it "should resolve the node name with an ip address look-up if CN parsing fails" do
        subj = stub 'subj'
        cert = stub 'cert', :subject => subj
        @request.stubs(:client_cert).returns cert
        Puppet::Util::SSL.expects(:cn_from_subject).with(subj).returns nil

        @handler.expects(:resolve_node).returns(:resolved_node)

        @handler.params(@request)[:node].should == :resolved_node
      end
   end
  end
end
