annotate .svn/pristine/20/2006d03f57217f185b674373ebf261dbb0c3c839.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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