To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / test / integration / api_test / issues_test.rb @ 442:753f1380d6bc

History | View | Annotate | Download (16.4 KB)

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

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

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

    
45
  def setup
46
    Setting.rest_api_enabled = '1'
47
  end
48

    
49
  context "/index.xml" do
50
    # Use a private project to make sure auth is really working and not just
51
    # only showing public issues.
52
    should_allow_api_authentication(:get, "/projects/private-child/issues.xml")
53
    
54
    should "contain metadata" do
55
      get '/issues.xml'
56
      
57
      assert_tag :tag => 'issues',
58
        :attributes => {
59
          :type => 'array',
60
          :total_count => assigns(:issue_count),
61
          :limit => 25,
62
          :offset => 0
63
        }
64
    end
65
    
66
    context "with offset and limit" do
67
      should "use the params" do
68
        get '/issues.xml?offset=2&limit=3'
69
        
70
        assert_equal 3, assigns(:limit)
71
        assert_equal 2, assigns(:offset)
72
        assert_tag :tag => 'issues', :children => {:count => 3, :only => {:tag => 'issue'}}
73
      end
74
    end
75

    
76
    context "with nometa param" do
77
      should "not contain metadata" do
78
        get '/issues.xml?nometa=1'
79
        
80
        assert_tag :tag => 'issues',
81
          :attributes => {
82
            :type => 'array',
83
            :total_count => nil,
84
            :limit => nil,
85
            :offset => nil
86
          }
87
      end
88
    end
89

    
90
    context "with nometa header" do
91
      should "not contain metadata" do
92
        get '/issues.xml', {}, {'X-Redmine-Nometa' => '1'}
93
        
94
        assert_tag :tag => 'issues',
95
          :attributes => {
96
            :type => 'array',
97
            :total_count => nil,
98
            :limit => nil,
99
            :offset => nil
100
          }
101
      end
102
    end
103
  end
104

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

    
109
  context "/index.xml with filter" do
110
    should_allow_api_authentication(:get, "/projects/private-child/issues.xml?status_id=5")
111

    
112
    should "show only issues with the status_id" do
113
      get '/issues.xml?status_id=5'
114
      assert_tag :tag => 'issues',
115
                 :children => { :count => Issue.visible.count(:conditions => {:status_id => 5}), 
116
                                :only => { :tag => 'issue' } }
117
    end
118
  end
119

    
120
  context "/index.json with filter" do
121
    should_allow_api_authentication(:get, "/projects/private-child/issues.json?status_id=5")
122

    
123
    should "show only issues with the status_id" do
124
      get '/issues.json?status_id=5'
125

    
126
      json = ActiveSupport::JSON.decode(response.body)
127
      status_ids_used = json['issues'].collect {|j| j['status']['id'] }
128
      assert_equal 3, status_ids_used.length
129
      assert status_ids_used.all? {|id| id == 5 }
130
    end
131

    
132
  end
133

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

    
139
  context "/issues/6.json" do
140
    should_allow_api_authentication(:get, "/issues/6.json")
141
  end
142
  
143
  context "GET /issues/:id" do
144
    context "with journals" do
145
      context ".xml" do
146
        should "display journals" do
147
          get '/issues/1.xml?include=journals'
148
          
149
          assert_tag :tag => 'issue',
150
            :child => {
151
              :tag => 'journals',
152
              :attributes => { :type => 'array' },
153
              :child => {
154
                :tag => 'journal',
155
                :attributes => { :id => '1'},
156
                :child => {
157
                  :tag => 'details',
158
                  :attributes => { :type => 'array' },
159
                  :child => {
160
                    :tag => 'detail',
161
                    :attributes => { :name => 'status_id' },
162
                    :child => {
163
                      :tag => 'old_value',
164
                      :content => '1',
165
                      :sibling => {
166
                        :tag => 'new_value',
167
                        :content => '2'
168
                      }
169
                    }
170
                  }
171
                }
172
              }
173
            }
174
        end
175
      end
176
    end
177
    
178
    context "with custom fields" do
179
      context ".xml" do
180
        should "display custom fields" do
181
          get '/issues/3.xml'
182
          
183
          assert_tag :tag => 'issue', 
184
            :child => {
185
              :tag => 'custom_fields',
186
              :attributes => { :type => 'array' },
187
              :child => {
188
                :tag => 'custom_field',
189
                :attributes => { :id => '1'},
190
                :child => {
191
                  :tag => 'value',
192
                  :content => 'MySQL'
193
                }
194
              }
195
            }
196
            
197
          assert_nothing_raised do
198
            Hash.from_xml(response.body).to_xml
199
          end
200
        end
201
      end
202
    end
203
    
204
    context "with subtasks" do
205
      setup do
206
        @c1 = Issue.generate!(:status_id => 1, :subject => "child c1", :tracker_id => 1, :project_id => 1, :parent_issue_id => 1)
207
        @c2 = Issue.generate!(:status_id => 1, :subject => "child c2", :tracker_id => 1, :project_id => 1, :parent_issue_id => 1)
208
        @c3 = Issue.generate!(:status_id => 1, :subject => "child c3", :tracker_id => 1, :project_id => 1, :parent_issue_id => @c1.id)
209
      end
210
      
211
      context ".xml" do
212
        should "display children" do
213
          get '/issues/1.xml?include=children'
214
          
215
          assert_tag :tag => 'issue', 
216
            :child => {
217
              :tag => 'children',
218
              :children => {:count => 2},
219
              :child => {
220
                :tag => 'issue',
221
                :attributes => {:id => @c1.id.to_s},
222
                :child => {
223
                  :tag => 'subject',
224
                  :content => 'child c1',
225
                  :sibling => {
226
                    :tag => 'children',
227
                    :children => {:count => 1},
228
                    :child => {
229
                      :tag => 'issue',
230
                      :attributes => {:id => @c3.id.to_s}
231
                    }
232
                  }
233
                }
234
              }
235
            }
236
        end
237
        
238
        context ".json" do
239
          should "display children" do
240
            get '/issues/1.json?include=children'
241
            
242
            json = ActiveSupport::JSON.decode(response.body)
243
            assert_equal([
244
              {
245
                'id' => @c1.id, 'subject' => 'child c1', 'tracker' => {'id' => 1, 'name' => 'Bug'},
246
                'children' => [{ 'id' => @c3.id, 'subject' => 'child c3', 'tracker' => {'id' => 1, 'name' => 'Bug'} }]
247
              },
248
              { 'id' => @c2.id, 'subject' => 'child c2', 'tracker' => {'id' => 1, 'name' => 'Bug'} }
249
              ],
250
              json['issue']['children'])
251
          end
252
        end
253
      end
254
    end
255
  end
256

    
257
  context "POST /issues.xml" do
258
    should_allow_api_authentication(:post,
259
                                    '/issues.xml',
260
                                    {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}},
261
                                    {:success_code => :created})
262

    
263
    should "create an issue with the attributes" do
264
      assert_difference('Issue.count') do
265
        post '/issues.xml', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, :authorization => credentials('jsmith')
266
      end
267
        
268
      issue = Issue.first(:order => 'id DESC')
269
      assert_equal 1, issue.project_id
270
      assert_equal 2, issue.tracker_id
271
      assert_equal 3, issue.status_id
272
      assert_equal 'API test', issue.subject
273
  
274
      assert_response :created
275
      assert_equal 'application/xml', @response.content_type
276
      assert_tag 'issue', :child => {:tag => 'id', :content => issue.id.to_s}
277
    end
278
  end
279
  
280
  context "POST /issues.xml with failure" do
281
    should_allow_api_authentication(:post,
282
                                    '/issues.xml',
283
                                    {:issue => {:project_id => 1}},
284
                                    {:success_code => :unprocessable_entity})
285

    
286
    should "have an errors tag" do
287
      assert_no_difference('Issue.count') do
288
        post '/issues.xml', {:issue => {:project_id => 1}}, :authorization => credentials('jsmith')
289
      end
290

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

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

    
301
    should "create an issue with the attributes" do
302
      assert_difference('Issue.count') do
303
        post '/issues.json', {:issue => {:project_id => 1, :subject => 'API test', :tracker_id => 2, :status_id => 3}}, :authorization => credentials('jsmith')
304
      end
305
        
306
      issue = Issue.first(:order => 'id DESC')
307
      assert_equal 1, issue.project_id
308
      assert_equal 2, issue.tracker_id
309
      assert_equal 3, issue.status_id
310
      assert_equal 'API test', issue.subject
311
    end
312
    
313
  end
314
  
315
  context "POST /issues.json with failure" do
316
    should_allow_api_authentication(:post,
317
                                    '/issues.json',
318
                                    {:issue => {:project_id => 1}},
319
                                    {:success_code => :unprocessable_entity})
320

    
321
    should "have an errors element" do
322
      assert_no_difference('Issue.count') do
323
        post '/issues.json', {:issue => {:project_id => 1}}, :authorization => credentials('jsmith')
324
      end
325

    
326
      json = ActiveSupport::JSON.decode(response.body)
327
      assert json['errors'].include?(['subject', "can't be blank"])
328
    end
329
  end
330

    
331
  # Issue 6 is on a private project
332
  context "PUT /issues/6.xml" do
333
    setup do
334
      @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
335
      @headers = { :authorization => credentials('jsmith') }
336
    end
337
    
338
    should_allow_api_authentication(:put,
339
                                    '/issues/6.xml',
340
                                    {:issue => {:subject => 'API update', :notes => 'A new note'}},
341
                                    {:success_code => :ok})
342

    
343
    should "not create a new issue" do
344
      assert_no_difference('Issue.count') do
345
        put '/issues/6.xml', @parameters, @headers
346
      end
347
    end
348

    
349
    should "create a new journal" do
350
      assert_difference('Journal.count') do
351
        put '/issues/6.xml', @parameters, @headers
352
      end
353
    end
354

    
355
    should "add the note to the journal" do
356
      put '/issues/6.xml', @parameters, @headers
357
      
358
      journal = Journal.last
359
      assert_equal "A new note", journal.notes
360
    end
361

    
362
    should "update the issue" do
363
      put '/issues/6.xml', @parameters, @headers
364
      
365
      issue = Issue.find(6)
366
      assert_equal "API update", issue.subject
367
    end
368
    
369
  end
370
  
371
  context "PUT /issues/3.xml with custom fields" do
372
    setup do
373
      @parameters = {:issue => {:custom_fields => [{'id' => '1', 'value' => 'PostgreSQL' }, {'id' => '2', 'value' => '150'}]}}
374
      @headers = { :authorization => credentials('jsmith') }
375
    end
376
    
377
    should "update custom fields" do
378
      assert_no_difference('Issue.count') do
379
        put '/issues/3.xml', @parameters, @headers
380
      end
381
      
382
      issue = Issue.find(3)
383
      assert_equal '150', issue.custom_value_for(2).value
384
      assert_equal 'PostgreSQL', issue.custom_value_for(1).value
385
    end
386
  end
387
  
388
  context "PUT /issues/6.xml with failed update" do
389
    setup do
390
      @parameters = {:issue => {:subject => ''}}
391
      @headers = { :authorization => credentials('jsmith') }
392
    end
393

    
394
    should_allow_api_authentication(:put,
395
                                    '/issues/6.xml',
396
                                    {:issue => {:subject => ''}}, # Missing subject should fail
397
                                    {:success_code => :unprocessable_entity})
398

    
399
    should "not create a new issue" do
400
      assert_no_difference('Issue.count') do
401
        put '/issues/6.xml', @parameters, @headers
402
      end
403
    end
404

    
405
    should "not create a new journal" do
406
      assert_no_difference('Journal.count') do
407
        put '/issues/6.xml', @parameters, @headers
408
      end
409
    end
410

    
411
    should "have an errors tag" do
412
      put '/issues/6.xml', @parameters, @headers
413

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

    
418
  context "PUT /issues/6.json" do
419
    setup do
420
      @parameters = {:issue => {:subject => 'API update', :notes => 'A new note'}}
421
      @headers = { :authorization => credentials('jsmith') }
422
    end
423
    
424
    should_allow_api_authentication(:put,
425
                                    '/issues/6.json',
426
                                    {:issue => {:subject => 'API update', :notes => 'A new note'}},
427
                                    {:success_code => :ok})
428

    
429
    should "not create a new issue" do
430
      assert_no_difference('Issue.count') do
431
        put '/issues/6.json', @parameters, @headers
432
      end
433
    end
434

    
435
    should "create a new journal" do
436
      assert_difference('Journal.count') do
437
        put '/issues/6.json', @parameters, @headers
438
      end
439
    end
440

    
441
    should "add the note to the journal" do
442
      put '/issues/6.json', @parameters, @headers
443
      
444
      journal = Journal.last
445
      assert_equal "A new note", journal.notes
446
    end
447

    
448
    should "update the issue" do
449
      put '/issues/6.json', @parameters, @headers
450
      
451
      issue = Issue.find(6)
452
      assert_equal "API update", issue.subject
453
    end
454
    
455
  end
456
  
457
  context "PUT /issues/6.json with failed update" do
458
    setup do
459
      @parameters = {:issue => {:subject => ''}}
460
      @headers = { :authorization => credentials('jsmith') }
461
    end
462

    
463
    should_allow_api_authentication(:put,
464
                                    '/issues/6.json',
465
                                    {:issue => {:subject => ''}}, # Missing subject should fail
466
                                    {:success_code => :unprocessable_entity})
467

    
468
    should "not create a new issue" do
469
      assert_no_difference('Issue.count') do
470
        put '/issues/6.json', @parameters, @headers
471
      end
472
    end
473

    
474
    should "not create a new journal" do
475
      assert_no_difference('Journal.count') do
476
        put '/issues/6.json', @parameters, @headers
477
      end
478
    end
479

    
480
    should "have an errors attribute" do
481
      put '/issues/6.json', @parameters, @headers
482

    
483
      json = ActiveSupport::JSON.decode(response.body)
484
      assert json['errors'].include?(['subject', "can't be blank"])
485
    end
486
  end
487

    
488
  context "DELETE /issues/1.xml" do
489
    should_allow_api_authentication(:delete,
490
                                    '/issues/6.xml',
491
                                    {},
492
                                    {:success_code => :ok})
493

    
494
    should "delete the issue" do
495
      assert_difference('Issue.count',-1) do
496
        delete '/issues/6.xml', {}, :authorization => credentials('jsmith')
497
      end
498
      
499
      assert_nil Issue.find_by_id(6)
500
    end
501
  end
502

    
503
  context "DELETE /issues/1.json" do
504
    should_allow_api_authentication(:delete,
505
                                    '/issues/6.json',
506
                                    {},
507
                                    {:success_code => :ok})
508

    
509
    should "delete the issue" do
510
      assert_difference('Issue.count',-1) do
511
        delete '/issues/6.json', {}, :authorization => credentials('jsmith')
512
      end
513
      
514
      assert_nil Issue.find_by_id(6)
515
    end
516
  end
517

    
518
  def credentials(user, password=nil)
519
    ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)
520
  end
521
end