annotate test/integration/api_test/memberships_test.rb @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents 433d4f72a19b
children
rev   line source
Chris@1115 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1115 3 #
Chris@1115 4 # This program is free software; you can redistribute it and/or
Chris@1115 5 # modify it under the terms of the GNU General Public License
Chris@1115 6 # as published by the Free Software Foundation; either version 2
Chris@1115 7 # of the License, or (at your option) any later version.
Chris@1115 8 #
Chris@1115 9 # This program is distributed in the hope that it will be useful,
Chris@1115 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1115 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1115 12 # GNU General Public License for more details.
Chris@1115 13 #
Chris@1115 14 # You should have received a copy of the GNU General Public License
Chris@1115 15 # along with this program; if not, write to the Free Software
Chris@1115 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1115 17
Chris@1115 18 require File.expand_path('../../../test_helper', __FILE__)
Chris@1115 19
Chris@1295 20 class Redmine::ApiTest::MembershipsTest < Redmine::ApiTest::Base
Chris@1115 21 fixtures :projects, :users, :roles, :members, :member_roles
Chris@1115 22
Chris@1115 23 def setup
Chris@1115 24 Setting.rest_api_enabled = '1'
Chris@1115 25 end
Chris@1115 26
Chris@1115 27 context "/projects/:project_id/memberships" do
Chris@1115 28 context "GET" do
Chris@1115 29 context "xml" do
Chris@1115 30 should "return memberships" do
Chris@1115 31 get '/projects/1/memberships.xml', {}, credentials('jsmith')
Chris@1115 32
Chris@1115 33 assert_response :success
Chris@1115 34 assert_equal 'application/xml', @response.content_type
Chris@1115 35 assert_tag :tag => 'memberships',
Chris@1115 36 :attributes => {:type => 'array'},
Chris@1115 37 :child => {
Chris@1115 38 :tag => 'membership',
Chris@1115 39 :child => {
Chris@1115 40 :tag => 'id',
Chris@1115 41 :content => '2',
Chris@1115 42 :sibling => {
Chris@1115 43 :tag => 'user',
Chris@1115 44 :attributes => {:id => '3', :name => 'Dave Lopper'},
Chris@1115 45 :sibling => {
Chris@1115 46 :tag => 'roles',
Chris@1115 47 :child => {
Chris@1115 48 :tag => 'role',
Chris@1115 49 :attributes => {:id => '2', :name => 'Developer'}
Chris@1115 50 }
Chris@1115 51 }
Chris@1115 52 }
Chris@1115 53 }
Chris@1115 54 }
Chris@1115 55 end
Chris@1115 56 end
Chris@1115 57
Chris@1115 58 context "json" do
Chris@1115 59 should "return memberships" do
Chris@1115 60 get '/projects/1/memberships.json', {}, credentials('jsmith')
Chris@1115 61
Chris@1115 62 assert_response :success
Chris@1115 63 assert_equal 'application/json', @response.content_type
Chris@1115 64 json = ActiveSupport::JSON.decode(response.body)
Chris@1115 65 assert_equal({
Chris@1115 66 "memberships" =>
Chris@1115 67 [{"id"=>1,
Chris@1115 68 "project" => {"name"=>"eCookbook", "id"=>1},
Chris@1115 69 "roles" => [{"name"=>"Manager", "id"=>1}],
Chris@1115 70 "user" => {"name"=>"John Smith", "id"=>2}},
Chris@1115 71 {"id"=>2,
Chris@1115 72 "project" => {"name"=>"eCookbook", "id"=>1},
Chris@1115 73 "roles" => [{"name"=>"Developer", "id"=>2}],
Chris@1115 74 "user" => {"name"=>"Dave Lopper", "id"=>3}}],
Chris@1115 75 "limit" => 25,
Chris@1115 76 "total_count" => 2,
Chris@1115 77 "offset" => 0},
Chris@1115 78 json)
Chris@1115 79 end
Chris@1115 80 end
Chris@1115 81 end
Chris@1115 82
Chris@1115 83 context "POST" do
Chris@1115 84 context "xml" do
Chris@1115 85 should "create membership" do
Chris@1115 86 assert_difference 'Member.count' do
Chris@1115 87 post '/projects/1/memberships.xml', {:membership => {:user_id => 7, :role_ids => [2,3]}}, credentials('jsmith')
Chris@1115 88
Chris@1115 89 assert_response :created
Chris@1115 90 end
Chris@1115 91 end
Chris@1115 92
Chris@1115 93 should "return errors on failure" do
Chris@1115 94 assert_no_difference 'Member.count' do
Chris@1115 95 post '/projects/1/memberships.xml', {:membership => {:role_ids => [2,3]}}, credentials('jsmith')
Chris@1115 96
Chris@1115 97 assert_response :unprocessable_entity
Chris@1115 98 assert_equal 'application/xml', @response.content_type
Chris@1115 99 assert_tag 'errors', :child => {:tag => 'error', :content => "Principal can't be blank"}
Chris@1115 100 end
Chris@1115 101 end
Chris@1115 102 end
Chris@1115 103 end
Chris@1115 104 end
Chris@1115 105
Chris@1115 106 context "/memberships/:id" do
Chris@1115 107 context "GET" do
Chris@1115 108 context "xml" do
Chris@1115 109 should "return the membership" do
Chris@1115 110 get '/memberships/2.xml', {}, credentials('jsmith')
Chris@1115 111
Chris@1115 112 assert_response :success
Chris@1115 113 assert_equal 'application/xml', @response.content_type
Chris@1115 114 assert_tag :tag => 'membership',
Chris@1115 115 :child => {
Chris@1115 116 :tag => 'id',
Chris@1115 117 :content => '2',
Chris@1115 118 :sibling => {
Chris@1115 119 :tag => 'user',
Chris@1115 120 :attributes => {:id => '3', :name => 'Dave Lopper'},
Chris@1115 121 :sibling => {
Chris@1115 122 :tag => 'roles',
Chris@1115 123 :child => {
Chris@1115 124 :tag => 'role',
Chris@1115 125 :attributes => {:id => '2', :name => 'Developer'}
Chris@1115 126 }
Chris@1115 127 }
Chris@1115 128 }
Chris@1115 129 }
Chris@1115 130 end
Chris@1115 131 end
Chris@1115 132
Chris@1115 133 context "json" do
Chris@1115 134 should "return the membership" do
Chris@1115 135 get '/memberships/2.json', {}, credentials('jsmith')
Chris@1115 136
Chris@1115 137 assert_response :success
Chris@1115 138 assert_equal 'application/json', @response.content_type
Chris@1115 139 json = ActiveSupport::JSON.decode(response.body)
Chris@1115 140 assert_equal(
Chris@1115 141 {"membership" => {
Chris@1115 142 "id" => 2,
Chris@1115 143 "project" => {"name"=>"eCookbook", "id"=>1},
Chris@1115 144 "roles" => [{"name"=>"Developer", "id"=>2}],
Chris@1115 145 "user" => {"name"=>"Dave Lopper", "id"=>3}}
Chris@1115 146 },
Chris@1115 147 json)
Chris@1115 148 end
Chris@1115 149 end
Chris@1115 150 end
Chris@1115 151
Chris@1115 152 context "PUT" do
Chris@1115 153 context "xml" do
Chris@1115 154 should "update membership" do
Chris@1115 155 assert_not_equal [1,2], Member.find(2).role_ids.sort
Chris@1115 156 assert_no_difference 'Member.count' do
Chris@1115 157 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [1,2]}}, credentials('jsmith')
Chris@1115 158
Chris@1115 159 assert_response :ok
Chris@1115 160 assert_equal '', @response.body
Chris@1115 161 end
Chris@1115 162 member = Member.find(2)
Chris@1115 163 assert_equal [1,2], member.role_ids.sort
Chris@1115 164 end
Chris@1115 165
Chris@1115 166 should "return errors on failure" do
Chris@1115 167 put '/memberships/2.xml', {:membership => {:user_id => 3, :role_ids => [99]}}, credentials('jsmith')
Chris@1115 168
Chris@1115 169 assert_response :unprocessable_entity
Chris@1115 170 assert_equal 'application/xml', @response.content_type
Chris@1115 171 assert_tag 'errors', :child => {:tag => 'error', :content => /member_roles is invalid/}
Chris@1115 172 end
Chris@1115 173 end
Chris@1115 174 end
Chris@1115 175
Chris@1115 176 context "DELETE" do
Chris@1115 177 context "xml" do
Chris@1115 178 should "destroy membership" do
Chris@1115 179 assert_difference 'Member.count', -1 do
Chris@1115 180 delete '/memberships/2.xml', {}, credentials('jsmith')
Chris@1115 181
Chris@1115 182 assert_response :ok
Chris@1115 183 assert_equal '', @response.body
Chris@1115 184 end
Chris@1115 185 assert_nil Member.find_by_id(2)
Chris@1115 186 end
Chris@1115 187
Chris@1115 188 should "respond with 422 on failure" do
Chris@1115 189 assert_no_difference 'Member.count' do
Chris@1115 190 # A membership with an inherited role can't be deleted
Chris@1115 191 Member.find(2).member_roles.first.update_attribute :inherited_from, 99
Chris@1115 192 delete '/memberships/2.xml', {}, credentials('jsmith')
Chris@1115 193
Chris@1115 194 assert_response :unprocessable_entity
Chris@1115 195 end
Chris@1115 196 end
Chris@1115 197 end
Chris@1115 198 end
Chris@1115 199 end
Chris@1115 200 end