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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. 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_tag 'input', :attributes => {:name => 'type', :value => 'AuthSourceLdap'}
  37. assert_tag 'input', :attributes => {:name => 'auth_source[host]'}
  38. end
  39. def test_create
  40. assert_difference 'AuthSourceLdap.count' do
  41. post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '127.0.0.1', :port => '389', :attr_login => 'cn'}
  42. assert_redirected_to '/auth_sources'
  43. end
  44. source = AuthSourceLdap.first(:order => 'id DESC')
  45. assert_equal 'Test', source.name
  46. assert_equal '127.0.0.1', source.host
  47. assert_equal 389, source.port
  48. assert_equal 'cn', source.attr_login
  49. end
  50. def test_create_with_failure
  51. assert_no_difference 'AuthSourceLdap.count' do
  52. post :create, :type => 'AuthSourceLdap', :auth_source => {:name => 'Test', :host => '', :port => '389', :attr_login => 'cn'}
  53. assert_response :success
  54. assert_template 'new'
  55. end
  56. assert_error_tag :content => /host can't be blank/i
  57. end
  58. def test_edit
  59. get :edit, :id => 1
  60. assert_response :success
  61. assert_template 'edit'
  62. assert_tag 'input', :attributes => {:name => 'auth_source[host]'}
  63. end
  64. def test_update
  65. put :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '192.168.0.10', :port => '389', :attr_login => 'uid'}
  66. assert_redirected_to '/auth_sources'
  67. source = AuthSourceLdap.find(1)
  68. assert_equal 'Renamed', source.name
  69. assert_equal '192.168.0.10', source.host
  70. end
  71. def test_update_with_failure
  72. put :update, :id => 1, :auth_source => {:name => 'Renamed', :host => '', :port => '389', :attr_login => 'uid'}
  73. assert_response :success
  74. assert_template 'edit'
  75. assert_error_tag :content => /host can't be blank/i
  76. end
  77. def test_destroy
  78. assert_difference 'AuthSourceLdap.count', -1 do
  79. delete :destroy, :id => 1
  80. end
  81. end
  82. def test_destroy_auth_source_in_use
  83. User.find(2).update_attribute :auth_source_id, 1
  84. assert_no_difference 'AuthSourceLdap.count' do
  85. delete :destroy, :id => 1
  86. end
  87. end
  88. def test_test_connection
  89. AuthSourceLdap.any_instance.stubs(:test_connection).returns(true)
  90. get :test_connection, :id => 1
  91. assert_redirected_to '/auth_sources'
  92. assert_not_nil flash[:notice]
  93. assert_match /successful/i, flash[:notice]
  94. end
  95. def test_test_connection_with_failure
  96. AuthSourceLdap.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError.new("Something went wrong"))
  97. get :test_connection, :id => 1
  98. assert_redirected_to '/auth_sources'
  99. assert_not_nil flash[:error]
  100. assert_include 'Something went wrong', flash[:error]
  101. end
  102. end