passiveldap 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,87 @@
1
+ require 'test/unit'
2
+ require 'passiveldap'
3
+
4
+ require 'tests/user.rb'
5
+
6
+ # test attribute changing
7
+ class Test_PassiveLDAP_Attributes < Test::Unit::TestCase
8
+ # creates a new user 12345 in the directory
9
+ # tests finding objects, creation of objects, mandatory_attribute insertions and such
10
+ def setup
11
+ a = TestUser.new(12345)
12
+ a.save!
13
+ end
14
+
15
+ # deletes the user 12345 from the directory
16
+ # tests finding objects, and the destroy method
17
+ def teardown
18
+ a = TestUser.new(12345)
19
+ a.destroy
20
+ end
21
+
22
+ # tests adding a new attribute to a record
23
+ def test_add_attribute
24
+ a = TestUser.new(12345)
25
+ a.mail = "test@mail.com"
26
+ a.save!
27
+ b = TestUser.new(12345)
28
+ assert_equal("test@mail.com",b.mail)
29
+ assert_raise(PassiveLDAP::AttributeAssignmentError) {
30
+ b.mail = ["not@good.de"]
31
+ }
32
+ assert_raise(PassiveLDAP::AttributeAssignmentError) {
33
+ b[:nonexistent] = "not@good.de"
34
+ }
35
+ end
36
+
37
+ # tests deletion of an attribute
38
+ def test_delete_attribute
39
+ a = TestUser.new(12345)
40
+ a.mail = "test@mail.com"
41
+ a.save!
42
+ b = TestUser.new(12345)
43
+ assert_equal("test@mail.com",b.mail)
44
+ b.mail = nil
45
+ b.save!
46
+ c = TestUser.new(12345)
47
+ assert_nil(c.mail)
48
+ end
49
+
50
+ # tests adding multi_valued attributes
51
+ def test_add_multi_attribute
52
+ a = TestUser.new(12345)
53
+ a.other_mailbox = ["test@mail.com", "othertest@mail.hu"]
54
+ a.save!
55
+ b = TestUser.new(12345)
56
+ assert_equal(["test@mail.com", "othertest@mail.hu"].sort,b.other_mailbox.sort)
57
+ assert_raise(PassiveLDAP::AttributeAssignmentError) {
58
+ b.other_mailbox = "not@good.de"
59
+ }
60
+ end
61
+
62
+ # tests adding values to a multi_valued attribute
63
+ def test_add_value_to_multi
64
+ a = TestUser.new(12345)
65
+ a.other_mailbox = ["test@mail.com"]
66
+ a.save!
67
+ b = TestUser.new(12345)
68
+ assert_equal(["test@mail.com"],b.other_mailbox)
69
+ b.other_mailbox <<= "othertest@mail.hu"
70
+ b.save!
71
+ c = TestUser.new(12345)
72
+ assert_equal(["test@mail.com", "othertest@mail.hu"].sort,c.other_mailbox.sort)
73
+ end
74
+
75
+ # test deletion of a value of a multi_valued attribute
76
+ def test_delete_value_from_multi
77
+ a = TestUser.new(12345)
78
+ a.other_mailbox = ["test@mail.com", "othertest@mail.hu"]
79
+ a.save!
80
+ b = TestUser.new(12345)
81
+ assert_equal(["test@mail.com", "othertest@mail.hu"].sort,b.other_mailbox.sort)
82
+ b.other_mailbox = ["othertest@mail.hu"]
83
+ b.save!
84
+ c = TestUser.new(12345)
85
+ assert_equal(["othertest@mail.hu"],c.other_mailbox)
86
+ end
87
+ end
@@ -0,0 +1,77 @@
1
+ require 'test/unit'
2
+ require 'passiveldap'
3
+
4
+ require 'tests/user.rb'
5
+
6
+ # tests class methods
7
+ class Test_PassiveLDAP_Class < Test::Unit::TestCase
8
+ # tests count method
9
+ def test_count
10
+ count = TestUser.count
11
+ a = TestUser.new(12345)
12
+ a.save!
13
+ assert_equal(count+1,TestUser.count)
14
+ a.destroy
15
+ end
16
+
17
+ # tests column_names method
18
+ def test_column_names
19
+ assert_equal(%w{id common_name surname mail other_mailbox}.sort,TestUser.column_names.sort)
20
+ # test twice because of caching
21
+ assert_equal(%w{id common_name surname mail other_mailbox}.sort,TestUser.column_names.sort)
22
+
23
+ # test columns and columns_hash
24
+ assert_equal(:integer,TestUser.columns_hash["id"].type)
25
+ assert_equal(:string,TestUser.columns_hash["common_name"].type)
26
+ assert_equal(:text,TestUser.columns_hash["other_mailbox"].type)
27
+ end
28
+
29
+ # tests exists? method
30
+ def test_exists?
31
+ a = TestUser.new(12345)
32
+ a.save!
33
+ assert(TestUser.exists?(12345), "User should exist")
34
+ assert(TestUser.exists?(Net::LDAP::Filter.eq("uidnumber","12345")), "User should exist")
35
+ a.destroy
36
+ assert(!TestUser.exists?(12345), "User shouldn't exist")
37
+ assert(!TestUser.exists?(Net::LDAP::Filter.eq("uidnumber","12345")), "User shouldn't exist")
38
+ end
39
+
40
+ # tests create
41
+ def test_create
42
+ TestUser.create(:id => 12345, :mail => "mail@mail.com")
43
+ a = TestUser.new(12345)
44
+ assert_equal("mail@mail.com",a.mail)
45
+ a.destroy
46
+ a = TestUser.create([{:id => 12345, :mail => "mail@mail.com"},{:id => 12346, :mail => "other@other.com"}])
47
+ assert_equal("mail@mail.com",a[0].mail)
48
+ assert_equal("other@other.com",a[1].mail)
49
+ a[0] = TestUser.new(12345)
50
+ a[1] = TestUser.new(12346)
51
+ assert_equal("mail@mail.com",a[0].mail)
52
+ assert_equal("other@other.com",a[1].mail)
53
+ a[0].destroy
54
+ a[1].destroy
55
+ end
56
+
57
+ # tests delete
58
+ def test_delete
59
+ TestUser.create(:id => 12345)
60
+ assert(TestUser.exists?(12345), "User should exist")
61
+ TestUser.delete(12345)
62
+ assert(!TestUser.exists?(12345), "User shouldn't exist")
63
+ end
64
+
65
+ # tests update
66
+ def test_update
67
+ TestUser.create([{:id => 12345},{:id=>12346}])
68
+ a = TestUser.update(12345, :mail => "other@mail.com")
69
+ assert_equal("other@mail.com",a.mail)
70
+ a = TestUser.update([12345,12346],[{:mail => "1@2.com"},{:mail => "2@1.com"}])
71
+ assert_equal("1@2.com",a[0].mail)
72
+ assert_equal("2@1.com",a[1].mail)
73
+ a[0].destroy
74
+ a[1].destroy
75
+ end
76
+ end
77
+
@@ -0,0 +1,98 @@
1
+ require 'test/unit'
2
+ require 'passiveldap'
3
+
4
+ require 'tests/user.rb'
5
+
6
+ # tests instance methods
7
+ class Test_PassiveLDAP_Instance < Test::Unit::TestCase
8
+ # tests distinguished name changeability
9
+ def test_dn_change
10
+ a = TestUser.new(12345)
11
+ a.save!
12
+ a = TestUser.new(12345)
13
+ assert_raise(PassiveLDAP::DistinguishedNameException) {
14
+ a.dn = "otherdn"
15
+ }
16
+ a.destroy
17
+ a = TestUser.new(12345)
18
+ assert_nothing_raised {
19
+ a.dn = "otherdn"
20
+ }
21
+ end
22
+
23
+ # tests old attribute getter method
24
+ def test_get_old_attribute
25
+ a = TestUser.new(12345)
26
+ a.mail = "old@mail.com"
27
+ a.save!
28
+ a = TestUser.new(12345)
29
+ a.mail = "new@mail.hu"
30
+ assert_equal("old@mail.com",a.get_old_attribute(:mail))
31
+ assert_equal("new@mail.hu",a.mail)
32
+ a.destroy
33
+ end
34
+
35
+ # tests attributes method
36
+ def test_attributes
37
+ a = TestUser.new(12345)
38
+ a.destroy unless a.new_record?
39
+ # cleared the record
40
+ a = TestUser.new(12345)
41
+ assert_equal({:id => 12345},a.attributes)
42
+ end
43
+
44
+ # tests attributes= method
45
+ def test_attributes_set
46
+ a = TestUser.new(12345)
47
+ a.attributes= {:mail => "a@b.com", :other_mailbox => ["aa@bb.com"]}
48
+ assert_equal("a@b.com",a.mail)
49
+ assert_equal(["aa@bb.com"],a.other_mailbox)
50
+ end
51
+
52
+ # tests reload method
53
+ def test_reload
54
+ a = TestUser.new(12345)
55
+ a.destroy unless a.new_record?
56
+ a = TestUser.new(12345)
57
+ a.other_mailbox = [ "one@two.com", "three@four.hu" ]
58
+ a.mail = "first@second.com"
59
+ a.save!
60
+ a = TestUser.new(12345)
61
+ assert_equal("first@second.com",a.mail)
62
+ assert_equal([ "one@two.com", "three@four.hu" ].sort,a.other_mailbox.sort)
63
+ a.other_mailbox = [ "one@two.com" ]
64
+ a.mail = "second@first.com"
65
+ assert_equal("second@first.com",a.mail)
66
+ assert_equal([ "one@two.com"],a.other_mailbox)
67
+ a.reload(:oldattr => true)
68
+ assert_equal("second@first.com",a.mail)
69
+ assert_equal([ "one@two.com"],a.other_mailbox)
70
+ a.reload()
71
+ assert_equal("first@second.com",a.mail)
72
+ assert_equal([ "one@two.com", "three@four.hu" ].sort,a.other_mailbox.sort)
73
+ a.destroy
74
+ end
75
+
76
+ # tests update_attribute* methods
77
+ def test_updates
78
+ a = TestUser.new(12345)
79
+ a.destroy unless a.new_record?
80
+ a = TestUser.new(12345)
81
+ a.save!
82
+ a.update_attribute(:mail,"mail@mail.com")
83
+ assert_equal("mail@mail.com",a.mail)
84
+ b = TestUser.new(12345)
85
+ assert_equal("mail@mail.com",b.mail)
86
+ b.destroy
87
+ a = TestUser.new(12345)
88
+ a.save!
89
+ a.update_attributes(:mail => "mail@mail.com", :other_mailbox => ["other@other.com"])
90
+ assert_equal("mail@mail.com",a.mail)
91
+ assert_equal(["other@other.com"],a.other_mailbox)
92
+ b = TestUser.new(12345)
93
+ assert_equal("mail@mail.com",b.mail)
94
+ assert_equal(["other@other.com"],b.other_mailbox)
95
+ b.destroy
96
+ end
97
+ end
98
+
@@ -0,0 +1,55 @@
1
+ require 'test/unit'
2
+ require 'passiveldap'
3
+
4
+ require 'tests/user.rb'
5
+
6
+ # tests whether ActiveRecord::Validation integration works
7
+ #
8
+ # also tests some advanced PassiveLDAP mechanism, like array_separators, or
9
+ # type conversions
10
+ class Test_PassiveLDAP_Validation < Test::Unit::TestCase
11
+ # tests validations
12
+ def test_validated_user
13
+ a = ValidatedUser.new(12345)
14
+ a.destroy unless a.new_record?
15
+ assert_raise(ActiveRecord::RecordInvalid) {
16
+ a.save!
17
+ }
18
+ a.mail = "abcd"
19
+ a.description = "Hi!"
20
+ assert_raise(ActiveRecord::RecordInvalid) {
21
+ a.save!
22
+ }
23
+ a.mail = "ab@bc.de"
24
+ assert_nothing_raised {
25
+ a.save!
26
+ }
27
+ a.other_mailbox = "one@mail.com\ntwo@mail.com"
28
+ assert_nothing_raised {
29
+ a.save!
30
+ }
31
+ a.other_mailbox = "notanemail\ntwo@gmail.com"
32
+ assert_raise(ActiveRecord::RecordInvalid) {
33
+ a.save!
34
+ }
35
+ a.destroy
36
+ end
37
+
38
+ # tests array_separator
39
+ def test_array_separator
40
+ a = ValidatedUser.new(12345)
41
+ a.other_mailbox = "one@mail.com\ntwo@mail.com"
42
+ assert_equal(["one@mail.com","two@mail.com"].sort,a.get_attribute(:other_mailbox).sort)
43
+ a.set_attribute(:other_mailbox,["ab@cd.ef","gh@ij.kl"])
44
+ assert((a.other_mailbox == "ab@cd.ef\ngh@ij.kl") || (a.other_mailbox == "gh@ij.kl\nab@cd.ef"),"Array to string conversion unsuccesful!")
45
+ end
46
+
47
+ def test_type_conversion
48
+ a = ValidatedUser.new(12345)
49
+ a.givenname = "Mr. Doe"
50
+ assert_equal("Doe",a.get_attribute(:givenname))
51
+ a.set_attribute(:givenname, "Joe")
52
+ assert_equal("Mr. Joe",a.givenname)
53
+ end
54
+ end
55
+
@@ -0,0 +1,5 @@
1
+ require "test/unit"
2
+ require "tests/tc_attributes"
3
+ require "tests/tc_class"
4
+ require "tests/tc_instance"
5
+ require "tests/tc_validate"
@@ -0,0 +1,39 @@
1
+ #test user subclass used in tests. Works with ADAM. Change the GUID
2
+ #and recordscope to your server's config
3
+ #
4
+ require 'passiveldap'
5
+
6
+ class TestUser < PassiveLDAP::Base
7
+ # change the recordbase to your recordbase!
8
+ passive_ldap \
9
+ :id_attribute => :uidnumber,
10
+ :record_base => "cn=Users,dc=eotvos,dc=elte,dc=hu"
11
+ passive_ldap_attr \
12
+ :cn => {:name => :common_name, :default_value => Proc.new {|s| "test_#{s.id}" } },
13
+ :sn => {:name => :surname},
14
+ :mail => {},
15
+ :othermailbox => {:multi_valued => true, :name => :other_mailbox},
16
+ :dn => { :default_value => Proc.new { |s| "cn=test_#{s.id},#{s.class.settings[:record_base]}" }, :hidden => true },
17
+ :objectclass => { :default_value => %w{top person organizationalPerson user}, :multi_valued => true, :hidden => true }
18
+ end
19
+
20
+ class ValidatedUser < PassiveLDAP::Base
21
+ # change the recordbase to your recordbase!
22
+ passive_ldap \
23
+ :id_attribute => :uidnumber,
24
+ :record_base => "cn=Users,dc=eotvos,dc=elte,dc=hu",
25
+ :default_array_separator => "\n"
26
+ passive_ldap_attr \
27
+ :cn => {:name => :common_name, :default_value => Proc.new {|s| "test_#{s.id}" } },
28
+ :sn => {:name => :surname},
29
+ :mail => {},
30
+ :description => {},
31
+ :othermailbox => {:multi_valued => true, :name => :other_mailbox},
32
+ :givenname => { :type => { :from => Proc.new { |s| "Mr. #{s}" }, :to => Proc.new { |s| s[4..-1] } } }, # Adds or cuts the "Mr. " prefix
33
+ :dn => { :default_value => Proc.new { |s| "cn=test_#{s.id},#{s.class.settings[:record_base]}" }, :hidden => true },
34
+ :objectclass => { :default_value => %w{top person organizationalPerson user}, :multi_valued => true, :hidden => true }
35
+
36
+ validates_presence_of :description
37
+ validates_format_of :mail, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
38
+ validates_format_of_each :other_mailbox, :with => /\A(?:(?:[^@;\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))\Z/i, :unless => Proc.new { |s| s.other_mailbox.nil? or s.other_mailbox == "" }
39
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: passiveldap
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2008-04-03 00:00:00 +02:00
8
+ summary: An Active Directory compatible LDAP-ActiveRecord interoperability library.
9
+ require_paths:
10
+ - lib
11
+ email: mail@sztupy.hu
12
+ homepage: http://passiveldap.sztupy.hu
13
+ rubyforge_project: passiveldap
14
+ description: PassiveLDAP is an LDAP-ActiveRecord interoperability library designed to allow an ActiveRecord model (usually the Users model) to be replaced with an LDAP-based model. Active Directory examples included!
15
+ autorequire: passiveldap
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - "Zsolt Sz. Sztup\xC3\xA1k"
31
+ files:
32
+ - README
33
+ - LICENCE
34
+ - ChangeLog
35
+ - COPYING
36
+ - lib/passiveldap.rb
37
+ - lib/user.rb
38
+ - tests/scheme_adam.ldif
39
+ - tests/tc_attributes.rb
40
+ - tests/tc_class.rb
41
+ - tests/tc_instance.rb
42
+ - tests/tc_validate.rb
43
+ - tests/ts_all.rb
44
+ - tests/user.rb
45
+ - tests/README
46
+ test_files:
47
+ - tests/ts_all.rb
48
+ rdoc_options:
49
+ - --title
50
+ - PassiveLDAP
51
+ - --main
52
+ - README
53
+ - --line-numbers
54
+ extra_rdoc_files:
55
+ - README
56
+ - ChangeLog
57
+ - LICENCE
58
+ - COPYING
59
+ - tests/README
60
+ executables: []
61
+
62
+ extensions: []
63
+
64
+ requirements: []
65
+
66
+ dependencies:
67
+ - !ruby/object:Gem::Dependency
68
+ name: ruby-net-ldap
69
+ version_requirement:
70
+ version_requirements: !ruby/object:Gem::Version::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 0.0.4
75
+ version:
76
+ - !ruby/object:Gem::Dependency
77
+ name: activerecord
78
+ version_requirement:
79
+ version_requirements: !ruby/object:Gem::Version::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.0.2
84
+ version:
85
+ - !ruby/object:Gem::Dependency
86
+ name: activesupport
87
+ version_requirement:
88
+ version_requirements: !ruby/object:Gem::Version::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.0.2
93
+ version: