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

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