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_test.rb 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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_relative '../../test_helper'
  19. class Redmine::ApiTest::GroupsTest < Redmine::ApiTest::Base
  20. fixtures :users, :groups_users, :email_addresses
  21. test "GET /groups.xml should require authentication" do
  22. get '/groups.xml'
  23. assert_response 401
  24. end
  25. test "GET /groups.xml should return givable groups" do
  26. get '/groups.xml', :headers => credentials('admin')
  27. assert_response :success
  28. assert_equal 'application/xml', response.media_type
  29. assert_select 'groups' do
  30. assert_select 'group', Group.givable.count
  31. assert_select 'group' do
  32. assert_select 'name', :text => 'A Team'
  33. assert_select 'id', :text => '10'
  34. end
  35. end
  36. end
  37. test "GET /groups.xml?builtin=1 should return all groups" do
  38. get '/groups.xml?builtin=1', :headers => credentials('admin')
  39. assert_response :success
  40. assert_equal 'application/xml', response.media_type
  41. assert_select 'groups' do
  42. assert_select 'group', Group.givable.count + 2
  43. assert_select 'group' do
  44. assert_select 'builtin', :text => 'non_member'
  45. assert_select 'id', :text => '12'
  46. end
  47. assert_select 'group' do
  48. assert_select 'builtin', :text => 'anonymous'
  49. assert_select 'id', :text => '13'
  50. end
  51. end
  52. end
  53. test "GET /groups.json should require authentication" do
  54. get '/groups.json'
  55. assert_response 401
  56. end
  57. test "GET /groups.json should return groups" do
  58. get '/groups.json', :headers => credentials('admin')
  59. assert_response :success
  60. assert_equal 'application/json', response.media_type
  61. json = ActiveSupport::JSON.decode(response.body)
  62. groups = json['groups']
  63. assert_kind_of Array, groups
  64. group = groups.detect {|g| g['name'] == 'A Team'}
  65. assert_not_nil group
  66. assert_equal({'id' => 10, 'name' => 'A Team'}, group)
  67. end
  68. test "GET /groups/:id.xml should return the group" do
  69. get '/groups/10.xml', :headers => credentials('admin')
  70. assert_response :success
  71. assert_equal 'application/xml', response.media_type
  72. assert_select 'group' do
  73. assert_select 'name', :text => 'A Team'
  74. assert_select 'id', :text => '10'
  75. end
  76. end
  77. test "GET /groups/:id.xml should return the builtin group" do
  78. get '/groups/12.xml', :headers => credentials('admin')
  79. assert_response :success
  80. assert_equal 'application/xml', response.media_type
  81. assert_select 'group' do
  82. assert_select 'builtin', :text => 'non_member'
  83. assert_select 'id', :text => '12'
  84. end
  85. end
  86. test "GET /groups/:id.xml should include users if requested" do
  87. get '/groups/10.xml?include=users', :headers => credentials('admin')
  88. assert_response :success
  89. assert_equal 'application/xml', response.media_type
  90. assert_select 'group' do
  91. assert_select 'users' do
  92. assert_select 'user', Group.find(10).users.count
  93. assert_select 'user[id="8"]'
  94. end
  95. end
  96. end
  97. test "GET /groups/:id.xml include memberships if requested" do
  98. get '/groups/10.xml?include=memberships', :headers => credentials('admin')
  99. assert_response :success
  100. assert_equal 'application/xml', response.media_type
  101. assert_select 'group' do
  102. assert_select 'memberships'
  103. end
  104. end
  105. test "POST /groups.xml with valid parameters should create the group" do
  106. assert_difference('Group.count') do
  107. post(
  108. '/groups.xml',
  109. :params => {:group => {:name => 'Test', :user_ids => [2, 3]}},
  110. :headers => credentials('admin')
  111. )
  112. assert_response :created
  113. assert_equal 'application/xml', response.media_type
  114. end
  115. group = Group.order('id DESC').first
  116. assert_equal 'Test', group.name
  117. assert_equal [2, 3], group.users.map(&:id).sort
  118. assert_select 'group' do
  119. assert_select 'name', :text => 'Test'
  120. end
  121. end
  122. test "POST /groups.xml with invalid parameters should return errors" do
  123. assert_no_difference('Group.count') do
  124. post(
  125. '/groups.xml',
  126. :params => {:group => {:name => ''}},
  127. :headers => credentials('admin')
  128. )
  129. end
  130. assert_response :unprocessable_entity
  131. assert_equal 'application/xml', response.media_type
  132. assert_select 'errors' do
  133. assert_select 'error', :text => /Name cannot be blank/
  134. end
  135. end
  136. test "PUT /groups/:id.xml with valid parameters should update the group" do
  137. group = Group.generate!
  138. put(
  139. "/groups/#{group.id}.xml",
  140. :params => {:group => {:name => 'New name', :user_ids => [2, 3]}},
  141. :headers => credentials('admin')
  142. )
  143. assert_response :no_content
  144. assert_equal '', @response.body
  145. assert_equal 'New name', group.reload.name
  146. assert_equal [2, 3], group.users.map(&:id).sort
  147. end
  148. test "PUT /groups/:id.xml with invalid parameters should return errors" do
  149. group = Group.generate!
  150. put(
  151. "/groups/#{group.id}.xml",
  152. :params => {:group => {:name => ''}},
  153. :headers => credentials('admin')
  154. )
  155. assert_response :unprocessable_entity
  156. assert_equal 'application/xml', response.media_type
  157. assert_select 'errors' do
  158. assert_select 'error', :text => /Name cannot be blank/
  159. end
  160. end
  161. test "DELETE /groups/:id.xml should delete the group" do
  162. group = Group.generate!
  163. assert_difference 'Group.count', -1 do
  164. delete "/groups/#{group.id}.xml", :headers => credentials('admin')
  165. assert_response :no_content
  166. assert_equal '', @response.body
  167. end
  168. end
  169. test "POST /groups/:id/users.xml should add user to the group" do
  170. group = Group.generate!
  171. assert_difference 'group.reload.users.count' do
  172. post(
  173. "/groups/#{group.id}/users.xml",
  174. :params => {:user_id => 5},
  175. :headers => credentials('admin')
  176. )
  177. assert_response :no_content
  178. assert_equal '', @response.body
  179. end
  180. assert_include User.find(5), group.reload.users
  181. end
  182. test "POST /groups/:id/users.xml should not add the user if already added" do
  183. group = Group.generate!
  184. group.users << User.find(5)
  185. assert_no_difference 'group.reload.users.count' do
  186. post(
  187. "/groups/#{group.id}/users.xml",
  188. :params => {:user_id => 5},
  189. :headers => credentials('admin')
  190. )
  191. assert_response :unprocessable_entity
  192. end
  193. assert_select 'errors' do
  194. assert_select 'error', :text => /User is invalid/
  195. end
  196. end
  197. test "DELETE /groups/:id/users/:user_id.xml should remove user from the group" do
  198. group = Group.generate!
  199. group.users << User.find(8)
  200. assert_difference 'group.reload.users.count', -1 do
  201. delete "/groups/#{group.id}/users/8.xml", :headers => credentials('admin')
  202. assert_response :no_content
  203. assert_equal '', @response.body
  204. end
  205. assert_not_include User.find(8), group.reload.users
  206. end
  207. end