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