comparison test/functional/auth_sources_controller_test.rb @ 1115:433d4f72a19b redmine-2.2

Update to Redmine SVN revision 11137 on 2.2-stable branch
author Chris Cannam
date Mon, 07 Jan 2013 12:01:42 +0000
parents cbb26bc654de
children 622f24f53b42
comparison
equal deleted inserted replaced
929:5f33065ddc4b 1115:433d4f72a19b
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
1 require File.expand_path('../../test_helper', __FILE__) 18 require File.expand_path('../../test_helper', __FILE__)
2 19
3 class AuthSourcesControllerTest < ActionController::TestCase 20 class AuthSourcesControllerTest < ActionController::TestCase
21 fixtures :users, :auth_sources
4 22
5 def setup 23 def setup
6 @request.session[:user_id] = 1 24 @request.session[:user_id] = 1
7 end 25 end
8 26
9 context "get :index" do 27 def test_index
10 setup do 28 get :index
11 get :index 29
30 assert_response :success
31 assert_template 'index'
32 assert_not_nil assigns(:auth_sources)
33 end
34
35 def test_new
36 get :new
37
38 assert_response :success
39 assert_template 'new'
40
41 source = assigns(:auth_source)
42 assert_equal AuthSourceLdap, source.class
43 assert source.new_record?
44
45 assert_tag 'input', :attributes => {:name => 'type', :value => 'AuthSourceLdap'}
46 assert_tag 'input', :attributes => {:name => 'auth_source[host]'}
47 end
48
49 def test_create
50 assert_difference 'AuthSourceLdap.count' do
51 post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '127.0.0.1', :port => '389', :attr_login => 'cn'}
52 assert_redirected_to '/auth_sources'
12 end 53 end
13 54
14 should_assign_to :auth_sources 55 source = AuthSourceLdap.first(:order => 'id DESC')
15 should_assign_to :auth_source_pages 56 assert_equal 'Test', source.name
16 should_respond_with :success 57 assert_equal '127.0.0.1', source.host
17 should_render_template :index 58 assert_equal 389, source.port
59 assert_equal 'cn', source.attr_login
18 end 60 end
19 61
20 context "get :new" do 62 def test_create_with_failure
21 setup do 63 assert_no_difference 'AuthSourceLdap.count' do
22 get :new 64 post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '', :port => '389', :attr_login => 'cn'}
65 assert_response :success
66 assert_template 'new'
23 end 67 end
68 assert_error_tag :content => /host can&#x27;t be blank/i
69 end
24 70
25 should_assign_to :auth_source 71 def test_edit
26 should_respond_with :success 72 get :edit, :id => 1
27 should_render_template :new
28 73
29 should "initilize a new AuthSource" do 74 assert_response :success
30 assert_equal AuthSource, assigns(:auth_source).class 75 assert_template 'edit'
31 assert assigns(:auth_source).new_record? 76
77 assert_tag 'input', :attributes => {:name => 'auth_source[host]'}
78 end
79
80 def test_update
81 put :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '192.168.0.10', :port => '389', :attr_login => 'uid'}
82 assert_redirected_to '/auth_sources'
83
84 source = AuthSourceLdap.find(1)
85 assert_equal 'Renamed', source.name
86 assert_equal '192.168.0.10', source.host
87 end
88
89 def test_update_with_failure
90 put :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '', :port => '389', :attr_login => 'uid'}
91 assert_response :success
92 assert_template 'edit'
93 assert_error_tag :content => /host can&#x27;t be blank/i
94 end
95
96 def test_destroy
97 assert_difference 'AuthSourceLdap.count', -1 do
98 delete :destroy, :id => 1
32 end 99 end
33 end 100 end
34 101
35 context "post :create" do 102 def test_destroy_auth_source_in_use
36 setup do 103 User.find(2).update_attribute :auth_source_id, 1
37 post :create, :auth_source => {:name => 'Test'} 104
105 assert_no_difference 'AuthSourceLdap.count' do
106 delete :destroy, :id => 1
38 end 107 end
39
40 should_respond_with :redirect
41 should_redirect_to("index") {{:action => 'index'}}
42 should_set_the_flash_to /success/i
43 end 108 end
44 109
45 context "get :edit" do 110 def test_test_connection
46 setup do 111 AuthSourceLdap.any_instance.stubs(:test_connection).returns(true)
47 @auth_source = AuthSource.generate!(:name => 'TestEdit')
48 get :edit, :id => @auth_source.id
49 end
50 112
51 should_assign_to(:auth_source) {@auth_source} 113 get :test_connection, :id => 1
52 should_respond_with :success 114 assert_redirected_to '/auth_sources'
53 should_render_template :edit 115 assert_not_nil flash[:notice]
116 assert_match /successful/i, flash[:notice]
54 end 117 end
55 118
56 context "post :update" do 119 def test_test_connection_with_failure
57 setup do 120 AuthSourceLdap.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError.new("Something went wrong"))
58 @auth_source = AuthSource.generate!(:name => 'TestEdit')
59 post :update, :id => @auth_source.id, :auth_source => {:name => 'TestUpdate'}
60 end
61 121
62 should_respond_with :redirect 122 get :test_connection, :id => 1
63 should_redirect_to("index") {{:action => 'index'}} 123 assert_redirected_to '/auth_sources'
64 should_set_the_flash_to /update/i 124 assert_not_nil flash[:error]
65 end 125 assert_include 'Something went wrong', flash[:error]
66
67 context "post :destroy" do
68 setup do
69 @auth_source = AuthSource.generate!(:name => 'TestEdit')
70 end
71
72 context "without users" do
73 setup do
74 post :destroy, :id => @auth_source.id
75 end
76
77 should_respond_with :redirect
78 should_redirect_to("index") {{:action => 'index'}}
79 should_set_the_flash_to /deletion/i
80 end
81
82 context "with users" do
83 setup do
84 User.generate!(:auth_source => @auth_source)
85 post :destroy, :id => @auth_source.id
86 end
87
88 should_respond_with :redirect
89 should "not destroy the AuthSource" do
90 assert AuthSource.find(@auth_source.id)
91 end
92 end
93 end 126 end
94 end 127 end