summaryrefslogtreecommitdiffstats
path: root/test/integration/api_test/issues_test.rb
blob: 176c1810574453ef56e3422b7d22f5cef8ee8a6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
# Redmine - project management software
# Copyright (C) 2006-2013  Jean-Philippe Lang
#
# 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.
#
# 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.
#
# 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.

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

class Redmine::ApiTest::IssuesTest < Redmine::ApiTest::Base
  fixtures :projects,
    :users,
    :roles,
    :members,
    :member_roles,
    :issues,
    :issue_statuses,
    :issue_relations,
    :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,
    :queries,
    :attachments

  def setup
    Setting.rest_api_enabled = '1'
  end

  context "/issues" do
    # Use a private project to make sure auth is really working and not just
    # only showing public issues.
    should_allow_api_authentication(:get, "/projects/private-child/issues.xml")

    should "contain metadata" do
      get '/issues.xml'

      assert_tag :tag => 'issues',
        :attributes => {
          :type => 'array',
          :total_count => assigns(:issue_count),
          :limit => 25,
          :offset => 0
        }
    end

    context "with offset and limit" do
      should "use the params" do
        get '/issues.xml?offset=2&limit=3'

        assert_equal 3, assigns(:limit)
        assert_equal 2, assigns(:offset)
        assert_tag :tag => 'issues', :children => {:count => 3, :only => {:tag => 'issue'}}
      end
    end

    context "with nometa param" do
      should "not contain metadata" do
        get '/issues.xml?nometa=1'

        assert_tag :tag => 'issues',
          :attributes => {
            :type => 'array',
            :total_count => nil,
            :limit => nil,
            :offset => nil
          }
      end
    end

    context "with nometa header" do
      should "not contain metadata" do
        get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}

        assert_tag :tag => 'issues',
          :attributes => {
            :type => 'array',
            :total_count => nil,
            :limit => nil,
            :offset => nil
          }
      end
    end

    context "with relations" do
      should "display relations" do
        get '/issues.xml?include=relations'

        assert_response :success
        assert_equal 'application/xml', @response.content_type
        assert_tag 'relations',
          :parent => {:tag => 'issue', :child => {:tag => 'id', :content => '3'}},
          :children => {:count => 1},
          :child => {
            :tag => 'relation',
            :attributes => {:id => '2', :issue_id => '2', :issue_to_id => '3',
                            :relation_type => 'relates'}
          }
        assert_tag 'relations',
          :parent => {:tag => 'issue', :child => {:tag => 'id', :content => '1'}},
          :children => {:count => 0}
      end
    end

    context "with invalid query params" do
      should "return errors" do
        get '/issues.xml', {:f => ['start_date'], :op => {:start_date => '='}}

        assert_response :unprocessable_entity
        assert_equal 'application/xml', @response.content_type
        assert_tag 'errors', :child => {:tag => 'error', :content => "Start date can't be blank"}
      end
    end

    context "with custom field filter" do
      should "show only issues with the custom field value" do
        get '/issues.xml',
            {:set_filter => 1, :f => ['cf_1'], :op => {:cf_1 => '='},
             :v => {:cf_1 => ['MySQL']}}
        expected_ids = Issue.visible.all(
            :include => :custom_values,
            :conditions => {:custom_values => {:custom_field_id => 1, :value => 'MySQL'}}).map(&:id)
        assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
           ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
        end
      end
    end

    context "with custom field filter (shorthand method)" do
      should "show only issues with the custom field value" do
        get '/issues.xml', { :cf_1 => 'MySQL' }

        expected_ids = Issue.visible.all(
            :include => :custom_values,
            :conditions => {:custom_values => {:custom_field_id => 1, :value => 'MySQL'}}).map(&:id)

        assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
          ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
        end
      end
    end
  end

  context "/index.json" do
    should_allow_api_authentication(:get, "/projects/private-child/issues.json")
  end

  context "/index.xml with filter" do
    should "show only issues with the status_id" do
      get '/issues.xml?status_id=5'

      expected_ids = Issue.visible.all(:conditions => {:status_id => 5}).map(&:id)

      assert_select 'issues > issue > id', :count => expected_ids.count do |ids|
         ids.each { |id| assert expected_ids.delete(id.children.first.content.to_i) }
      end
    end
  end

  context "/index.json with filter" do
    should "show only issues with the status_id" do
      get '/issues.json?status_id=5'

      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 }
    end

  end

  # Issue 6 is on a private project
  context "/issues/6.xml" do
    should_allow_api_authentication(:get, "/issues/6.xml")
  end

  context "/issues/6.json" do
    should_allow_api_authentication(:get, "/issues/6.json")
  end

  context "GET /issues/:id" do
    context "with journals" do
      context ".xml" do
        should "display journals" do
          get '/issues/1.xml?include=journals'

          assert_tag :tag => 'issue',
            :child => {
              :tag => 'journals',
              :attributes => { :type => 'array' },
              :child => {
                :tag => 'journal',
                :attributes => { :id => '1'},
                :child => {
                  :tag => 'details',
                  :attributes => { :type => 'array' },
                  :child => {
                    :tag => 'detail',
                    :attributes => { :name => 'status_id' },
                    :child => {
                      :tag => 'old_value',
                      :content => '1',
                      :sibling => {
                        :tag => 'new_value',
                        :content => '2'
                      }
                    }
                  }
                }
              }
            }
        end
      end
    end

    context "with custom fields" do
      context ".xml" do
        should "display custom fields" do
          get '/issues/3.xml'

          assert_tag :tag => 'issue',
            :child => {
              :tag => 'custom_fields',
              :attributes => { :type => 'array' },
              :child => {
                :tag => 'custom_field',
                :attributes => { :id => '1'},
                :child => {
                  :tag => 'value',
                  :content => 'MySQL'
                }
              }
            }

          assert_nothing_raised do
            Hash.from_xml(response.body).to_xml
          end
        end
      end
    end

    context "with multi custom fields" do
      setup do
        field = CustomField.find(1)
        field.update_attribute :multiple, true
        issue = Issue.find(3)
        issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
        issue.save!
      end

      context ".xml" do
        should "display custom fields" do
          get '/issues/3.xml'
          assert_response :success
          assert_tag :tag => 'issue',
            :child => {
              :tag => 'custom_fields',
              :attributes => { :type => 'array' },
              :child => {
                :tag => 'custom_field',
                :attributes => { :id => '1'},
                :child => {
                  :tag => 'value',
                  :attributes => { :type => 'array' },
                  :children => { :count => 2 }
                }
              }
            }

          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
      end

      context ".json" do
        should "display custom fields" do
          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
      end
    end

    context "with empty value for multi custom field" do
      setup do
        field = CustomField.find(1)
        field.update_attribute :multiple, true
        issue = Issue.find(3)
        issue.custom_field_values = {1 => ['']}
        issue.save!
      end

      context ".xml" do
        should "display custom fields" do
          get '/issues/3.xml'
          assert_response :success
          assert_tag :tag => 'issue',
            :child => {
              :tag => 'custom_fields',
              :attributes => { :type => 'array' },
              :child => {
                :tag => 'custom_field',
                :attributes => { :id => '1'},
                :child => {
                  :tag => 'value',
                  :attributes => { :type => 'array' },
                  :children => { :count => 0 }
                }
              }
            }

          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
      end

      context ".json" do
        should "display custom fields" do
          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
      end
    end

    context "with attachments" do
      context ".xml" do
        should "display attachments" do
          get '/issues/3.xml?include=attachments'

          assert_tag :tag => 'issue',
            :child => {
              :tag => 'attachments',
              :children => {:count => 5},
              :child => {
                :tag => 'attachment',
                :child => {
                  :tag => 'filename',
                  :content => 'source.rb',
                  :sibling => {
                    :tag => 'content_url',
                    :content => 'http://www.example.com/attachments/download/4/source.rb'
                  }
                }
              }
            }
        end
      end
    end

    context "with subtasks" do
      setup do
        @c1 = Issue.create!(
                :status_id => 1, :subject => "child c1",
                :tracker_id => 1, :project_id => 1, :author_id => 1,
                :parent_issue_id => 1
              )
        @c2 = Issue.create!(
                :status_id => 1, :subject => "child c2",
                :tracker_id => 1, :project_id => 1, :author_id => 1,
                :parent_issue_id => 1
              )
        @c3 = Issue.create!(
                :status_id => 1, :subject => "child c3",
                :tracker_id => 1, :project_id => 1, :author_id => 1,
                :parent_issue_id => @c1.id
              )
      end

      context ".xml" do
        should "display children" do
          get '/issues/1.xml?include=children'

          assert_tag :tag => 'issue',
            :child => {
              :tag => 'children',
              :children => {:count => 2},
              :child => {
                :tag => 'issue',
                :attributes => {:id => @c1.id.to_s},
                :child => {
                  :tag => 'subject',
                  :content => 'child c1',
                  :sibling => {
                    :tag => 'children',
                    :children => {:count => 1},
                    :child => {
                      :tag => 'issue',
                      :attributes => {:id => @c3.id.to_s}
                    }
                  }
                }
              }
            }
        end

        context ".json" do
          should "display children" do
            get '/issues/1.json?include=children'

            json = ActiveSupport::JSON.decode(response.body)
            assert_equal([
              {
                'id' => @c1.id, 'subject' => 'child c1', 'tracker' => {'id' => 1, 'name' => 'Bug'},
                'children' => [{'id' => @c3.id, 'subject' => 'child c3',
                                'tracker' => {'id' => 1, 'name' => 'Bug'} }]
              },
              { 'id' => @c2.id, 'subject' => 'child c2', 'tracker' => {'id' => 1, 'name' => 'Bug'} }
              ],
              json['issue']['children'])
          end
        end
      end
    end
  end

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

    get '/issues/1.xml?include=watchers', {}, credentials('jsmith')

    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
        assert_select 'user[id=3]'
      end
    end
  end

  context "POST /issues.xml" do
    should_allow_api_authentication(
      :post,
      '/issues.xml',
      {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
      {:success_code => :created}
    )
    should "create an issue with the attributes" do
      assert_difference('Issue.count') do
        post '/issues.xml',
             {:issue => {:project_id => 1, :subject => 'API test',
              :tracker_id => 2, :status_id => 3}}, credentials('jsmith')
      end
      issue = Issue.first(:order => 'id DESC')
      assert_equal 1, issue.project_id
      assert_equal 2, issue.tracker_id
      assert_equal 3, issue.status_id
      assert_equal 'API test', issue.subject

      assert_response :created
      assert_equal 'application/xml', @response.content_type
      assert_tag 'issue', :child => {:tag => 'id', :content => issue.id.to_s}
    end
  end

  test "POST /issues.xml with watcher_user_ids should create issue with watchers" do
    assert_difference('Issue.count') do
      post '/issues.xml',
           {:issue => {:project_id => 1, :subject => 'Watchers',
            :tracker_id => 2, :status_id => 3, :watcher_user_ids => [3, 1]}}, credentials('jsmith')
      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

  context "POST /issues.xml with failure" do
    should "have an errors tag" do
      assert_no_difference('Issue.count') do
        post '/issues.xml', {:issue => {:project_id => 1}}, credentials('jsmith')
      end

      assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
    end
  end

  context "POST /issues.json" do
    should_allow_api_authentication(:post,
                                    '/issues.json',
                                    {:issue => {:project_id => 1, :subject => 'API test',
                                     :tracker_id => 2, :status_id => 3}},
                                    {:success_code => :created})

    should "create an issue with the attributes" do
      assert_difference('Issue.count') do
        post '/issues.json',
             {:issue => {:project_id => 1, :subject => 'API test',
                         :tracker_id => 2, :status_id => 3}},
             credentials('jsmith')
      end

      issue = Issue.first(:order => 'id DESC')
      assert_equal 1, issue.project_id
      assert_equal 2, issue.tracker_id
      assert_equal 3, issue.status_id
      assert_equal 'API test', issue.subject
    end

  end

  context "POST /issues.json with failure" do
    should "have an errors element" do
      assert_no_difference('Issue.count') do
        post '/issues.json', {:issue => {:project_id => 1}}, credentials('jsmith')
      end

      json = ActiveSupport::JSON.decode(response.body)
      assert json['errors'].include?("Subject can't be blank")
    end
  end

  # Issue 6 is on a private project
  context "PUT /issues/6.xml" do
    setup do
      @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
    end

    should_allow_api_authentication(:put,
                                    '/issues/6.xml',
                                    {:issue => {:subject => 'API update', :notes => 'A new note'}},
                                    {:success_code => :ok})

    should "not create a new issue" do
      assert_no_difference('Issue.count') do
        put '/issues/6.xml', @parameters, credentials('jsmith')
      end
    end

    should "create a new journal" do
      assert_difference('Journal.count') do
        put '/issues/6.xml', @parameters, credentials('jsmith')
      end
    end

    should "add the note to the journal" do
      put '/issues/6.xml', @parameters, credentials('jsmith')

      journal = Journal.last
      assert_equal "A new note", journal.notes
    end

    should "update the issue" do
      put '/issues/6.xml', @parameters, credentials('jsmith')

      issue = Issue.find(6)
      assert_equal "API update", issue.subject
    end

  end

  context "PUT /issues/3.xml with custom fields" do
    setup do
      @parameters = {
        :issue => {:custom_fields => [{'id' => '1', 'value' => 'PostgreSQL' },
        {'id' => '2', 'value' => '150'}]}
      }
    end

    should "update custom fields" do
      assert_no_difference('Issue.count') do
        put '/issues/3.xml', @parameters, credentials('jsmith')
      end

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

  context "PUT /issues/3.xml with multi custom fields" do
    setup do
      field = CustomField.find(1)
      field.update_attribute :multiple, true
      @parameters = {
        :issue => {:custom_fields => [{'id' => '1', 'value' => ['MySQL', 'PostgreSQL'] },
        {'id' => '2', 'value' => '150'}]}
      }
    end

    should "update custom fields" do
      assert_no_difference('Issue.count') do
        put '/issues/3.xml', @parameters, credentials('jsmith')
      end

      issue = Issue.find(3)
      assert_equal '150', issue.custom_value_for(2).value
      assert_equal ['MySQL', 'PostgreSQL'], issue.custom_field_value(1).sort
    end
  end

  context "PUT /issues/3.xml with project change" do
    setup do
      @parameters = {:issue => {:project_id => 2, :subject => 'Project changed'}}
    end

    should "update project" do
      assert_no_difference('Issue.count') do
        put '/issues/3.xml', @parameters, credentials('jsmith')
      end

      issue = Issue.find(3)
      assert_equal 2, issue.project_id
      assert_equal 'Project changed', issue.subject
    end
  end

  context "PUT /issues/6.xml with failed update" do
    setup do
      @parameters = {:issue => {:subject => ''}}
    end

    should "not create a new issue" do
      assert_no_difference('Issue.count') do
        put '/issues/6.xml', @parameters, credentials('jsmith')
      end
    end

    should "not create a new journal" do
      assert_no_difference('Journal.count') do
        put '/issues/6.xml', @parameters, credentials('jsmith')
      end
    end

    should "have an errors tag" do
      put '/issues/6.xml', @parameters, credentials('jsmith')

      assert_tag :errors, :child => {:tag => 'error', :content => "Subject can't be blank"}
    end
  end

  context "PUT /issues/6.json" do
    setup do
      @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
    end

    should_allow_api_authentication(:put,
                                    '/issues/6.json',
                                    {:issue => {:subject => 'API update', :notes => 'A new note'}},
                                    {:success_code => :ok})

    should "update the issue" do
      assert_no_difference('Issue.count') do
        assert_difference('Journal.count') do
          put '/issues/6.json', @parameters, credentials('jsmith')

          assert_response :ok
          assert_equal '', response.body
        end
      end

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

  context "PUT /issues/6.json with failed update" do
    should "return errors" do
      assert_no_difference('Issue.count') do
        assert_no_difference('Journal.count') do
          put '/issues/6.json', {:issue => {:subject => ''}}, credentials('jsmith')

          assert_response :unprocessable_entity
        end
      end

      json = ActiveSupport::JSON.decode(response.body)
      assert json['errors'].include?("Subject can't be blank")
    end
  end

  context "DELETE /issues/1.xml" do
    should_allow_api_authentication(:delete,
                                    '/issues/6.xml',
                                    {},
                                    {:success_code => :ok})

    should "delete the issue" do
      assert_difference('Issue.count', -1) do
        delete '/issues/6.xml', {}, credentials('jsmith')

        assert_response :ok
        assert_equal '', response.body
      end

      assert_nil Issue.find_by_id(6)
    end
  end

  context "DELETE /issues/1.json" do
    should_allow_api_authentication(:delete,
                                    '/issues/6.json',
                                    {},
                                    {:success_code => :ok})

    should "delete the issue" do
      assert_difference('Issue.count', -1) do
        delete '/issues/6.json', {}, credentials('jsmith')

        assert_response :ok
        assert_equal '', response.body
      end

      assert_nil Issue.find_by_id(6)
    end
  end

  test "POST /issues/:id/watchers.xml should add watcher" do
    assert_difference 'Watcher.count' do
      post '/issues/1/watchers.xml', {:user_id => 3}, credentials('jsmith')

      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
      delete '/issues/1/watchers/3.xml', {}, credentials('jsmith')

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

  def test_create_issue_with_uploaded_file
    set_tmp_attachments_directory
    # upload the file
    assert_difference 'Attachment.count' do
      post '/uploads.xml', 'test_create_with_upload',
           {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
      assert_response :created
    end
    xml = Hash.from_xml(response.body)
    token = xml['upload']['token']
    attachment = Attachment.first(:order => 'id DESC')

    # create the issue with the upload's token
    assert_difference 'Issue.count' do
      post '/issues.xml',
           {:issue => {:project_id => 1, :subject => 'Uploaded file',
                       :uploads => [{:token => token, :filename => 'test.txt',
                                     :content_type => 'text/plain'}]}},
           credentials('jsmith')
      assert_response :created
    end
    issue = Issue.first(:order => 'id DESC')
    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
    get "/issues/#{issue.id}.xml", :include => 'attachments'
    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
  end

  def test_update_issue_with_uploaded_file
    set_tmp_attachments_directory
    # upload the file
    assert_difference 'Attachment.count' do
      post '/uploads.xml', 'test_upload_with_upload',
           {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials('jsmith'))
      assert_response :created
    end
    xml = Hash.from_xml(response.body)
    token = xml['upload']['token']
    attachment = Attachment.first(:order => 'id DESC')

    # update the issue with the upload's token
    assert_difference 'Journal.count' do
      put '/issues/1.xml',
          {:issue => {:notes => 'Attachment added',
                      :uploads => [{:token => token, :filename => 'test.txt',
                                    :content_type => 'text/plain'}]}},
          credentials('jsmith')
      assert_response :ok
      assert_equal '', @response.body
    end

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