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

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