annotate .svn/pristine/00/00e064a58814bd1506484c9e41b6bc1f8553a834.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::IssuesTest < ActionController::IntegrationTest
Chris@1295 21 fixtures :projects,
Chris@1295 22 :users,
Chris@1295 23 :roles,
Chris@1295 24 :members,
Chris@1295 25 :member_roles,
Chris@1295 26 :issues,
Chris@1295 27 :issue_statuses,
Chris@1295 28 :issue_relations,
Chris@1295 29 :versions,
Chris@1295 30 :trackers,
Chris@1295 31 :projects_trackers,
Chris@1295 32 :issue_categories,
Chris@1295 33 :enabled_modules,
Chris@1295 34 :enumerations,
Chris@1295 35 :attachments,
Chris@1295 36 :workflows,
Chris@1295 37 :custom_fields,
Chris@1295 38 :custom_values,
Chris@1295 39 :custom_fields_projects,
Chris@1295 40 :custom_fields_trackers,
Chris@1295 41 :time_entries,
Chris@1295 42 :journals,
Chris@1295 43 :journal_details,
Chris@1295 44 :queries,
Chris@1295 45 :attachments
Chris@1295 46
Chris@1295 47 def setup
Chris@1295 48 Setting.rest_api_enabled = '1'
Chris@1295 49 end
Chris@1295 50
Chris@1295 51 context "/issues" do
Chris@1295 52 # Use a private project to make sure auth is really working and not just
Chris@1295 53 # only showing public issues.
Chris@1295 54 should_allow_api_authentication(:get, "/projects/private-child/issues.xml")
Chris@1295 55
Chris@1295 56 should "contain metadata" do
Chris@1295 57 get '/issues.xml'
Chris@1295 58
Chris@1295 59 assert_tag :tag => 'issues',
Chris@1295 60 :attributes => {
Chris@1295 61 :type => 'array',
Chris@1295 62 :total_count => assigns(:issue_count),
Chris@1295 63 :limit => 25,
Chris@1295 64 :offset => 0
Chris@1295 65 }
Chris@1295 66 end
Chris@1295 67
Chris@1295 68 context "with offset and limit" do
Chris@1295 69 should "use the params" do
Chris@1295 70 get '/issues.xml?offset=2&limit=3'
Chris@1295 71
Chris@1295 72 assert_equal 3, assigns(:limit)
Chris@1295 73 assert_equal 2, assigns(:offset)
Chris@1295 74 assert_tag :tag => 'issues', :children => {:count => 3, :only => {:tag => 'issue'}}
Chris@1295 75 end
Chris@1295 76 end
Chris@1295 77
Chris@1295 78 context "with nometa param" do
Chris@1295 79 should "not contain metadata" do
Chris@1295 80 get '/issues.xml?nometa=1'
Chris@1295 81
Chris@1295 82 assert_tag :tag => 'issues',
Chris@1295 83 :attributes => {
Chris@1295 84 :type => 'array',
Chris@1295 85 :total_count => nil,
Chris@1295 86 :limit => nil,
Chris@1295 87 :offset => nil
Chris@1295 88 }
Chris@1295 89 end
Chris@1295 90 end
Chris@1295 91
Chris@1295 92 context "with nometa header" do
Chris@1295 93 should "not contain metadata" do
Chris@1295 94 get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
Chris@1295 95
Chris@1295 96 assert_tag :tag => 'issues',
Chris@1295 97 :attributes => {
Chris@1295 98 :type => 'array',
Chris@1295 99 :total_count => nil,
Chris@1295 100 :limit => nil,
Chris@1295 101 :offset => nil
Chris@1295 102 }
Chris@1295 103 end
Chris@1295 104 end
Chris@1295 105
Chris@1295 106 context "with relations" do
Chris@1295 107 should "display relations" do
Chris@1295 108 get '/issues.xml?include=relations'
Chris@1295 109
Chris@1295 110 assert_response :success
Chris@1295 111 assert_equal 'application/xml', @response.content_type
Chris@1295 112 assert_tag 'relations',
Chris@1295 113 :parent => {:tag => 'issue', :child => {:tag => 'id', :content => '3'}},
Chris@1295 114 :children => {:count => 1},
Chris@1295 115 :child => {
Chris@1295 116 :tag => 'relation',
Chris@1295 117 :attributes => {:id => '2', :issue_id => '2', :issue_to_id => '3',
Chris@1295 118 :relation_type => 'relates'}
Chris@1295 119 }
Chris@1295 120 assert_tag 'relations',
Chris@1295 121 :parent => {:tag => 'issue', :child => {:tag => 'id', :content => '1'}},
Chris@1295 122 :children => {:count => 0}
Chris@1295 123 end
Chris@1295 124 end
Chris@1295 125
Chris@1295 126 context "with invalid query params" do
Chris@1295 127 should "return errors" do
Chris@1295 128 get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}
Chris@1295 129
Chris@1295 130 assert_response :unprocessable_entity
Chris@1295 131 assert_equal 'application/xml', @response.content_type
Chris@1295 132 assert_tag 'errors', :child => {:tag => 'error', :content => "Start date can't be blank"}
Chris@1295 133 end
Chris@1295 134 end
Chris@1295 135
Chris@1295 136 context "with custom field filter" do
Chris@1295 137 should "show only issues with the custom field value" do
Chris@1295 138 get '/issues.xml',
Chris@1295 139 {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='},
Chris@1295 140 :v => {:cf_1 => ['MySQL']}}
Chris@1295 141 expected_ids = Issue.visible.all(
Chris@1295 142 :include => :custom_values,
Chris@1295 143 :conditions => {:custom_values => {:custom_field_id => 1, :value => 'MySQL'}}).map(&:id)
Chris@1295 144 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
Chris@1295 145 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
Chris@1295 146 end
Chris@1295 147 end
Chris@1295 148 end
Chris@1295 149
Chris@1295 150 context "with custom field filter (shorthand method)" do
Chris@1295 151 should "show only issues with the custom field value" do
Chris@1295 152 get '/issues.xml', { :cf_1 => 'MySQL' }
Chris@1295 153
Chris@1295 154 expected_ids = Issue.visible.all(
Chris@1295 155 :include => :custom_values,
Chris@1295 156 :conditions => {:custom_values => {:custom_field_id => 1, :value => 'MySQL'}}).map(&:id)
Chris@1295 157
Chris@1295 158 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
Chris@1295 159 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
Chris@1295 160 end
Chris@1295 161 end
Chris@1295 162 end
Chris@1295 163 end
Chris@1295 164
Chris@1295 165 context "/index.json" do
Chris@1295 166 should_allow_api_authentication(:get, "/projects/private-child/issues.json")
Chris@1295 167 end
Chris@1295 168
Chris@1295 169 context "/index.xml with filter" do
Chris@1295 170 should "show only issues with the status_id" do
Chris@1295 171 get '/issues.xml?status_id=5'
Chris@1295 172
Chris@1295 173 expected_ids = Issue.visible.all(:conditions => {:status_id => 5}).map(&:id)
Chris@1295 174
Chris@1295 175 assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
Chris@1295 176 ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
Chris@1295 177 end
Chris@1295 178 end
Chris@1295 179 end
Chris@1295 180
Chris@1295 181 context "/index.json with filter" do
Chris@1295 182 should "show only issues with the status_id" do
Chris@1295 183 get '/issues.json?status_id=5'
Chris@1295 184
Chris@1295 185 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 186 status_ids_used = json['issues'].collect {|j| j['status']['id'] }
Chris@1295 187 assert_equal 3, status_ids_used.length
Chris@1295 188 assert status_ids_used.all? {|id| id == 5 }
Chris@1295 189 end
Chris@1295 190
Chris@1295 191 end
Chris@1295 192
Chris@1295 193 # Issue 6 is on a private project
Chris@1295 194 context "/issues/6.xml" do
Chris@1295 195 should_allow_api_authentication(:get, "/issues/6.xml")
Chris@1295 196 end
Chris@1295 197
Chris@1295 198 context "/issues/6.json" do
Chris@1295 199 should_allow_api_authentication(:get, "/issues/6.json")
Chris@1295 200 end
Chris@1295 201
Chris@1295 202 context "GET /issues/:id" do
Chris@1295 203 context "with journals" do
Chris@1295 204 context ".xml" do
Chris@1295 205 should "display journals" do
Chris@1295 206 get '/issues/1.xml?include=journals'
Chris@1295 207
Chris@1295 208 assert_tag :tag => 'issue',
Chris@1295 209 :child => {
Chris@1295 210 :tag => 'journals',
Chris@1295 211 :attributes => { :type => 'array' },
Chris@1295 212 :child => {
Chris@1295 213 :tag => 'journal',
Chris@1295 214 :attributes => { :id => '1'},
Chris@1295 215 :child => {
Chris@1295 216 :tag => 'details',
Chris@1295 217 :attributes => { :type => 'array' },
Chris@1295 218 :child => {
Chris@1295 219 :tag => 'detail',
Chris@1295 220 :attributes => { :name => 'status_id' },
Chris@1295 221 :child => {
Chris@1295 222 :tag => 'old_value',
Chris@1295 223 :content => '1',
Chris@1295 224 :sibling => {
Chris@1295 225 :tag => 'new_value',
Chris@1295 226 :content => '2'
Chris@1295 227 }
Chris@1295 228 }
Chris@1295 229 }
Chris@1295 230 }
Chris@1295 231 }
Chris@1295 232 }
Chris@1295 233 end
Chris@1295 234 end
Chris@1295 235 end
Chris@1295 236
Chris@1295 237 context "with custom fields" do
Chris@1295 238 context ".xml" do
Chris@1295 239 should "display custom fields" do
Chris@1295 240 get '/issues/3.xml'
Chris@1295 241
Chris@1295 242 assert_tag :tag => 'issue',
Chris@1295 243 :child => {
Chris@1295 244 :tag => 'custom_fields',
Chris@1295 245 :attributes => { :type => 'array' },
Chris@1295 246 :child => {
Chris@1295 247 :tag => 'custom_field',
Chris@1295 248 :attributes => { :id => '1'},
Chris@1295 249 :child => {
Chris@1295 250 :tag => 'value',
Chris@1295 251 :content => 'MySQL'
Chris@1295 252 }
Chris@1295 253 }
Chris@1295 254 }
Chris@1295 255
Chris@1295 256 assert_nothing_raised do
Chris@1295 257 Hash.from_xml(response.body).to_xml
Chris@1295 258 end
Chris@1295 259 end
Chris@1295 260 end
Chris@1295 261 end
Chris@1295 262
Chris@1295 263 context "with multi custom fields" do
Chris@1295 264 setup do
Chris@1295 265 field = CustomField.find(1)
Chris@1295 266 field.update_attribute :multiple, true
Chris@1295 267 issue = Issue.find(3)
Chris@1295 268 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
Chris@1295 269 issue.save!
Chris@1295 270 end
Chris@1295 271
Chris@1295 272 context ".xml" do
Chris@1295 273 should "display custom fields" do
Chris@1295 274 get '/issues/3.xml'
Chris@1295 275 assert_response :success
Chris@1295 276 assert_tag :tag => 'issue',
Chris@1295 277 :child => {
Chris@1295 278 :tag => 'custom_fields',
Chris@1295 279 :attributes => { :type => 'array' },
Chris@1295 280 :child => {
Chris@1295 281 :tag => 'custom_field',
Chris@1295 282 :attributes => { :id => '1'},
Chris@1295 283 :child => {
Chris@1295 284 :tag => 'value',
Chris@1295 285 :attributes => { :type => 'array' },
Chris@1295 286 :children => { :count => 2 }
Chris@1295 287 }
Chris@1295 288 }
Chris@1295 289 }
Chris@1295 290
Chris@1295 291 xml = Hash.from_xml(response.body)
Chris@1295 292 custom_fields = xml['issue']['custom_fields']
Chris@1295 293 assert_kind_of Array, custom_fields
Chris@1295 294 field = custom_fields.detect {|f| f['id'] == '1'}
Chris@1295 295 assert_kind_of Hash, field
Chris@1295 296 assert_equal ['MySQL', 'Oracle'], field['value'].sort
Chris@1295 297 end
Chris@1295 298 end
Chris@1295 299
Chris@1295 300 context ".json" do
Chris@1295 301 should "display custom fields" do
Chris@1295 302 get '/issues/3.json'
Chris@1295 303 assert_response :success
Chris@1295 304 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 305 custom_fields = json['issue']['custom_fields']
Chris@1295 306 assert_kind_of Array, custom_fields
Chris@1295 307 field = custom_fields.detect {|f| f['id'] == 1}
Chris@1295 308 assert_kind_of Hash, field
Chris@1295 309 assert_equal ['MySQL', 'Oracle'], field['value'].sort
Chris@1295 310 end
Chris@1295 311 end
Chris@1295 312 end
Chris@1295 313
Chris@1295 314 context "with empty value for multi custom field" do
Chris@1295 315 setup do
Chris@1295 316 field = CustomField.find(1)
Chris@1295 317 field.update_attribute :multiple, true
Chris@1295 318 issue = Issue.find(3)
Chris@1295 319 issue.custom_field_values = {1 => ['']}
Chris@1295 320 issue.save!
Chris@1295 321 end
Chris@1295 322
Chris@1295 323 context ".xml" do
Chris@1295 324 should "display custom fields" do
Chris@1295 325 get '/issues/3.xml'
Chris@1295 326 assert_response :success
Chris@1295 327 assert_tag :tag => 'issue',
Chris@1295 328 :child => {
Chris@1295 329 :tag => 'custom_fields',
Chris@1295 330 :attributes => { :type => 'array' },
Chris@1295 331 :child => {
Chris@1295 332 :tag => 'custom_field',
Chris@1295 333 :attributes => { :id => '1'},
Chris@1295 334 :child => {
Chris@1295 335 :tag => 'value',
Chris@1295 336 :attributes => { :type => 'array' },
Chris@1295 337 :children => { :count => 0 }
Chris@1295 338 }
Chris@1295 339 }
Chris@1295 340 }
Chris@1295 341
Chris@1295 342 xml = Hash.from_xml(response.body)
Chris@1295 343 custom_fields = xml['issue']['custom_fields']
Chris@1295 344 assert_kind_of Array, custom_fields
Chris@1295 345 field = custom_fields.detect {|f| f['id'] == '1'}
Chris@1295 346 assert_kind_of Hash, field
Chris@1295 347 assert_equal [], field['value']
Chris@1295 348 end
Chris@1295 349 end
Chris@1295 350
Chris@1295 351 context ".json" do
Chris@1295 352 should "display custom fields" do
Chris@1295 353 get '/issues/3.json'
Chris@1295 354 assert_response :success
Chris@1295 355 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 356 custom_fields = json['issue']['custom_fields']
Chris@1295 357 assert_kind_of Array, custom_fields
Chris@1295 358 field = custom_fields.detect {|f| f['id'] == 1}
Chris@1295 359 assert_kind_of Hash, field
Chris@1295 360 assert_equal [], field['value'].sort
Chris@1295 361 end
Chris@1295 362 end
Chris@1295 363 end
Chris@1295 364
Chris@1295 365 context "with attachments" do
Chris@1295 366 context ".xml" do
Chris@1295 367 should "display attachments" do
Chris@1295 368 get '/issues/3.xml?include=attachments'
Chris@1295 369
Chris@1295 370 assert_tag :tag => 'issue',
Chris@1295 371 :child => {
Chris@1295 372 :tag => 'attachments',
Chris@1295 373 :children => {:count => 5},
Chris@1295 374 :child => {
Chris@1295 375 :tag => 'attachment',
Chris@1295 376 :child => {
Chris@1295 377 :tag => 'filename',
Chris@1295 378 :content => 'source.rb',
Chris@1295 379 :sibling => {
Chris@1295 380 :tag => 'content_url',
Chris@1295 381 :content => 'http://www.example.com/attachments/download/4/source.rb'
Chris@1295 382 }
Chris@1295 383 }
Chris@1295 384 }
Chris@1295 385 }
Chris@1295 386 end
Chris@1295 387 end
Chris@1295 388 end
Chris@1295 389
Chris@1295 390 context "with subtasks" do
Chris@1295 391 setup do
Chris@1295 392 @c1 = Issue.create!(
Chris@1295 393 :status_id => 1, :subject => "child c1",
Chris@1295 394 :tracker_id => 1, :project_id => 1, :author_id => 1,
Chris@1295 395 :parent_issue_id => 1
Chris@1295 396 )
Chris@1295 397 @c2 = Issue.create!(
Chris@1295 398 :status_id => 1, :subject => "child c2",
Chris@1295 399 :tracker_id => 1, :project_id => 1, :author_id => 1,
Chris@1295 400 :parent_issue_id => 1
Chris@1295 401 )
Chris@1295 402 @c3 = Issue.create!(
Chris@1295 403 :status_id => 1, :subject => "child c3",
Chris@1295 404 :tracker_id => 1, :project_id => 1, :author_id => 1,
Chris@1295 405 :parent_issue_id => @c1.id
Chris@1295 406 )
Chris@1295 407 end
Chris@1295 408
Chris@1295 409 context ".xml" do
Chris@1295 410 should "display children" do
Chris@1295 411 get '/issues/1.xml?include=children'
Chris@1295 412
Chris@1295 413 assert_tag :tag => 'issue',
Chris@1295 414 :child => {
Chris@1295 415 :tag => 'children',
Chris@1295 416 :children => {:count => 2},
Chris@1295 417 :child => {
Chris@1295 418 :tag => 'issue',
Chris@1295 419 :attributes => {:id => @c1.id.to_s},
Chris@1295 420 :child => {
Chris@1295 421 :tag => 'subject',
Chris@1295 422 :content => 'child c1',
Chris@1295 423 :sibling => {
Chris@1295 424 :tag => 'children',
Chris@1295 425 :children => {:count => 1},
Chris@1295 426 :child => {
Chris@1295 427 :tag => 'issue',
Chris@1295 428 :attributes => {:id => @c3.id.to_s}
Chris@1295 429 }
Chris@1295 430 }
Chris@1295 431 }
Chris@1295 432 }
Chris@1295 433 }
Chris@1295 434 end
Chris@1295 435
Chris@1295 436 context ".json" do
Chris@1295 437 should "display children" do
Chris@1295 438 get '/issues/1.json?include=children'
Chris@1295 439
Chris@1295 440 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 441 assert_equal([
Chris@1295 442 {
Chris@1295 443 'id' => @c1.id, 'subject' => 'child c1', 'tracker' => {'id' => 1, 'name' => 'Bug'},
Chris@1295 444 'children' => [{'id' => @c3.id, 'subject' => 'child c3',
Chris@1295 445 'tracker' => {'id' => 1, 'name' => 'Bug'} }]
Chris@1295 446 },
Chris@1295 447 { 'id' => @c2.id, 'subject' => 'child c2', 'tracker' => {'id' => 1, 'name' => 'Bug'} }
Chris@1295 448 ],
Chris@1295 449 json['issue']['children'])
Chris@1295 450 end
Chris@1295 451 end
Chris@1295 452 end
Chris@1295 453 end
Chris@1295 454 end
Chris@1295 455
Chris@1295 456 context "POST /issues.xml" do
Chris@1295 457 should_allow_api_authentication(
Chris@1295 458 :post,
Chris@1295 459 '/issues.xml',
Chris@1295 460 {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
Chris@1295 461 {:success_code => :created}
Chris@1295 462 )
Chris@1295 463 should "create an issue with the attributes" do
Chris@1295 464 assert_difference('Issue.count') do
Chris@1295 465 post '/issues.xml',
Chris@1295 466 {:issue => {:project_id => 1, :subject => 'API test',
Chris@1295 467 :tracker_id => 2, :status_id => 3}}, credentials('jsmith')
Chris@1295 468 end
Chris@1295 469 issue = Issue.first(:order => 'id DESC')
Chris@1295 470 assert_equal 1, issue.project_id
Chris@1295 471 assert_equal 2, issue.tracker_id
Chris@1295 472 assert_equal 3, issue.status_id
Chris@1295 473 assert_equal 'API test', issue.subject
Chris@1295 474
Chris@1295 475 assert_response :created
Chris@1295 476 assert_equal 'application/xml', @response.content_type
Chris@1295 477 assert_tag 'issue', :child => {:tag => 'id', :content => issue.id.to_s}
Chris@1295 478 end
Chris@1295 479 end
Chris@1295 480
Chris@1295 481 context "POST /issues.xml with failure" do
Chris@1295 482 should "have an errors tag" do
Chris@1295 483 assert_no_difference('Issue.count') do
Chris@1295 484 post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
Chris@1295 485 end
Chris@1295 486
Chris@1295 487 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
Chris@1295 488 end
Chris@1295 489 end
Chris@1295 490
Chris@1295 491 context "POST /issues.json" do
Chris@1295 492 should_allow_api_authentication(:post,
Chris@1295 493 '/issues.json',
Chris@1295 494 {:issue => {:project_id => 1, :subject => 'API test',
Chris@1295 495 :tracker_id => 2, :status_id => 3}},
Chris@1295 496 {:success_code => :created})
Chris@1295 497
Chris@1295 498 should "create an issue with the attributes" do
Chris@1295 499 assert_difference('Issue.count') do
Chris@1295 500 post '/issues.json',
Chris@1295 501 {:issue => {:project_id => 1, :subject => 'API test',
Chris@1295 502 :tracker_id => 2, :status_id => 3}},
Chris@1295 503 credentials('jsmith')
Chris@1295 504 end
Chris@1295 505
Chris@1295 506 issue = Issue.first(:order => 'id DESC')
Chris@1295 507 assert_equal 1, issue.project_id
Chris@1295 508 assert_equal 2, issue.tracker_id
Chris@1295 509 assert_equal 3, issue.status_id
Chris@1295 510 assert_equal 'API test', issue.subject
Chris@1295 511 end
Chris@1295 512
Chris@1295 513 end
Chris@1295 514
Chris@1295 515 context "POST /issues.json with failure" do
Chris@1295 516 should "have an errors element" do
Chris@1295 517 assert_no_difference('Issue.count') do
Chris@1295 518 post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
Chris@1295 519 end
Chris@1295 520
Chris@1295 521 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 522 assert json['errors'].include?("Subject can't be blank")
Chris@1295 523 end
Chris@1295 524 end
Chris@1295 525
Chris@1295 526 # Issue 6 is on a private project
Chris@1295 527 context "PUT /issues/6.xml" do
Chris@1295 528 setup do
Chris@1295 529 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
Chris@1295 530 end
Chris@1295 531
Chris@1295 532 should_allow_api_authentication(:put,
Chris@1295 533 '/issues/6.xml',
Chris@1295 534 {:issue => {:subject => 'API update', :notes => 'A new note'}},
Chris@1295 535 {:success_code => :ok})
Chris@1295 536
Chris@1295 537 should "not create a new issue" do
Chris@1295 538 assert_no_difference('Issue.count') do
Chris@1295 539 put '/issues/6.xml', @parameters, credentials('jsmith')
Chris@1295 540 end
Chris@1295 541 end
Chris@1295 542
Chris@1295 543 should "create a new journal" do
Chris@1295 544 assert_difference('Journal.count') do
Chris@1295 545 put '/issues/6.xml', @parameters, credentials('jsmith')
Chris@1295 546 end
Chris@1295 547 end
Chris@1295 548
Chris@1295 549 should "add the note to the journal" do
Chris@1295 550 put '/issues/6.xml', @parameters, credentials('jsmith')
Chris@1295 551
Chris@1295 552 journal = Journal.last
Chris@1295 553 assert_equal "A new note", journal.notes
Chris@1295 554 end
Chris@1295 555
Chris@1295 556 should "update the issue" do
Chris@1295 557 put '/issues/6.xml', @parameters, credentials('jsmith')
Chris@1295 558
Chris@1295 559 issue = Issue.find(6)
Chris@1295 560 assert_equal "API update", issue.subject
Chris@1295 561 end
Chris@1295 562
Chris@1295 563 end
Chris@1295 564
Chris@1295 565 context "PUT /issues/3.xml with custom fields" do
Chris@1295 566 setup do
Chris@1295 567 @parameters = {
Chris@1295 568 :issue => {:custom_fields => [{'id' => '1', 'value' => 'PostgreSQL' },
Chris@1295 569 {'id' => '2', 'value' => '150'}]}
Chris@1295 570 }
Chris@1295 571 end
Chris@1295 572
Chris@1295 573 should "update custom fields" do
Chris@1295 574 assert_no_difference('Issue.count') do
Chris@1295 575 put '/issues/3.xml', @parameters, credentials('jsmith')
Chris@1295 576 end
Chris@1295 577
Chris@1295 578 issue = Issue.find(3)
Chris@1295 579 assert_equal '150', issue.custom_value_for(2).value
Chris@1295 580 assert_equal 'PostgreSQL', issue.custom_value_for(1).value
Chris@1295 581 end
Chris@1295 582 end
Chris@1295 583
Chris@1295 584 context "PUT /issues/3.xml with multi custom fields" do
Chris@1295 585 setup do
Chris@1295 586 field = CustomField.find(1)
Chris@1295 587 field.update_attribute :multiple, true
Chris@1295 588 @parameters = {
Chris@1295 589 :issue => {:custom_fields => [{'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
Chris@1295 590 {'id' => '2', 'value' => '150'}]}
Chris@1295 591 }
Chris@1295 592 end
Chris@1295 593
Chris@1295 594 should "update custom fields" do
Chris@1295 595 assert_no_difference('Issue.count') do
Chris@1295 596 put '/issues/3.xml', @parameters, credentials('jsmith')
Chris@1295 597 end
Chris@1295 598
Chris@1295 599 issue = Issue.find(3)
Chris@1295 600 assert_equal '150', issue.custom_value_for(2).value
Chris@1295 601 assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
Chris@1295 602 end
Chris@1295 603 end
Chris@1295 604
Chris@1295 605 context "PUT /issues/3.xml with project change" do
Chris@1295 606 setup do
Chris@1295 607 @parameters = {:issue => {:project_id => 2, :subject => 'Project changed'}}
Chris@1295 608 end
Chris@1295 609
Chris@1295 610 should "update project" do
Chris@1295 611 assert_no_difference('Issue.count') do
Chris@1295 612 put '/issues/3.xml', @parameters, credentials('jsmith')
Chris@1295 613 end
Chris@1295 614
Chris@1295 615 issue = Issue.find(3)
Chris@1295 616 assert_equal 2, issue.project_id
Chris@1295 617 assert_equal 'Project changed', issue.subject
Chris@1295 618 end
Chris@1295 619 end
Chris@1295 620
Chris@1295 621 context "PUT /issues/6.xml with failed update" do
Chris@1295 622 setup do
Chris@1295 623 @parameters = {:issue => {:subject => ''}}
Chris@1295 624 end
Chris@1295 625
Chris@1295 626 should "not create a new issue" do
Chris@1295 627 assert_no_difference('Issue.count') do
Chris@1295 628 put '/issues/6.xml', @parameters, credentials('jsmith')
Chris@1295 629 end
Chris@1295 630 end
Chris@1295 631
Chris@1295 632 should "not create a new journal" do
Chris@1295 633 assert_no_difference('Journal.count') do
Chris@1295 634 put '/issues/6.xml', @parameters, credentials('jsmith')
Chris@1295 635 end
Chris@1295 636 end
Chris@1295 637
Chris@1295 638 should "have an errors tag" do
Chris@1295 639 put '/issues/6.xml', @parameters, credentials('jsmith')
Chris@1295 640
Chris@1295 641 assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
Chris@1295 642 end
Chris@1295 643 end
Chris@1295 644
Chris@1295 645 context "PUT /issues/6.json" do
Chris@1295 646 setup do
Chris@1295 647 @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
Chris@1295 648 end
Chris@1295 649
Chris@1295 650 should_allow_api_authentication(:put,
Chris@1295 651 '/issues/6.json',
Chris@1295 652 {:issue => {:subject => 'API update', :notes => 'A new note'}},
Chris@1295 653 {:success_code => :ok})
Chris@1295 654
Chris@1295 655 should "update the issue" do
Chris@1295 656 assert_no_difference('Issue.count') do
Chris@1295 657 assert_difference('Journal.count') do
Chris@1295 658 put '/issues/6.json', @parameters, credentials('jsmith')
Chris@1295 659
Chris@1295 660 assert_response :ok
Chris@1295 661 assert_equal '', response.body
Chris@1295 662 end
Chris@1295 663 end
Chris@1295 664
Chris@1295 665 issue = Issue.find(6)
Chris@1295 666 assert_equal "API update", issue.subject
Chris@1295 667 journal = Journal.last
Chris@1295 668 assert_equal "A new note", journal.notes
Chris@1295 669 end
Chris@1295 670 end
Chris@1295 671
Chris@1295 672 context "PUT /issues/6.json with failed update" do
Chris@1295 673 should "return errors" do
Chris@1295 674 assert_no_difference('Issue.count') do
Chris@1295 675 assert_no_difference('Journal.count') do
Chris@1295 676 put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')
Chris@1295 677
Chris@1295 678 assert_response :unprocessable_entity
Chris@1295 679 end
Chris@1295 680 end
Chris@1295 681
Chris@1295 682 json = ActiveSupport::JSON.decode(response.body)
Chris@1295 683 assert json['errors'].include?("Subject can't be blank")
Chris@1295 684 end
Chris@1295 685 end
Chris@1295 686
Chris@1295 687 context "DELETE /issues/1.xml" do
Chris@1295 688 should_allow_api_authentication(:delete,
Chris@1295 689 '/issues/6.xml',
Chris@1295 690 {},
Chris@1295 691 {:success_code => :ok})
Chris@1295 692
Chris@1295 693 should "delete the issue" do
Chris@1295 694 assert_difference('Issue.count', -1) do
Chris@1295 695 delete '/issues/6.xml', {}, credentials('jsmith')
Chris@1295 696
Chris@1295 697 assert_response :ok
Chris@1295 698 assert_equal '', response.body
Chris@1295 699 end
Chris@1295 700
Chris@1295 701 assert_nil Issue.find_by_id(6)
Chris@1295 702 end
Chris@1295 703 end
Chris@1295 704
Chris@1295 705 context "DELETE /issues/1.json" do
Chris@1295 706 should_allow_api_authentication(:delete,
Chris@1295 707 '/issues/6.json',
Chris@1295 708 {},
Chris@1295 709 {:success_code => :ok})
Chris@1295 710
Chris@1295 711 should "delete the issue" do
Chris@1295 712 assert_difference('Issue.count', -1) do
Chris@1295 713 delete '/issues/6.json', {}, credentials('jsmith')
Chris@1295 714
Chris@1295 715 assert_response :ok
Chris@1295 716 assert_equal '', response.body
Chris@1295 717 end
Chris@1295 718
Chris@1295 719 assert_nil Issue.find_by_id(6)
Chris@1295 720 end
Chris@1295 721 end
Chris@1295 722
Chris@1295 723 def test_create_issue_with_uploaded_file
Chris@1295 724 set_tmp_attachments_directory
Chris@1295 725 # upload the file
Chris@1295 726 assert_difference 'Attachment.count' do
Chris@1295 727 post '/uploads.xml', 'test_create_with_upload',
Chris@1295 728 {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
Chris@1295 729 assert_response :created
Chris@1295 730 end
Chris@1295 731 xml = Hash.from_xml(response.body)
Chris@1295 732 token = xml['upload']['token']
Chris@1295 733 attachment = Attachment.first(:order => 'id DESC')
Chris@1295 734
Chris@1295 735 # create the issue with the upload's token
Chris@1295 736 assert_difference 'Issue.count' do
Chris@1295 737 post '/issues.xml',
Chris@1295 738 {:issue => {:project_id => 1, :subject => 'Uploaded file',
Chris@1295 739 :uploads => [{:token => token, :filename => 'test.txt',
Chris@1295 740 :content_type => 'text/plain'}]}},
Chris@1295 741 credentials('jsmith')
Chris@1295 742 assert_response :created
Chris@1295 743 end
Chris@1295 744 issue = Issue.first(:order => 'id DESC')
Chris@1295 745 assert_equal 1, issue.attachments.count
Chris@1295 746 assert_equal attachment, issue.attachments.first
Chris@1295 747
Chris@1295 748 attachment.reload
Chris@1295 749 assert_equal 'test.txt', attachment.filename
Chris@1295 750 assert_equal 'text/plain', attachment.content_type
Chris@1295 751 assert_equal 'test_create_with_upload'.size, attachment.filesize
Chris@1295 752 assert_equal 2, attachment.author_id
Chris@1295 753
Chris@1295 754 # get the issue with its attachments
Chris@1295 755 get "/issues/#{issue.id}.xml", :include => 'attachments'
Chris@1295 756 assert_response :success
Chris@1295 757 xml = Hash.from_xml(response.body)
Chris@1295 758 attachments = xml['issue']['attachments']
Chris@1295 759 assert_kind_of Array, attachments
Chris@1295 760 assert_equal 1, attachments.size
Chris@1295 761 url = attachments.first['content_url']
Chris@1295 762 assert_not_nil url
Chris@1295 763
Chris@1295 764 # download the attachment
Chris@1295 765 get url
Chris@1295 766 assert_response :success
Chris@1295 767 end
Chris@1295 768
Chris@1295 769 def test_update_issue_with_uploaded_file
Chris@1295 770 set_tmp_attachments_directory
Chris@1295 771 # upload the file
Chris@1295 772 assert_difference 'Attachment.count' do
Chris@1295 773 post '/uploads.xml', 'test_upload_with_upload',
Chris@1295 774 {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
Chris@1295 775 assert_response :created
Chris@1295 776 end
Chris@1295 777 xml = Hash.from_xml(response.body)
Chris@1295 778 token = xml['upload']['token']
Chris@1295 779 attachment = Attachment.first(:order => 'id DESC')
Chris@1295 780
Chris@1295 781 # update the issue with the upload's token
Chris@1295 782 assert_difference 'Journal.count' do
Chris@1295 783 put '/issues/1.xml',
Chris@1295 784 {:issue => {:notes => 'Attachment added',
Chris@1295 785 :uploads => [{:token => token, :filename => 'test.txt',
Chris@1295 786 :content_type => 'text/plain'}]}},
Chris@1295 787 credentials('jsmith')
Chris@1295 788 assert_response :ok
Chris@1295 789 assert_equal '', @response.body
Chris@1295 790 end
Chris@1295 791
Chris@1295 792 issue = Issue.find(1)
Chris@1295 793 assert_include attachment, issue.attachments
Chris@1295 794 end
Chris@1295 795 end