Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

auth_sources_controller_test.rb 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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 < Redmine::ControllerTest
  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. end
  27. def test_new
  28. get :new
  29. assert_response :success
  30. assert_select 'form#auth_source_form' do
  31. assert_select 'input[name=type][value=AuthSourceLdap]'
  32. assert_select 'input[name=?]', 'auth_source[host]'
  33. end
  34. end
  35. def test_new_with_invalid_type_should_respond_with_404
  36. get :new, :params => {
  37. :type => 'foo'
  38. }
  39. assert_response 404
  40. end
  41. def test_create
  42. assert_difference 'AuthSourceLdap.count' do
  43. post :create, :params => {
  44. :type => 'AuthSourceLdap',
  45. :auth_source => {
  46. :name => 'Test',
  47. :host => '127.0.0.1',
  48. :port => '389',
  49. :attr_login => 'cn'
  50. }
  51. }
  52. assert_redirected_to '/auth_sources'
  53. end
  54. source = AuthSourceLdap.order('id DESC').first
  55. assert_equal 'Test', source.name
  56. assert_equal '127.0.0.1', source.host
  57. assert_equal 389, source.port
  58. assert_equal 'cn', source.attr_login
  59. end
  60. def test_create_with_failure
  61. assert_no_difference 'AuthSourceLdap.count' do
  62. post :create, :params => {
  63. :type => 'AuthSourceLdap',
  64. :auth_source => {
  65. :name => 'Test',
  66. :host => '',
  67. :port => '389',
  68. :attr_login => 'cn'
  69. }
  70. }
  71. assert_response :success
  72. end
  73. assert_select_error /host cannot be blank/i
  74. end
  75. def test_edit
  76. get :edit, :params => {
  77. :id => 1
  78. }
  79. assert_response :success
  80. assert_select 'form#auth_source_form' do
  81. assert_select 'input[name=?]', 'auth_source[host]'
  82. end
  83. end
  84. def test_edit_should_not_contain_password
  85. AuthSource.find(1).update_column :account_password, 'secret'
  86. get :edit, :params => {
  87. :id => 1
  88. }
  89. assert_response :success
  90. assert_select 'input[value=secret]', 0
  91. assert_select 'input[name=dummy_password][value^=xxxxxx]'
  92. end
  93. def test_edit_invalid_should_respond_with_404
  94. get :edit, :params => {
  95. :id => 99
  96. }
  97. assert_response 404
  98. end
  99. def test_update
  100. put :update, :params => {
  101. :id => 1,
  102. :auth_source => {
  103. :name => 'Renamed',
  104. :host => '192.168.0.10',
  105. :port => '389',
  106. :attr_login => 'uid'
  107. }
  108. }
  109. assert_redirected_to '/auth_sources'
  110. source = AuthSourceLdap.find(1)
  111. assert_equal 'Renamed', source.name
  112. assert_equal '192.168.0.10', source.host
  113. end
  114. def test_update_with_failure
  115. put :update, :params => {
  116. :id => 1,
  117. :auth_source => {
  118. :name => 'Renamed',
  119. :host => '',
  120. :port => '389',
  121. :attr_login => 'uid'
  122. }
  123. }
  124. assert_response :success
  125. assert_select_error /host cannot be blank/i
  126. end
  127. def test_destroy
  128. assert_difference 'AuthSourceLdap.count', -1 do
  129. delete :destroy, :params => {
  130. :id => 1
  131. }
  132. assert_redirected_to '/auth_sources'
  133. end
  134. end
  135. def test_destroy_auth_source_in_use
  136. User.find(2).update_attribute :auth_source_id, 1
  137. assert_no_difference 'AuthSourceLdap.count' do
  138. delete :destroy, :params => {
  139. :id => 1
  140. }
  141. assert_redirected_to '/auth_sources'
  142. assert_equal 'This authentication mode is in use and cannot be deleted.', flash[:error]
  143. end
  144. end
  145. def test_test_connection
  146. AuthSourceLdap.any_instance.stubs(:test_connection).returns(true)
  147. get :test_connection, :params => {
  148. :id => 1
  149. }
  150. assert_redirected_to '/auth_sources'
  151. assert_not_nil flash[:notice]
  152. assert_match /successful/i, flash[:notice]
  153. end
  154. def test_test_connection_with_failure
  155. AuthSourceLdap.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::Error.new("Something went wrong"))
  156. get :test_connection, :params => {
  157. :id => 1
  158. }
  159. assert_redirected_to '/auth_sources'
  160. assert_not_nil flash[:error]
  161. assert_include 'Something went wrong', flash[:error]
  162. end
  163. def test_autocomplete_for_new_user
  164. AuthSource.expects(:search).with('foo').returns([
  165. {:login => 'foo1', :firstname => 'John', :lastname => 'Smith', :mail => 'foo1@example.net', :auth_source_id => 1},
  166. {:login => 'Smith', :firstname => 'John', :lastname => 'Doe', :mail => 'foo2@example.net', :auth_source_id => 1}
  167. ])
  168. get :autocomplete_for_new_user, :params => {
  169. :term => 'foo'
  170. }
  171. assert_response :success
  172. assert_equal 'application/json', response.content_type
  173. json = ActiveSupport::JSON.decode(response.body)
  174. assert_kind_of Array, json
  175. assert_equal 2, json.size
  176. assert_equal 'foo1', json.first['value']
  177. assert_equal 'foo1 (John Smith)', json.first['label']
  178. end
  179. end