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.

groups_controller_test.rb 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 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 => {
  37. :name => "cli"
  38. }
  39. assert_response :success
  40. assert_select 'table.groups tbody tr', 1
  41. assert_select 'table.groups tbody td.name', :text => 'Clients'
  42. end
  43. def test_show
  44. get :show, :params => {
  45. :id => 10
  46. }
  47. assert_response :success
  48. end
  49. def test_show_should_display_custom_fields
  50. GroupCustomField.generate!(name: 'field_visible', visible: true)
  51. Group.find(10).update(custom_field_values: {GroupCustomField.last.id => 'value_visible'})
  52. GroupCustomField.generate!(name: 'field_invisible', visible: false)
  53. Group.find(10).update(custom_field_values: {GroupCustomField.last.id => 'value_invisible'})
  54. get :show, :params => {:id => 10}
  55. assert_response :success
  56. assert_select 'li', :text => /field_visible/
  57. assert_select 'li', :text => /value_visible/
  58. assert_select 'li', :text => /field_invisible/, :count => 0
  59. assert_select 'li', :text => /value_invisible/, :count => 0
  60. end
  61. def test_show_invalid_should_return_404
  62. get :show, :params => {
  63. :id => 99
  64. }
  65. assert_response 404
  66. end
  67. def test_new
  68. get :new
  69. assert_response :success
  70. assert_select 'input[name=?]', 'group[name]'
  71. end
  72. def test_create
  73. assert_difference 'Group.count' do
  74. post :create, :params => {
  75. :group => {
  76. :name => 'New group'
  77. }
  78. }
  79. end
  80. assert_redirected_to '/groups'
  81. group = Group.order('id DESC').first
  82. assert_equal 'New group', group.name
  83. assert_equal [], group.users
  84. end
  85. def test_create_and_continue
  86. assert_difference 'Group.count' do
  87. post :create, :params => {
  88. :group => {
  89. :name => 'New group'
  90. },
  91. :continue => 'Create and continue'
  92. }
  93. end
  94. assert_redirected_to '/groups/new'
  95. group = Group.order('id DESC').first
  96. assert_equal 'New group', group.name
  97. end
  98. def test_create_with_failure
  99. assert_no_difference 'Group.count' do
  100. post :create, :params => {
  101. :group => {
  102. :name => ''
  103. }
  104. }
  105. end
  106. assert_response :success
  107. assert_select_error /Name cannot be blank/i
  108. end
  109. def test_edit
  110. get :edit, :params => {
  111. :id => 10
  112. }
  113. assert_response :success
  114. assert_select 'div#tab-content-users'
  115. assert_select 'div#tab-content-memberships' do
  116. assert_select 'a', :text => 'Private child of eCookbook'
  117. end
  118. end
  119. def test_update
  120. new_name = 'New name'
  121. put :update, :params => {
  122. :id => 10,
  123. :group => {
  124. :name => new_name
  125. }
  126. }
  127. assert_redirected_to '/groups'
  128. group = Group.find(10)
  129. assert_equal new_name, group.name
  130. end
  131. def test_update_with_failure
  132. put :update, :params => {
  133. :id => 10,
  134. :group => {
  135. :name => ''
  136. }
  137. }
  138. assert_response :success
  139. assert_select_error /Name cannot be blank/i
  140. end
  141. def test_destroy
  142. assert_difference 'Group.count', -1 do
  143. post :destroy, :params => {
  144. :id => 10
  145. }
  146. end
  147. assert_redirected_to '/groups'
  148. end
  149. def test_new_users
  150. get :new_users, :params => {
  151. :id => 10
  152. }
  153. assert_response :success
  154. assert_select 'input[name=?]', 'user_search'
  155. end
  156. def test_xhr_new_users
  157. get :new_users, :params => {
  158. :id => 10
  159. },
  160. :xhr => true
  161. assert_response :success
  162. assert_equal 'text/javascript', response.content_type
  163. end
  164. def test_add_users
  165. assert_difference 'Group.find(10).users.count', 2 do
  166. post :add_users, :params => {
  167. :id => 10,
  168. :user_ids => ['2', '3']
  169. }
  170. end
  171. end
  172. def test_xhr_add_users
  173. assert_difference 'Group.find(10).users.count', 2 do
  174. post :add_users, :params => {
  175. :id => 10,
  176. :user_ids => ['2', '3']
  177. },
  178. :xhr => true
  179. assert_response :success
  180. assert_equal 'text/javascript', response.content_type
  181. end
  182. assert_match /John Smith/, response.body
  183. end
  184. def test_remove_user
  185. assert_difference 'Group.find(10).users.count', -1 do
  186. delete :remove_user, :params => {
  187. :id => 10,
  188. :user_id => '8'
  189. }
  190. end
  191. end
  192. def test_xhr_remove_user
  193. assert_difference 'Group.find(10).users.count', -1 do
  194. delete :remove_user, :params => {
  195. :id => 10,
  196. :user_id => '8'
  197. },
  198. :xhr => true
  199. assert_response :success
  200. assert_equal 'text/javascript', response.content_type
  201. end
  202. end
  203. def test_autocomplete_for_user
  204. get :autocomplete_for_user, :params => {
  205. :id => 10,
  206. :q => 'smi',
  207. :format => 'js'
  208. },
  209. :xhr => true
  210. assert_response :success
  211. assert_include 'John Smith', response.body
  212. end
  213. end