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

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