##// END OF EJS Templates
use escaped "can't" constant at AuthSourcesControllerTest...
Toshi MARUYAMA -
r12526:afa18701fdf3
parent child
Show More
@@ -1,173 +1,173
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 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 AuthSourcesControllerTest < ActionController::TestCase
20 class AuthSourcesControllerTest < ActionController::TestCase
21 fixtures :users, :auth_sources
21 fixtures :users, :auth_sources
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_index
27 def test_index
28 get :index
28 get :index
29
29
30 assert_response :success
30 assert_response :success
31 assert_template 'index'
31 assert_template 'index'
32 assert_not_nil assigns(:auth_sources)
32 assert_not_nil assigns(:auth_sources)
33 end
33 end
34
34
35 def test_new
35 def test_new
36 get :new
36 get :new
37
37
38 assert_response :success
38 assert_response :success
39 assert_template 'new'
39 assert_template 'new'
40
40
41 source = assigns(:auth_source)
41 source = assigns(:auth_source)
42 assert_equal AuthSourceLdap, source.class
42 assert_equal AuthSourceLdap, source.class
43 assert source.new_record?
43 assert source.new_record?
44
44
45 assert_select 'form#auth_source_form' do
45 assert_select 'form#auth_source_form' do
46 assert_select 'input[name=type][value=AuthSourceLdap]'
46 assert_select 'input[name=type][value=AuthSourceLdap]'
47 assert_select 'input[name=?]', 'auth_source[host]'
47 assert_select 'input[name=?]', 'auth_source[host]'
48 end
48 end
49 end
49 end
50
50
51 def test_new_with_invalid_type_should_respond_with_404
51 def test_new_with_invalid_type_should_respond_with_404
52 get :new, :type => 'foo'
52 get :new, :type => 'foo'
53 assert_response 404
53 assert_response 404
54 end
54 end
55
55
56 def test_create
56 def test_create
57 assert_difference 'AuthSourceLdap.count' do
57 assert_difference 'AuthSourceLdap.count' do
58 post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '127.0.0.1', :port => '389', :attr_login => 'cn'}
58 post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '127.0.0.1', :port => '389', :attr_login => 'cn'}
59 assert_redirected_to '/auth_sources'
59 assert_redirected_to '/auth_sources'
60 end
60 end
61
61
62 source = AuthSourceLdap.order('id DESC').first
62 source = AuthSourceLdap.order('id DESC').first
63 assert_equal 'Test', source.name
63 assert_equal 'Test', source.name
64 assert_equal '127.0.0.1', source.host
64 assert_equal '127.0.0.1', source.host
65 assert_equal 389, source.port
65 assert_equal 389, source.port
66 assert_equal 'cn', source.attr_login
66 assert_equal 'cn', source.attr_login
67 end
67 end
68
68
69 def test_create_with_failure
69 def test_create_with_failure
70 assert_no_difference 'AuthSourceLdap.count' do
70 assert_no_difference 'AuthSourceLdap.count' do
71 post :create, :type => 'AuthSourceLdap',
71 post :create, :type => 'AuthSourceLdap',
72 :auth_source => {:name => 'Test', :host => '',
72 :auth_source => {:name => 'Test', :host => '',
73 :port => '389', :attr_login => 'cn'}
73 :port => '389', :attr_login => 'cn'}
74 assert_response :success
74 assert_response :success
75 assert_template 'new'
75 assert_template 'new'
76 end
76 end
77 assert_error_tag :content => /host can&#x27;t be blank/i
77 assert_error_tag :content => /host #{ESCAPED_CANT} be blank/i
78 end
78 end
79
79
80 def test_edit
80 def test_edit
81 get :edit, :id => 1
81 get :edit, :id => 1
82
82
83 assert_response :success
83 assert_response :success
84 assert_template 'edit'
84 assert_template 'edit'
85
85
86 assert_select 'form#auth_source_form' do
86 assert_select 'form#auth_source_form' do
87 assert_select 'input[name=?]', 'auth_source[host]'
87 assert_select 'input[name=?]', 'auth_source[host]'
88 end
88 end
89 end
89 end
90
90
91 def test_edit_should_not_contain_password
91 def test_edit_should_not_contain_password
92 AuthSource.find(1).update_column :account_password, 'secret'
92 AuthSource.find(1).update_column :account_password, 'secret'
93
93
94 get :edit, :id => 1
94 get :edit, :id => 1
95 assert_response :success
95 assert_response :success
96 assert_select 'input[value=secret]', 0
96 assert_select 'input[value=secret]', 0
97 assert_select 'input[name=dummy_password][value=?]', /x+/
97 assert_select 'input[name=dummy_password][value=?]', /x+/
98 end
98 end
99
99
100 def test_edit_invalid_should_respond_with_404
100 def test_edit_invalid_should_respond_with_404
101 get :edit, :id => 99
101 get :edit, :id => 99
102 assert_response 404
102 assert_response 404
103 end
103 end
104
104
105 def test_update
105 def test_update
106 put :update, :id => 1,
106 put :update, :id => 1,
107 :auth_source => {:name => 'Renamed', :host => '192.168.0.10',
107 :auth_source => {:name => 'Renamed', :host => '192.168.0.10',
108 :port => '389', :attr_login => 'uid'}
108 :port => '389', :attr_login => 'uid'}
109 assert_redirected_to '/auth_sources'
109 assert_redirected_to '/auth_sources'
110 source = AuthSourceLdap.find(1)
110 source = AuthSourceLdap.find(1)
111 assert_equal 'Renamed', source.name
111 assert_equal 'Renamed', source.name
112 assert_equal '192.168.0.10', source.host
112 assert_equal '192.168.0.10', source.host
113 end
113 end
114
114
115 def test_update_with_failure
115 def test_update_with_failure
116 put :update, :id => 1,
116 put :update, :id => 1,
117 :auth_source => {:name => 'Renamed', :host => '',
117 :auth_source => {:name => 'Renamed', :host => '',
118 :port => '389', :attr_login => 'uid'}
118 :port => '389', :attr_login => 'uid'}
119 assert_response :success
119 assert_response :success
120 assert_template 'edit'
120 assert_template 'edit'
121 assert_error_tag :content => /host can&#x27;t be blank/i
121 assert_error_tag :content => /host #{ESCAPED_CANT} be blank/i
122 end
122 end
123
123
124 def test_destroy
124 def test_destroy
125 assert_difference 'AuthSourceLdap.count', -1 do
125 assert_difference 'AuthSourceLdap.count', -1 do
126 delete :destroy, :id => 1
126 delete :destroy, :id => 1
127 assert_redirected_to '/auth_sources'
127 assert_redirected_to '/auth_sources'
128 end
128 end
129 end
129 end
130
130
131 def test_destroy_auth_source_in_use
131 def test_destroy_auth_source_in_use
132 User.find(2).update_attribute :auth_source_id, 1
132 User.find(2).update_attribute :auth_source_id, 1
133
133
134 assert_no_difference 'AuthSourceLdap.count' do
134 assert_no_difference 'AuthSourceLdap.count' do
135 delete :destroy, :id => 1
135 delete :destroy, :id => 1
136 assert_redirected_to '/auth_sources'
136 assert_redirected_to '/auth_sources'
137 end
137 end
138 end
138 end
139
139
140 def test_test_connection
140 def test_test_connection
141 AuthSourceLdap.any_instance.stubs(:test_connection).returns(true)
141 AuthSourceLdap.any_instance.stubs(:test_connection).returns(true)
142
142
143 get :test_connection, :id => 1
143 get :test_connection, :id => 1
144 assert_redirected_to '/auth_sources'
144 assert_redirected_to '/auth_sources'
145 assert_not_nil flash[:notice]
145 assert_not_nil flash[:notice]
146 assert_match /successful/i, flash[:notice]
146 assert_match /successful/i, flash[:notice]
147 end
147 end
148
148
149 def test_test_connection_with_failure
149 def test_test_connection_with_failure
150 AuthSourceLdap.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError.new("Something went wrong"))
150 AuthSourceLdap.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError.new("Something went wrong"))
151
151
152 get :test_connection, :id => 1
152 get :test_connection, :id => 1
153 assert_redirected_to '/auth_sources'
153 assert_redirected_to '/auth_sources'
154 assert_not_nil flash[:error]
154 assert_not_nil flash[:error]
155 assert_include 'Something went wrong', flash[:error]
155 assert_include 'Something went wrong', flash[:error]
156 end
156 end
157
157
158 def test_autocomplete_for_new_user
158 def test_autocomplete_for_new_user
159 AuthSource.expects(:search).with('foo').returns([
159 AuthSource.expects(:search).with('foo').returns([
160 {:login => 'foo1', :firstname => 'John', :lastname => 'Smith', :mail => 'foo1@example.net', :auth_source_id => 1},
160 {:login => 'foo1', :firstname => 'John', :lastname => 'Smith', :mail => 'foo1@example.net', :auth_source_id => 1},
161 {:login => 'Smith', :firstname => 'John', :lastname => 'Doe', :mail => 'foo2@example.net', :auth_source_id => 1}
161 {:login => 'Smith', :firstname => 'John', :lastname => 'Doe', :mail => 'foo2@example.net', :auth_source_id => 1}
162 ])
162 ])
163
163
164 get :autocomplete_for_new_user, :term => 'foo'
164 get :autocomplete_for_new_user, :term => 'foo'
165 assert_response :success
165 assert_response :success
166 assert_equal 'application/json', response.content_type
166 assert_equal 'application/json', response.content_type
167 json = ActiveSupport::JSON.decode(response.body)
167 json = ActiveSupport::JSON.decode(response.body)
168 assert_kind_of Array, json
168 assert_kind_of Array, json
169 assert_equal 2, json.size
169 assert_equal 2, json.size
170 assert_equal 'foo1', json.first['value']
170 assert_equal 'foo1', json.first['value']
171 assert_equal 'foo1 (John Smith)', json.first['label']
171 assert_equal 'foo1 (John Smith)', json.first['label']
172 end
172 end
173 end
173 end
General Comments 0
You need to be logged in to leave comments. Login now