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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2014 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
  20. def setup
  21. Setting.rest_api_enabled = '1'
  22. end
  23. test "GET /groups.xml should require authentication" do
  24. get '/groups.xml'
  25. assert_response 401
  26. end
  27. test "GET /groups.xml should return groups" do
  28. get '/groups.xml', {}, credentials('admin')
  29. assert_response :success
  30. assert_equal 'application/xml', response.content_type
  31. assert_select 'groups' do
  32. assert_select 'group' do
  33. assert_select 'name', :text => 'A Team'
  34. assert_select 'id', :text => '10'
  35. end
  36. end
  37. end
  38. test "GET /groups.json should require authentication" do
  39. get '/groups.json'
  40. assert_response 401
  41. end
  42. test "GET /groups.json should return groups" do
  43. get '/groups.json', {}, credentials('admin')
  44. assert_response :success
  45. assert_equal 'application/json', response.content_type
  46. json = MultiJson.load(response.body)
  47. groups = json['groups']
  48. assert_kind_of Array, groups
  49. group = groups.detect {|g| g['name'] == 'A Team'}
  50. assert_not_nil group
  51. assert_equal({'id' => 10, 'name' => 'A Team'}, group)
  52. end
  53. test "GET /groups/:id.xml should return the group with its users" do
  54. get '/groups/10.xml', {}, credentials('admin')
  55. assert_response :success
  56. assert_equal 'application/xml', response.content_type
  57. assert_select 'group' do
  58. assert_select 'name', :text => 'A Team'
  59. assert_select 'id', :text => '10'
  60. end
  61. end
  62. test "GET /groups/:id.xml should include users if requested" do
  63. get '/groups/10.xml?include=users', {}, credentials('admin')
  64. assert_response :success
  65. assert_equal 'application/xml', response.content_type
  66. assert_select 'group' do
  67. assert_select 'users' do
  68. assert_select 'user', Group.find(10).users.count
  69. assert_select 'user[id=8]'
  70. end
  71. end
  72. end
  73. test "GET /groups/:id.xml include memberships if requested" do
  74. get '/groups/10.xml?include=memberships', {}, credentials('admin')
  75. assert_response :success
  76. assert_equal 'application/xml', response.content_type
  77. assert_select 'group' do
  78. assert_select 'memberships'
  79. end
  80. end
  81. test "POST /groups.xml with valid parameters should create the group" do
  82. assert_difference('Group.count') do
  83. post '/groups.xml', {:group => {:name => 'Test', :user_ids => [2, 3]}}, credentials('admin')
  84. assert_response :created
  85. assert_equal 'application/xml', response.content_type
  86. end
  87. group = Group.order('id DESC').first
  88. assert_equal 'Test', group.name
  89. assert_equal [2, 3], group.users.map(&:id).sort
  90. assert_select 'group' do
  91. assert_select 'name', :text => 'Test'
  92. end
  93. end
  94. test "POST /groups.xml with invalid parameters should return errors" do
  95. assert_no_difference('Group.count') do
  96. post '/groups.xml', {:group => {:name => ''}}, credentials('admin')
  97. end
  98. assert_response :unprocessable_entity
  99. assert_equal 'application/xml', response.content_type
  100. assert_select 'errors' do
  101. assert_select 'error', :text => /Name can't be blank/
  102. end
  103. end
  104. test "PUT /groups/:id.xml with valid parameters should update the group" do
  105. put '/groups/10.xml', {:group => {:name => 'New name', :user_ids => [2, 3]}}, credentials('admin')
  106. assert_response :ok
  107. assert_equal '', @response.body
  108. group = Group.find(10)
  109. assert_equal 'New name', group.name
  110. assert_equal [2, 3], group.users.map(&:id).sort
  111. end
  112. test "PUT /groups/:id.xml with invalid parameters should return errors" do
  113. put '/groups/10.xml', {:group => {:name => ''}}, credentials('admin')
  114. assert_response :unprocessable_entity
  115. assert_equal 'application/xml', response.content_type
  116. assert_select 'errors' do
  117. assert_select 'error', :text => /Name can't be blank/
  118. end
  119. end
  120. test "DELETE /groups/:id.xml should delete the group" do
  121. assert_difference 'Group.count', -1 do
  122. delete '/groups/10.xml', {}, credentials('admin')
  123. assert_response :ok
  124. assert_equal '', @response.body
  125. end
  126. end
  127. test "POST /groups/:id/users.xml should add user to the group" do
  128. assert_difference 'Group.find(10).users.count' do
  129. post '/groups/10/users.xml', {:user_id => 5}, credentials('admin')
  130. assert_response :ok
  131. assert_equal '', @response.body
  132. end
  133. assert_include User.find(5), Group.find(10).users
  134. end
  135. test "DELETE /groups/:id/users/:user_id.xml should remove user from the group" do
  136. assert_difference 'Group.find(10).users.count', -1 do
  137. delete '/groups/10/users/8.xml', {}, credentials('admin')
  138. assert_response :ok
  139. assert_equal '', @response.body
  140. end
  141. assert_not_include User.find(8), Group.find(10).users
  142. end
  143. end