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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class AuthSourcesControllerTest < Redmine::ControllerTest
  20. fixtures :users, :auth_sources
  21. def setup
  22. @request.session[:user_id] = 1
  23. end
  24. def test_index
  25. get :index
  26. assert_response :success
  27. end
  28. def test_new
  29. get :new
  30. assert_response :success
  31. assert_select 'form#auth_source_form' do
  32. assert_select 'input[name=type][value=AuthSourceLdap]'
  33. assert_select 'input[name=?]', 'auth_source[host]'
  34. end
  35. end
  36. def test_new_with_invalid_type_should_respond_with_404
  37. get :new, :params => {
  38. :type => 'foo'
  39. }
  40. assert_response 404
  41. end
  42. def test_create
  43. assert_difference 'AuthSourceLdap.count' do
  44. post :create, :params => {
  45. :type => 'AuthSourceLdap',
  46. :auth_source => {
  47. :name => 'Test',
  48. :host => '127.0.0.1',
  49. :port => '389',
  50. :attr_login => 'cn'
  51. }
  52. }
  53. assert_redirected_to '/auth_sources'
  54. end
  55. source = AuthSourceLdap.order('id DESC').first
  56. assert_equal 'Test', source.name
  57. assert_equal '127.0.0.1', source.host
  58. assert_equal 389, source.port
  59. assert_equal 'cn', source.attr_login
  60. end
  61. def test_create_with_failure
  62. assert_no_difference 'AuthSourceLdap.count' do
  63. post :create, :params => {
  64. :type => 'AuthSourceLdap',
  65. :auth_source => {
  66. :name => 'Test',
  67. :host => '',
  68. :port => '389',
  69. :attr_login => 'cn'
  70. }
  71. }
  72. assert_response :success
  73. end
  74. assert_select_error /host cannot be blank/i
  75. end
  76. def test_edit
  77. get :edit, :params => {
  78. :id => 1
  79. }
  80. assert_response :success
  81. assert_select 'form#auth_source_form' do
  82. assert_select 'input[name=?]', 'auth_source[host]'
  83. end
  84. end
  85. def test_edit_should_not_contain_password
  86. AuthSource.find(1).update_column :account_password, 'secret'
  87. get :edit, :params => {
  88. :id => 1
  89. }
  90. assert_response :success
  91. assert_select 'input[value=secret]', 0
  92. assert_select 'input[name=dummy_password][value^=xxxxxx]'
  93. end
  94. def test_edit_invalid_should_respond_with_404
  95. get :edit, :params => {
  96. :id => 99
  97. }
  98. assert_response 404
  99. end
  100. def test_update
  101. put :update, :params => {
  102. :id => 1,
  103. :auth_source => {
  104. :name => 'Renamed',
  105. :host => '192.168.0.10',
  106. :port => '389',
  107. :attr_login => 'uid'
  108. }
  109. }
  110. assert_redirected_to '/auth_sources'
  111. source = AuthSourceLdap.find(1)
  112. assert_equal 'Renamed', source.name
  113. assert_equal '192.168.0.10', source.host
  114. end
  115. def test_update_with_failure
  116. put :update, :params => {
  117. :id => 1,
  118. :auth_source => {
  119. :name => 'Renamed',
  120. :host => '',
  121. :port => '389',
  122. :attr_login => 'uid'
  123. }
  124. }
  125. assert_response :success
  126. assert_select_error /host cannot be blank/i
  127. end
  128. def test_destroy
  129. assert_difference 'AuthSourceLdap.count', -1 do
  130. delete :destroy, :params => {
  131. :id => 1
  132. }
  133. assert_redirected_to '/auth_sources'
  134. end
  135. end
  136. def test_destroy_auth_source_in_use
  137. User.find(2).update_attribute :auth_source_id, 1
  138. assert_no_difference 'AuthSourceLdap.count' do
  139. delete :destroy, :params => {
  140. :id => 1
  141. }
  142. assert_redirected_to '/auth_sources'
  143. assert_equal 'This authentication mode is in use and cannot be deleted.', flash[:error]
  144. end
  145. end
  146. def test_test_connection
  147. AuthSourceLdap.any_instance.stubs(:test_connection).returns(true)
  148. get :test_connection, :params => {
  149. :id => 1
  150. }
  151. assert_redirected_to '/auth_sources'
  152. assert_not_nil flash[:notice]
  153. assert_match /successful/i, flash[:notice]
  154. end
  155. def test_test_connection_with_failure
  156. AuthSourceLdap.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::Error.new("Something went wrong"))
  157. get :test_connection, :params => {
  158. :id => 1
  159. }
  160. assert_redirected_to '/auth_sources'
  161. assert_not_nil flash[:error]
  162. assert_include 'Something went wrong', flash[:error]
  163. end
  164. def test_autocomplete_for_new_user
  165. AuthSource.expects(:search).with('foo').returns([
  166. {:login => 'foo1', :firstname => 'John', :lastname => 'Smith', :mail => 'foo1@example.net', :auth_source_id => 1},
  167. {:login => 'Smith', :firstname => 'John', :lastname => 'Doe', :mail => 'foo2@example.net', :auth_source_id => 1}
  168. ])
  169. get :autocomplete_for_new_user, :params => {
  170. :term => 'foo'
  171. }
  172. assert_response :success
  173. assert_equal 'application/json', response.media_type
  174. json = ActiveSupport::JSON.decode(response.body)
  175. assert_kind_of Array, json
  176. assert_equal 2, json.size
  177. assert_equal 'foo1', json.first['value']
  178. assert_equal 'foo1 (John Smith)', json.first['label']
  179. end
  180. end