<commit_before>#!/usr/bin/env rspec
require 'spec_helper'

require 'puppet/util/feature'
require 'puppet/indirector/facts/puppetdb'
require 'puppet/util/puppetdb'
require 'puppet/util/puppetdb/command_names'
require 'json'

describe Puppet::Node::Facts::Puppetdb do

  CommandReplaceFacts = Puppet::Util::Puppetdb::CommandNames::CommandReplaceFacts

  before :each do
    Puppet::Util::Puppetdb.stubs(:server).returns 'localhost'
    Puppet::Util::Puppetdb.stubs(:port).returns 0
    Puppet::Node::Facts.indirection.stubs(:terminus).returns(subject)
    create_environmentdir("my_environment")
  end

  describe "#save" do
    let(:response) { Net::HTTPOK.new('1.1', 200, 'OK') }
    let(:facts)    { Puppet::Node::Facts.new('foo') }
    let(:http)     { mock 'http' }

    let(:options) {{
      :producer_timestamp => 'a test',
      :environment => "my_environment",
    }}

    before :each do
      Puppet::Network::HttpPool.expects(:http_instance).returns http
      response.stubs(:body).returns '{"uuid": "a UUID"}'
    end

    def save
      subject.save(Puppet::Node::Facts.indirection.request(:save, facts.name, facts, options))
    end

    it "should POST the facts as a JSON string" do
      Puppet::Util::Puppetdb.stubs(:puppet3compat?).returns(true)
      f = {
        "name" => facts.name,
        "values" => facts.strip_internal,
        "environment" => "my_environment",
        "producer-timestamp" => "a test",
      }

      payload = {
        :command => CommandReplaceFacts,
        :version => 3,
        :payload => f,
      }.to_pson

      http.expects(:post).with do |uri, body, headers|
        expect(body).to eq(payload)
      end.returns response

      save
    end

    it "should POST the trusted data we tell it to" do

      if Puppet::Util::Puppetdb.puppet3compat?
        Puppet[:trusted_node_data] = true
      end

      trusted_data = {"foo" => "foobar", "certname" => "<PASSWORD>post<PASSWORD>"}
      subject.stubs(:get_trusted_info).returns trusted_data

      f = {
        "name" => facts.name,
        "values" => facts.strip_internal.merge({"trusted" => trusted_data}),
        "environment" => "my_environment",
        "producer-timestamp" => "a test",
      }

      payload = {
        :command => CommandReplaceFacts,
        :version => 3,
        :payload => f,
      }.to_pson

      http.expects(:post).with do |uri, body, headers|
        expect(body).to eq(payload)
      end.returns response

      save
    end


    it "should retain integer type when submitting" do
      facts.values['something'] = 100

      sent_payload = nil
      http.expects(:post).with do |uri, body, headers|
        sent_payload = body
      end.returns response

      save

      message = JSON.parse(sent_payload)
      sent_facts = message['payload']

      # We shouldn't modify the original instance
      facts.values['something'].should == 100
      sent_facts['values']['something'].should == 100
    end
  end

  describe "#get_trusted_info" do
    it 'should return trusted data' do
      node = Puppet::Node.new('my_certname')
      trusted = subject.get_trusted_info(node)
      # Extra keys domainname & hostname introduced by PUP-5097, Puppet 4.2.1-dirty (4.3.0 probably)
      if trusted.has_key?("domainname")
        expect(trusted).to eq({'authenticated'=>'local', 'certname'=>'testing',
                               'extensions'=>{}, 'hostname'=>'testing', 'domainname'=>nil})
      else
        # Puppet 4.2.x and older
        expect(trusted).to eq({'authenticated'=>'local', 'certname'=>'testing', 'extensions'=>{}})
      end
    end

    it 'should return trusted data when falling back to the node' do
      # This removes :trusted_information from the global context, triggering our fallback code.
      if Puppet.methods.include? :rollback_context
        Puppet.rollback_context('initial testing state')
      else
        Puppet.pop_context # puppet 3.5.1
      end

      node = Puppet::Node.new('my_certname', :parameters => {'clientcert' => 'trusted_certname'})
      trusted = subject.get_trusted_info(node)

      # Extra keys domainname & hostname introduced by PUP-5097, Puppet 4.2.1-dirty (4.3.0 probably)
      if trusted.has_key?("domainname")
        expect(trusted).to eq({'authenticated'=>'local', 'certname'=>'trusted_certname',
                               'extensions'=>{}, 'hostname'=>'trusted_certname', 'domainname'=>nil})
      else
        # Puppet 4.2.x and older
        expect(trusted).to eq({'authenticated'=>'local', 'certname'=>'trusted_certname', 'extensions'=>{}})
      end

      # Put the context back the way the test harness expects
      Puppet.push_context({}, 'context to make the tests happy')
      if Puppet.methods.include? :mark_context
        Puppet.mark_context('initial testing state')
      end
    end
  end

  describe "#find" do
    def find_facts()
      Puppet::Node::Facts.indirection.find('some_node')
    end<commit_msg>(maint) Fix trusted facts tests<commit_after>#!/usr/bin/env rspec
require 'spec_helper'

require 'puppet/util/feature'
require 'puppet/indirector/facts/puppetdb'
require 'puppet/util/puppetdb'
require 'puppet/util/puppetdb/command_names'
require 'json'

describe Puppet::Node::Facts::Puppetdb do

  CommandReplaceFacts = Puppet::Util::Puppetdb::CommandNames::CommandReplaceFacts

  before :each do
    Puppet::Util::Puppetdb.stubs(:server).returns 'localhost'
    Puppet::Util::Puppetdb.stubs(:port).returns 0
    Puppet::Node::Facts.indirection.stubs(:terminus).returns(subject)
    create_environmentdir("my_environment")
  end

  describe "#save" do
    let(:response) { Net::HTTPOK.new('1.1', 200, 'OK') }
    let(:facts)    { Puppet::Node::Facts.new('foo') }
    let(:http)     { mock 'http' }

    let(:options) {{
      :producer_timestamp => 'a test',
      :environment => "my_environment",
    }}

    before :each do
      Puppet::Network::HttpPool.expects(:http_instance).returns http
      response.stubs(:body).returns '{"uuid": "a UUID"}'
    end

    def save
      subject.save(Puppet::Node::Facts.indirection.request(:save, facts.name, facts, options))
    end

    it "should POST the facts as a JSON string" do
      Puppet::Util::Puppetdb.stubs(:puppet3compat?).returns(true)
      f = {
        "name" => facts.name,
        "values" => facts.strip_internal,
        "environment" => "my_environment",
        "producer-timestamp" => "a test",
      }

      payload = {
        :command => CommandReplaceFacts,
        :version => 3,
        :payload => f,
      }.to_pson

      http.expects(:post).with do |uri, body, headers|
        expect(body).to eq(payload)
      end.returns response

      save
    end

    it "should POST the trusted data we tell it to" do

      if Puppet::Util::Puppetdb.puppet3compat?
        Puppet[:trusted_node_data] = true
      end

      trusted_data = {"foo" => "foobar", "certname" => "testing_posting"}
      subject.stubs(:get_trusted_info).returns trusted_data

      f = {
        "name" => facts.name,
        "values" => facts.strip_internal.merge({"trusted" => trusted_data}),
        "environment" => "my_environment",
        "producer-timestamp" => "a test",
      }

      payload = {
        :command => CommandReplaceFacts,
        :version => 3,
        :payload => f,
      }.to_pson

      http.expects(:post).with do |uri, body, headers|
        expect(body).to eq(payload)
      end.returns response

      save
    end


    it "should retain integer type when submitting" do
      facts.values['something'] = 100

      sent_payload = nil
      http.expects(:post).with do |uri, body, headers|
        sent_payload = body
      end.returns response

      save

      message = JSON.parse(sent_payload)
      sent_facts = message['payload']

      # We shouldn't modify the original instance
      facts.values['something'].should == 100
      sent_facts['values']['something'].should == 100
    end
  end

  describe "#get_trusted_info" do
    it 'should return trusted data' do
      node = Puppet::Node.new('my_certname')
      trusted = subject.get_trusted_info(node)
      # Extra keys domain & hostname introduced by PUP-5097, Puppet 4.3.0
      if trusted.has_key?("domain")
        expect(trusted).to eq({'authenticated'=>'local', 'certname'=>'testing',
                               'extensions'=>{}, 'hostname'=>'testing', 'domain'=>nil})
      else
        # Puppet 4.2.x and older
        expect(trusted).to eq({'authenticated'=>'local', 'certname'=>'testing', 'extensions'=>{}})
      end
    end

    it 'should return trusted data when falling back to the node' do
      # This removes :trusted_information from the global context, triggering our fallback code.
      if Puppet.methods.include? :rollback_context
        Puppet.rollback_context('initial testing state')
      else
        Puppet.pop_context # puppet 3.5.1
      end

      node = Puppet::Node.new('my_certname', :parameters => {'clientcert' => 'trusted_certname'})
      trusted = subject.get_trusted_info(node)

      # Extra keys domainname & hostname introduced by PUP-5097, Puppet 4.3.0
      if trusted.has_key?("domain")
        expect(trusted).to eq({'authenticated'=>'local', 'certname'=>'trusted_certname',
                               'extensions'=>{}, 'hostname'=>'trusted_certname', 'domain'=>nil})
      else
        # Puppet 4.2.x and older
        expect(trusted).to eq({'authenticated'=>'local', 'certname'=>'trusted_certname', 'extensions'=>{}})
      end

      # Put the context back the way the test harness expects
      Puppet.push_context({}, 'context to make the tests happy')
      if Puppet.methods.include? :mark_context
        Puppet.mark_context('initial testing state')
      end
    end
  end

  describe "#find" do
    def find_facts()
      Puppet::Node::Facts.indirection.find('some_node')
    end
{'fraction_non_alphanumeric': 0.1365804160324708, 'fraction_numerical': 0.008726534753932013, 'mean_word_length': 2.9033663366336633, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 79, 'https://': 0, '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': '6607946', 'n_tokens_mistral': 3180, 'n_tokens_neox': 3052, 'n_words': 681}