annotate .svn/pristine/79/79339062a596d0cdaa28d0573ebbdb890c156ff0.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::ProjectsTest < ActionController::IntegrationTest
Chris@1295 21 fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
Chris@1295 22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
Chris@1295 23 :attachments, :custom_fields, :custom_values, :time_entries, :issue_categories
Chris@1295 24
Chris@1295 25 def setup
Chris@1295 26 Setting.rest_api_enabled = '1'
Chris@1295 27 set_tmp_attachments_directory
Chris@1295 28 end
Chris@1295 29
Chris@1295 30 context "GET /projects" do
Chris@1295 31 context ".xml" do
Chris@1295 32 should "return projects" do
Chris@1295 33 get '/projects.xml'
Chris@1295 34 assert_response :success
Chris@1295 35 assert_equal 'application/xml', @response.content_type
Chris@1295 36
Chris@1295 37 assert_tag :tag => 'projects',
Chris@1295 38 :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
Chris@1295 39 end
Chris@1295 40 end
Chris@1295 41
Chris@1295 42 context ".json" do
Chris@1295 43 should "return projects" do
Chris@1295 44 get '/projects.json'
Chris@1295 45 assert_response :success
Chris@1295 46 assert_equal 'application/json', @response.content_type
Chris@1295 47
Chris@1295 48 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 49 assert_kind_of Hash, json
Chris@1295 50 assert_kind_of Array, json['projects']
Chris@1295 51 assert_kind_of Hash, json['projects'].first
Chris@1295 52 assert json['projects'].first.has_key?('id')
Chris@1295 53 end
Chris@1295 54 end
Chris@1295 55 end
Chris@1295 56
Chris@1295 57 context "GET /projects/:id" do
Chris@1295 58 context ".xml" do
Chris@1295 59 # TODO: A private project is needed because should_allow_api_authentication
Chris@1295 60 # actually tests that authentication is *required*, not just allowed
Chris@1295 61 should_allow_api_authentication(:get, "/projects/2.xml")
Chris@1295 62
Chris@1295 63 should "return requested project" do
Chris@1295 64 get '/projects/1.xml'
Chris@1295 65 assert_response :success
Chris@1295 66 assert_equal 'application/xml', @response.content_type
Chris@1295 67
Chris@1295 68 assert_tag :tag => 'project',
Chris@1295 69 :child => {:tag => 'id', :content => '1'}
Chris@1295 70 assert_tag :tag => 'custom_field',
Chris@1295 71 :attributes => {:name => 'Development status'}, :content => 'Stable'
Chris@1295 72
Chris@1295 73 assert_no_tag 'trackers'
Chris@1295 74 assert_no_tag 'issue_categories'
Chris@1295 75 end
Chris@1295 76
Chris@1295 77 context "with hidden custom fields" do
Chris@1295 78 setup do
Chris@1295 79 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
Chris@1295 80 end
Chris@1295 81
Chris@1295 82 should "not display hidden custom fields" do
Chris@1295 83 get '/projects/1.xml'
Chris@1295 84 assert_response :success
Chris@1295 85 assert_equal 'application/xml', @response.content_type
Chris@1295 86
Chris@1295 87 assert_no_tag 'custom_field',
Chris@1295 88 :attributes => {:name => 'Development status'}
Chris@1295 89 end
Chris@1295 90 end
Chris@1295 91
Chris@1295 92 should "return categories with include=issue_categories" do
Chris@1295 93 get '/projects/1.xml?include=issue_categories'
Chris@1295 94 assert_response :success
Chris@1295 95 assert_equal 'application/xml', @response.content_type
Chris@1295 96
Chris@1295 97 assert_tag 'issue_categories',
Chris@1295 98 :attributes => {:type => 'array'},
Chris@1295 99 :child => {
Chris@1295 100 :tag => 'issue_category',
Chris@1295 101 :attributes => {
Chris@1295 102 :id => '2',
Chris@1295 103 :name => 'Recipes'
Chris@1295 104 }
Chris@1295 105 }
Chris@1295 106 end
Chris@1295 107
Chris@1295 108 should "return trackers with include=trackers" do
Chris@1295 109 get '/projects/1.xml?include=trackers'
Chris@1295 110 assert_response :success
Chris@1295 111 assert_equal 'application/xml', @response.content_type
Chris@1295 112
Chris@1295 113 assert_tag 'trackers',
Chris@1295 114 :attributes => {:type => 'array'},
Chris@1295 115 :child => {
Chris@1295 116 :tag => 'tracker',
Chris@1295 117 :attributes => {
Chris@1295 118 :id => '2',
Chris@1295 119 :name => 'Feature request'
Chris@1295 120 }
Chris@1295 121 }
Chris@1295 122 end
Chris@1295 123 end
Chris@1295 124
Chris@1295 125 context ".json" do
Chris@1295 126 should_allow_api_authentication(:get, "/projects/2.json")
Chris@1295 127
Chris@1295 128 should "return requested project" do
Chris@1295 129 get '/projects/1.json'
Chris@1295 130
Chris@1295 131 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 132 assert_kind_of Hash, json
Chris@1295 133 assert_kind_of Hash, json['project']
Chris@1295 134 assert_equal 1, json['project']['id']
Chris@1295 135 end
Chris@1295 136 end
Chris@1295 137 end
Chris@1295 138
Chris@1295 139 context "POST /projects" do
Chris@1295 140 context "with valid parameters" do
Chris@1295 141 setup do
Chris@1295 142 Setting.default_projects_modules = ['issue_tracking', 'repository']
Chris@1295 143 @parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
Chris@1295 144 end
Chris@1295 145
Chris@1295 146 context ".xml" do
Chris@1295 147 should_allow_api_authentication(:post,
Chris@1295 148 '/projects.xml',
Chris@1295 149 {:project => {:name => 'API test', :identifier => 'api-test'}},
Chris@1295 150 {:success_code => :created})
Chris@1295 151
Chris@1295 152
Chris@1295 153 should "create a project with the attributes" do
Chris@1295 154 assert_difference('Project.count') do
Chris@1295 155 post '/projects.xml', @parameters, credentials('admin')
Chris@1295 156 end
Chris@1295 157
Chris@1295 158 project = Project.first(:order => 'id DESC')
Chris@1295 159 assert_equal 'API test', project.name
Chris@1295 160 assert_equal 'api-test', project.identifier
Chris@1295 161 assert_equal ['issue_tracking', 'repository'], project.enabled_module_names.sort
Chris@1295 162 assert_equal Tracker.all.size, project.trackers.size
Chris@1295 163
Chris@1295 164 assert_response :created
Chris@1295 165 assert_equal 'application/xml', @response.content_type
Chris@1295 166 assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
Chris@1295 167 end
Chris@1295 168
Chris@1295 169 should "accept enabled_module_names attribute" do
Chris@1295 170 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
Chris@1295 171
Chris@1295 172 assert_difference('Project.count') do
Chris@1295 173 post '/projects.xml', @parameters, credentials('admin')
Chris@1295 174 end
Chris@1295 175
Chris@1295 176 project = Project.first(:order => 'id DESC')
Chris@1295 177 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
Chris@1295 178 end
Chris@1295 179
Chris@1295 180 should "accept tracker_ids attribute" do
Chris@1295 181 @parameters[:project].merge!({:tracker_ids => [1, 3]})
Chris@1295 182
Chris@1295 183 assert_difference('Project.count') do
Chris@1295 184 post '/projects.xml', @parameters, credentials('admin')
Chris@1295 185 end
Chris@1295 186
Chris@1295 187 project = Project.first(:order => 'id DESC')
Chris@1295 188 assert_equal [1, 3], project.trackers.map(&:id).sort
Chris@1295 189 end
Chris@1295 190 end
Chris@1295 191 end
Chris@1295 192
Chris@1295 193 context "with invalid parameters" do
Chris@1295 194 setup do
Chris@1295 195 @parameters = {:project => {:name => 'API test'}}
Chris@1295 196 end
Chris@1295 197
Chris@1295 198 context ".xml" do
Chris@1295 199 should "return errors" do
Chris@1295 200 assert_no_difference('Project.count') do
Chris@1295 201 post '/projects.xml', @parameters, credentials('admin')
Chris@1295 202 end
Chris@1295 203
Chris@1295 204 assert_response :unprocessable_entity
Chris@1295 205 assert_equal 'application/xml', @response.content_type
Chris@1295 206 assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
Chris@1295 207 end
Chris@1295 208 end
Chris@1295 209 end
Chris@1295 210 end
Chris@1295 211
Chris@1295 212 context "PUT /projects/:id" do
Chris@1295 213 context "with valid parameters" do
Chris@1295 214 setup do
Chris@1295 215 @parameters = {:project => {:name => 'API update'}}
Chris@1295 216 end
Chris@1295 217
Chris@1295 218 context ".xml" do
Chris@1295 219 should_allow_api_authentication(:put,
Chris@1295 220 '/projects/2.xml',
Chris@1295 221 {:project => {:name => 'API update'}},
Chris@1295 222 {:success_code => :ok})
Chris@1295 223
Chris@1295 224 should "update the project" do
Chris@1295 225 assert_no_difference 'Project.count' do
Chris@1295 226 put '/projects/2.xml', @parameters, credentials('jsmith')
Chris@1295 227 end
Chris@1295 228 assert_response :ok
Chris@1295 229 assert_equal '', @response.body
Chris@1295 230 assert_equal 'application/xml', @response.content_type
Chris@1295 231 project = Project.find(2)
Chris@1295 232 assert_equal 'API update', project.name
Chris@1295 233 end
Chris@1295 234
Chris@1295 235 should "accept enabled_module_names attribute" do
Chris@1295 236 @parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
Chris@1295 237
Chris@1295 238 assert_no_difference 'Project.count' do
Chris@1295 239 put '/projects/2.xml', @parameters, credentials('admin')
Chris@1295 240 end
Chris@1295 241 assert_response :ok
Chris@1295 242 assert_equal '', @response.body
Chris@1295 243 project = Project.find(2)
Chris@1295 244 assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
Chris@1295 245 end
Chris@1295 246
Chris@1295 247 should "accept tracker_ids attribute" do
Chris@1295 248 @parameters[:project].merge!({:tracker_ids => [1, 3]})
Chris@1295 249
Chris@1295 250 assert_no_difference 'Project.count' do
Chris@1295 251 put '/projects/2.xml', @parameters, credentials('admin')
Chris@1295 252 end
Chris@1295 253 assert_response :ok
Chris@1295 254 assert_equal '', @response.body
Chris@1295 255 project = Project.find(2)
Chris@1295 256 assert_equal [1, 3], project.trackers.map(&:id).sort
Chris@1295 257 end
Chris@1295 258 end
Chris@1295 259 end
Chris@1295 260
Chris@1295 261 context "with invalid parameters" do
Chris@1295 262 setup do
Chris@1295 263 @parameters = {:project => {:name => ''}}
Chris@1295 264 end
Chris@1295 265
Chris@1295 266 context ".xml" do
Chris@1295 267 should "return errors" do
Chris@1295 268 assert_no_difference('Project.count') do
Chris@1295 269 put '/projects/2.xml', @parameters, credentials('admin')
Chris@1295 270 end
Chris@1295 271
Chris@1295 272 assert_response :unprocessable_entity
Chris@1295 273 assert_equal 'application/xml', @response.content_type
Chris@1295 274 assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
Chris@1295 275 end
Chris@1295 276 end
Chris@1295 277 end
Chris@1295 278 end
Chris@1295 279
Chris@1295 280 context "DELETE /projects/:id" do
Chris@1295 281 context ".xml" do
Chris@1295 282 should_allow_api_authentication(:delete,
Chris@1295 283 '/projects/2.xml',
Chris@1295 284 {},
Chris@1295 285 {:success_code => :ok})
Chris@1295 286
Chris@1295 287 should "delete the project" do
Chris@1295 288 assert_difference('Project.count',-1) do
Chris@1295 289 delete '/projects/2.xml', {}, credentials('admin')
Chris@1295 290 end
Chris@1295 291 assert_response :ok
Chris@1295 292 assert_equal '', @response.body
Chris@1295 293 assert_nil Project.find_by_id(2)
Chris@1295 294 end
Chris@1295 295 end
Chris@1295 296 end
Chris@1295 297 end