You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth_sources_controller_test.rb 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2015 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. require File.expand_path('../../test_helper', __FILE__)
  18. class AuthSourcesControllerTest < ActionController::TestCase
  19. fixtures :users, :auth_sources
  20. def setup
  21. @request.session[:user_id] = 1
  22. end
  23. def test_index
  24. get :index
  25. assert_response :success
  26. assert_template 'index'
  27. assert_not_nil assigns(:auth_sources)
  28. end
  29. def test_new
  30. get :new
  31. assert_response :success
  32. assert_template 'new'
  33. source = assigns(:auth_source)
  34. assert_equal AuthSourceLdap, source.class
  35. assert source.new_record?
  36. assert_select 'form#auth_source_form' do
  37. assert_select 'input[name=type][value=AuthSourceLdap]'
  38. assert_select 'input[name=?]', 'auth_source[host]'
  39. end
  40. end
  41. def test_new_with_invalid_type_should_respond_with_404
  42. get :new, :type => 'foo'
  43. assert_response 404
  44. end
  45. def test_create
  46. assert_difference 'AuthSourceLdap.count' do
  47. post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '127.0.0.1', :port => '389', :attr_login => 'cn'}
  48. assert_redirected_to '/auth_sources'
  49. end
  50. source = AuthSourceLdap.order('id DESC').first
  51. assert_equal 'Test', source.name
  52. assert_equal '127.0.0.1', source.host
  53. assert_equal 389, source.port
  54. assert_equal 'cn', source.attr_login
  55. end
  56. def test_create_with_failure
  57. assert_no_difference 'AuthSourceLdap.count' do
  58. post :create, :type => 'AuthSourceLdap',
  59. :auth_source => {:name => 'Test', :host => '',
  60. :port => '389', :attr_login => 'cn'}
  61. assert_response :success
  62. assert_template 'new'
  63. end
  64. assert_select_error /host cannot be blank/i
  65. end
  66. def test_edit
  67. get :edit, :id => 1
  68. assert_response :success
  69. assert_template 'edit'
  70. assert_select 'form#auth_source_form' do
  71. assert_select 'input[name=?]', 'auth_source[host]'
  72. end
  73. end
  74. def test_edit_should_not_contain_password
  75. AuthSource.find(1).update_column :account_password, 'secret'
  76. get :edit, :id => 1
  77. assert_response :success
  78. assert_select 'input[value=secret]', 0
  79. assert_select 'input[name=dummy_password][value^=xxxxxx]'
  80. end
  81. def test_edit_invalid_should_respond_with_404
  82. get :edit, :id => 99
  83. assert_response 404
  84. end
  85. def test_update
  86. put :update, :id => 1,
  87. :auth_source => {:name => 'Renamed', :host => '192.168.0.10',
  88. :port => '389', :attr_login => 'uid'}
  89. assert_redirected_to '/auth_sources'
  90. source = AuthSourceLdap.find(1)
  91. assert_equal 'Renamed', source.name
  92. assert_equal '192.168.0.10', source.host
  93. end
  94. def test_update_with_failure
  95. put :update, :id => 1,
  96. :auth_source => {:name => 'Renamed', :host => '',
  97. :port => '389', :attr_login => 'uid'}
  98. assert_response :success
  99. assert_template 'edit'
  100. assert_select_error /host cannot be blank/i
  101. end
  102. def test_destroy
  103. assert_difference 'AuthSourceLdap.count', -1 do
  104. delete :destroy, :id => 1
  105. assert_redirected_to '/auth_sources'
  106. end
  107. end
  108. def test_destroy_auth_source_in_use
  109. User.find(2).update_attribute :auth_source_id, 1
  110. assert_no_difference 'AuthSourceLdap.count' do
  111. delete :destroy, :id => 1
  112. assert_redirected_to '/auth_sources'
  113. end
  114. end
  115. def test_test_connection
  116. AuthSourceLdap.any_instance.stubs(:test_connection).returns(true)
  117. get :test_connection, :id => 1
  118. assert_redirected_to '/auth_sources'
  119. assert_not_nil flash[:notice]
  120. assert_match /successful/i, flash[:notice]
  121. end
  122. def test_test_connection_with_failure
  123. AuthSourceLdap.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError.new("Something went wrong"))
  124. get :test_connection, :id => 1
  125. assert_redirected_to '/auth_sources'
  126. assert_not_nil flash[:error]
  127. assert_include 'Something went wrong', flash[:error]
  128. end
  129. def test_autocomplete_for_new_user
  130. AuthSource.expects(:search).with('foo').returns([
  131. {:login => 'foo1', :firstname => 'John', :lastname => 'Smith', :mail => 'foo1@example.net', :auth_source_id => 1},
  132. {:login => 'Smith', :firstname => 'John', :lastname => 'Doe', :mail => 'foo2@example.net', :auth_source_id => 1}
  133. ])
  134. get :autocomplete_for_new_user, :term => 'foo'
  135. assert_response :success
  136. assert_equal 'application/json', response.content_type
  137. json = ActiveSupport::JSON.decode(response.body)
  138. assert_kind_of Array, json
  139. assert_equal 2, json.size
  140. assert_equal 'foo1', json.first['value']
  141. assert_equal 'foo1 (John Smith)', json.first['label']
  142. end
  143. end