Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

groups_controller_test.rb 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 GroupsControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :users, :members, :member_roles, :roles, :groups_users
  21. def setup
  22. @request.session[:user_id] = 1
  23. end
  24. def test_index
  25. get :index
  26. assert_response :success
  27. assert_select 'table.groups'
  28. end
  29. def test_index_should_show_user_count
  30. get :index
  31. assert_response :success
  32. assert_select 'tr#group-11 td.user_count', :text => '1'
  33. end
  34. def test_index_with_name_filter
  35. Group.generate!(:name => "Clients")
  36. get(:index, :params => {:name => "cli"})
  37. assert_response :success
  38. assert_select 'table.groups tbody tr', 1
  39. assert_select 'table.groups tbody td.name', :text => 'Clients'
  40. end
  41. def test_show
  42. Role.anonymous.update! :users_visibility => 'all'
  43. @request.session[:user_id] = nil
  44. get(:show, :params => {:id => 10})
  45. assert_response :success
  46. assert_select 'li a.user.active[href=?]', '/users/8', :text => 'User Misc'
  47. end
  48. def test_show_should_display_custom_fields
  49. GroupCustomField.generate!(name: 'field_visible', visible: true)
  50. Group.find(10).update(custom_field_values: {GroupCustomField.last.id => 'value_visible'})
  51. GroupCustomField.generate!(name: 'field_invisible', visible: false)
  52. Group.find(10).update(custom_field_values: {GroupCustomField.last.id => 'value_invisible'})
  53. get :show, :params => {:id => 10}
  54. assert_response :success
  55. assert_select 'li', :text => /field_visible/
  56. assert_select 'li', :text => /value_visible/
  57. assert_select 'li', :text => /field_invisible/, :count => 0
  58. assert_select 'li', :text => /value_invisible/, :count => 0
  59. end
  60. def test_show_invalid_should_return_404
  61. get(:show, :params => {:id => 99})
  62. assert_response 404
  63. end
  64. def test_show_group_that_is_not_visible_should_return_404
  65. Role.anonymous.update! :users_visibility => 'members_of_visible_projects'
  66. @request.session[:user_id] = nil
  67. get :show, :params => {:id => 10}
  68. assert_response 404
  69. end
  70. def test_show_should_display_only_visible_users
  71. group = Group.find(10)
  72. locked_user = User.find(5)
  73. group.users << locked_user
  74. assert locked_user.locked?
  75. @request.session[:user_id] = nil
  76. get :show, :params => {:id => group.id}
  77. assert_response :success
  78. assert_select 'li', :text => 'User Misc'
  79. assert_select 'li', :text => locked_user.name, :count => 0
  80. end
  81. def test_new
  82. get :new
  83. assert_response :success
  84. assert_select 'input[name=?]', 'group[name]'
  85. end
  86. def test_create
  87. assert_difference 'Group.count' do
  88. post(
  89. :create, :params => {
  90. :group => {
  91. :name => 'New group'
  92. }
  93. }
  94. )
  95. end
  96. assert_redirected_to '/groups'
  97. group = Group.order('id DESC').first
  98. assert_equal 'New group', group.name
  99. assert_equal [], group.users
  100. end
  101. def test_create_and_continue
  102. assert_difference 'Group.count' do
  103. post(
  104. :create,
  105. :params => {
  106. :group => {
  107. :name => 'New group'
  108. },
  109. :continue => 'Create and continue'
  110. }
  111. )
  112. end
  113. assert_redirected_to '/groups/new'
  114. group = Group.order('id DESC').first
  115. assert_equal 'New group', group.name
  116. end
  117. def test_create_with_failure
  118. assert_no_difference 'Group.count' do
  119. post(
  120. :create,
  121. :params => {
  122. :group => {
  123. :name => ''
  124. }
  125. }
  126. )
  127. end
  128. assert_response :success
  129. assert_select_error /Name cannot be blank/i
  130. end
  131. def test_edit
  132. get(
  133. :edit,
  134. :params => {
  135. :id => 10
  136. }
  137. )
  138. assert_response :success
  139. assert_select 'div#tab-content-users'
  140. assert_select 'div#tab-content-memberships' do
  141. assert_select 'a', :text => 'Private child of eCookbook'
  142. end
  143. end
  144. def test_update
  145. new_name = 'New name'
  146. put(
  147. :update,
  148. :params => {
  149. :id => 10,
  150. :group => {
  151. :name => new_name
  152. }
  153. }
  154. )
  155. assert_redirected_to '/groups'
  156. group = Group.find(10)
  157. assert_equal new_name, group.name
  158. end
  159. def test_update_with_failure
  160. put(
  161. :update,
  162. :params => {
  163. :id => 10,
  164. :group => {
  165. :name => ''
  166. }
  167. }
  168. )
  169. assert_response :success
  170. assert_select_error /Name cannot be blank/i
  171. end
  172. def test_destroy
  173. assert_difference 'Group.count', -1 do
  174. post(:destroy, :params => {:id => 10})
  175. end
  176. assert_redirected_to '/groups'
  177. end
  178. def test_new_users
  179. get(:new_users, :params => {:id => 10})
  180. assert_response :success
  181. assert_select 'input[name=?]', 'user_search'
  182. end
  183. def test_xhr_new_users
  184. get(
  185. :new_users,
  186. :params => {
  187. :id => 10
  188. },
  189. :xhr => true
  190. )
  191. assert_response :success
  192. assert_equal 'text/javascript', response.media_type
  193. end
  194. def test_add_users
  195. assert_difference 'Group.find(10).users.count', 2 do
  196. post(
  197. :add_users,
  198. :params => {
  199. :id => 10,
  200. :user_ids => ['2', '3']
  201. }
  202. )
  203. end
  204. end
  205. def test_xhr_add_users
  206. assert_difference 'Group.find(10).users.count', 2 do
  207. post(
  208. :add_users,
  209. :params => {
  210. :id => 10,
  211. :user_ids => ['2', '3']
  212. },
  213. :xhr => true
  214. )
  215. assert_response :success
  216. assert_equal 'text/javascript', response.media_type
  217. end
  218. assert_match /John Smith/, response.body
  219. end
  220. def test_remove_user
  221. assert_difference 'Group.find(10).users.count', -1 do
  222. delete(
  223. :remove_user,
  224. :params => {
  225. :id => 10,
  226. :user_id => '8'
  227. }
  228. )
  229. end
  230. end
  231. def test_xhr_remove_user
  232. assert_difference 'Group.find(10).users.count', -1 do
  233. delete(
  234. :remove_user,
  235. :params => {
  236. :id => 10,
  237. :user_id => '8'
  238. },
  239. :xhr => true
  240. )
  241. assert_response :success
  242. assert_equal 'text/javascript', response.media_type
  243. end
  244. end
  245. def test_autocomplete_for_user
  246. get(
  247. :autocomplete_for_user,
  248. :params => {
  249. :id => 10,
  250. :q => 'smi',
  251. :format => 'js'
  252. },
  253. :xhr => true
  254. )
  255. assert_response :success
  256. assert_include 'John Smith', response.body
  257. end
  258. end