File: test_connection.rb

package info (click to toggle)
ruby-activeldap 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,588 kB
  • sloc: ruby: 18,143; sh: 12; makefile: 5
file content (89 lines) | stat: -rw-r--r-- 2,976 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'al-test-utils'

class TestConnection < Test::Unit::TestCase
  include AlTestUtils::Config
  include AlTestUtils::MockLogger
  include AlTestUtils::Omittable

  def setup
    super
  end

  def teardown
    ActiveLdap::Base.clear_active_connections!
    super
  end

  priority :must
  def test_retry_limit_0_with_existent_host
    config = current_configuration.merge("retry_limit" => 0)
    ActiveLdap::Base.setup_connection(config)
    assert_nothing_raised do
      ActiveLdap::Base.find(:all)
    end
  end

  def test_retry_limit_0_with_nonexistent_host_with_timeout
    omit_if_ldap("this test will take a long time...")
    omit_if_jruby("JNI adapter returns connection error immediately. " +
                  "So timeout isn't invoked.")
    config = current_configuration.merge("host" => "192.168.29.29",
                                         "retry_limit" => 0,
                                         "timeout" => 1)
    ActiveLdap::Base.setup_connection(config)
    assert_raise(ActiveLdap::TimeoutError) do
      ActiveLdap::Base.find(:first)
    end
  end

  def test_bind_format_check
    connector = Class.new(ActiveLdap::Base)
    assert(!connector.connected?)
    exception = nil
    assert_raises(ArgumentError) do
      begin
        connector.setup_connection(:adapter => adapter,
                                   :bind_format => "uid=%s,dc=test",
                                   :allow_anonymous => false)
        connector.connection.connect
      rescue Exception
        exception = $!
        raise
      end
    end
    expected_message = "Unknown key: :bind_format. Valid keys are: "
    valid_keys = ActiveLdap::Adapter::Base::VALID_ADAPTER_CONFIGURATION_KEYS
    expected_message << valid_keys.collect(&:inspect).join(", ")
    assert_equal(expected_message, exception.message)
  end

  def test_can_reconnect?
    assert(!ActiveLdap::Base.connected?)

    config = current_configuration.merge("retry_limit" => 10)
    ActiveLdap::Base.setup_connection(config)
    connection = ActiveLdap::Base.connection
    assert(!connection.send(:can_reconnect?, :reconnect_attempts => 11))

    config = current_configuration.merge("retry_limit" => 10)
    ActiveLdap::Base.setup_connection(config)
    connection = ActiveLdap::Base.connection
    assert(connection.send(:can_reconnect?, :reconnect_attempts => 10))

    config = current_configuration.merge("retry_limit" => -1)
    ActiveLdap::Base.setup_connection(config)
    connection = ActiveLdap::Base.connection
    assert(connection.send(:can_reconnect?, :reconnect_attempts => -10))
  end

  priority :low
  def test_retry_limit_0_with_nonexistent_host
    omit("this test will take a long time...")
    config = current_configuration.merge("host" => "192.168.29.29",
                                         "retry_limit" => 0)
    ActiveLdap::Base.setup_connection(config)
    assert_raise(ActiveLdap::ConnectionError) do
      ActiveLdap::Base.find(:first)
    end
  end
end