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.

memberships_test.rb 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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::MembershipsTest < Redmine::ApiTest::Base
  19. fixtures :projects, :users, :roles, :members, :member_roles
  20. def setup
  21. Setting.rest_api_enabled = '1'
  22. end
  23. test "GET /projects/:project_id/memberships.xml should return memberships" do
  24. get '/projects/1/memberships.xml', {}, credentials('jsmith')
  25. assert_response :success
  26. assert_equal 'application/xml', @response.content_type
  27. assert_tag :tag => 'memberships',
  28. :attributes => {:type => 'array'},
  29. :child => {
  30. :tag => 'membership',
  31. :child => {
  32. :tag => 'id',
  33. :content => '2',
  34. :sibling => {
  35. :tag => 'user',
  36. :attributes => {:id => '3', :name => 'Dave Lopper'},
  37. :sibling => {
  38. :tag => 'roles',
  39. :child => {
  40. :tag => 'role',
  41. :attributes => {:id => '2', :name => 'Developer'}
  42. }
  43. }
  44. }
  45. }
  46. }
  47. end
  48. test "GET /projects/:project_id/memberships.json should return memberships" do
  49. get '/projects/1/memberships.json', {}, credentials('jsmith')
  50. assert_response :success
  51. assert_equal 'application/json', @response.content_type
  52. json = ActiveSupport::JSON.decode(response.body)
  53. assert_equal({
  54. "memberships" =>
  55. [{"id"=>1,
  56. "project" => {"name"=>"eCookbook", "id"=>1},
  57. "roles" => [{"name"=>"Manager", "id"=>1}],
  58. "user" => {"name"=>"John Smith", "id"=>2}},
  59. {"id"=>2,
  60. "project" => {"name"=>"eCookbook", "id"=>1},
  61. "roles" => [{"name"=>"Developer", "id"=>2}],
  62. "user" => {"name"=>"Dave Lopper", "id"=>3}}],
  63. "limit" => 25,
  64. "total_count" => 2,
  65. "offset" => 0},
  66. json)
  67. end
  68. test "POST /projects/:project_id/memberships.xml should create the membership" do
  69. assert_difference 'Member.count' do
  70. post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith')
  71. assert_response :created
  72. end
  73. end
  74. test "POST /projects/:project_id/memberships.xml with invalid parameters should return errors" do
  75. assert_no_difference 'Member.count' do
  76. post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith')
  77. assert_response :unprocessable_entity
  78. assert_equal 'application/xml', @response.content_type
  79. assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"}
  80. end
  81. end
  82. test "GET /memberships/:id.xml should return the membership" do
  83. get '/memberships/2.xml', {}, credentials('jsmith')
  84. assert_response :success
  85. assert_equal 'application/xml', @response.content_type
  86. assert_tag :tag => 'membership',
  87. :child => {
  88. :tag => 'id',
  89. :content => '2',
  90. :sibling => {
  91. :tag => 'user',
  92. :attributes => {:id => '3', :name => 'Dave Lopper'},
  93. :sibling => {
  94. :tag => 'roles',
  95. :child => {
  96. :tag => 'role',
  97. :attributes => {:id => '2', :name => 'Developer'}
  98. }
  99. }
  100. }
  101. }
  102. end
  103. test "GET /memberships/:id.json should return the membership" do
  104. get '/memberships/2.json', {}, credentials('jsmith')
  105. assert_response :success
  106. assert_equal 'application/json', @response.content_type
  107. json = ActiveSupport::JSON.decode(response.body)
  108. assert_equal(
  109. {"membership" => {
  110. "id" => 2,
  111. "project" => {"name"=>"eCookbook", "id"=>1},
  112. "roles" => [{"name"=>"Developer", "id"=>2}],
  113. "user" => {"name"=>"Dave Lopper", "id"=>3}}
  114. },
  115. json)
  116. end
  117. test "PUT /memberships/:id.xml should update the membership" do
  118. assert_not_equal [1,2], Member.find(2).role_ids.sort
  119. assert_no_difference 'Member.count' do
  120. put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith')
  121. assert_response :ok
  122. assert_equal '', @response.body
  123. end
  124. member = Member.find(2)
  125. assert_equal [1,2], member.role_ids.sort
  126. end
  127. test "PUT /memberships/:id.xml with invalid parameters should return errors" do
  128. put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [99]}}, credentials('jsmith')
  129. assert_response :unprocessable_entity
  130. assert_equal 'application/xml', @response.content_type
  131. assert_tag 'errors', :child => {:tag => 'error', :content => /member_roles is invalid/}
  132. end
  133. test "DELETE /memberships/:id.xml should destroy the membership" do
  134. assert_difference 'Member.count', -1 do
  135. delete '/memberships/2.xml', {}, credentials('jsmith')
  136. assert_response :ok
  137. assert_equal '', @response.body
  138. end
  139. assert_nil Member.find_by_id(2)
  140. end
  141. test "DELETE /memberships/:id.xml should respond with 422 on failure" do
  142. assert_no_difference 'Member.count' do
  143. # A membership with an inherited role can't be deleted
  144. Member.find(2).member_roles.first.update_attribute :inherited_from, 99
  145. delete '/memberships/2.xml', {}, credentials('jsmith')
  146. assert_response :unprocessable_entity
  147. end
  148. end
  149. end