##// END OF EJS Templates
Added some tests for the LDAP authentication....
Eric Davis -
r3324:7b6b147761a6
parent child
Show More
@@ -0,0 +1,82
1 dn: dc=redmine,dc=org
2 objectClass: top
3 objectClass: dcObject
4 objectClass: organization
5 o: redmine.org
6 dc: redmine
7 structuralObjectClass: organization
8 entryUUID: 886f5fca-0a87-102e-8d06-67c361d9bd2d
9 creatorsName:
10 createTimestamp: 20090721211642Z
11 entryCSN: 20090721211642.955188Z#000000#000#000000
12 modifiersName:
13 modifyTimestamp: 20090721211642Z
14
15 dn: cn=admin,dc=redmine,dc=org
16 objectClass: simpleSecurityObject
17 objectClass: organizationalRole
18 cn: admin
19 description: LDAP administrator
20 userPassword:: e2NyeXB0fWlWTU9DcUt6WWxXRDI=
21 structuralObjectClass: organizationalRole
22 entryUUID: 88704e44-0a87-102e-8d07-67c361d9bd2d
23 creatorsName:
24 createTimestamp: 20090721211642Z
25 entryCSN: 20090721211642.961418Z#000000#000#000000
26 modifiersName:
27 modifyTimestamp: 20090721211642Z
28
29 dn: ou=Person,dc=redmine,dc=org
30 ou: Person
31 objectClass: top
32 objectClass: organizationalUnit
33 structuralObjectClass: organizationalUnit
34 entryUUID: d39dd388-0c84-102e-82fa-dff86c63a7d6
35 creatorsName: cn=admin,dc=redmine,dc=org
36 createTimestamp: 20090724100222Z
37 entryCSN: 20090724100222.924226Z#000000#000#000000
38 modifiersName: cn=admin,dc=redmine,dc=org
39 modifyTimestamp: 20090724100222Z
40
41 dn: uid=example1,ou=Person,dc=redmine,dc=org
42 objectClass: posixAccount
43 objectClass: top
44 objectClass: inetOrgPerson
45 gidNumber: 0
46 givenName: Example
47 sn: One
48 uid: example1
49 homeDirectory: /home/example1
50 cn: Example One
51 structuralObjectClass: inetOrgPerson
52 entryUUID: 285d304e-0c8a-102e-82fc-dff86c63a7d6
53 creatorsName: cn=admin,dc=redmine,dc=org
54 createTimestamp: 20090724104032Z
55 uidNumber: 0
56 mail: example1@redmine.org
57 userPassword:: e1NIQX1mRXFOQ2NvM1lxOWg1WlVnbEQzQ1pKVDRsQnM9
58 entryCSN: 20090724105945.375801Z#000000#000#000000
59 modifiersName: cn=admin,dc=redmine,dc=org
60 modifyTimestamp: 20090724105945Z
61
62 dn: uid=edavis,ou=Person,dc=redmine,dc=org
63 objectClass: posixAccount
64 objectClass: top
65 objectClass: inetOrgPerson
66 gidNumber: 0
67 givenName: Eric
68 sn: Davis
69 uid: edavis
70 mail: edavis@littlestreamsoftware.com
71 structuralObjectClass: inetOrgPerson
72 entryUUID: 9c5f0502-0c8b-102e-82fe-dff86c63a7d6
73 creatorsName: cn=admin,dc=redmine,dc=org
74 createTimestamp: 20090724105056Z
75 homeDirectory: /home/edavis
76 cn: Eric Davis
77 uidNumber: 0
78 userPassword:: e1NIQX1mRXFOQ2NvM1lxOWg1WlVnbEQzQ1pKVDRsQnM9
79 entryCSN: 20090724105937.734480Z#000000#000#000000
80 modifiersName: cn=admin,dc=redmine,dc=org
81 modifyTimestamp: 20090724105937Z
82
@@ -1,24 +1,35
1 Installing gems for testing
1 Installing gems for testing
2 ===========================
2 ===========================
3
3
4 Run `rake gems RAILS_ENV=test` to list the required gems. Run
4 Run `rake gems RAILS_ENV=test` to list the required gems. Run
5 `rake gems:install RAILS_ENV=test` to install any missing gems.
5 `rake gems:install RAILS_ENV=test` to install any missing gems.
6
6
7 Running Tests
7 Running Tests
8 =============
8 =============
9
9
10 Run `rake --tasks test` to see available tests.
10 Run `rake --tasks test` to see available tests.
11 `rake test` will run the entire testsuite.
11 `rake test` will run the entire testsuite.
12
12
13 Before running `rake test` you need to configure both development
13 Before running `rake test` you need to configure both development
14 and test databases.
14 and test databases.
15
15
16 Creating test repositories
16 Creating test repositories
17 ===================
17 ===================
18
18
19 Redmine supports a wide array of different version control systems.
19 Redmine supports a wide array of different version control systems.
20 To test the support, a test repository needs to be created for each of those.
20 To test the support, a test repository needs to be created for each of those.
21
21
22 Run `rake --tasks test:scm:setup` for a list of available test-repositories or
22 Run `rake --tasks test:scm:setup` for a list of available test-repositories or
23 run `rake test:scm:setup:all` to set up all of them
23 run `rake test:scm:setup:all` to set up all of them
24
24
25 Creating a test ldap database
26 =============================
27
28 Redmine supports using LDAP for user authentications. To test LDAP
29 with Redmine, load the LDAP export from test/fixtures/ldap/test-ldap.ldif
30 into a testing LDAP server. Test that the ldap server can be accessed
31 at 127.0.0.1 on port 389.
32
33 Setting up the test ldap server is beyond the scope of this documentation.
34 The OpenLDAP project provides a simple LDAP implementation that should work
35 good as a test server.
@@ -1,106 +1,111
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 ENV["RAILS_ENV"] ||= "test"
18 ENV["RAILS_ENV"] ||= "test"
19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
20 require 'test_help'
20 require 'test_help'
21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
23
23
24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
25 include ObjectDaddyHelpers
25 include ObjectDaddyHelpers
26
26
27 class ActiveSupport::TestCase
27 class ActiveSupport::TestCase
28 # Transactional fixtures accelerate your tests by wrapping each test method
28 # Transactional fixtures accelerate your tests by wrapping each test method
29 # in a transaction that's rolled back on completion. This ensures that the
29 # in a transaction that's rolled back on completion. This ensures that the
30 # test database remains unchanged so your fixtures don't have to be reloaded
30 # test database remains unchanged so your fixtures don't have to be reloaded
31 # between every test method. Fewer database queries means faster tests.
31 # between every test method. Fewer database queries means faster tests.
32 #
32 #
33 # Read Mike Clark's excellent walkthrough at
33 # Read Mike Clark's excellent walkthrough at
34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
35 #
35 #
36 # Every Active Record database supports transactions except MyISAM tables
36 # Every Active Record database supports transactions except MyISAM tables
37 # in MySQL. Turn off transactional fixtures in this case; however, if you
37 # in MySQL. Turn off transactional fixtures in this case; however, if you
38 # don't care one way or the other, switching from MyISAM to InnoDB tables
38 # don't care one way or the other, switching from MyISAM to InnoDB tables
39 # is recommended.
39 # is recommended.
40 self.use_transactional_fixtures = true
40 self.use_transactional_fixtures = true
41
41
42 # Instantiated fixtures are slow, but give you @david where otherwise you
42 # Instantiated fixtures are slow, but give you @david where otherwise you
43 # would need people(:david). If you don't want to migrate your existing
43 # would need people(:david). If you don't want to migrate your existing
44 # test cases which use the @david style and don't mind the speed hit (each
44 # test cases which use the @david style and don't mind the speed hit (each
45 # instantiated fixtures translates to a database query per test method),
45 # instantiated fixtures translates to a database query per test method),
46 # then set this back to true.
46 # then set this back to true.
47 self.use_instantiated_fixtures = false
47 self.use_instantiated_fixtures = false
48
48
49 # Add more helper methods to be used by all tests here...
49 # Add more helper methods to be used by all tests here...
50
50
51 def log_user(login, password)
51 def log_user(login, password)
52 User.anonymous
52 User.anonymous
53 get "/login"
53 get "/login"
54 assert_equal nil, session[:user_id]
54 assert_equal nil, session[:user_id]
55 assert_response :success
55 assert_response :success
56 assert_template "account/login"
56 assert_template "account/login"
57 post "/login", :username => login, :password => password
57 post "/login", :username => login, :password => password
58 assert_equal login, User.find(session[:user_id]).login
58 assert_equal login, User.find(session[:user_id]).login
59 end
59 end
60
60
61 def uploaded_test_file(name, mime)
61 def uploaded_test_file(name, mime)
62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
63 end
63 end
64
64
65 # Use a temporary directory for attachment related tests
65 # Use a temporary directory for attachment related tests
66 def set_tmp_attachments_directory
66 def set_tmp_attachments_directory
67 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
67 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
68 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
68 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
69 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
69 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
70 end
70 end
71
71
72 def with_settings(options, &block)
72 def with_settings(options, &block)
73 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
73 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
74 options.each {|k, v| Setting[k] = v}
74 options.each {|k, v| Setting[k] = v}
75 yield
75 yield
76 saved_settings.each {|k, v| Setting[k] = v}
76 saved_settings.each {|k, v| Setting[k] = v}
77 end
77 end
78
78
79 def self.ldap_configured?
80 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
81 return @test_ldap.bind
82 end
83
79 # Shoulda macros
84 # Shoulda macros
80 def self.should_render_404
85 def self.should_render_404
81 should_respond_with :not_found
86 should_respond_with :not_found
82 should_render_template 'common/404'
87 should_render_template 'common/404'
83 end
88 end
84
89
85 def self.should_have_before_filter(expected_method, options = {})
90 def self.should_have_before_filter(expected_method, options = {})
86 should_have_filter('before', expected_method, options)
91 should_have_filter('before', expected_method, options)
87 end
92 end
88
93
89 def self.should_have_after_filter(expected_method, options = {})
94 def self.should_have_after_filter(expected_method, options = {})
90 should_have_filter('after', expected_method, options)
95 should_have_filter('after', expected_method, options)
91 end
96 end
92
97
93 def self.should_have_filter(filter_type, expected_method, options)
98 def self.should_have_filter(filter_type, expected_method, options)
94 description = "have #{filter_type}_filter :#{expected_method}"
99 description = "have #{filter_type}_filter :#{expected_method}"
95 description << " with #{options.inspect}" unless options.empty?
100 description << " with #{options.inspect}" unless options.empty?
96
101
97 should description do
102 should description do
98 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
103 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
99 expected = klass.new(:filter, expected_method.to_sym, options)
104 expected = klass.new(:filter, expected_method.to_sym, options)
100 assert_equal 1, @controller.class.filter_chain.select { |filter|
105 assert_equal 1, @controller.class.filter_chain.select { |filter|
101 filter.method == expected.method && filter.kind == expected.kind &&
106 filter.method == expected.method && filter.kind == expected.kind &&
102 filter.options == expected.options && filter.class == expected.class
107 filter.options == expected.options && filter.class == expected.class
103 }.size
108 }.size
104 end
109 end
105 end
110 end
106 end
111 end
@@ -1,36 +1,94
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class AuthSourceLdapTest < ActiveSupport::TestCase
20 class AuthSourceLdapTest < ActiveSupport::TestCase
21
21
22 def setup
22 def setup
23 end
23 end
24
24
25 def test_create
25 def test_create
26 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName')
26 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName')
27 assert a.save
27 assert a.save
28 end
28 end
29
29
30 def test_should_strip_ldap_attributes
30 def test_should_strip_ldap_attributes
31 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
31 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
32 :attr_firstname => 'givenName ')
32 :attr_firstname => 'givenName ')
33 assert a.save
33 assert a.save
34 assert_equal 'givenName', a.reload.attr_firstname
34 assert_equal 'givenName', a.reload.attr_firstname
35 end
35 end
36
37 if ldap_configured?
38 context '#authenticate' do
39 setup do
40 @auth = AuthSourceLdap.generate!(:name => 'on the fly',
41 :host => '127.0.0.1',
42 :port => 389,
43 :base_dn => 'OU=Person,DC=redmine,DC=org',
44 :attr_login => 'uid',
45 :attr_firstname => 'givenName',
46 :attr_lastname => 'sn',
47 :attr_mail => 'mail',
48 :onthefly_register => true)
49
50 end
51
52 context 'with a valid LDAP user' do
53 should 'return the firstname user attributes' do
54 response = @auth.authenticate('example1','123456')
55 assert response
56 assert_equal 'Example', response.first[:firstname]
57 end
58
59 should 'return the lastname user attributes' do
60 response = @auth.authenticate('example1','123456')
61 assert response
62 assert_equal 'One', response.first[:lastname]
63 end
64
65 should 'return mail user attributes' do
66 response = @auth.authenticate('example1','123456')
67 assert response
68 assert_equal 'example1@redmine.org', response.first[:mail]
69 end
70 end
71
72 context 'with an invalid LDAP user' do
73 should 'return nil' do
74 assert_equal nil, @auth.authenticate('nouser','123456')
75 end
76 end
77
78 context 'without a login' do
79 should 'return nil' do
80 assert_equal nil, @auth.authenticate('','123456')
81 end
82 end
83
84 context 'without a password' do
85 should 'return nil' do
86 assert_equal nil, @auth.authenticate('edavis','')
87 end
88 end
89
90 end
91 else
92 puts '(Test LDAP server not configured)'
93 end
36 end
94 end
General Comments 0
You need to be logged in to leave comments. Login now