GitLab steht Mittwoch, den 08. Juli, zwischen 09:00 und 13:00 Uhr aufgrund von Wartungsarbeiten nicht zur Verfügung.

issues_test.rb 33.5 KB
Newer Older
1
# Redmine - project management software
jplang's avatar
jplang committed
2
# Copyright (C) 2006-2017  Jean-Philippe Lang
3 4 5 6 7
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
8
#
9 10 11 12
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
13
#
14 15 16 17
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

18
require File.expand_path('../../../test_helper', __FILE__)
19

20
class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
21 22 23 24 25 26 27
  fixtures :projects,
    :users,
    :roles,
    :members,
    :member_roles,
    :issues,
    :issue_statuses,
28
    :issue_relations,
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    :versions,
    :trackers,
    :projects_trackers,
    :issue_categories,
    :enabled_modules,
    :enumerations,
    :attachments,
    :workflows,
    :custom_fields,
    :custom_values,
    :custom_fields_projects,
    :custom_fields_trackers,
    :time_entries,
    :journals,
    :journal_details,
44 45
    :queries,
    :attachments
46

jplang's avatar
jplang committed
47 48
  test "GET /issues.xml should contain metadata" do
    get '/issues.xml'
49
    assert_select 'issues[type=array][total_count][limit="25"][offset="0"]'
jplang's avatar
jplang committed
50
  end
51

jplang's avatar
jplang committed
52 53 54 55
  test "GET /issues.xml with nometa param should not contain metadata" do
    get '/issues.xml?nometa=1'
    assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
  end
56

jplang's avatar
jplang committed
57
  test "GET /issues.xml with nometa header should not contain metadata" do
58
    get '/issues.xml', :headers => {'X-Redmine-Nometa' => '1'}
jplang's avatar
jplang committed
59 60
    assert_select 'issues[type=array]:not([total_count]):not([limit]):not([offset])'
  end
61

jplang's avatar
jplang committed
62 63
  test "GET /issues.xml with offset and limit" do
    get '/issues.xml?offset=2&limit=3'
64
    assert_select 'issues[type=array][total_count][limit="3"][offset="2"]'
jplang's avatar
jplang committed
65 66
    assert_select 'issues issue', 3
  end
67

jplang's avatar
jplang committed
68 69
  test "GET /issues.xml with relations" do
    get '/issues.xml?include=relations'
70

jplang's avatar
jplang committed
71 72
    assert_response :success
    assert_equal 'application/xml', @response.content_type
73

jplang's avatar
jplang committed
74
    assert_select 'issue id', :text => '3' do
jplang's avatar
jplang committed
75 76
      assert_select '~ relations relation', 1
      assert_select '~ relations relation[id="2"][issue_id="2"][issue_to_id="3"][relation_type=relates]'
77
    end
78

jplang's avatar
jplang committed
79
    assert_select 'issue id', :text => '1' do
jplang's avatar
jplang committed
80 81 82 83
      assert_select '~ relations'
      assert_select '~ relations relation', 0
    end
  end
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  test "GET /issues.xml with attachments" do
    get '/issues.xml?include=attachments'

    assert_response :success
    assert_equal 'application/xml', @response.content_type

    assert_select 'issue id', :text => '3' do
      assert_select '~ attachments attachment', 4
    end

    assert_select 'issue id', :text => '1' do
      assert_select '~ attachments'
      assert_select '~ attachments attachment', 0
    end
  end

jplang's avatar
jplang committed
101
  test "GET /issues.xml with invalid query params" do
102
    get '/issues.xml', :params => {:f => ['start_date'], :op => {:start_date => '='}}
103

jplang's avatar
jplang committed
104 105
    assert_response :unprocessable_entity
    assert_equal 'application/xml', @response.content_type
106
    assert_select 'errors error', :text => "Start date cannot be blank"
jplang's avatar
jplang committed
107
  end
108

jplang's avatar
jplang committed
109 110
  test "GET /issues.xml with custom field filter" do
    get '/issues.xml',
111
      :params => {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='}, :v => {:cf_1 => ['MySQL']}}
112

jplang's avatar
jplang committed
113 114 115 116
    expected_ids = Issue.visible.
        joins(:custom_values).
        where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
    assert expected_ids.any?
117

jplang's avatar
jplang committed
118 119
    assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
       ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
120
    end
jplang's avatar
jplang committed
121
  end
122

jplang's avatar
jplang committed
123
  test "GET /issues.xml with custom field filter (shorthand method)" do
124
    get '/issues.xml', :params => {:cf_1 => 'MySQL'}
125

jplang's avatar
jplang committed
126 127 128 129
    expected_ids = Issue.visible.
        joins(:custom_values).
        where(:custom_values => {:custom_field_id => 1, :value => 'MySQL'}).map(&:id)
    assert expected_ids.any?
130

jplang's avatar
jplang committed
131 132
    assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
      ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
133
    end
134
  end
135

136 137 138 139 140
  def test_index_should_include_issue_attributes
    get '/issues.xml'
    assert_select 'issues>issue>is_private', :text => 'false'
  end

141 142 143 144 145
  def test_index_should_allow_timestamp_filtering
    Issue.delete_all
    Issue.generate!(:subject => '1').update_column(:updated_on, Time.parse("2014-01-02T10:25:00Z"))
    Issue.generate!(:subject => '2').update_column(:updated_on, Time.parse("2014-01-02T12:13:00Z"))

146 147 148 149
    get '/issues.xml', :params => {
        :set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '<='},
        :v => {:updated_on => ['2014-01-02T12:00:00Z']}
      }
150 151 152
    assert_select 'issues>issue', :count => 1
    assert_select 'issues>issue>subject', :text => '1'

153 154 155 156
    get '/issues.xml', :params => {
        :set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
        :v => {:updated_on => ['2014-01-02T12:00:00Z']}
      }
157 158 159
    assert_select 'issues>issue', :count => 1
    assert_select 'issues>issue>subject', :text => '2'

160 161 162 163
    get '/issues.xml', :params => {
        :set_filter => 1, :f => ['updated_on'], :op => {:updated_on => '>='},
        :v => {:updated_on => ['2014-01-02T08:00:00Z']}
      }
164 165 166
    assert_select 'issues>issue', :count => 2
  end

jplang's avatar
jplang committed
167 168
  test "GET /issues.xml with filter" do
    get '/issues.xml?status_id=5'
169

jplang's avatar
jplang committed
170 171
    expected_ids = Issue.visible.where(:status_id => 5).map(&:id)
    assert expected_ids.any?
172

jplang's avatar
jplang committed
173 174
    assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
       ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
edavis10's avatar
edavis10 committed
175
    end
176
  end
edavis10's avatar
edavis10 committed
177

jplang's avatar
jplang committed
178 179
  test "GET /issues.json with filter" do
    get '/issues.json?status_id=5'
180

jplang's avatar
jplang committed
181 182 183 184
    json = ActiveSupport::JSON.decode(response.body)
    status_ids_used = json['issues'].collect {|j| j['status']['id'] }
    assert_equal 3, status_ids_used.length
    assert status_ids_used.all? {|id| id == 5 }
185 186
  end

jplang's avatar
jplang committed
187
  test "GET /issues/:id.xml with journals" do
188 189
    Journal.find(2).update_attribute(:private_notes, true)

190
    get '/issues/1.xml?include=journals', :headers => credentials('jsmith')
edavis10's avatar
edavis10 committed
191

jplang's avatar
jplang committed
192 193
    assert_select 'issue journals[type=array]' do
      assert_select 'journal[id="1"]' do
194
        assert_select 'private_notes', :text => 'false'
jplang's avatar
jplang committed
195 196 197 198
        assert_select 'details[type=array]' do
          assert_select 'detail[name=status_id]' do
            assert_select 'old_value', :text => '1'
            assert_select 'new_value', :text => '2'
199
          end
200 201
        end
      end
202 203 204 205
      assert_select 'journal[id="2"]' do
        assert_select 'private_notes', :text => 'true'
        assert_select 'details[type=array]'
      end
206
    end
jplang's avatar
jplang committed
207
  end
208

209 210 211 212 213 214 215 216 217
  test "GET /issues/:id.xml with journals should format timestamps in ISO 8601" do
    get '/issues/1.xml?include=journals'

    iso_date = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/
    assert_select 'issue>created_on', :text => iso_date
    assert_select 'issue>updated_on', :text => iso_date
    assert_select 'issue journal>created_on', :text => iso_date
  end

jplang's avatar
jplang committed
218 219
  test "GET /issues/:id.xml with custom fields" do
    get '/issues/3.xml'
220

jplang's avatar
jplang committed
221 222 223
    assert_select 'issue custom_fields[type=array]' do
      assert_select 'custom_field[id="1"]' do
        assert_select 'value', :text => 'MySQL'
224 225
      end
    end
jplang's avatar
jplang committed
226 227 228 229
    assert_nothing_raised do
      Hash.from_xml(response.body).to_xml
    end
  end
230

jplang's avatar
jplang committed
231 232 233 234 235 236
  test "GET /issues/:id.xml with multi custom fields" do
    field = CustomField.find(1)
    field.update_attribute :multiple, true
    issue = Issue.find(3)
    issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
    issue.save!
237

jplang's avatar
jplang committed
238 239
    get '/issues/3.xml'
    assert_response :success
240

jplang's avatar
jplang committed
241 242 243
    assert_select 'issue custom_fields[type=array]' do
      assert_select 'custom_field[id="1"]' do
        assert_select 'value[type=array] value', 2
244 245
      end
    end
jplang's avatar
jplang committed
246 247 248 249 250 251 252
    xml = Hash.from_xml(response.body)
    custom_fields = xml['issue']['custom_fields']
    assert_kind_of Array, custom_fields
    field = custom_fields.detect {|f| f['id'] == '1'}
    assert_kind_of Hash, field
    assert_equal ['MySQL', 'Oracle'], field['value'].sort
  end
253

jplang's avatar
jplang committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  test "GET /issues/:id.json with multi custom fields" do
    field = CustomField.find(1)
    field.update_attribute :multiple, true
    issue = Issue.find(3)
    issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
    issue.save!

    get '/issues/3.json'
    assert_response :success

    json = ActiveSupport::JSON.decode(response.body)
    custom_fields = json['issue']['custom_fields']
    assert_kind_of Array, custom_fields
    field = custom_fields.detect {|f| f['id'] == 1}
    assert_kind_of Hash, field
    assert_equal ['MySQL', 'Oracle'], field['value'].sort
  end
271

jplang's avatar
jplang committed
272 273 274 275 276 277
  test "GET /issues/:id.xml with empty value for multi custom field" do
    field = CustomField.find(1)
    field.update_attribute :multiple, true
    issue = Issue.find(3)
    issue.custom_field_values = {1 => ['']}
    issue.save!
278

jplang's avatar
jplang committed
279
    get '/issues/3.xml'
280

jplang's avatar
jplang committed
281 282 283
    assert_select 'issue custom_fields[type=array]' do
      assert_select 'custom_field[id="1"]' do
        assert_select 'value[type=array]:empty'
284 285
      end
    end
jplang's avatar
jplang committed
286 287 288 289 290 291 292
    xml = Hash.from_xml(response.body)
    custom_fields = xml['issue']['custom_fields']
    assert_kind_of Array, custom_fields
    field = custom_fields.detect {|f| f['id'] == '1'}
    assert_kind_of Hash, field
    assert_equal [], field['value']
  end
293

jplang's avatar
jplang committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
  test "GET /issues/:id.json with empty value for multi custom field" do
    field = CustomField.find(1)
    field.update_attribute :multiple, true
    issue = Issue.find(3)
    issue.custom_field_values = {1 => ['']}
    issue.save!

    get '/issues/3.json'
    assert_response :success
    json = ActiveSupport::JSON.decode(response.body)
    custom_fields = json['issue']['custom_fields']
    assert_kind_of Array, custom_fields
    field = custom_fields.detect {|f| f['id'] == 1}
    assert_kind_of Hash, field
    assert_equal [], field['value'].sort
  end

  test "GET /issues/:id.xml with attachments" do
    get '/issues/3.xml?include=attachments'
313

jplang's avatar
jplang committed
314
    assert_select 'issue attachments[type=array]' do
315 316 317 318
      assert_select 'attachment', 4
      assert_select 'attachment id', :text => '1' do
        assert_select '~ filename', :text => 'error281.txt'
        assert_select '~ content_url', :text => 'http://www.example.com/attachments/download/1/error281.txt'
319
      end
jplang's avatar
jplang committed
320 321
    end
  end
322

jplang's avatar
jplang committed
323 324 325
  test "GET /issues/:id.xml with subtasks" do
    issue = Issue.generate_with_descendants!(:project_id => 1)
    get "/issues/#{issue.id}.xml?include=children"
326

jplang's avatar
jplang committed
327 328 329
    assert_select 'issue id', :text => issue.id.to_s do
      assert_select '~ children[type=array] > issue', 2
      assert_select '~ children[type=array] > issue > children', 1
330 331
    end
  end
332

jplang's avatar
jplang committed
333 334 335 336 337 338 339 340 341
  test "GET /issues/:id.json with subtasks" do
    issue = Issue.generate_with_descendants!(:project_id => 1)
    get "/issues/#{issue.id}.json?include=children"

    json = ActiveSupport::JSON.decode(response.body)
    assert_equal 2, json['issue']['children'].size
    assert_equal 1, json['issue']['children'].select {|child| child.key?('children')}.size
  end

jplang's avatar
jplang committed
342 343 344 345 346 347 348 349 350
  test "GET /issues/:id.json with no spent time should return floats" do
    issue = Issue.generate!
    get "/issues/#{issue.id}.json"

    json = ActiveSupport::JSON.decode(response.body)
    assert_kind_of Float, json['issue']['spent_hours']
    assert_kind_of Float, json['issue']['total_spent_hours']
  end

351 352 353 354 355
  def test_show_should_include_issue_attributes
    get '/issues/1.xml'
    assert_select 'issue>is_private', :text => 'false'
  end

356 357 358
  test "GET /issues/:id.xml?include=watchers should include watchers" do
    Watcher.create!(:user_id => 3, :watchable => Issue.find(1))

359
    get '/issues/1.xml?include=watchers', :headers => credentials('jsmith')
360 361 362 363 364 365

    assert_response :ok
    assert_equal 'application/xml', response.content_type
    assert_select 'issue' do
      assert_select 'watchers', Issue.find(1).watchers.count
      assert_select 'watchers' do
jplang's avatar
jplang committed
366
        assert_select 'user[id="3"]'
367 368 369 370
      end
    end
  end

371 372 373 374 375 376
  test "GET /issues/:id.xml should not disclose associated changesets from projects the user has no access to" do
    project = Project.generate!(:is_public => false)
    repository = Repository::Subversion.create!(:project => project, :url => "svn://localhost")
    Issue.find(1).changesets << Changeset.generate!(:repository => repository)
    assert Issue.find(1).changesets.any?

377
    get '/issues/1.xml?include=changesets', :headers => credentials('jsmith')
378 379 380 381 382 383 384

    # the user jsmith has no permission to view the associated changeset
    assert_select 'issue changesets[type=array]' do
      assert_select 'changeset', 0
    end
  end

385 386
  test "GET /issues/:id.xml should contains total_estimated_hours and total_spent_hours" do
    parent = Issue.find(3)
jplang's avatar
jplang committed
387
    parent.update_columns :estimated_hours => 2.0
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    child = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 3.0)
    TimeEntry.create!(:project => child.project, :issue => child, :user => child.author, :spent_on => child.author.today,
                      :hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first.id)
    get '/issues/3.xml'

    assert_equal 'application/xml', response.content_type
    assert_select 'issue' do
      assert_select 'estimated_hours',       parent.estimated_hours.to_s
      assert_select 'total_estimated_hours', (parent.estimated_hours.to_f + 3.0).to_s
      assert_select 'spent_hours',           parent.spent_hours.to_s
      assert_select 'total_spent_hours',     (parent.spent_hours.to_f + 2.5).to_s
    end
  end

  test "GET /issues/:id.xml should contains total_estimated_hours, and should not contains spent_hours and total_spent_hours when permission does not exists" do
    parent = Issue.find(3)
jplang's avatar
jplang committed
404
    parent.update_columns :estimated_hours => 2.0
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    child = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 3.0)
    # remove permission!
    Role.anonymous.remove_permission! :view_time_entries
    #Role.all.each { |role| role.remove_permission! :view_time_entries }
    get '/issues/3.xml'

    assert_equal 'application/xml', response.content_type
    assert_select 'issue' do
      assert_select 'estimated_hours',       parent.estimated_hours.to_s
      assert_select 'total_estimated_hours', (parent.estimated_hours.to_f + 3.0).to_s
      assert_select 'spent_hours',           false
      assert_select 'total_spent_hours',     false
    end
  end

420 421 422 423 424 425 426 427
  test "GET /issues/:id.xml should contains visible spent_hours only" do
    user = User.find_by_login('jsmith')
    Role.find(1).update(:time_entries_visibility => 'own')
    parent = Issue.find(3)
    child = Issue.generate!(:parent_issue_id => parent.id)
    TimeEntry.generate!(:user => user, :hours => 5.5, :issue_id => parent.id)
    TimeEntry.generate!(:user => user, :hours => 2, :issue_id => child.id)
    TimeEntry.generate!(:user => User.find(1), :hours => 100, :issue_id => child.id)
428
    get '/issues/3.xml', :headers => credentials(user.login)
429 430 431 432 433 434 435 436

    assert_equal 'application/xml', response.content_type
    assert_select 'issue' do
      assert_select 'spent_hours',           '5.5'
      assert_select 'total_spent_hours',     '7.5'
    end
  end

437 438
  test "GET /issues/:id.json should contains total_estimated_hours and total_spent_hours" do
    parent = Issue.find(3)
jplang's avatar
jplang committed
439
    parent.update_columns :estimated_hours => 2.0
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    child = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 3.0)
    TimeEntry.create!(:project => child.project, :issue => child, :user => child.author, :spent_on => child.author.today,
                      :hours => '2.5', :comments => '', :activity_id => TimeEntryActivity.first.id)
    get '/issues/3.json'

    assert_equal 'application/json', response.content_type
    json = ActiveSupport::JSON.decode(response.body)
    assert_equal parent.estimated_hours, json['issue']['estimated_hours']
    assert_equal (parent.estimated_hours.to_f + 3.0), json['issue']['total_estimated_hours']
    assert_equal parent.spent_hours, json['issue']['spent_hours']
    assert_equal (parent.spent_hours.to_f + 2.5), json['issue']['total_spent_hours']
  end

  test "GET /issues/:id.json should contains total_estimated_hours, and should not contains spent_hours and total_spent_hours when permission does not exists" do
    parent = Issue.find(3)
jplang's avatar
jplang committed
455
    parent.update_columns :estimated_hours => 2.0
456 457 458 459 460 461 462 463 464 465
    child = Issue.generate!(:parent_issue_id => parent.id, :estimated_hours => 3.0)
    # remove permission!
    Role.anonymous.remove_permission! :view_time_entries
    #Role.all.each { |role| role.remove_permission! :view_time_entries }
    get '/issues/3.json'

    assert_equal 'application/json', response.content_type
    json = ActiveSupport::JSON.decode(response.body)
    assert_equal parent.estimated_hours, json['issue']['estimated_hours']
    assert_equal (parent.estimated_hours.to_f + 3.0), json['issue']['total_estimated_hours']
466 467
    assert_nil json['issue']['spent_hours']
    assert_nil json['issue']['total_spent_hours']
468 469
  end

470 471 472 473 474 475 476 477
  test "GET /issues/:id.json should contains visible spent_hours only" do
    user = User.find_by_login('jsmith')
    Role.find(1).update(:time_entries_visibility => 'own')
    parent = Issue.find(3)
    child = Issue.generate!(:parent_issue_id => parent.id)
    TimeEntry.generate!(:user => user, :hours => 5.5, :issue_id => parent.id)
    TimeEntry.generate!(:user => user, :hours => 2, :issue_id => child.id)
    TimeEntry.generate!(:user => User.find(1), :hours => 100, :issue_id => child.id)
478
    get '/issues/3.json', :headers => credentials(user.login)
479 480 481 482 483 484 485

    assert_equal 'application/json', response.content_type
    json = ActiveSupport::JSON.decode(response.body)
    assert_equal 5.5, json['issue']['spent_hours']
    assert_equal 7.5, json['issue']['total_spent_hours']
  end

jplang's avatar
jplang committed
486
  test "POST /issues.xml should create an issue with the attributes" do
487 488 489 490 491 492 493

payload = <<-XML
<?xml version="1.0" encoding="UTF-8" ?>
<issue>
  <project_id>1</project_id>
  <tracker_id>2</tracker_id>
  <status_id>3</status_id>
494
  <category_id>2</category_id>
marutosijp's avatar
marutosijp committed
495
  <subject>API test</subject>
496 497 498
</issue>
XML

jplang's avatar
jplang committed
499
    assert_difference('Issue.count') do
500 501 502
      post '/issues.xml',
        :params => payload,
        :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
jplang's avatar
jplang committed
503 504 505 506 507
    end
    issue = Issue.order('id DESC').first
    assert_equal 1, issue.project_id
    assert_equal 2, issue.tracker_id
    assert_equal 3, issue.status_id
508
    assert_equal 2, issue.category_id
jplang's avatar
jplang committed
509 510 511 512 513
    assert_equal 'API test', issue.subject

    assert_response :created
    assert_equal 'application/xml', @response.content_type
    assert_select 'issue > id', :text => issue.id.to_s
514
  end
515

516 517 518
  test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
    assert_difference('Issue.count') do
      post '/issues.xml',
519 520 521 522 523 524 525
        :params => {
          :issue => {
            :project_id => 1, :subject => 'Watchers',
            :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]
          }
        },
        :headers => credentials('jsmith')
526 527 528 529 530 531 532
      assert_response :created
    end
    issue = Issue.order('id desc').first
    assert_equal 2, issue.watchers.size
    assert_equal [1, 3], issue.watcher_user_ids.sort
  end

jplang's avatar
jplang committed
533 534
  test "POST /issues.xml with failure should return errors" do
    assert_no_difference('Issue.count') do
535 536 537
      post '/issues.xml',
        :params => {:issue => {:project_id => 1}},
        :headers => credentials('jsmith')
538
    end
539

540
    assert_select 'errors error', :text => "Subject cannot be blank"
541
  end
542

jplang's avatar
jplang committed
543
  test "POST /issues.json should create an issue with the attributes" do
544 545 546 547 548 549 550

payload = <<-JSON
{
  "issue": {
    "project_id": "1",
    "tracker_id": "2",
    "status_id": "3",
551
    "category_id": "2",
552 553 554 555 556
    "subject": "API test"
  }
}
JSON

jplang's avatar
jplang committed
557
    assert_difference('Issue.count') do
558 559 560
      post '/issues.json',
        :params => payload,
        :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
561
    end
562

jplang's avatar
jplang committed
563 564 565 566
    issue = Issue.order('id DESC').first
    assert_equal 1, issue.project_id
    assert_equal 2, issue.tracker_id
    assert_equal 3, issue.status_id
567
    assert_equal 2, issue.category_id
jplang's avatar
jplang committed
568
    assert_equal 'API test', issue.subject
569
  end
570

571 572 573
  test "POST /issues.json should accept project identifier as project_id" do
    assert_difference('Issue.count') do
      post '/issues.json',
574 575
        :params => {:issue => {:project_id => 'subproject1', :tracker_id => 2, :subject => 'Foo'}},
        :headers => credentials('jsmith')
576 577 578 579 580

      assert_response :created
    end
  end

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
  test "POST /issues.json without tracker_id should accept custom fields" do
    field = IssueCustomField.generate!(
      :field_format => 'list',
      :multiple => true,
      :possible_values => ["V1", "V2", "V3"],
      :default_value => "V2",
      :is_for_all => true,
      :trackers => Tracker.all.to_a
    )

payload = <<-JSON
{
  "issue": {
    "project_id": "1",
    "subject": "Multivalued custom field",
    "custom_field_values":{"#{field.id}":["V1","V3"]}
  }
}
JSON

    assert_difference('Issue.count') do
602 603 604
      post '/issues.json',
        :params => payload,
        :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
605 606 607 608
    end

    assert_response :created
    issue = Issue.order('id DESC').first
jplang's avatar
jplang committed
609
    assert_equal ["V1", "V3"], issue.custom_field_value(field).sort
610 611
  end

612 613 614 615 616
  test "POST /issues.json with omitted custom field should set default value" do
    field = IssueCustomField.generate!(:default_value => "Default")

    issue = new_record(Issue) do
      post '/issues.json',
617 618
        :params => {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {}}},
        :headers => credentials('jsmith')
619 620 621 622 623 624 625 626 627
    end
    assert_equal "Default", issue.custom_field_value(field)
  end

  test "POST /issues.json with custom field set to blank should not set default value" do
    field = IssueCustomField.generate!(:default_value => "Default")

    issue = new_record(Issue) do
      post '/issues.json',
628 629
        :params => {:issue => {:project_id => 1, :subject => 'API', :custom_field_values => {field.id.to_s => ""}}},
        :headers => credentials('jsmith')
630 631 632 633
    end
    assert_equal "", issue.custom_field_value(field)
  end

jplang's avatar
jplang committed
634 635
  test "POST /issues.json with failure should return errors" do
    assert_no_difference('Issue.count') do
636 637 638
      post '/issues.json',
        :params => {:issue => {:project_id => 1}},
        :headers => credentials('jsmith')
639
    end
640

jplang's avatar
jplang committed
641
    json = ActiveSupport::JSON.decode(response.body)
642
    assert json['errors'].include?("Subject cannot be blank")
643
  end
644

645
  test "POST /issues.json with invalid project_id should respond with 422" do
646 647 648
    post '/issues.json',
      :params => {:issue => {:project_id => 999, :subject => "API"}},
      :headers => credentials('jsmith')
649 650 651
    assert_response 422
  end

jplang's avatar
jplang committed
652 653 654
  test "PUT /issues/:id.xml" do
    assert_difference('Journal.count') do
      put '/issues/6.xml',
655 656
        :params => {:issue => {:subject => 'API update', :notes => 'A new note'}},
        :headers => credentials('jsmith')
657 658
    end

jplang's avatar
jplang committed
659 660 661 662
    issue = Issue.find(6)
    assert_equal "API update", issue.subject
    journal = Journal.last
    assert_equal "A new note", journal.notes
663 664
  end

jplang's avatar
jplang committed
665 666
  test "PUT /issues/:id.xml with custom fields" do
    put '/issues/3.xml',
667
      :params => {:issue => {:custom_fields => [
jplang's avatar
jplang committed
668 669
        {'id' => '1', 'value' => 'PostgreSQL' },
        {'id' => '2', 'value' => '150'}
670 671 672
        ]}
      },
      :headers => credentials('jsmith')
jplang's avatar
jplang committed
673 674 675 676

    issue = Issue.find(3)
    assert_equal '150', issue.custom_value_for(2).value
    assert_equal 'PostgreSQL', issue.custom_value_for(1).value
677 678
  end

jplang's avatar
jplang committed
679 680 681
  test "PUT /issues/:id.xml with multi custom fields" do
    field = CustomField.find(1)
    field.update_attribute :multiple, true
682

jplang's avatar
jplang committed
683
    put '/issues/3.xml',
684
      :params => {:issue => {:custom_fields => [
jplang's avatar
jplang committed
685 686
        {'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
        {'id' => '2', 'value' => '150'}
687 688 689
        ]}
      },
      :headers => credentials('jsmith')
690

jplang's avatar
jplang committed
691 692 693
    issue = Issue.find(3)
    assert_equal '150', issue.custom_value_for(2).value
    assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
edavis10's avatar
edavis10 committed
694 695
  end

jplang's avatar
jplang committed
696 697
  test "PUT /issues/:id.xml with project change" do
    put '/issues/3.xml',
698 699
      :params => {:issue => {:project_id => 2, :subject => 'Project changed'}},
      :headers => credentials('jsmith')
700

jplang's avatar
jplang committed
701 702 703 704
    issue = Issue.find(3)
    assert_equal 2, issue.project_id
    assert_equal 'Project changed', issue.subject
  end
705

jplang's avatar
jplang committed
706 707 708
  test "PUT /issues/:id.xml with notes only" do
    assert_difference('Journal.count') do
      put '/issues/6.xml',
709 710
        :params => {:issue => {:notes => 'Notes only'}},
        :headers => credentials('jsmith')
jplang's avatar
jplang committed
711 712 713 714 715 716
    end

    journal = Journal.last
    assert_equal "Notes only", journal.notes
  end

717 718 719 720 721 722 723
  test "PUT /issues/:id.json with omitted custom field should not change blank value to default value" do
    field = IssueCustomField.generate!(:default_value => "Default")
    issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
    assert_equal "", issue.reload.custom_field_value(field)

    assert_difference('Journal.count') do
      put "/issues/#{issue.id}.json",
724 725
        :params => {:issue => {:custom_field_values => {}, :notes => 'API'}},
        :headers => credentials('jsmith')
726 727 728 729 730 731 732 733 734 735 736 737
    end

    assert_equal "", issue.reload.custom_field_value(field)
  end

  test "PUT /issues/:id.json with custom field set to blank should not change blank value to default value" do
    field = IssueCustomField.generate!(:default_value => "Default")
    issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id.to_s => ""})
    assert_equal "", issue.reload.custom_field_value(field)

    assert_difference('Journal.count') do
      put "/issues/#{issue.id}.json",
738 739
        :params => {:issue => {:custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
        :headers => credentials('jsmith')
740 741 742 743 744
    end

    assert_equal "", issue.reload.custom_field_value(field)
  end

745
  test "PUT /issues/:id.json with tracker change and omitted custom field specific to that tracker should set default value" do
746 747 748 749 750
    field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
    issue = Issue.generate!(:project_id => 1, :tracker_id => 1)

    assert_difference('Journal.count') do
      put "/issues/#{issue.id}.json",
751 752
        :params => {:issue => {:tracker_id => 2, :custom_field_values => {}, :notes => 'API'}},
        :headers => credentials('jsmith')
753 754 755
    end

    assert_equal 2, issue.reload.tracker_id
756
    assert_equal "Default", issue.reload.custom_field_value(field)
757 758 759 760 761 762 763 764
  end

  test "PUT /issues/:id.json with tracker change and custom field specific to that tracker set to blank should not set default value" do
    field = IssueCustomField.generate!(:default_value => "Default", :tracker_ids => [2])
    issue = Issue.generate!(:project_id => 1, :tracker_id => 1)

    assert_difference('Journal.count') do
      put "/issues/#{issue.id}.json",
765 766
        :params => {:issue => {:tracker_id => 2, :custom_field_values => {field.id.to_s => ""}, :notes => 'API'}},
        :headers => credentials('jsmith')
767 768 769 770 771 772
    end

    assert_equal 2, issue.reload.tracker_id
    assert_equal "", issue.reload.custom_field_value(field)
  end

jplang's avatar
jplang committed
773
  test "PUT /issues/:id.xml with failed update" do
774 775 776
    put '/issues/6.xml',
      :params => {:issue => {:subject => ''}},
      :headers => credentials('jsmith')
777

jplang's avatar
jplang committed
778
    assert_response :unprocessable_entity
779
    assert_select 'errors error', :text => "Subject cannot be blank"
780
  end
781

782 783
  test "PUT /issues/:id.xml with invalid assignee should return error" do
    user = User.generate!
784 785 786
    put '/issues/6.xml',
      :params => {:issue => {:assigned_to_id => user.id}},
      :headers => credentials('jsmith')
787 788 789 790 791

    assert_response :unprocessable_entity
    assert_select 'errors error', :text => "Assignee is invalid"
  end

jplang's avatar
jplang committed
792 793 794
  test "PUT /issues/:id.json" do
    assert_difference('Journal.count') do
      put '/issues/6.json',
795 796
        :params => {:issue => {:subject => 'API update', :notes => 'A new note'}},
        :headers => credentials('jsmith')
797

jplang's avatar
jplang committed
798 799
      assert_response :ok
      assert_equal '', response.body
800
    end
jplang's avatar
jplang committed
801 802 803 804 805

    issue = Issue.find(6)
    assert_equal "API update", issue.subject
    journal = Journal.last
    assert_equal "A new note", journal.notes
806 807
  end

jplang's avatar
jplang committed
808
  test "PUT /issues/:id.json with failed update" do
809 810 811
    put '/issues/6.json',
      :params => {:issue => {:subject => ''}},
      :headers => credentials('jsmith')
edavis10's avatar
edavis10 committed
812

jplang's avatar
jplang committed
813 814
    assert_response :unprocessable_entity
    json = ActiveSupport::JSON.decode(response.body)
815
    assert json['errors'].include?("Subject cannot be blank")
jplang's avatar
jplang committed
816
  end
817

jplang's avatar
jplang committed
818 819
  test "DELETE /issues/:id.xml" do
    assert_difference('Issue.count', -1) do
820
      delete '/issues/6.xml', :headers => credentials('jsmith')
821

jplang's avatar
jplang committed
822 823
      assert_response :ok
      assert_equal '', response.body
edavis10's avatar
edavis10 committed
824
    end
jplang's avatar
jplang committed
825
    assert_nil Issue.find_by_id(6)
826
  end
827

jplang's avatar
jplang committed
828 829
  test "DELETE /issues/:id.json" do
    assert_difference('Issue.count', -1) do
830
      delete '/issues/6.json', :headers => credentials('jsmith')
831

jplang's avatar
jplang committed
832 833
      assert_response :ok
      assert_equal '', response.body
834
    end
jplang's avatar
jplang committed
835
    assert_nil Issue.find_by_id(6)
836
  end
837

838 839
  test "POST /issues/:id/watchers.xml should add watcher" do
    assert_difference 'Watcher.count' do
840 841 842
      post '/issues/1/watchers.xml',
        :params => {:user_id => 3},
        :headers => credentials('jsmith')
843 844 845 846 847 848 849 850 851 852 853 854 855

      assert_response :ok
      assert_equal '', response.body
    end
    watcher = Watcher.order('id desc').first
    assert_equal Issue.find(1), watcher.watchable
    assert_equal User.find(3), watcher.user
  end

  test "DELETE /issues/:id/watchers/:user_id.xml should remove watcher" do
    Watcher.create!(:user_id => 3, :watchable => Issue.find(1))

    assert_difference 'Watcher.count', -1 do
856
      delete '/issues/1/watchers/3.xml', :headers => credentials('jsmith')
857 858 859 860 861 862 863

      assert_response :ok
      assert_equal '', response.body
    end
    assert_equal false, Issue.find(1).watched_by?(User.find(3))
  end

864
  def test_create_issue_with_uploaded_file
jplang's avatar
jplang committed
865 866
    token = xml_upload('test_create_with_upload', credentials('jsmith'))
    attachment = Attachment.find_by_token(token)
867 868 869 870

    # create the issue with the upload's token
    assert_difference 'Issue.count' do
      post '/issues.xml',
871
        :params => {:issue => {:project_id => 1, :subject => 'Uploaded file',
872 873
                       :uploads => [{:token => token, :filename => 'test.txt',
                                     :content_type => 'text/plain'}]}},
874
        :headers => credentials('jsmith')
875 876
      assert_response :created
    end
877
    issue = Issue.order('id DESC').first
878 879 880 881 882 883 884 885 886 887
    assert_equal 1, issue.attachments.count
    assert_equal attachment, issue.attachments.first

    attachment.reload
    assert_equal 'test.txt', attachment.filename
    assert_equal 'text/plain', attachment.content_type
    assert_equal 'test_create_with_upload'.size, attachment.filesize
    assert_equal 2, attachment.author_id

    # get the issue with its attachments
888
    get "/issues/#{issue.id}.xml?include=attachments"
889 890 891 892 893 894 895 896 897 898 899
    assert_response :success
    xml = Hash.from_xml(response.body)
    attachments = xml['issue']['attachments']
    assert_kind_of Array, attachments
    assert_equal 1, attachments.size
    url = attachments.first['content_url']
    assert_not_nil url

    # download the attachment
    get url
    assert_response :success
jplang's avatar
jplang committed
900
    assert_equal 'test_create_with_upload', response.body
901 902
  end

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
  def test_create_issue_with_multiple_uploaded_files_as_xml
    token1 = xml_upload('File content 1', credentials('jsmith'))
    token2 = xml_upload('File content 2', credentials('jsmith'))

    payload = <<-XML
<?xml version="1.0" encoding="UTF-8" ?>
<issue>
  <project_id>1</project_id>
  <tracker_id>1</tracker_id>
  <subject>Issue with multiple attachments</subject>
  <uploads type="array">
    <upload>
      <token>#{token1}</token>
      <filename>test1.txt</filename>
    </upload>
    <upload>
      <token>#{token2}</token>
      <filename>test1.txt</filename>
    </upload>
  </uploads>
</issue>
XML

    assert_difference 'Issue.count' do
927 928 929
      post '/issues.xml',
        :params => payload,
        :headers => {"CONTENT_TYPE" => 'application/xml'}.merge(credentials('jsmith'))
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
      assert_response :created
    end
    issue = Issue.order('id DESC').first
    assert_equal 2, issue.attachments.count
  end

  def test_create_issue_with_multiple_uploaded_files_as_json
    token1 = json_upload('File content 1', credentials('jsmith'))
    token2 = json_upload('File content 2', credentials('jsmith'))

    payload = <<-JSON
{
  "issue": {
    "project_id": "1",
    "tracker_id": "1",
    "subject": "Issue with multiple attachments",
    "uploads": [
      {"token": "#{token1}", "filename": "test1.txt"},
      {"token": "#{token2}", "filename": "test2.txt"}
    ]
  }
}
JSON

    assert_difference 'Issue.count' do
955 956 957
      post '/issues.json',
        :params => payload,
        :headers => {"CONTENT_TYPE" => 'application/json'}.merge(credentials('jsmith'))
958 959 960 961 962 963
      assert_response :created
    end
    issue = Issue.order('id DESC').first
    assert_equal 2, issue.attachments.count
  end

964
  def test_update_issue_with_uploaded_file
jplang's avatar
jplang committed
965 966
    token = xml_upload('test_upload_with_upload', credentials('jsmith'))
    attachment = Attachment.find_by_token(token)
967 968 969 970

    # update the issue with the upload's token
    assert_difference 'Journal.count' do
      put '/issues/1.xml',
971
        :params => {:issue => {:notes => 'Attachment added',
972 973
                      :uploads => [{:token => token, :filename => 'test.txt',
                                    :content_type => 'text/plain'}]}},
974
        :headers => credentials('jsmith')
975
      assert_response :ok
976
      assert_equal '', @response.body
977 978 979 980 981
    end

    issue = Issue.find(1)
    assert_include attachment, issue.attachments
  end
982
end