File: test_attributes.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 (183 lines) | stat: -rw-r--r-- 5,548 bytes parent folder | download | duplicates (4)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-

require 'al-test-utils'

class TestAttributes < Test::Unit::TestCase
  include AlTestUtils

  priority :must

  priority :normal
  def test_to_real_attribute_name
    user = @user_class.new("user")
    assert_equal("objectClass",
                 user.__send__(:to_real_attribute_name, "objectclass"))
    assert_equal("objectClass",
                 user.__send__(:to_real_attribute_name, "objectclass", true))
    assert_nil(user.__send__(:to_real_attribute_name, "objectclass", false))
  end

  def test_normalize_attribute
    assert_normalize_attribute(["usercertificate", [{"binary" => []}]],
                               "userCertificate",
                               [])
    assert_normalize_attribute(["usercertificate", [{"binary" => []}]],
                               "userCertificate",
                               nil)
    assert_normalize_attribute(["usercertificate",
                                [{"binary" => "BINARY DATA"}]],
                               "userCertificate",
                               "BINARY DATA")
    assert_normalize_attribute(["usercertificate",
                                [{"binary" => ["BINARY DATA"]}]],
                               "userCertificate",
                               {"binary" => ["BINARY DATA"]})
  end

  def test_unnormalize_attribute
    assert_unnormalize_attribute({"sn" => ["Surname"]},
                                 "sn",
                                 ["Surname"])
    assert_unnormalize_attribute({"userCertificate;binary" => []},
                                 "userCertificate",
                                 [{"binary" => []}])
    assert_unnormalize_attribute({"userCertificate;binary" => ["BINARY DATA"]},
                                 "userCertificate",
                                 [{"binary" => ["BINARY DATA"]}])
    assert_unnormalize_attribute({
                                   "sn" => ["Yamada"],
                                   "sn;lang-ja" => ["山田"],
                                   "sn;lang-ja;phonetic" => ["やまだ"]
                                 },
                                 "sn",
                                 ["Yamada",
                                  {"lang-ja" => ["山田",
                                                 {"phonetic" => ["やまだ"]}]}])
  end

  private
  def assert_normalize_attribute(expected, name, value)
    assert_equal(expected, ActiveLdap::Base.normalize_attribute(name, value))
  end

  def assert_unnormalize_attribute(expected, name, value)
    assert_equal(expected, ActiveLdap::Base.unnormalize_attribute(name, value))
  end

  class TestBlankValue < self
    private
    def assert_blank_value(value)
      assert_true(ActiveLdap::Base.blank_value?(value),
                  "value: <#{value.inspect}>")
    end

    def assert_not_blank_value(value)
      assert_false(ActiveLdap::Base.blank_value?(value),
                   "value: <#{value.inspect}>")
    end

    class TestHash < self
      def test_empty
        assert_blank_value({})
      end

      def test_have_elements
        assert_not_blank_value({"name" => "Taro", "age" => 29})
      end

      def test_have_blank_element
        assert_not_blank_value({"name" => nil, "age" => 29})
      end

      def test_all_blank_elements
        assert_blank_value({"name" => nil, "age" => nil})
      end
    end

    class TestArray < self
      def test_empty
        assert_blank_value([])
      end

      def test_have_elements
        assert_not_blank_value(["Taro", "Jiro"])
      end

      def test_have_blank_element
        assert_not_blank_value(["Taro", nil])
      end

      def test_all_blank_elements
        assert_blank_value([nil, nil])
      end
    end

    class TestString < self
      def test_empty
        assert_blank_value("")
      end

      def test_only_spaces
        assert_blank_value(" \t\n")
      end

      def test_have_non_spaces
        assert_not_blank_value("Taro")
      end
    end

    class TestBoolean < self
      def test_true
        assert_not_blank_value(true)
      end

      def test_false
        assert_not_blank_value(true)
      end
    end
  end

  class TestMassAssignment < self
    def test_forbid
      attributes = {:cn => "Alice"}
      def attributes.permitted?
        false
      end
      assert_raise(ActiveModel::ForbiddenAttributesError) do
        @user_class.new(attributes)
      end
    end

    def test_permit
      attributes = {:cn => "Alice"}
      def attributes.permitted?
        true
      end
      alice = @user_class.new(attributes)
      assert_equal("Alice", alice.cn)
    end

    def test_forbid_object_class
      classes = @user_class.required_classes + ["inetOrgPerson"]
      user = @user_class.new(:uid => "XXX", :object_class => classes)
      assert_equal(["inetOrgPerson"],
                   user.classes -  @user_class.required_classes)

      user = @user_class.new(:uid => "XXX", :object_class => ['inetOrgPerson'])
      assert_equal(["inetOrgPerson"],
                   user.classes -  @user_class.required_classes)

      user = @user_class.new("XXX")
      assert_equal([], user.classes -  @user_class.required_classes)
      user.attributes = {:object_class => classes}
      assert_equal([], user.classes -  @user_class.required_classes)
    end

    def test_rename
      make_temporary_user(:simple => true) do |user,|
        assert_true(user.update_attributes(:id => "user2"))
        assert_equal("user2", user.id)
      end
    end
  end
end