##// END OF EJS Templates
add test to respond nothing in case of content type is not html with invalid query params (#8883, #6317)....
Toshi MARUYAMA -
r6906:9281df1bff81
parent child
Show More
@@ -1,1619 +1,1625
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 require 'issues_controller'
20 20
21 21 class IssuesControllerTest < ActionController::TestCase
22 22 fixtures :projects,
23 23 :users,
24 24 :roles,
25 25 :members,
26 26 :member_roles,
27 27 :issues,
28 28 :issue_statuses,
29 29 :versions,
30 30 :trackers,
31 31 :projects_trackers,
32 32 :issue_categories,
33 33 :enabled_modules,
34 34 :enumerations,
35 35 :attachments,
36 36 :workflows,
37 37 :custom_fields,
38 38 :custom_values,
39 39 :custom_fields_projects,
40 40 :custom_fields_trackers,
41 41 :time_entries,
42 42 :journals,
43 43 :journal_details,
44 44 :queries
45 45
46 46 def setup
47 47 @controller = IssuesController.new
48 48 @request = ActionController::TestRequest.new
49 49 @response = ActionController::TestResponse.new
50 50 User.current = nil
51 51 end
52 52
53 53 def test_index
54 54 Setting.default_language = 'en'
55 55
56 56 get :index
57 57 assert_response :success
58 58 assert_template 'index.rhtml'
59 59 assert_not_nil assigns(:issues)
60 60 assert_nil assigns(:project)
61 61 assert_tag :tag => 'a', :content => /Can't print recipes/
62 62 assert_tag :tag => 'a', :content => /Subproject issue/
63 63 # private projects hidden
64 64 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
65 65 assert_no_tag :tag => 'a', :content => /Issue on project 2/
66 66 # project column
67 67 assert_tag :tag => 'th', :content => /Project/
68 68 end
69 69
70 70 def test_index_should_not_list_issues_when_module_disabled
71 71 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
72 72 get :index
73 73 assert_response :success
74 74 assert_template 'index.rhtml'
75 75 assert_not_nil assigns(:issues)
76 76 assert_nil assigns(:project)
77 77 assert_no_tag :tag => 'a', :content => /Can't print recipes/
78 78 assert_tag :tag => 'a', :content => /Subproject issue/
79 79 end
80 80
81 81 def test_index_should_not_list_issues_when_module_disabled
82 82 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
83 83 get :index
84 84 assert_response :success
85 85 assert_template 'index.rhtml'
86 86 assert_not_nil assigns(:issues)
87 87 assert_nil assigns(:project)
88 88 assert_no_tag :tag => 'a', :content => /Can't print recipes/
89 89 assert_tag :tag => 'a', :content => /Subproject issue/
90 90 end
91 91
92 92 def test_index_should_list_visible_issues_only
93 93 get :index, :per_page => 100
94 94 assert_response :success
95 95 assert_not_nil assigns(:issues)
96 96 assert_nil assigns(:issues).detect {|issue| !issue.visible?}
97 97 end
98 98
99 99 def test_index_with_project
100 100 Setting.display_subprojects_issues = 0
101 101 get :index, :project_id => 1
102 102 assert_response :success
103 103 assert_template 'index.rhtml'
104 104 assert_not_nil assigns(:issues)
105 105 assert_tag :tag => 'a', :content => /Can't print recipes/
106 106 assert_no_tag :tag => 'a', :content => /Subproject issue/
107 107 end
108 108
109 109 def test_index_with_project_and_subprojects
110 110 Setting.display_subprojects_issues = 1
111 111 get :index, :project_id => 1
112 112 assert_response :success
113 113 assert_template 'index.rhtml'
114 114 assert_not_nil assigns(:issues)
115 115 assert_tag :tag => 'a', :content => /Can't print recipes/
116 116 assert_tag :tag => 'a', :content => /Subproject issue/
117 117 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
118 118 end
119 119
120 120 def test_index_with_project_and_subprojects_should_show_private_subprojects
121 121 @request.session[:user_id] = 2
122 122 Setting.display_subprojects_issues = 1
123 123 get :index, :project_id => 1
124 124 assert_response :success
125 125 assert_template 'index.rhtml'
126 126 assert_not_nil assigns(:issues)
127 127 assert_tag :tag => 'a', :content => /Can't print recipes/
128 128 assert_tag :tag => 'a', :content => /Subproject issue/
129 129 assert_tag :tag => 'a', :content => /Issue of a private subproject/
130 130 end
131 131
132 132 def test_index_with_project_and_default_filter
133 133 get :index, :project_id => 1, :set_filter => 1
134 134 assert_response :success
135 135 assert_template 'index.rhtml'
136 136 assert_not_nil assigns(:issues)
137 137
138 138 query = assigns(:query)
139 139 assert_not_nil query
140 140 # default filter
141 141 assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
142 142 end
143 143
144 144 def test_index_with_project_and_filter
145 145 get :index, :project_id => 1, :set_filter => 1,
146 146 :f => ['tracker_id'],
147 147 :op => {'tracker_id' => '='},
148 148 :v => {'tracker_id' => ['1']}
149 149 assert_response :success
150 150 assert_template 'index.rhtml'
151 151 assert_not_nil assigns(:issues)
152 152
153 153 query = assigns(:query)
154 154 assert_not_nil query
155 155 assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
156 156 end
157 157
158 158 def test_index_with_project_and_empty_filters
159 159 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
160 160 assert_response :success
161 161 assert_template 'index.rhtml'
162 162 assert_not_nil assigns(:issues)
163 163
164 164 query = assigns(:query)
165 165 assert_not_nil query
166 166 # no filter
167 167 assert_equal({}, query.filters)
168 168 end
169 169
170 170 def test_index_with_query
171 171 get :index, :project_id => 1, :query_id => 5
172 172 assert_response :success
173 173 assert_template 'index.rhtml'
174 174 assert_not_nil assigns(:issues)
175 175 assert_nil assigns(:issue_count_by_group)
176 176 end
177 177
178 178 def test_index_with_query_grouped_by_tracker
179 179 get :index, :project_id => 1, :query_id => 6
180 180 assert_response :success
181 181 assert_template 'index.rhtml'
182 182 assert_not_nil assigns(:issues)
183 183 assert_not_nil assigns(:issue_count_by_group)
184 184 end
185 185
186 186 def test_index_with_query_grouped_by_list_custom_field
187 187 get :index, :project_id => 1, :query_id => 9
188 188 assert_response :success
189 189 assert_template 'index.rhtml'
190 190 assert_not_nil assigns(:issues)
191 191 assert_not_nil assigns(:issue_count_by_group)
192 192 end
193 193
194 194 def test_private_query_should_not_be_available_to_other_users
195 195 q = Query.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
196 196 @request.session[:user_id] = 3
197 197
198 198 get :index, :query_id => q.id
199 199 assert_response 403
200 200 end
201 201
202 202 def test_private_query_should_be_available_to_its_user
203 203 q = Query.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
204 204 @request.session[:user_id] = 2
205 205
206 206 get :index, :query_id => q.id
207 207 assert_response :success
208 208 end
209 209
210 210 def test_public_query_should_be_available_to_other_users
211 211 q = Query.create!(:name => "private", :user => User.find(2), :is_public => true, :project => nil)
212 212 @request.session[:user_id] = 3
213 213
214 214 get :index, :query_id => q.id
215 215 assert_response :success
216 216 end
217 217
218 218 def test_index_sort_by_field_not_included_in_columns
219 219 Setting.issue_list_default_columns = %w(subject author)
220 220 get :index, :sort => 'tracker'
221 221 end
222 222
223 223 def test_index_csv_with_project
224 224 Setting.default_language = 'en'
225 225
226 226 get :index, :format => 'csv'
227 227 assert_response :success
228 228 assert_not_nil assigns(:issues)
229 229 assert_equal 'text/csv', @response.content_type
230 230 assert @response.body.starts_with?("#,")
231 231
232 232 get :index, :project_id => 1, :format => 'csv'
233 233 assert_response :success
234 234 assert_not_nil assigns(:issues)
235 235 assert_equal 'text/csv', @response.content_type
236 236 end
237 237
238 238 def test_index_pdf
239 239 get :index, :format => 'pdf'
240 240 assert_response :success
241 241 assert_not_nil assigns(:issues)
242 242 assert_equal 'application/pdf', @response.content_type
243 243
244 244 get :index, :project_id => 1, :format => 'pdf'
245 245 assert_response :success
246 246 assert_not_nil assigns(:issues)
247 247 assert_equal 'application/pdf', @response.content_type
248 248
249 249 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
250 250 assert_response :success
251 251 assert_not_nil assigns(:issues)
252 252 assert_equal 'application/pdf', @response.content_type
253 253 end
254 254
255 255 def test_index_pdf_with_query_grouped_by_list_custom_field
256 256 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
257 257 assert_response :success
258 258 assert_not_nil assigns(:issues)
259 259 assert_not_nil assigns(:issue_count_by_group)
260 260 assert_equal 'application/pdf', @response.content_type
261 261 end
262 262
263 263 def test_index_sort
264 264 get :index, :sort => 'tracker,id:desc'
265 265 assert_response :success
266 266
267 267 sort_params = @request.session['issues_index_sort']
268 268 assert sort_params.is_a?(String)
269 269 assert_equal 'tracker,id:desc', sort_params
270 270
271 271 issues = assigns(:issues)
272 272 assert_not_nil issues
273 273 assert !issues.empty?
274 274 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
275 275 end
276 276
277 277 def test_index_with_columns
278 278 columns = ['tracker', 'subject', 'assigned_to']
279 279 get :index, :set_filter => 1, :c => columns
280 280 assert_response :success
281 281
282 282 # query should use specified columns
283 283 query = assigns(:query)
284 284 assert_kind_of Query, query
285 285 assert_equal columns, query.column_names.map(&:to_s)
286 286
287 287 # columns should be stored in session
288 288 assert_kind_of Hash, session[:query]
289 289 assert_kind_of Array, session[:query][:column_names]
290 290 assert_equal columns, session[:query][:column_names].map(&:to_s)
291 291
292 292 # ensure only these columns are kept in the selected columns list
293 293 assert_tag :tag => 'select', :attributes => { :id => 'selected_columns' },
294 294 :children => { :count => 3 }
295 295 assert_no_tag :tag => 'option', :attributes => { :value => 'project' },
296 296 :parent => { :tag => 'select', :attributes => { :id => "selected_columns" } }
297 297 end
298 298
299 299 def test_index_with_custom_field_column
300 300 columns = %w(tracker subject cf_2)
301 301 get :index, :set_filter => 1, :c => columns
302 302 assert_response :success
303 303
304 304 # query should use specified columns
305 305 query = assigns(:query)
306 306 assert_kind_of Query, query
307 307 assert_equal columns, query.column_names.map(&:to_s)
308 308
309 309 assert_tag :td,
310 310 :attributes => {:class => 'cf_2 string'},
311 311 :ancestor => {:tag => 'table', :attributes => {:class => /issues/}}
312 312 end
313 313
314 314 def test_index_send_html_if_query_is_invalid
315 315 get :index, :f => ['start_date'], :op => {:start_date => '='}
316 316 assert_equal 'text/html', @response.content_type
317 317 assert_template 'index'
318 318 end
319 319
320 def test_index_send_nothing_if_query_is_invalid
321 get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
322 assert_equal 'text/csv', @response.content_type
323 assert @response.body.blank?
324 end
325
320 326 def test_show_by_anonymous
321 327 get :show, :id => 1
322 328 assert_response :success
323 329 assert_template 'show.rhtml'
324 330 assert_not_nil assigns(:issue)
325 331 assert_equal Issue.find(1), assigns(:issue)
326 332
327 333 # anonymous role is allowed to add a note
328 334 assert_tag :tag => 'form',
329 335 :descendant => { :tag => 'fieldset',
330 336 :child => { :tag => 'legend',
331 337 :content => /Notes/ } }
332 338 end
333 339
334 340 def test_show_by_manager
335 341 @request.session[:user_id] = 2
336 342 get :show, :id => 1
337 343 assert_response :success
338 344
339 345 assert_tag :tag => 'a',
340 346 :content => /Quote/
341 347
342 348 assert_tag :tag => 'form',
343 349 :descendant => { :tag => 'fieldset',
344 350 :child => { :tag => 'legend',
345 351 :content => /Change properties/ } },
346 352 :descendant => { :tag => 'fieldset',
347 353 :child => { :tag => 'legend',
348 354 :content => /Log time/ } },
349 355 :descendant => { :tag => 'fieldset',
350 356 :child => { :tag => 'legend',
351 357 :content => /Notes/ } }
352 358 end
353 359
354 360 def test_update_form_should_not_display_inactive_enumerations
355 361 @request.session[:user_id] = 2
356 362 get :show, :id => 1
357 363 assert_response :success
358 364
359 365 assert ! IssuePriority.find(15).active?
360 366 assert_no_tag :option, :attributes => {:value => '15'},
361 367 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
362 368 end
363 369
364 370 def test_update_form_should_allow_attachment_upload
365 371 @request.session[:user_id] = 2
366 372 get :show, :id => 1
367 373
368 374 assert_tag :tag => 'form',
369 375 :attributes => {:id => 'issue-form', :method => 'post', :enctype => 'multipart/form-data'},
370 376 :descendant => {
371 377 :tag => 'input',
372 378 :attributes => {:type => 'file', :name => 'attachments[1][file]'}
373 379 }
374 380 end
375 381
376 382 def test_show_should_deny_anonymous_access_without_permission
377 383 Role.anonymous.remove_permission!(:view_issues)
378 384 get :show, :id => 1
379 385 assert_response :redirect
380 386 end
381 387
382 388 def test_show_should_deny_anonymous_access_to_private_issue
383 389 Issue.update_all(["is_private = ?", true], "id = 1")
384 390 get :show, :id => 1
385 391 assert_response :redirect
386 392 end
387 393
388 394 def test_show_should_deny_non_member_access_without_permission
389 395 Role.non_member.remove_permission!(:view_issues)
390 396 @request.session[:user_id] = 9
391 397 get :show, :id => 1
392 398 assert_response 403
393 399 end
394 400
395 401 def test_show_should_deny_non_member_access_to_private_issue
396 402 Issue.update_all(["is_private = ?", true], "id = 1")
397 403 @request.session[:user_id] = 9
398 404 get :show, :id => 1
399 405 assert_response 403
400 406 end
401 407
402 408 def test_show_should_deny_member_access_without_permission
403 409 Role.find(1).remove_permission!(:view_issues)
404 410 @request.session[:user_id] = 2
405 411 get :show, :id => 1
406 412 assert_response 403
407 413 end
408 414
409 415 def test_show_should_deny_member_access_to_private_issue_without_permission
410 416 Issue.update_all(["is_private = ?", true], "id = 1")
411 417 @request.session[:user_id] = 3
412 418 get :show, :id => 1
413 419 assert_response 403
414 420 end
415 421
416 422 def test_show_should_allow_author_access_to_private_issue
417 423 Issue.update_all(["is_private = ?, author_id = 3", true], "id = 1")
418 424 @request.session[:user_id] = 3
419 425 get :show, :id => 1
420 426 assert_response :success
421 427 end
422 428
423 429 def test_show_should_allow_assignee_access_to_private_issue
424 430 Issue.update_all(["is_private = ?, assigned_to_id = 3", true], "id = 1")
425 431 @request.session[:user_id] = 3
426 432 get :show, :id => 1
427 433 assert_response :success
428 434 end
429 435
430 436 def test_show_should_allow_member_access_to_private_issue_with_permission
431 437 Issue.update_all(["is_private = ?", true], "id = 1")
432 438 User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
433 439 @request.session[:user_id] = 3
434 440 get :show, :id => 1
435 441 assert_response :success
436 442 end
437 443
438 444 def test_show_should_not_disclose_relations_to_invisible_issues
439 445 Setting.cross_project_issue_relations = '1'
440 446 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
441 447 # Relation to a private project issue
442 448 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
443 449
444 450 get :show, :id => 1
445 451 assert_response :success
446 452
447 453 assert_tag :div, :attributes => { :id => 'relations' },
448 454 :descendant => { :tag => 'a', :content => /#2$/ }
449 455 assert_no_tag :div, :attributes => { :id => 'relations' },
450 456 :descendant => { :tag => 'a', :content => /#4$/ }
451 457 end
452 458
453 459 def test_show_atom
454 460 get :show, :id => 2, :format => 'atom'
455 461 assert_response :success
456 462 assert_template 'journals/index.rxml'
457 463 # Inline image
458 464 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
459 465 end
460 466
461 467 def test_show_export_to_pdf
462 468 get :show, :id => 3, :format => 'pdf'
463 469 assert_response :success
464 470 assert_equal 'application/pdf', @response.content_type
465 471 assert @response.body.starts_with?('%PDF')
466 472 assert_not_nil assigns(:issue)
467 473 end
468 474
469 475 def test_get_new
470 476 @request.session[:user_id] = 2
471 477 get :new, :project_id => 1, :tracker_id => 1
472 478 assert_response :success
473 479 assert_template 'new'
474 480
475 481 assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
476 482 :value => 'Default string' }
477 483
478 484 # Be sure we don't display inactive IssuePriorities
479 485 assert ! IssuePriority.find(15).active?
480 486 assert_no_tag :option, :attributes => {:value => '15'},
481 487 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
482 488 end
483 489
484 490 def test_get_new_form_should_allow_attachment_upload
485 491 @request.session[:user_id] = 2
486 492 get :new, :project_id => 1, :tracker_id => 1
487 493
488 494 assert_tag :tag => 'form',
489 495 :attributes => {:id => 'issue-form', :method => 'post', :enctype => 'multipart/form-data'},
490 496 :descendant => {
491 497 :tag => 'input',
492 498 :attributes => {:type => 'file', :name => 'attachments[1][file]'}
493 499 }
494 500 end
495 501
496 502 def test_get_new_without_tracker_id
497 503 @request.session[:user_id] = 2
498 504 get :new, :project_id => 1
499 505 assert_response :success
500 506 assert_template 'new'
501 507
502 508 issue = assigns(:issue)
503 509 assert_not_nil issue
504 510 assert_equal Project.find(1).trackers.first, issue.tracker
505 511 end
506 512
507 513 def test_get_new_with_no_default_status_should_display_an_error
508 514 @request.session[:user_id] = 2
509 515 IssueStatus.delete_all
510 516
511 517 get :new, :project_id => 1
512 518 assert_response 500
513 519 assert_error_tag :content => /No default issue/
514 520 end
515 521
516 522 def test_get_new_with_no_tracker_should_display_an_error
517 523 @request.session[:user_id] = 2
518 524 Tracker.delete_all
519 525
520 526 get :new, :project_id => 1
521 527 assert_response 500
522 528 assert_error_tag :content => /No tracker/
523 529 end
524 530
525 531 def test_update_new_form
526 532 @request.session[:user_id] = 2
527 533 xhr :post, :new, :project_id => 1,
528 534 :issue => {:tracker_id => 2,
529 535 :subject => 'This is the test_new issue',
530 536 :description => 'This is the description',
531 537 :priority_id => 5}
532 538 assert_response :success
533 539 assert_template 'attributes'
534 540
535 541 issue = assigns(:issue)
536 542 assert_kind_of Issue, issue
537 543 assert_equal 1, issue.project_id
538 544 assert_equal 2, issue.tracker_id
539 545 assert_equal 'This is the test_new issue', issue.subject
540 546 end
541 547
542 548 def test_post_create
543 549 @request.session[:user_id] = 2
544 550 assert_difference 'Issue.count' do
545 551 post :create, :project_id => 1,
546 552 :issue => {:tracker_id => 3,
547 553 :status_id => 2,
548 554 :subject => 'This is the test_new issue',
549 555 :description => 'This is the description',
550 556 :priority_id => 5,
551 557 :start_date => '2010-11-07',
552 558 :estimated_hours => '',
553 559 :custom_field_values => {'2' => 'Value for field 2'}}
554 560 end
555 561 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
556 562
557 563 issue = Issue.find_by_subject('This is the test_new issue')
558 564 assert_not_nil issue
559 565 assert_equal 2, issue.author_id
560 566 assert_equal 3, issue.tracker_id
561 567 assert_equal 2, issue.status_id
562 568 assert_equal Date.parse('2010-11-07'), issue.start_date
563 569 assert_nil issue.estimated_hours
564 570 v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
565 571 assert_not_nil v
566 572 assert_equal 'Value for field 2', v.value
567 573 end
568 574
569 575 def test_post_new_with_group_assignment
570 576 group = Group.find(11)
571 577 project = Project.find(1)
572 578 project.members << Member.new(:principal => group, :roles => [Role.first])
573 579
574 580 with_settings :issue_group_assignment => '1' do
575 581 @request.session[:user_id] = 2
576 582 assert_difference 'Issue.count' do
577 583 post :create, :project_id => project.id,
578 584 :issue => {:tracker_id => 3,
579 585 :status_id => 1,
580 586 :subject => 'This is the test_new_with_group_assignment issue',
581 587 :assigned_to_id => group.id}
582 588 end
583 589 end
584 590 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
585 591
586 592 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
587 593 assert_not_nil issue
588 594 assert_equal group, issue.assigned_to
589 595 end
590 596
591 597 def test_post_create_without_start_date
592 598 @request.session[:user_id] = 2
593 599 assert_difference 'Issue.count' do
594 600 post :create, :project_id => 1,
595 601 :issue => {:tracker_id => 3,
596 602 :status_id => 2,
597 603 :subject => 'This is the test_new issue',
598 604 :description => 'This is the description',
599 605 :priority_id => 5,
600 606 :start_date => '',
601 607 :estimated_hours => '',
602 608 :custom_field_values => {'2' => 'Value for field 2'}}
603 609 end
604 610 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
605 611
606 612 issue = Issue.find_by_subject('This is the test_new issue')
607 613 assert_not_nil issue
608 614 assert_nil issue.start_date
609 615 end
610 616
611 617 def test_post_create_and_continue
612 618 @request.session[:user_id] = 2
613 619 assert_difference 'Issue.count' do
614 620 post :create, :project_id => 1,
615 621 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
616 622 :continue => ''
617 623 end
618 624
619 625 issue = Issue.first(:order => 'id DESC')
620 626 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
621 627 assert_not_nil flash[:notice], "flash was not set"
622 628 assert flash[:notice].include?("<a href='/issues/#{issue.id}'>##{issue.id}</a>"), "issue link not found in flash: #{flash[:notice]}"
623 629 end
624 630
625 631 def test_post_create_without_custom_fields_param
626 632 @request.session[:user_id] = 2
627 633 assert_difference 'Issue.count' do
628 634 post :create, :project_id => 1,
629 635 :issue => {:tracker_id => 1,
630 636 :subject => 'This is the test_new issue',
631 637 :description => 'This is the description',
632 638 :priority_id => 5}
633 639 end
634 640 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
635 641 end
636 642
637 643 def test_post_create_with_required_custom_field_and_without_custom_fields_param
638 644 field = IssueCustomField.find_by_name('Database')
639 645 field.update_attribute(:is_required, true)
640 646
641 647 @request.session[:user_id] = 2
642 648 post :create, :project_id => 1,
643 649 :issue => {:tracker_id => 1,
644 650 :subject => 'This is the test_new issue',
645 651 :description => 'This is the description',
646 652 :priority_id => 5}
647 653 assert_response :success
648 654 assert_template 'new'
649 655 issue = assigns(:issue)
650 656 assert_not_nil issue
651 657 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
652 658 end
653 659
654 660 def test_post_create_with_watchers
655 661 @request.session[:user_id] = 2
656 662 ActionMailer::Base.deliveries.clear
657 663
658 664 assert_difference 'Watcher.count', 2 do
659 665 post :create, :project_id => 1,
660 666 :issue => {:tracker_id => 1,
661 667 :subject => 'This is a new issue with watchers',
662 668 :description => 'This is the description',
663 669 :priority_id => 5,
664 670 :watcher_user_ids => ['2', '3']}
665 671 end
666 672 issue = Issue.find_by_subject('This is a new issue with watchers')
667 673 assert_not_nil issue
668 674 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
669 675
670 676 # Watchers added
671 677 assert_equal [2, 3], issue.watcher_user_ids.sort
672 678 assert issue.watched_by?(User.find(3))
673 679 # Watchers notified
674 680 mail = ActionMailer::Base.deliveries.last
675 681 assert_kind_of TMail::Mail, mail
676 682 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
677 683 end
678 684
679 685 def test_post_create_subissue
680 686 @request.session[:user_id] = 2
681 687
682 688 assert_difference 'Issue.count' do
683 689 post :create, :project_id => 1,
684 690 :issue => {:tracker_id => 1,
685 691 :subject => 'This is a child issue',
686 692 :parent_issue_id => 2}
687 693 end
688 694 issue = Issue.find_by_subject('This is a child issue')
689 695 assert_not_nil issue
690 696 assert_equal Issue.find(2), issue.parent
691 697 end
692 698
693 699 def test_post_create_subissue_with_non_numeric_parent_id
694 700 @request.session[:user_id] = 2
695 701
696 702 assert_difference 'Issue.count' do
697 703 post :create, :project_id => 1,
698 704 :issue => {:tracker_id => 1,
699 705 :subject => 'This is a child issue',
700 706 :parent_issue_id => 'ABC'}
701 707 end
702 708 issue = Issue.find_by_subject('This is a child issue')
703 709 assert_not_nil issue
704 710 assert_nil issue.parent
705 711 end
706 712
707 713 def test_post_create_private
708 714 @request.session[:user_id] = 2
709 715
710 716 assert_difference 'Issue.count' do
711 717 post :create, :project_id => 1,
712 718 :issue => {:tracker_id => 1,
713 719 :subject => 'This is a private issue',
714 720 :is_private => '1'}
715 721 end
716 722 issue = Issue.first(:order => 'id DESC')
717 723 assert issue.is_private?
718 724 end
719 725
720 726 def test_post_create_private_with_set_own_issues_private_permission
721 727 role = Role.find(1)
722 728 role.remove_permission! :set_issues_private
723 729 role.add_permission! :set_own_issues_private
724 730
725 731 @request.session[:user_id] = 2
726 732
727 733 assert_difference 'Issue.count' do
728 734 post :create, :project_id => 1,
729 735 :issue => {:tracker_id => 1,
730 736 :subject => 'This is a private issue',
731 737 :is_private => '1'}
732 738 end
733 739 issue = Issue.first(:order => 'id DESC')
734 740 assert issue.is_private?
735 741 end
736 742
737 743 def test_post_create_should_send_a_notification
738 744 ActionMailer::Base.deliveries.clear
739 745 @request.session[:user_id] = 2
740 746 assert_difference 'Issue.count' do
741 747 post :create, :project_id => 1,
742 748 :issue => {:tracker_id => 3,
743 749 :subject => 'This is the test_new issue',
744 750 :description => 'This is the description',
745 751 :priority_id => 5,
746 752 :estimated_hours => '',
747 753 :custom_field_values => {'2' => 'Value for field 2'}}
748 754 end
749 755 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
750 756
751 757 assert_equal 1, ActionMailer::Base.deliveries.size
752 758 end
753 759
754 760 def test_post_create_should_preserve_fields_values_on_validation_failure
755 761 @request.session[:user_id] = 2
756 762 post :create, :project_id => 1,
757 763 :issue => {:tracker_id => 1,
758 764 # empty subject
759 765 :subject => '',
760 766 :description => 'This is a description',
761 767 :priority_id => 6,
762 768 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
763 769 assert_response :success
764 770 assert_template 'new'
765 771
766 772 assert_tag :textarea, :attributes => { :name => 'issue[description]' },
767 773 :content => 'This is a description'
768 774 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
769 775 :child => { :tag => 'option', :attributes => { :selected => 'selected',
770 776 :value => '6' },
771 777 :content => 'High' }
772 778 # Custom fields
773 779 assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
774 780 :child => { :tag => 'option', :attributes => { :selected => 'selected',
775 781 :value => 'Oracle' },
776 782 :content => 'Oracle' }
777 783 assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
778 784 :value => 'Value for field 2'}
779 785 end
780 786
781 787 def test_post_create_should_ignore_non_safe_attributes
782 788 @request.session[:user_id] = 2
783 789 assert_nothing_raised do
784 790 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
785 791 end
786 792 end
787 793
788 794 def test_post_create_with_attachment
789 795 set_tmp_attachments_directory
790 796 @request.session[:user_id] = 2
791 797
792 798 assert_difference 'Issue.count' do
793 799 assert_difference 'Attachment.count' do
794 800 post :create, :project_id => 1,
795 801 :issue => { :tracker_id => '1', :subject => 'With attachment' },
796 802 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
797 803 end
798 804 end
799 805
800 806 issue = Issue.first(:order => 'id DESC')
801 807 attachment = Attachment.first(:order => 'id DESC')
802 808
803 809 assert_equal issue, attachment.container
804 810 assert_equal 2, attachment.author_id
805 811 assert_equal 'testfile.txt', attachment.filename
806 812 assert_equal 'text/plain', attachment.content_type
807 813 assert_equal 'test file', attachment.description
808 814 assert_equal 59, attachment.filesize
809 815 assert File.exists?(attachment.diskfile)
810 816 assert_equal 59, File.size(attachment.diskfile)
811 817 end
812 818
813 819 context "without workflow privilege" do
814 820 setup do
815 821 Workflow.delete_all(["role_id = ?", Role.anonymous.id])
816 822 Role.anonymous.add_permission! :add_issues, :add_issue_notes
817 823 end
818 824
819 825 context "#new" do
820 826 should "propose default status only" do
821 827 get :new, :project_id => 1
822 828 assert_response :success
823 829 assert_template 'new'
824 830 assert_tag :tag => 'select',
825 831 :attributes => {:name => 'issue[status_id]'},
826 832 :children => {:count => 1},
827 833 :child => {:tag => 'option', :attributes => {:value => IssueStatus.default.id.to_s}}
828 834 end
829 835
830 836 should "accept default status" do
831 837 assert_difference 'Issue.count' do
832 838 post :create, :project_id => 1,
833 839 :issue => {:tracker_id => 1,
834 840 :subject => 'This is an issue',
835 841 :status_id => 1}
836 842 end
837 843 issue = Issue.last(:order => 'id')
838 844 assert_equal IssueStatus.default, issue.status
839 845 end
840 846
841 847 should "ignore unauthorized status" do
842 848 assert_difference 'Issue.count' do
843 849 post :create, :project_id => 1,
844 850 :issue => {:tracker_id => 1,
845 851 :subject => 'This is an issue',
846 852 :status_id => 3}
847 853 end
848 854 issue = Issue.last(:order => 'id')
849 855 assert_equal IssueStatus.default, issue.status
850 856 end
851 857 end
852 858
853 859 context "#update" do
854 860 should "ignore status change" do
855 861 assert_difference 'Journal.count' do
856 862 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
857 863 end
858 864 assert_equal 1, Issue.find(1).status_id
859 865 end
860 866
861 867 should "ignore attributes changes" do
862 868 assert_difference 'Journal.count' do
863 869 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed', :assigned_to_id => 2}
864 870 end
865 871 issue = Issue.find(1)
866 872 assert_equal "Can't print recipes", issue.subject
867 873 assert_nil issue.assigned_to
868 874 end
869 875 end
870 876 end
871 877
872 878 context "with workflow privilege" do
873 879 setup do
874 880 Workflow.delete_all(["role_id = ?", Role.anonymous.id])
875 881 Workflow.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
876 882 Workflow.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
877 883 Role.anonymous.add_permission! :add_issues, :add_issue_notes
878 884 end
879 885
880 886 context "#update" do
881 887 should "accept authorized status" do
882 888 assert_difference 'Journal.count' do
883 889 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
884 890 end
885 891 assert_equal 3, Issue.find(1).status_id
886 892 end
887 893
888 894 should "ignore unauthorized status" do
889 895 assert_difference 'Journal.count' do
890 896 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 2}
891 897 end
892 898 assert_equal 1, Issue.find(1).status_id
893 899 end
894 900
895 901 should "accept authorized attributes changes" do
896 902 assert_difference 'Journal.count' do
897 903 put :update, :id => 1, :notes => 'just trying', :issue => {:assigned_to_id => 2}
898 904 end
899 905 issue = Issue.find(1)
900 906 assert_equal 2, issue.assigned_to_id
901 907 end
902 908
903 909 should "ignore unauthorized attributes changes" do
904 910 assert_difference 'Journal.count' do
905 911 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed'}
906 912 end
907 913 issue = Issue.find(1)
908 914 assert_equal "Can't print recipes", issue.subject
909 915 end
910 916 end
911 917
912 918 context "and :edit_issues permission" do
913 919 setup do
914 920 Role.anonymous.add_permission! :add_issues, :edit_issues
915 921 end
916 922
917 923 should "accept authorized status" do
918 924 assert_difference 'Journal.count' do
919 925 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
920 926 end
921 927 assert_equal 3, Issue.find(1).status_id
922 928 end
923 929
924 930 should "ignore unauthorized status" do
925 931 assert_difference 'Journal.count' do
926 932 put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 2}
927 933 end
928 934 assert_equal 1, Issue.find(1).status_id
929 935 end
930 936
931 937 should "accept authorized attributes changes" do
932 938 assert_difference 'Journal.count' do
933 939 put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed', :assigned_to_id => 2}
934 940 end
935 941 issue = Issue.find(1)
936 942 assert_equal "changed", issue.subject
937 943 assert_equal 2, issue.assigned_to_id
938 944 end
939 945 end
940 946 end
941 947
942 948 def test_copy_issue
943 949 @request.session[:user_id] = 2
944 950 get :new, :project_id => 1, :copy_from => 1
945 951 assert_template 'new'
946 952 assert_not_nil assigns(:issue)
947 953 orig = Issue.find(1)
948 954 assert_equal orig.subject, assigns(:issue).subject
949 955 end
950 956
951 957 def test_get_edit
952 958 @request.session[:user_id] = 2
953 959 get :edit, :id => 1
954 960 assert_response :success
955 961 assert_template 'edit'
956 962 assert_not_nil assigns(:issue)
957 963 assert_equal Issue.find(1), assigns(:issue)
958 964
959 965 # Be sure we don't display inactive IssuePriorities
960 966 assert ! IssuePriority.find(15).active?
961 967 assert_no_tag :option, :attributes => {:value => '15'},
962 968 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
963 969 end
964 970
965 971 def test_get_edit_with_params
966 972 @request.session[:user_id] = 2
967 973 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
968 974 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => TimeEntryActivity.first.id }
969 975 assert_response :success
970 976 assert_template 'edit'
971 977
972 978 issue = assigns(:issue)
973 979 assert_not_nil issue
974 980
975 981 assert_equal 5, issue.status_id
976 982 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
977 983 :child => { :tag => 'option',
978 984 :content => 'Closed',
979 985 :attributes => { :selected => 'selected' } }
980 986
981 987 assert_equal 7, issue.priority_id
982 988 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
983 989 :child => { :tag => 'option',
984 990 :content => 'Urgent',
985 991 :attributes => { :selected => 'selected' } }
986 992
987 993 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => '2.5' }
988 994 assert_tag :select, :attributes => { :name => 'time_entry[activity_id]' },
989 995 :child => { :tag => 'option',
990 996 :attributes => { :selected => 'selected', :value => TimeEntryActivity.first.id } }
991 997 assert_tag :input, :attributes => { :name => 'time_entry[comments]', :value => 'test_get_edit_with_params' }
992 998 end
993 999
994 1000 def test_update_edit_form
995 1001 @request.session[:user_id] = 2
996 1002 xhr :post, :new, :project_id => 1,
997 1003 :id => 1,
998 1004 :issue => {:tracker_id => 2,
999 1005 :subject => 'This is the test_new issue',
1000 1006 :description => 'This is the description',
1001 1007 :priority_id => 5}
1002 1008 assert_response :success
1003 1009 assert_template 'attributes'
1004 1010
1005 1011 issue = assigns(:issue)
1006 1012 assert_kind_of Issue, issue
1007 1013 assert_equal 1, issue.id
1008 1014 assert_equal 1, issue.project_id
1009 1015 assert_equal 2, issue.tracker_id
1010 1016 assert_equal 'This is the test_new issue', issue.subject
1011 1017 end
1012 1018
1013 1019 def test_update_using_invalid_http_verbs
1014 1020 @request.session[:user_id] = 2
1015 1021 subject = 'Updated by an invalid http verb'
1016 1022
1017 1023 get :update, :id => 1, :issue => {:subject => subject}
1018 1024 assert_not_equal subject, Issue.find(1).subject
1019 1025
1020 1026 post :update, :id => 1, :issue => {:subject => subject}
1021 1027 assert_not_equal subject, Issue.find(1).subject
1022 1028
1023 1029 delete :update, :id => 1, :issue => {:subject => subject}
1024 1030 assert_not_equal subject, Issue.find(1).subject
1025 1031 end
1026 1032
1027 1033 def test_put_update_without_custom_fields_param
1028 1034 @request.session[:user_id] = 2
1029 1035 ActionMailer::Base.deliveries.clear
1030 1036
1031 1037 issue = Issue.find(1)
1032 1038 assert_equal '125', issue.custom_value_for(2).value
1033 1039 old_subject = issue.subject
1034 1040 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
1035 1041
1036 1042 assert_difference('Journal.count') do
1037 1043 assert_difference('JournalDetail.count', 2) do
1038 1044 put :update, :id => 1, :issue => {:subject => new_subject,
1039 1045 :priority_id => '6',
1040 1046 :category_id => '1' # no change
1041 1047 }
1042 1048 end
1043 1049 end
1044 1050 assert_redirected_to :action => 'show', :id => '1'
1045 1051 issue.reload
1046 1052 assert_equal new_subject, issue.subject
1047 1053 # Make sure custom fields were not cleared
1048 1054 assert_equal '125', issue.custom_value_for(2).value
1049 1055
1050 1056 mail = ActionMailer::Base.deliveries.last
1051 1057 assert_kind_of TMail::Mail, mail
1052 1058 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
1053 1059 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
1054 1060 end
1055 1061
1056 1062 def test_put_update_with_custom_field_change
1057 1063 @request.session[:user_id] = 2
1058 1064 issue = Issue.find(1)
1059 1065 assert_equal '125', issue.custom_value_for(2).value
1060 1066
1061 1067 assert_difference('Journal.count') do
1062 1068 assert_difference('JournalDetail.count', 3) do
1063 1069 put :update, :id => 1, :issue => {:subject => 'Custom field change',
1064 1070 :priority_id => '6',
1065 1071 :category_id => '1', # no change
1066 1072 :custom_field_values => { '2' => 'New custom value' }
1067 1073 }
1068 1074 end
1069 1075 end
1070 1076 assert_redirected_to :action => 'show', :id => '1'
1071 1077 issue.reload
1072 1078 assert_equal 'New custom value', issue.custom_value_for(2).value
1073 1079
1074 1080 mail = ActionMailer::Base.deliveries.last
1075 1081 assert_kind_of TMail::Mail, mail
1076 1082 assert mail.body.include?("Searchable field changed from 125 to New custom value")
1077 1083 end
1078 1084
1079 1085 def test_put_update_with_status_and_assignee_change
1080 1086 issue = Issue.find(1)
1081 1087 assert_equal 1, issue.status_id
1082 1088 @request.session[:user_id] = 2
1083 1089 assert_difference('TimeEntry.count', 0) do
1084 1090 put :update,
1085 1091 :id => 1,
1086 1092 :issue => { :status_id => 2, :assigned_to_id => 3 },
1087 1093 :notes => 'Assigned to dlopper',
1088 1094 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
1089 1095 end
1090 1096 assert_redirected_to :action => 'show', :id => '1'
1091 1097 issue.reload
1092 1098 assert_equal 2, issue.status_id
1093 1099 j = Journal.find(:first, :order => 'id DESC')
1094 1100 assert_equal 'Assigned to dlopper', j.notes
1095 1101 assert_equal 2, j.details.size
1096 1102
1097 1103 mail = ActionMailer::Base.deliveries.last
1098 1104 assert mail.body.include?("Status changed from New to Assigned")
1099 1105 # subject should contain the new status
1100 1106 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
1101 1107 end
1102 1108
1103 1109 def test_put_update_with_note_only
1104 1110 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
1105 1111 # anonymous user
1106 1112 put :update,
1107 1113 :id => 1,
1108 1114 :notes => notes
1109 1115 assert_redirected_to :action => 'show', :id => '1'
1110 1116 j = Journal.find(:first, :order => 'id DESC')
1111 1117 assert_equal notes, j.notes
1112 1118 assert_equal 0, j.details.size
1113 1119 assert_equal User.anonymous, j.user
1114 1120
1115 1121 mail = ActionMailer::Base.deliveries.last
1116 1122 assert mail.body.include?(notes)
1117 1123 end
1118 1124
1119 1125 def test_put_update_with_note_and_spent_time
1120 1126 @request.session[:user_id] = 2
1121 1127 spent_hours_before = Issue.find(1).spent_hours
1122 1128 assert_difference('TimeEntry.count') do
1123 1129 put :update,
1124 1130 :id => 1,
1125 1131 :notes => '2.5 hours added',
1126 1132 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
1127 1133 end
1128 1134 assert_redirected_to :action => 'show', :id => '1'
1129 1135
1130 1136 issue = Issue.find(1)
1131 1137
1132 1138 j = Journal.find(:first, :order => 'id DESC')
1133 1139 assert_equal '2.5 hours added', j.notes
1134 1140 assert_equal 0, j.details.size
1135 1141
1136 1142 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
1137 1143 assert_not_nil t
1138 1144 assert_equal 2.5, t.hours
1139 1145 assert_equal spent_hours_before + 2.5, issue.spent_hours
1140 1146 end
1141 1147
1142 1148 def test_put_update_with_attachment_only
1143 1149 set_tmp_attachments_directory
1144 1150
1145 1151 # Delete all fixtured journals, a race condition can occur causing the wrong
1146 1152 # journal to get fetched in the next find.
1147 1153 Journal.delete_all
1148 1154
1149 1155 # anonymous user
1150 1156 assert_difference 'Attachment.count' do
1151 1157 put :update, :id => 1,
1152 1158 :notes => '',
1153 1159 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
1154 1160 end
1155 1161
1156 1162 assert_redirected_to :action => 'show', :id => '1'
1157 1163 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
1158 1164 assert j.notes.blank?
1159 1165 assert_equal 1, j.details.size
1160 1166 assert_equal 'testfile.txt', j.details.first.value
1161 1167 assert_equal User.anonymous, j.user
1162 1168
1163 1169 attachment = Attachment.first(:order => 'id DESC')
1164 1170 assert_equal Issue.find(1), attachment.container
1165 1171 assert_equal User.anonymous, attachment.author
1166 1172 assert_equal 'testfile.txt', attachment.filename
1167 1173 assert_equal 'text/plain', attachment.content_type
1168 1174 assert_equal 'test file', attachment.description
1169 1175 assert_equal 59, attachment.filesize
1170 1176 assert File.exists?(attachment.diskfile)
1171 1177 assert_equal 59, File.size(attachment.diskfile)
1172 1178
1173 1179 mail = ActionMailer::Base.deliveries.last
1174 1180 assert mail.body.include?('testfile.txt')
1175 1181 end
1176 1182
1177 1183 def test_put_update_with_attachment_that_fails_to_save
1178 1184 set_tmp_attachments_directory
1179 1185
1180 1186 # Delete all fixtured journals, a race condition can occur causing the wrong
1181 1187 # journal to get fetched in the next find.
1182 1188 Journal.delete_all
1183 1189
1184 1190 # Mock out the unsaved attachment
1185 1191 Attachment.any_instance.stubs(:create).returns(Attachment.new)
1186 1192
1187 1193 # anonymous user
1188 1194 put :update,
1189 1195 :id => 1,
1190 1196 :notes => '',
1191 1197 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
1192 1198 assert_redirected_to :action => 'show', :id => '1'
1193 1199 assert_equal '1 file(s) could not be saved.', flash[:warning]
1194 1200
1195 1201 end if Object.const_defined?(:Mocha)
1196 1202
1197 1203 def test_put_update_with_no_change
1198 1204 issue = Issue.find(1)
1199 1205 issue.journals.clear
1200 1206 ActionMailer::Base.deliveries.clear
1201 1207
1202 1208 put :update,
1203 1209 :id => 1,
1204 1210 :notes => ''
1205 1211 assert_redirected_to :action => 'show', :id => '1'
1206 1212
1207 1213 issue.reload
1208 1214 assert issue.journals.empty?
1209 1215 # No email should be sent
1210 1216 assert ActionMailer::Base.deliveries.empty?
1211 1217 end
1212 1218
1213 1219 def test_put_update_should_send_a_notification
1214 1220 @request.session[:user_id] = 2
1215 1221 ActionMailer::Base.deliveries.clear
1216 1222 issue = Issue.find(1)
1217 1223 old_subject = issue.subject
1218 1224 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
1219 1225
1220 1226 put :update, :id => 1, :issue => {:subject => new_subject,
1221 1227 :priority_id => '6',
1222 1228 :category_id => '1' # no change
1223 1229 }
1224 1230 assert_equal 1, ActionMailer::Base.deliveries.size
1225 1231 end
1226 1232
1227 1233 def test_put_update_with_invalid_spent_time_hours_only
1228 1234 @request.session[:user_id] = 2
1229 1235 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
1230 1236
1231 1237 assert_no_difference('Journal.count') do
1232 1238 put :update,
1233 1239 :id => 1,
1234 1240 :notes => notes,
1235 1241 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
1236 1242 end
1237 1243 assert_response :success
1238 1244 assert_template 'edit'
1239 1245
1240 1246 assert_error_tag :descendant => {:content => /Activity can't be blank/}
1241 1247 assert_tag :textarea, :attributes => { :name => 'notes' }, :content => notes
1242 1248 assert_tag :input, :attributes => { :name => 'time_entry[hours]', :value => "2z" }
1243 1249 end
1244 1250
1245 1251 def test_put_update_with_invalid_spent_time_comments_only
1246 1252 @request.session[:user_id] = 2
1247 1253 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
1248 1254
1249 1255 assert_no_difference('Journal.count') do
1250 1256 put :update,
1251 1257 :id => 1,
1252 1258 :notes => notes,
1253 1259 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
1254 1260 end
1255 1261 assert_response :success
1256 1262 assert_template 'edit'
1257 1263
1258 1264 assert_error_tag :descendant => {:content => /Activity can't be blank/}
1259 1265 assert_error_tag :descendant => {:content => /Hours can't be blank/}
1260 1266 assert_tag :textarea, :attributes => { :name => 'notes' }, :content => notes
1261 1267 assert_tag :input, :attributes => { :name => 'time_entry[comments]', :value => "this is my comment" }
1262 1268 end
1263 1269
1264 1270 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
1265 1271 issue = Issue.find(2)
1266 1272 @request.session[:user_id] = 2
1267 1273
1268 1274 put :update,
1269 1275 :id => issue.id,
1270 1276 :issue => {
1271 1277 :fixed_version_id => 4
1272 1278 }
1273 1279
1274 1280 assert_response :redirect
1275 1281 issue.reload
1276 1282 assert_equal 4, issue.fixed_version_id
1277 1283 assert_not_equal issue.project_id, issue.fixed_version.project_id
1278 1284 end
1279 1285
1280 1286 def test_put_update_should_redirect_back_using_the_back_url_parameter
1281 1287 issue = Issue.find(2)
1282 1288 @request.session[:user_id] = 2
1283 1289
1284 1290 put :update,
1285 1291 :id => issue.id,
1286 1292 :issue => {
1287 1293 :fixed_version_id => 4
1288 1294 },
1289 1295 :back_url => '/issues'
1290 1296
1291 1297 assert_response :redirect
1292 1298 assert_redirected_to '/issues'
1293 1299 end
1294 1300
1295 1301 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1296 1302 issue = Issue.find(2)
1297 1303 @request.session[:user_id] = 2
1298 1304
1299 1305 put :update,
1300 1306 :id => issue.id,
1301 1307 :issue => {
1302 1308 :fixed_version_id => 4
1303 1309 },
1304 1310 :back_url => 'http://google.com'
1305 1311
1306 1312 assert_response :redirect
1307 1313 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
1308 1314 end
1309 1315
1310 1316 def test_get_bulk_edit
1311 1317 @request.session[:user_id] = 2
1312 1318 get :bulk_edit, :ids => [1, 2]
1313 1319 assert_response :success
1314 1320 assert_template 'bulk_edit'
1315 1321
1316 1322 assert_tag :input, :attributes => {:name => 'issue[parent_issue_id]'}
1317 1323
1318 1324 # Project specific custom field, date type
1319 1325 field = CustomField.find(9)
1320 1326 assert !field.is_for_all?
1321 1327 assert_equal 'date', field.field_format
1322 1328 assert_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
1323 1329
1324 1330 # System wide custom field
1325 1331 assert CustomField.find(1).is_for_all?
1326 1332 assert_tag :select, :attributes => {:name => 'issue[custom_field_values][1]'}
1327 1333
1328 1334 # Be sure we don't display inactive IssuePriorities
1329 1335 assert ! IssuePriority.find(15).active?
1330 1336 assert_no_tag :option, :attributes => {:value => '15'},
1331 1337 :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
1332 1338 end
1333 1339
1334 1340 def test_get_bulk_edit_on_different_projects
1335 1341 @request.session[:user_id] = 2
1336 1342 get :bulk_edit, :ids => [1, 2, 6]
1337 1343 assert_response :success
1338 1344 assert_template 'bulk_edit'
1339 1345
1340 1346 # Can not set issues from different projects as children of an issue
1341 1347 assert_no_tag :input, :attributes => {:name => 'issue[parent_issue_id]'}
1342 1348
1343 1349 # Project specific custom field, date type
1344 1350 field = CustomField.find(9)
1345 1351 assert !field.is_for_all?
1346 1352 assert !field.project_ids.include?(Issue.find(6).project_id)
1347 1353 assert_no_tag :input, :attributes => {:name => 'issue[custom_field_values][9]'}
1348 1354 end
1349 1355
1350 1356 def test_get_bulk_edit_with_user_custom_field
1351 1357 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true)
1352 1358
1353 1359 @request.session[:user_id] = 2
1354 1360 get :bulk_edit, :ids => [1, 2]
1355 1361 assert_response :success
1356 1362 assert_template 'bulk_edit'
1357 1363
1358 1364 assert_tag :select,
1359 1365 :attributes => {:name => "issue[custom_field_values][#{field.id}]"},
1360 1366 :children => {
1361 1367 :only => {:tag => 'option'},
1362 1368 :count => Project.find(1).users.count + 1
1363 1369 }
1364 1370 end
1365 1371
1366 1372 def test_get_bulk_edit_with_version_custom_field
1367 1373 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true)
1368 1374
1369 1375 @request.session[:user_id] = 2
1370 1376 get :bulk_edit, :ids => [1, 2]
1371 1377 assert_response :success
1372 1378 assert_template 'bulk_edit'
1373 1379
1374 1380 assert_tag :select,
1375 1381 :attributes => {:name => "issue[custom_field_values][#{field.id}]"},
1376 1382 :children => {
1377 1383 :only => {:tag => 'option'},
1378 1384 :count => Project.find(1).versions.count + 1
1379 1385 }
1380 1386 end
1381 1387
1382 1388 def test_bulk_update
1383 1389 @request.session[:user_id] = 2
1384 1390 # update issues priority
1385 1391 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
1386 1392 :issue => {:priority_id => 7,
1387 1393 :assigned_to_id => '',
1388 1394 :custom_field_values => {'2' => ''}}
1389 1395
1390 1396 assert_response 302
1391 1397 # check that the issues were updated
1392 1398 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
1393 1399
1394 1400 issue = Issue.find(1)
1395 1401 journal = issue.journals.find(:first, :order => 'created_on DESC')
1396 1402 assert_equal '125', issue.custom_value_for(2).value
1397 1403 assert_equal 'Bulk editing', journal.notes
1398 1404 assert_equal 1, journal.details.size
1399 1405 end
1400 1406
1401 1407 def test_bulk_update_with_group_assignee
1402 1408 group = Group.find(11)
1403 1409 project = Project.find(1)
1404 1410 project.members << Member.new(:principal => group, :roles => [Role.first])
1405 1411
1406 1412 @request.session[:user_id] = 2
1407 1413 # update issues assignee
1408 1414 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
1409 1415 :issue => {:priority_id => '',
1410 1416 :assigned_to_id => group.id,
1411 1417 :custom_field_values => {'2' => ''}}
1412 1418
1413 1419 assert_response 302
1414 1420 assert_equal [group, group], Issue.find_all_by_id([1, 2]).collect {|i| i.assigned_to}
1415 1421 end
1416 1422
1417 1423 def test_bulk_update_on_different_projects
1418 1424 @request.session[:user_id] = 2
1419 1425 # update issues priority
1420 1426 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
1421 1427 :issue => {:priority_id => 7,
1422 1428 :assigned_to_id => '',
1423 1429 :custom_field_values => {'2' => ''}}
1424 1430
1425 1431 assert_response 302
1426 1432 # check that the issues were updated
1427 1433 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
1428 1434
1429 1435 issue = Issue.find(1)
1430 1436 journal = issue.journals.find(:first, :order => 'created_on DESC')
1431 1437 assert_equal '125', issue.custom_value_for(2).value
1432 1438 assert_equal 'Bulk editing', journal.notes
1433 1439 assert_equal 1, journal.details.size
1434 1440 end
1435 1441
1436 1442 def test_bulk_update_on_different_projects_without_rights
1437 1443 @request.session[:user_id] = 3
1438 1444 user = User.find(3)
1439 1445 action = { :controller => "issues", :action => "bulk_update" }
1440 1446 assert user.allowed_to?(action, Issue.find(1).project)
1441 1447 assert ! user.allowed_to?(action, Issue.find(6).project)
1442 1448 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
1443 1449 :issue => {:priority_id => 7,
1444 1450 :assigned_to_id => '',
1445 1451 :custom_field_values => {'2' => ''}}
1446 1452 assert_response 403
1447 1453 assert_not_equal "Bulk should fail", Journal.last.notes
1448 1454 end
1449 1455
1450 1456 def test_bullk_update_should_send_a_notification
1451 1457 @request.session[:user_id] = 2
1452 1458 ActionMailer::Base.deliveries.clear
1453 1459 post(:bulk_update,
1454 1460 {
1455 1461 :ids => [1, 2],
1456 1462 :notes => 'Bulk editing',
1457 1463 :issue => {
1458 1464 :priority_id => 7,
1459 1465 :assigned_to_id => '',
1460 1466 :custom_field_values => {'2' => ''}
1461 1467 }
1462 1468 })
1463 1469
1464 1470 assert_response 302
1465 1471 assert_equal 2, ActionMailer::Base.deliveries.size
1466 1472 end
1467 1473
1468 1474 def test_bulk_update_status
1469 1475 @request.session[:user_id] = 2
1470 1476 # update issues priority
1471 1477 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
1472 1478 :issue => {:priority_id => '',
1473 1479 :assigned_to_id => '',
1474 1480 :status_id => '5'}
1475 1481
1476 1482 assert_response 302
1477 1483 issue = Issue.find(1)
1478 1484 assert issue.closed?
1479 1485 end
1480 1486
1481 1487 def test_bulk_update_parent_id
1482 1488 @request.session[:user_id] = 2
1483 1489 post :bulk_update, :ids => [1, 3],
1484 1490 :notes => 'Bulk editing parent',
1485 1491 :issue => {:priority_id => '', :assigned_to_id => '', :status_id => '', :parent_issue_id => '2'}
1486 1492
1487 1493 assert_response 302
1488 1494 parent = Issue.find(2)
1489 1495 assert_equal parent.id, Issue.find(1).parent_id
1490 1496 assert_equal parent.id, Issue.find(3).parent_id
1491 1497 assert_equal [1, 3], parent.children.collect(&:id).sort
1492 1498 end
1493 1499
1494 1500 def test_bulk_update_custom_field
1495 1501 @request.session[:user_id] = 2
1496 1502 # update issues priority
1497 1503 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
1498 1504 :issue => {:priority_id => '',
1499 1505 :assigned_to_id => '',
1500 1506 :custom_field_values => {'2' => '777'}}
1501 1507
1502 1508 assert_response 302
1503 1509
1504 1510 issue = Issue.find(1)
1505 1511 journal = issue.journals.find(:first, :order => 'created_on DESC')
1506 1512 assert_equal '777', issue.custom_value_for(2).value
1507 1513 assert_equal 1, journal.details.size
1508 1514 assert_equal '125', journal.details.first.old_value
1509 1515 assert_equal '777', journal.details.first.value
1510 1516 end
1511 1517
1512 1518 def test_bulk_update_unassign
1513 1519 assert_not_nil Issue.find(2).assigned_to
1514 1520 @request.session[:user_id] = 2
1515 1521 # unassign issues
1516 1522 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
1517 1523 assert_response 302
1518 1524 # check that the issues were updated
1519 1525 assert_nil Issue.find(2).assigned_to
1520 1526 end
1521 1527
1522 1528 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
1523 1529 @request.session[:user_id] = 2
1524 1530
1525 1531 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
1526 1532
1527 1533 assert_response :redirect
1528 1534 issues = Issue.find([1,2])
1529 1535 issues.each do |issue|
1530 1536 assert_equal 4, issue.fixed_version_id
1531 1537 assert_not_equal issue.project_id, issue.fixed_version.project_id
1532 1538 end
1533 1539 end
1534 1540
1535 1541 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
1536 1542 @request.session[:user_id] = 2
1537 1543 post :bulk_update, :ids => [1,2], :back_url => '/issues'
1538 1544
1539 1545 assert_response :redirect
1540 1546 assert_redirected_to '/issues'
1541 1547 end
1542 1548
1543 1549 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
1544 1550 @request.session[:user_id] = 2
1545 1551 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
1546 1552
1547 1553 assert_response :redirect
1548 1554 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
1549 1555 end
1550 1556
1551 1557 def test_destroy_issue_with_no_time_entries
1552 1558 assert_nil TimeEntry.find_by_issue_id(2)
1553 1559 @request.session[:user_id] = 2
1554 1560 post :destroy, :id => 2
1555 1561 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1556 1562 assert_nil Issue.find_by_id(2)
1557 1563 end
1558 1564
1559 1565 def test_destroy_issues_with_time_entries
1560 1566 @request.session[:user_id] = 2
1561 1567 post :destroy, :ids => [1, 3]
1562 1568 assert_response :success
1563 1569 assert_template 'destroy'
1564 1570 assert_not_nil assigns(:hours)
1565 1571 assert Issue.find_by_id(1) && Issue.find_by_id(3)
1566 1572 end
1567 1573
1568 1574 def test_destroy_issues_and_destroy_time_entries
1569 1575 @request.session[:user_id] = 2
1570 1576 post :destroy, :ids => [1, 3], :todo => 'destroy'
1571 1577 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1572 1578 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1573 1579 assert_nil TimeEntry.find_by_id([1, 2])
1574 1580 end
1575 1581
1576 1582 def test_destroy_issues_and_assign_time_entries_to_project
1577 1583 @request.session[:user_id] = 2
1578 1584 post :destroy, :ids => [1, 3], :todo => 'nullify'
1579 1585 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1580 1586 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1581 1587 assert_nil TimeEntry.find(1).issue_id
1582 1588 assert_nil TimeEntry.find(2).issue_id
1583 1589 end
1584 1590
1585 1591 def test_destroy_issues_and_reassign_time_entries_to_another_issue
1586 1592 @request.session[:user_id] = 2
1587 1593 post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
1588 1594 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
1589 1595 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
1590 1596 assert_equal 2, TimeEntry.find(1).issue_id
1591 1597 assert_equal 2, TimeEntry.find(2).issue_id
1592 1598 end
1593 1599
1594 1600 def test_destroy_issues_from_different_projects
1595 1601 @request.session[:user_id] = 2
1596 1602 post :destroy, :ids => [1, 2, 6], :todo => 'destroy'
1597 1603 assert_redirected_to :controller => 'issues', :action => 'index'
1598 1604 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
1599 1605 end
1600 1606
1601 1607 def test_destroy_parent_and_child_issues
1602 1608 parent = Issue.generate!(:project_id => 1, :tracker_id => 1)
1603 1609 child = Issue.generate!(:project_id => 1, :tracker_id => 1, :parent_issue_id => parent.id)
1604 1610 assert child.is_descendant_of?(parent.reload)
1605 1611
1606 1612 @request.session[:user_id] = 2
1607 1613 assert_difference 'Issue.count', -2 do
1608 1614 post :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
1609 1615 end
1610 1616 assert_response 302
1611 1617 end
1612 1618
1613 1619 def test_default_search_scope
1614 1620 get :index
1615 1621 assert_tag :div, :attributes => {:id => 'quick-search'},
1616 1622 :child => {:tag => 'form',
1617 1623 :child => {:tag => 'input', :attributes => {:name => 'issues', :type => 'hidden', :value => '1'}}}
1618 1624 end
1619 1625 end
General Comments 0
You need to be logged in to leave comments. Login now