<commit_before># encoding: UTF-8

require 'vines'
require 'minitest/autorun'

class HostTest < MiniTest::Unit::TestCase
  def test_missing_storage
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          # missing storage
        end
      end
    end
  end

  def test_bad_storage
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage 'bogus' do
            # no bogus storage implementation
          end
        end
      end
    end
  end

  def test_duplicate_storage
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage('fs') { dir '.' }
          storage('fs') { dir '.' }
        end
      end
    end
  end

  def test_good_storage_raises_no_errors
    config = Vines::Config.new do
      host 'wonderland.lit' do
        storage 'fs' do
          dir '.'
        end
      end
    end
    refute_nil config.vhosts['wonderland.lit'].storage
  end

  def test_ldap_added_to_storage
    config = Vines::Config.new do
      host 'wonderland.lit' do
        storage(:fs) { dir '.' }
        # added after storage
        ldap 'ldap.wonderland.lit', 1636 do
          tls true
          dn 'cn=Directory Manager'
          password '<PASSWORD>'
          basedn 'dc=wonderland,dc=lit'
          groupdn 'cn=chatters,dc=wonderland,dc=lit'
          object_class 'person'
          user_attr 'uid'
          name_attr 'cn'
        end
      end

      host 'verona.lit' do
        ldap 'ldap.verona.lit', 1636 do
          tls true
          dn 'cn=Directory Manager'
          password '<PASSWORD>'
          basedn 'dc=wonderland,dc=lit'
          object_class 'person'
          user_attr 'uid'
          name_attr 'cn'
        end
        # added before storage
        storage(:fs) { dir '.' }
      end
    end
    %w[wonderland.lit verona.lit].each do |domain|
      refute_nil config.vhosts[domain].storage.ldap
      assert config.vhosts[domain].storage.ldap?
    end
  end

  def test_empty_component_name_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components '' => 'secr3t'
        end
      end
    end
  end

  def test_nil_component_name_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components nil => 'secr3t'
        end
      end
    end
  end

  def test_empty_component_password_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components 'tea' => ''
        end
      end
    end
  end

  def test_nil_component_password_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components 'tea' => nil
        end
      end
    end
  end

  def test_duplicate_component_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components 'tea' => 'one'
          components 'TEA' => 'two'
        end
      end
    end
  end

  def test_duplicate_component_in_one_call_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components 'tea' => 'one', 'TEA' => 'two'
        end
      end
    end
  end

  def test_duplicate_component_symbol_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components 'tea' => 'one'
          components :TEA => 'two'
        end
      end
    end<commit_msg>Add test for lowercase component domain names.<commit_after># encoding: UTF-8

require 'vines'
require 'minitest/autorun'

class HostTest < MiniTest::Unit::TestCase
  def test_missing_storage
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          # missing storage
        end
      end
    end
  end

  def test_bad_storage
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage 'bogus' do
            # no bogus storage implementation
          end
        end
      end
    end
  end

  def test_duplicate_storage
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage('fs') { dir '.' }
          storage('fs') { dir '.' }
        end
      end
    end
  end

  def test_good_storage_raises_no_errors
    config = Vines::Config.new do
      host 'wonderland.lit' do
        storage 'fs' do
          dir '.'
        end
      end
    end
    refute_nil config.vhosts['wonderland.lit'].storage
  end

  def test_ldap_added_to_storage
    config = Vines::Config.new do
      host 'wonderland.lit' do
        storage(:fs) { dir '.' }
        # added after storage
        ldap 'ldap.wonderland.lit', 1636 do
          tls true
          dn 'cn=Directory Manager'
          password '<PASSWORD>'
          basedn 'dc=wonderland,dc=lit'
          groupdn 'cn=chatters,dc=wonderland,dc=lit'
          object_class 'person'
          user_attr 'uid'
          name_attr 'cn'
        end
      end

      host 'verona.lit' do
        ldap 'ldap.verona.lit', 1636 do
          tls true
          dn 'cn=Directory Manager'
          password '<PASSWORD>'
          basedn 'dc=wonderland,dc=lit'
          object_class 'person'
          user_attr 'uid'
          name_attr 'cn'
        end
        # added before storage
        storage(:fs) { dir '.' }
      end
    end
    %w[wonderland.lit verona.lit].each do |domain|
      refute_nil config.vhosts[domain].storage.ldap
      assert config.vhosts[domain].storage.ldap?
    end
  end

  def test_empty_component_name_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components '' => 'secr3t'
        end
      end
    end
  end

  def test_nil_component_name_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components nil => 'secr3t'
        end
      end
    end
  end

  def test_empty_component_password_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components 'tea' => ''
        end
      end
    end
  end

  def test_nil_component_password_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components 'tea' => nil
        end
      end
    end
  end

  def test_duplicate_component_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components 'tea' => 'one'
          components 'TEA' => 'two'
        end
      end
    end
  end

  def test_duplicate_component_in_one_call_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components 'tea' => 'one', 'TEA' => 'two'
        end
      end
    end
  end

  def test_duplicate_component_symbol_raises
    assert_raises(RuntimeError) do
      Vines::Config.new do
        host 'wonderland.lit' do
          storage(:fs) { dir '.' }
          components 'tea' => 'one'
          components :TEA => 'two'
        end
      end
    end
  end

  def test_case_insensitive_component_name
    config = Vines::Config.new do
      host 'WONDERLAND.LIT' do
        storage(:fs) { dir '.' }
        components 'TEA' => 'secr3t', CAKE: 'Passw0rd'
      end
    end
    host = config.vhosts['wonderland.lit']
    refute_nil host
    assert_equal 2, host.components.size
    assert_equal host.components['tea.wonderland.lit'], 'secr3t'
    assert_equal host.components['cake.wonderland.lit'], 'Passw0rd'
{'fraction_non_alphanumeric': 0.09196243444322479, 'fraction_numerical': 0.003293084522502744, 'mean_word_length': 1.9026548672566372, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 28, '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': '4005465', 'n_tokens_mistral': 2664, 'n_tokens_neox': 2608, 'n_words': 612}