##// END OF EJS Templates
Adds functional test for #test_connection....
Jean-Philippe Lang -
r8933:9ffccf5304dc
parent child
Show More
@@ -1,87 +1,87
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class AuthSourcesController < ApplicationController
19 19 layout 'admin'
20 20
21 21 before_filter :require_admin
22 22
23 23 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
24 24 verify :method => :post, :only => [ :destroy, :create, :update ],
25 25 :redirect_to => { :template => :index }
26 26
27 27 def index
28 28 @auth_source_pages, @auth_sources = paginate auth_source_class.name.tableize, :per_page => 10
29 29 render "auth_sources/index"
30 30 end
31 31
32 32 def new
33 33 @auth_source = auth_source_class.new
34 34 render 'auth_sources/new'
35 35 end
36 36
37 37 def create
38 38 @auth_source = auth_source_class.new(params[:auth_source])
39 39 if @auth_source.save
40 40 flash[:notice] = l(:notice_successful_create)
41 41 redirect_to :action => 'index'
42 42 else
43 43 render 'auth_sources/new'
44 44 end
45 45 end
46 46
47 47 def edit
48 48 @auth_source = AuthSource.find(params[:id])
49 49 render 'auth_sources/edit'
50 50 end
51 51
52 52 def update
53 53 @auth_source = AuthSource.find(params[:id])
54 54 if @auth_source.update_attributes(params[:auth_source])
55 55 flash[:notice] = l(:notice_successful_update)
56 56 redirect_to :action => 'index'
57 57 else
58 58 render 'auth_sources/edit'
59 59 end
60 60 end
61 61
62 62 def test_connection
63 63 @auth_method = AuthSource.find(params[:id])
64 64 begin
65 65 @auth_method.test_connection
66 66 flash[:notice] = l(:notice_successful_connection)
67 rescue => text
68 flash[:error] = l(:error_unable_to_connect, text.message)
67 rescue Exception => e
68 flash[:error] = l(:error_unable_to_connect, e.message)
69 69 end
70 70 redirect_to :action => 'index'
71 71 end
72 72
73 73 def destroy
74 74 @auth_source = AuthSource.find(params[:id])
75 75 unless @auth_source.users.find(:first)
76 76 @auth_source.destroy
77 77 flash[:notice] = l(:notice_successful_delete)
78 78 end
79 79 redirect_to :action => 'index'
80 80 end
81 81
82 82 protected
83 83
84 84 def auth_source_class
85 85 AuthSource
86 86 end
87 87 end
@@ -1,96 +1,114
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class LdapAuthSourcesControllerTest < ActionController::TestCase
21 21 fixtures :auth_sources, :users
22 22
23 23 def setup
24 24 @request.session[:user_id] = 1
25 25 end
26 26
27 27 def test_new
28 28 get :new
29 29
30 30 assert_response :success
31 31 assert_template 'new'
32 32
33 33 source = assigns(:auth_source)
34 34 assert_equal AuthSourceLdap, source.class
35 35 assert source.new_record?
36 36 end
37 37
38 38 def test_create
39 39 assert_difference 'AuthSourceLdap.count' do
40 40 post :create, :auth_source => {:name => 'Test', :host => '127.0.0.1', :port => '389', :attr_login => 'cn'}
41 41 assert_redirected_to '/ldap_auth_sources'
42 42 end
43 43
44 44 source = AuthSourceLdap.first(:order => 'id DESC')
45 45 assert_equal 'Test', source.name
46 46 assert_equal '127.0.0.1', source.host
47 47 assert_equal 389, source.port
48 48 assert_equal 'cn', source.attr_login
49 49 end
50 50
51 51 def test_create_with_failure
52 52 assert_no_difference 'AuthSourceLdap.count' do
53 53 post :create, :auth_source => {:name => 'Test', :host => '', :port => '389', :attr_login => 'cn'}
54 54 assert_response :success
55 55 assert_template 'new'
56 56 end
57 57 assert_error_tag :content => /host can't be blank/i
58 58 end
59 59
60 60 def test_edit
61 61 get :edit, :id => 1
62 62
63 63 assert_response :success
64 64 assert_template 'edit'
65 65 end
66 66
67 67 def test_update
68 68 post :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '192.168.0.10', :port => '389', :attr_login => 'uid'}
69 69 assert_redirected_to '/ldap_auth_sources'
70 70
71 71 source = AuthSourceLdap.find(1)
72 72 assert_equal 'Renamed', source.name
73 73 assert_equal '192.168.0.10', source.host
74 74 end
75 75
76 76 def test_update_with_failure
77 77 post :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '', :port => '389', :attr_login => 'uid'}
78 78 assert_response :success
79 79 assert_template 'edit'
80 80 assert_error_tag :content => /host can't be blank/i
81 81 end
82 82
83 83 def test_destroy
84 84 assert_difference 'AuthSourceLdap.count', -1 do
85 85 post :destroy, :id => 1
86 86 end
87 87 end
88 88
89 89 def test_destroy_auth_source_in_use
90 90 User.find(2).update_attribute :auth_source_id, 1
91 91
92 92 assert_no_difference 'AuthSourceLdap.count' do
93 93 post :destroy, :id => 1
94 94 end
95 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 114 end
General Comments 0
You need to be logged in to leave comments. Login now