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