<commit_before>require "spec_helper"

describe DN do
  let!(:raw_dn) { '/C=US/O=RB/OU=DEV/OU=JS/OU=People/CN=Last First M (initial)' }
  let!(:dn_to_s) { 'CN=LAST FIRST M (INITIAL),OU=PEOPLE,OU=JS,OU=DEV,O=RB,C=US' }
  let(:valid_subject) { DN.new(dn_string: raw_dn) }
  let(:dn_elements) { ["CN=LAST FIRST M (INITIAL)", "OU=PEOPLE", "OU=JS", "OU=DEV", "O=RB", "C=US"] }

  #
  # Unit specs
  #
  describe "#new" do
    it "should create a new DN object when valid" do
      expect(valid_subject.class.to_s).to eq(DN.to_s)
    end

    it "should identify the most likely delimiter" do
      expect(valid_subject.delimiter).to eq('/')
      %w(, ; - _ : \\ % ^ ! & * @ # $).each do |delim|
        custom_delim_subject = DN.new(dn_string: raw_dn.gsub('/',delim))
        expect(custom_delim_subject.delimiter).to eq(delim)
      end
    end

    it "should split on identified delimiter" do
      expect(valid_subject.split_by_delimiter).to eq(dn_elements)
    end

    it "should parse each key value pair and assign an attribute" do
      expect(valid_subject.c).to eq('US')
      expect(valid_subject.o).to eq('RB')
    end

    it "should return appropriate attrs as arrays" do
      expect(valid_subject.ou).to eq(['PEOPLE','JS','DEV'])
      expect(DN.new(dn_string: dn_to_s).ou).to eq(['PEOPLE','JS','DEV'])
      dn = DN.new(dn_string: '/C=US/O=RB/OU=DEV/OU=JS/OU=People/DC=example/DC=org/CN=Last First M (initial)')
      expect(dn.ou).to eq(['PEOPLE', 'JS', 'DEV'])
      expect(dn.dc).to eq(['ORG', 'EXAMPLE'])
    end

    it "should handle multiple RDN key value pairs in the CN and return an array of elements" do
      dn = DN.new(dn_string: '/C=US/O=RB/OU=DEV/OU=JS/OU=People/DC=example/DC=org/CN=Last First M (initial)+email=<EMAIL>+office=home')
      expect(dn.cn).to eq({CN: 'LAST FIRST M (INITIAL)', EMAIL: '<EMAIL>', OFFICE: 'HOME'})
    end
  end

  describe "validations" do
    it "should require a parsable DN string" do
      expect{ DN.new(dn_string: "") }.to raise_error(DnDelimiterUnparsableError)
      expect{ DN.new(dn_string: "nope") }.to raise_error(DnDelimiterUnparsableError)
    end
  end

  describe ".to_s" do
    it "should return a properly formatted string for CAS & RFC1779 use" do
      dn = DN.new(dn_string: '/C=US/O=RB/OU=DEV/OU=JS/OU=People/DC=example/DC=org/CN=Last First M (initial)+email=<EMAIL>+office=home')
      dn_string = 'CN=LAST FIRST M (INITIAL)+EMAIL=<EMAIL>+OFFICE=HOME,O=RB,OU=PEOPLE,OU=JS,OU=DEV,C=US,DC=ORG,DC=EXAMPLE'
      expect(dn.to_s).to eq(dn_string)
    end

    it "should support customized return order for RDN elements" do
      dn_string = 'CN=Last First M (initial),O=rb,OU=people,C=us,DC=org,DC=example'
      dn = DN.new(dn_string: dn_string, string_order: %w(cn ou o dc c))
      customized_string = 'CN=LAST FIRST M (INITIAL),OU=PEOPLE,O=RB,DC=ORG,DC=EXAMPLE,C=US'
      expect(dn.to_s).to eq(customized_string)
    end

#    it "should parse common DN formats into DN objects" do
#      File.readlines('spec/fixtures/common_dns.txt').each do |line|
#        dn_in = line.rstrip.split('%')[0]
#        dn_out = line.rstrip.split('%')[1]
#        expect(DN.new(dn_string: dn_in).to_s).to eq(dn_out)
#      end
#    end
  end
end<commit_msg>Custom DN samples spec<commit_after>require "spec_helper"

describe DN do
  let!(:raw_dn) { '/C=US/O=RB/OU=DEV/OU=JS/OU=People/CN=Last First M (initial)' }
  let!(:dn_to_s) { 'CN=LAST FIRST M (INITIAL),OU=PEOPLE,OU=JS,OU=DEV,O=RB,C=US' }
  let(:valid_subject) { DN.new(dn_string: raw_dn) }
  let(:dn_elements) { ["CN=LAST FIRST M (INITIAL)", "OU=PEOPLE", "OU=JS", "OU=DEV", "O=RB", "C=US"] }

  #
  # Unit specs
  #
  describe "#new" do
    it "should create a new DN object when valid" do
      expect(valid_subject.class.to_s).to eq(DN.to_s)
    end

    it "should identify the most likely delimiter" do
      expect(valid_subject.delimiter).to eq('/')
      %w(, ; - _ : \\ % ^ ! & * @ # $).each do |delim|
        custom_delim_subject = DN.new(dn_string: raw_dn.gsub('/',delim))
        expect(custom_delim_subject.delimiter).to eq(delim)
      end
    end

    it "should split on identified delimiter" do
      expect(valid_subject.split_by_delimiter).to eq(dn_elements)
    end

    it "should parse each key value pair and assign an attribute" do
      expect(valid_subject.c).to eq('US')
      expect(valid_subject.o).to eq('RB')
    end

    it "should return appropriate attrs as arrays" do
      expect(valid_subject.ou).to eq(['PEOPLE','JS','DEV'])
      expect(DN.new(dn_string: dn_to_s).ou).to eq(['PEOPLE','JS','DEV'])
      dn = DN.new(dn_string: '/C=US/O=RB/OU=DEV/OU=JS/OU=People/DC=example/DC=org/CN=Last First M (initial)')
      expect(dn.ou).to eq(['PEOPLE', 'JS', 'DEV'])
      expect(dn.dc).to eq(['ORG', 'EXAMPLE'])
    end

    it "should handle multiple RDN key value pairs in the CN and return an array of elements" do
      dn = DN.new(dn_string: '/C=US/O=RB/OU=DEV/OU=JS/OU=People/DC=example/DC=org/CN=Last First M (initial)+email=<EMAIL>+office=home')
      expect(dn.cn).to eq({CN: 'LAST FIRST M (INITIAL)', EMAIL: '<EMAIL>', OFFICE: 'HOME'})
    end
  end

  describe "validations" do
    it "should require a parsable DN string" do
      expect{ DN.new(dn_string: "") }.to raise_error(DnDelimiterUnparsableError)
      expect{ DN.new(dn_string: "nope") }.to raise_error(DnDelimiterUnparsableError)
    end
  end

  describe ".to_s" do
    it "should return a properly formatted string for CAS & RFC1779 use" do
      dn = DN.new(dn_string: '/C=US/O=RB/OU=DEV/OU=JS/OU=People/DC=example/DC=org/CN=Last First M (initial)+email=<EMAIL>+office=home')
      dn_string = 'CN=LAST FIRST M (INITIAL)+EMAIL=<EMAIL>+OFFICE=HOME,O=RB,OU=PEOPLE,OU=JS,OU=DEV,C=US,DC=ORG,DC=EXAMPLE'
      expect(dn.to_s).to eq(dn_string)
    end

    it "should support customized return order for RDN elements" do
      dn_string = 'CN=Last First M (initial),O=rb,OU=people,C=us,DC=org,DC=example'
      dn = DN.new(dn_string: dn_string, string_order: %w(cn ou o dc c))
      customized_string = 'CN=LAST FIRST M (INITIAL),OU=PEOPLE,O=RB,DC=ORG,DC=EXAMPLE,C=US'
      expect(dn.to_s).to eq(customized_string)
    end

    it "should parse custom DN formats into DN objects" do
      # Sample data sets
      File.readlines('spec/fixtures/custom_sort_dns.txt').each do |line|
        dn_in = line.rstrip.split('%')[0]
        dn_out = line.rstrip.split('%')[1]
        custom_order = %w(cn l st ou o c street dc uid)
        expect(DN.new(dn_string: dn_in, string_order: custom_order).to_s).to eq(dn_out)
      end
    end

    it "should parse common DN formats into DN objects" do
      pending "Implement common DN test strings"
      File.readlines('spec/fixtures/common_dns.txt').each do |line|
        dn_in = line.rstrip.split('%')[0]
        dn_out = line.rstrip.split('%')[1]
        expect(DN.new(dn_string: dn_in).to_s).to eq(dn_out)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.15807012460156478, 'fraction_numerical': 0.002028397565922921, 'mean_word_length': 3.5534300791556728, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6469151', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2587, 'n_words': 573}