annotate .svn/pristine/20/2006d03f57217f185b674373ebf261dbb0c3c839.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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