@@ -1,87 +1,87 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2011 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 | class AuthSourcesController < ApplicationController |
|
18 | class AuthSourcesController < ApplicationController | |
19 | layout 'admin' |
|
19 | layout 'admin' | |
20 |
|
20 | |||
21 | before_filter :require_admin |
|
21 | before_filter :require_admin | |
22 |
|
22 | |||
23 | # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) |
|
23 | # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) | |
24 | verify :method => :post, :only => [ :destroy, :create, :update ], |
|
24 | verify :method => :post, :only => [ :destroy, :create, :update ], | |
25 | :redirect_to => { :template => :index } |
|
25 | :redirect_to => { :template => :index } | |
26 |
|
26 | |||
27 | def index |
|
27 | def index | |
28 | @auth_source_pages, @auth_sources = paginate auth_source_class.name.tableize, :per_page => 10 |
|
28 | @auth_source_pages, @auth_sources = paginate auth_source_class.name.tableize, :per_page => 10 | |
29 | render "auth_sources/index" |
|
29 | render "auth_sources/index" | |
30 | end |
|
30 | end | |
31 |
|
31 | |||
32 | def new |
|
32 | def new | |
33 | @auth_source = auth_source_class.new |
|
33 | @auth_source = auth_source_class.new | |
34 | render 'auth_sources/new' |
|
34 | render 'auth_sources/new' | |
35 | end |
|
35 | end | |
36 |
|
36 | |||
37 | def create |
|
37 | def create | |
38 | @auth_source = auth_source_class.new(params[:auth_source]) |
|
38 | @auth_source = auth_source_class.new(params[:auth_source]) | |
39 | if @auth_source.save |
|
39 | if @auth_source.save | |
40 | flash[:notice] = l(:notice_successful_create) |
|
40 | flash[:notice] = l(:notice_successful_create) | |
41 | redirect_to :action => 'index' |
|
41 | redirect_to :action => 'index' | |
42 | else |
|
42 | else | |
43 | render 'auth_sources/new' |
|
43 | render 'auth_sources/new' | |
44 | end |
|
44 | end | |
45 | end |
|
45 | end | |
46 |
|
46 | |||
47 | def edit |
|
47 | def edit | |
48 | @auth_source = AuthSource.find(params[:id]) |
|
48 | @auth_source = AuthSource.find(params[:id]) | |
49 | render 'auth_sources/edit' |
|
49 | render 'auth_sources/edit' | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | def update |
|
52 | def update | |
53 | @auth_source = AuthSource.find(params[:id]) |
|
53 | @auth_source = AuthSource.find(params[:id]) | |
54 | if @auth_source.update_attributes(params[:auth_source]) |
|
54 | if @auth_source.update_attributes(params[:auth_source]) | |
55 | flash[:notice] = l(:notice_successful_update) |
|
55 | flash[:notice] = l(:notice_successful_update) | |
56 | redirect_to :action => 'index' |
|
56 | redirect_to :action => 'index' | |
57 | else |
|
57 | else | |
58 | render 'auth_sources/edit' |
|
58 | render 'auth_sources/edit' | |
59 | end |
|
59 | end | |
60 | end |
|
60 | end | |
61 |
|
61 | |||
62 | def test_connection |
|
62 | def test_connection | |
63 | @auth_method = AuthSource.find(params[:id]) |
|
63 | @auth_method = AuthSource.find(params[:id]) | |
64 | begin |
|
64 | begin | |
65 | @auth_method.test_connection |
|
65 | @auth_method.test_connection | |
66 | flash[:notice] = l(:notice_successful_connection) |
|
66 | flash[:notice] = l(:notice_successful_connection) | |
67 |
rescue => |
|
67 | rescue Exception => e | |
68 |
flash[:error] = l(:error_unable_to_connect, |
|
68 | flash[:error] = l(:error_unable_to_connect, e.message) | |
69 | end |
|
69 | end | |
70 | redirect_to :action => 'index' |
|
70 | redirect_to :action => 'index' | |
71 | end |
|
71 | end | |
72 |
|
72 | |||
73 | def destroy |
|
73 | def destroy | |
74 | @auth_source = AuthSource.find(params[:id]) |
|
74 | @auth_source = AuthSource.find(params[:id]) | |
75 | unless @auth_source.users.find(:first) |
|
75 | unless @auth_source.users.find(:first) | |
76 | @auth_source.destroy |
|
76 | @auth_source.destroy | |
77 | flash[:notice] = l(:notice_successful_delete) |
|
77 | flash[:notice] = l(:notice_successful_delete) | |
78 | end |
|
78 | end | |
79 | redirect_to :action => 'index' |
|
79 | redirect_to :action => 'index' | |
80 | end |
|
80 | end | |
81 |
|
81 | |||
82 | protected |
|
82 | protected | |
83 |
|
83 | |||
84 | def auth_source_class |
|
84 | def auth_source_class | |
85 | AuthSource |
|
85 | AuthSource | |
86 | end |
|
86 | end | |
87 | end |
|
87 | end |
@@ -1,96 +1,114 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class LdapAuthSourcesControllerTest < ActionController::TestCase |
|
20 | class LdapAuthSourcesControllerTest < ActionController::TestCase | |
21 | fixtures :auth_sources, :users |
|
21 | fixtures :auth_sources, :users | |
22 |
|
22 | |||
23 | def setup |
|
23 | def setup | |
24 | @request.session[:user_id] = 1 |
|
24 | @request.session[:user_id] = 1 | |
25 | end |
|
25 | end | |
26 |
|
26 | |||
27 | def test_new |
|
27 | def test_new | |
28 | get :new |
|
28 | get :new | |
29 |
|
29 | |||
30 | assert_response :success |
|
30 | assert_response :success | |
31 | assert_template 'new' |
|
31 | assert_template 'new' | |
32 |
|
32 | |||
33 | source = assigns(:auth_source) |
|
33 | source = assigns(:auth_source) | |
34 | assert_equal AuthSourceLdap, source.class |
|
34 | assert_equal AuthSourceLdap, source.class | |
35 | assert source.new_record? |
|
35 | assert source.new_record? | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | def test_create |
|
38 | def test_create | |
39 | assert_difference 'AuthSourceLdap.count' do |
|
39 | assert_difference 'AuthSourceLdap.count' do | |
40 | post :create, :auth_source => {:name => 'Test', :host => '127.0.0.1', :port => '389', :attr_login => 'cn'} |
|
40 | post :create, :auth_source => {:name => 'Test', :host => '127.0.0.1', :port => '389', :attr_login => 'cn'} | |
41 | assert_redirected_to '/ldap_auth_sources' |
|
41 | assert_redirected_to '/ldap_auth_sources' | |
42 | end |
|
42 | end | |
43 |
|
43 | |||
44 | source = AuthSourceLdap.first(:order => 'id DESC') |
|
44 | source = AuthSourceLdap.first(:order => 'id DESC') | |
45 | assert_equal 'Test', source.name |
|
45 | assert_equal 'Test', source.name | |
46 | assert_equal '127.0.0.1', source.host |
|
46 | assert_equal '127.0.0.1', source.host | |
47 | assert_equal 389, source.port |
|
47 | assert_equal 389, source.port | |
48 | assert_equal 'cn', source.attr_login |
|
48 | assert_equal 'cn', source.attr_login | |
49 | end |
|
49 | end | |
50 |
|
50 | |||
51 | def test_create_with_failure |
|
51 | def test_create_with_failure | |
52 | assert_no_difference 'AuthSourceLdap.count' do |
|
52 | assert_no_difference 'AuthSourceLdap.count' do | |
53 | post :create, :auth_source => {:name => 'Test', :host => '', :port => '389', :attr_login => 'cn'} |
|
53 | post :create, :auth_source => {:name => 'Test', :host => '', :port => '389', :attr_login => 'cn'} | |
54 | assert_response :success |
|
54 | assert_response :success | |
55 | assert_template 'new' |
|
55 | assert_template 'new' | |
56 | end |
|
56 | end | |
57 | assert_error_tag :content => /host can't be blank/i |
|
57 | assert_error_tag :content => /host can't be blank/i | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | def test_edit |
|
60 | def test_edit | |
61 | get :edit, :id => 1 |
|
61 | get :edit, :id => 1 | |
62 |
|
62 | |||
63 | assert_response :success |
|
63 | assert_response :success | |
64 | assert_template 'edit' |
|
64 | assert_template 'edit' | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
67 | def test_update |
|
67 | def test_update | |
68 | post :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '192.168.0.10', :port => '389', :attr_login => 'uid'} |
|
68 | post :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '192.168.0.10', :port => '389', :attr_login => 'uid'} | |
69 | assert_redirected_to '/ldap_auth_sources' |
|
69 | assert_redirected_to '/ldap_auth_sources' | |
70 |
|
70 | |||
71 | source = AuthSourceLdap.find(1) |
|
71 | source = AuthSourceLdap.find(1) | |
72 | assert_equal 'Renamed', source.name |
|
72 | assert_equal 'Renamed', source.name | |
73 | assert_equal '192.168.0.10', source.host |
|
73 | assert_equal '192.168.0.10', source.host | |
74 | end |
|
74 | end | |
75 |
|
75 | |||
76 | def test_update_with_failure |
|
76 | def test_update_with_failure | |
77 | post :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '', :port => '389', :attr_login => 'uid'} |
|
77 | post :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '', :port => '389', :attr_login => 'uid'} | |
78 | assert_response :success |
|
78 | assert_response :success | |
79 | assert_template 'edit' |
|
79 | assert_template 'edit' | |
80 | assert_error_tag :content => /host can't be blank/i |
|
80 | assert_error_tag :content => /host can't be blank/i | |
81 | end |
|
81 | end | |
82 |
|
82 | |||
83 | def test_destroy |
|
83 | def test_destroy | |
84 | assert_difference 'AuthSourceLdap.count', -1 do |
|
84 | assert_difference 'AuthSourceLdap.count', -1 do | |
85 | post :destroy, :id => 1 |
|
85 | post :destroy, :id => 1 | |
86 | end |
|
86 | end | |
87 | end |
|
87 | end | |
88 |
|
88 | |||
89 | def test_destroy_auth_source_in_use |
|
89 | def test_destroy_auth_source_in_use | |
90 | User.find(2).update_attribute :auth_source_id, 1 |
|
90 | User.find(2).update_attribute :auth_source_id, 1 | |
91 |
|
91 | |||
92 | assert_no_difference 'AuthSourceLdap.count' do |
|
92 | assert_no_difference 'AuthSourceLdap.count' do | |
93 | post :destroy, :id => 1 |
|
93 | post :destroy, :id => 1 | |
94 | end |
|
94 | end | |
95 | end |
|
95 | end | |
|
96 | ||||
|
97 | def test_test_connection | |||
|
98 | AuthSourceLdap.any_instance.stubs(:test_connection).returns(true) | |||
|
99 | ||||
|
100 | get :test_connection, :id => 1 | |||
|
101 | assert_redirected_to '/ldap_auth_sources' | |||
|
102 | assert_not_nil flash[:notice] | |||
|
103 | assert_match /successful/i, flash[:notice] | |||
|
104 | end | |||
|
105 | ||||
|
106 | def test_test_connection_with_failure | |||
|
107 | AuthSourceLdap.any_instance.stubs(:test_connection).raises(Exception.new("Something went wrong")) | |||
|
108 | ||||
|
109 | get :test_connection, :id => 1 | |||
|
110 | assert_redirected_to '/ldap_auth_sources' | |||
|
111 | assert_not_nil flash[:error] | |||
|
112 | assert_include '(Something went wrong)', flash[:error] | |||
|
113 | end | |||
96 | end |
|
114 | end |
General Comments 0
You need to be logged in to leave comments.
Login now