<commit_before>require 'spec_helper'

describe G5::Logger::Log do
  context 'json' do
    before { G5::Logger::Config[:format] = G5::Logger::JSON_FORMAT }

    describe 'redact sensitive data' do
      before do
        G5::Logger::Config[:default_log_hash] = {external_parent_source_name: 'g5-jobs'}
        G5::Logger::Config[:redact_keys] << 'redactme'
      end
      after do
        G5::Logger::Config[:default_log_hash] = {}
      end
      let(:request) { 
        {
          'credit_card' => '4344122144442222', 
          foo: 'bar', 
          ssn: '333224444', 
          date_of_birth: '05-15-1976', 
          annual_salary: '$89000', 
          'e-content' => "this should be redacted",
          "comments"  => "this should be redacted",
          "message"   => "this should be redacted",
          "notes"     => "this should be redacted",
          parent: {
            cvv: '323', 
            jimmy: 'joe'
          }, 
          'array' => [
            {
              'my_password' => '<PASSWORD>', 
              'redactme' => 'secretval'
            }
          ]
        } 
      }
      let(:body) { {credit_exp_date: '10/2015', whatever: 'brah'} }
      let(:response) { double(:response, code: 201, body: body) }
      let(:redact_value) { G5::Logger::Config[:redact_value] }

      subject do
        G5::Logger::Log.log_json_req_resp(request, response, {foo: 'bar', 'j_cvv' => 343})
        indifferent_hash_from_json TestLogger.last_payload
      end

      it { is_expected.to eq({"source_app_name" => "test", "external_parent_source_name" => "g5-jobs", "foo" => "bar", "j_cvv" => redact_value, "status" => 201,
                              "request"         => {"credit_card"   => redact_value, 
                                                    "foo"           => "bar",
                                                    "ssn"           => redact_value, 
                                                    "date_of_birth" => redact_value, 
                                                    "annual_salary" => redact_value,
                                                    "e-content"     => redact_value,
                                                    "comments"      => redact_value,
                                                    "message"       => redact_value,
                                                    "notes"         => redact_value,
                                                    "parent"        => {"cvv" => redact_value, "jimmy" => "joe"},
                                                    "array"         => [{"my_password" => <PASSWORD>, "redactme" => redact_value}]},
                              "response"        => {"credit_exp_date" => redact_value, "whatever" => "brah"}}) }
    end

    describe :log_json_req_resp do
      let(:request) { {payload: 'foo'} }
      let(:code) { 201 }
      let(:body) { {model: {id: 1, name: 'yeah'}} }
      let(:response) { double(:response, code: code, body: body) }
      subject do
        G5::Logger::Log.log_json_req_resp(request, response, {foo: 'bar'})
        indifferent_hash_from_json TestLogger.last_payload
      end

      its([:source_app_name]) { is_expected.to eq('test') }
      its([:foo]) { is_expected.to eq('bar') }
      its([:status]) { is_expected.to eq(201) }
      its([:response]) { is_expected.to eq({"model" => {"id" => 1, "name" => "yeah"}}) }
      its([:request]) { is_expected.to eq({"payload" => "foo"}) }
    end
  end

  context 'key_value' do
    before { G5::Logger::Config[:format] = G5::Logger::KEY_VALUE_FORMAT }

    describe 'levels' do
      G5::Logger::Log::Levels.each do |method|<commit_msg>Shore up specs<commit_after>require 'spec_helper'

describe G5::Logger::Log do
  context 'json' do
    before { G5::Logger::Config[:format] = G5::Logger::JSON_FORMAT }

    describe 'redact sensitive data' do
      before do
        G5::Logger::Config[:default_log_hash] = {external_parent_source_name: 'g5-jobs'}
        G5::Logger::Config[:redact_keys] << 'redactme'
      end
      after do
        G5::Logger::Config[:default_log_hash] = {}
      end
      let(:request) { 
        {
          'credit_card' => '4344122144442222', 
          foo: 'bar', 
          ssn: '333224444', 
          date_of_birth: '05-15-1976', 
          annual_salary: '$89000', 
          'e-content' => "this should be redacted",
          "comments"  => "this should be redacted",
          "message"   => "this should be redacted",
          "notes"     => "this should be redacted",
          "p-tel"     => "this should be redacted",
          "phone"     => "this should be redacted",
          "address1"  => "this should be redacted",
          "p-email"   => "this should be redacted",
          parent: {

            cvv: '323', 
            jimmy: 'joe'
          }, 
          'array' => [
            {
              'my_password' => '<PASSWORD>', 
              'redactme' => '<PASSWORD>'
            }
          ]
        } 
      }
      let(:body) { {credit_exp_date: '10/2015', whatever: 'brah'} }
      let(:response) { double(:response, code: 201, body: body) }
      let(:redact_value) { G5::Logger::Config[:redact_value] }

      subject do
        G5::Logger::Log.log_json_req_resp(request, response, {foo: 'bar', 'j_cvv' => 343})
        indifferent_hash_from_json TestLogger.last_payload
      end

      it { is_expected.to eq({"source_app_name" => "test", "external_parent_source_name" => "g5-jobs", "foo" => "bar", "j_cvv" => redact_value, "status" => 201,
                              "request"         => {"credit_card"   => redact_value, 
                                                    "foo"           => "bar",
                                                    "ssn"           => redact_value, 
                                                    "date_of_birth" => redact_value, 
                                                    "annual_salary" => redact_value,
                                                    "e-content"     => redact_value,
                                                    "comments"      => redact_value,
                                                    "message"       => redact_value,
                                                    "notes"         => redact_value,
                                                    "p-tel"         => redact_value,
                                                    "phone"         => redact_value,
                                                    "address1"      => redact_value,
                                                    "p-email"       => redact_value,
                                                    "parent"        => {"cvv" => redact_value, "jimmy" => "joe"},
                                                    "array"         => [{"my_password" => <PASSWORD>, "<PASSWORD>" => red<PASSWORD>}]},
                              "response"        => {"credit_exp_date" => redact_value, "whatever" => "brah"}}) }
    end

    describe :log_json_req_resp do
      let(:request) { {payload: 'foo'} }
      let(:code) { 201 }
      let(:body) { {model: {id: 1, name: 'yeah'}} }
      let(:response) { double(:response, code: code, body: body) }
      subject do
        G5::Logger::Log.log_json_req_resp(request, response, {foo: 'bar'})
        indifferent_hash_from_json TestLogger.last_payload
      end

      its([:source_app_name]) { is_expected.to eq('test') }
      its([:foo]) { is_expected.to eq('bar') }
      its([:status]) { is_expected.to eq(201) }
      its([:response]) { is_expected.to eq({"model" => {"id" => 1, "name" => "yeah"}}) }
      its([:request]) { is_expected.to eq({"payload" => "foo"}) }
    end
  end

  context 'key_value' do
    before { G5::Logger::Config[:format] = G5::Logger::KEY_VALUE_FORMAT }

    describe 'levels' do
      G5::Logger::Log::Levels.each do |method|
{'fraction_non_alphanumeric': 0.14195183776932827, 'fraction_numerical': 0.02002534854245881, 'mean_word_length': 1.455196017423771, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 92, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 49, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3920066', 'n_tokens_mistral': 2384, 'n_tokens_neox': 2214, 'n_words': 435}