cirandas.net

ref: master

test/functional/tasks_controller_test.rb


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

class TasksControllerTest < ActionController::TestCase

  self.default_params = {profile: 'testuser'}

  def setup
    @controller = TasksController.new

    self.profile = create_user('testuser').person
    @controller.stubs(:profile).returns(profile)
    login_as 'testuser'
  end
  attr_accessor :profile

  def assert_redirected_to(options)
    super({ :controller => 'tasks', :profile => profile.identifier }.merge(options))
  end

  should 'list pending tasks' do
    get :index

    assert_response :success
    assert_template 'index'
    assert assigns(:tasks)
  end

 should 'get filtered tasks to autocomplete text field' do

   #Create a admin user and a simple user
   profile_admin = create_user('admin_tester').person
   Environment.default.add_admin(profile_admin)
   user = fast_create(Person,:name => 'FakeUser')

   #Create a task of type 'ModerateUserRegistration'
   task_data = {
       :target => Environment.default,
       :spam => false,
       :data => {:user_id => user.id,:name => user.name}
   }
   ModerateUserRegistration.create!(task_data)

   #Use admin user to your profile with a pending task above
   @controller.stubs(:profile).returns(profile_admin)
   login_as profile_admin.identifier

   #Perform a http request to 'search_task' action with params
   post :search_tasks, :filter_type =>'ModerateUserRegistration', :filter_text => 'Fak'

   assert_response :success

   #Check if json response matches with a 'FakeUser'
   json_response = ActiveSupport::JSON.decode(@response.body)
   value = json_response[0]['value']

   assert_equal value, 'FakeUser'
 end

  should 'list pending tasks without spam' do
    requestor = fast_create(Person)
    task_spam = Task.create!(:requestor => requestor, :target => profile, :spam => true)
    task_ham = Task.create!(:requestor => requestor, :target => profile, :spam => false)

    get :index
    assert_response :success
    assert_includes assigns(:tasks), task_ham
    assert_not_includes assigns(:tasks), task_spam
  end

  should 'list processed tasks' do
    get :processed

    assert_response :success
    assert_template 'processed'
    assert !assigns(:tasks).nil?
    assert_kind_of ActiveRecord::Relation, assigns(:tasks)
  end

  should 'display task created_at' do
    Task.create!(:requestor => fast_create(Person), :target => profile, :spam => false)
    get :index
    assert_select '.task_date'
  end

  should 'list processed tasks without spam' do
    requestor = fast_create(Person)
    task_spam = create(Task, :status => Task::Status::FINISHED, :requestor => requestor, :target => profile, :spam => true)
    task_ham = create(Task, :status => Task::Status::FINISHED, :requestor => requestor, :target => profile, :spam => false)

    get :processed
    assert_response :success
    assert_includes assigns(:tasks), task_ham
    assert_not_includes assigns(:tasks), task_spam
  end

  should 'be able to finish a task' do
    t = profile.tasks.build; t.save!

    post :close, :tasks => {t.id => {:decision => 'finish', :task => {}}}
    assert_redirected_to :action => 'index'

    t.reload
    ok('task should be finished') { t.status == Task::Status::FINISHED }
  end

  should 'keep filters after close a task' do
    t = profile.tasks.build; t.save!

    post :close, :tasks => {t.id => {:decision => 'finish', :task => {}}}, :filter_type => t.type
    assert_redirected_to :action => 'index', :filter_type => t.type
    assert_equal @controller.params[:filter_type], t.type

    t.reload
    ok('task should be finished') { t.status == Task::Status::FINISHED }
  end

  should 'be able to cancel a task' do
    t = profile.tasks.build; t.save!

    post :close, :tasks => {t.id => {:decision => 'cancel', :task => {}}}
    assert_redirected_to :action => 'index'

    t.reload
    ok('task should be cancelled') { t.status == Task::Status::CANCELLED }
  end

  should 'be able to skip a task' do
    t = profile.tasks.build; t.save!

    post :close, :tasks => {t.id => {:decision => 'skip', :task => {}}}
    assert_redirected_to :action => 'index'

    t.reload
    ok('task should be skipped') { t.status == Task::Status::ACTIVE }
  end

  should 'be able to apply different decisions to multiples tasks at the same time' do
    t1 = profile.tasks.build; t1.save!
    t2 = profile.tasks.build; t2.save!
    t3 = profile.tasks.build; t3.save!

    post :close, :tasks => {t1.id => {:decision => 'finish', :task => {}}, t2.id => {:decision => 'cancel', :task => {}}, t3.id => {:decision => 'skip', :task => {}}}
    assert_redirected_to :action => 'index'

    t1.reload
    t2.reload
    t3.reload

    ok('task should be finished') { t1.status == Task::Status::FINISHED }
    ok('task should be canceled') { t2.status == Task::Status::CANCELLED }
    ok('task should be skipped')  { t3.status == Task::Status::ACTIVE }
  end

  should 'affiliate roles to user after finish add member task' do
    c = fast_create(Community)
    p = create_user('member').person

    @controller.stubs(:profile).returns(c)
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))

    t = AddMember.create!(:person => p, :organization => c)

    count = c.members.size

    post :close, :tasks => {t.id => {:decision => 'finish', :task => {}}}
    t.reload

    ok('task should be finished') { t.status == Task::Status::FINISHED }

    c.reload
    assert_equal count + 1, c.members.size
  end

  should 'display a create ticket form' do
    get :new, :profile => profile.identifier

    assert_template 'new'
  end

  should 'add a hidden field with target_id when informed in the URL' do
    friend = create_user('myfriend').person
    profile.add_friend(friend)

    get :new, :profile => profile.identifier, :target_id => friend.id.to_s

    assert_tag :tag => 'input', :attributes => { :type => 'hidden', :name => 'ticket[target_id]', :value => friend.id }
  end

  should 'select friend from list when not already informed' do
    get :new, :profile => profile.identifier
    assert_tag :tag => 'select', :attributes => { :name =>  'ticket[target_id]' }
  end

  should 'create a ticket' do
    assert_difference 'Ticket.count' do
      post :new, :profile => profile.identifier, :ticket => {:name => 'test ticket'}
    end
  end

  should 'create a ticket with profile requestor' do
    post :new, :profile => profile.identifier, :ticket => {:name => 'new task'}

    assert_equal profile, assigns(:ticket).requestor
  end

  should 'list tasks that this profile created' do
    task = Ticket.create!(:name => 'test', :requestor => profile)
    get :list_requested, :profile => profile.identifier

    assert_includes assigns(:tasks), task
  end

  should 'list tasks that this profile created without spam' do
    task_spam = Ticket.create!(:name => 'test', :requestor => profile, :spam => true)
    task_ham = Ticket.create!(:name => 'test', :requestor => profile, :spam => false)
    get :list_requested, :profile => profile.identifier

    assert_includes assigns(:tasks), task_ham
    assert_not_includes assigns(:tasks), task_spam
  end

  should 'set target of ticket when creating it' do
     f = create_user('friend').person
     profile.add_friend f

     post :new, :profile => profile.identifier, :ticket => {:name => 'test ticket', :target_id => f.id, :target_type => 'Profile'}
     assert_response :redirect

     assert_equal f, assigns(:ticket).target
  end

  should 'create article with reference_article after finish approve article task' do
    c = fast_create(Community)
    c.update(:moderated_articles => false)
    @controller.stubs(:profile).returns(c)
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    article = profile.articles.create!(:name => 'something interesting', :body => 'ruby on rails')
    t = ApproveArticle.create!(:name => 'test name', :article => article, :target => c, :requestor => profile)

    post :close, :tasks => {t.id => {:decision => 'finish', :task => {:name => 'new_name'}}}
    assert_equal article, c.articles.find_by(name: 'new_name').reference_article
  end

  should 'create published article in folder after finish approve article task' do
    c = fast_create(Community)
    c.update(:moderated_articles => false)
    @controller.stubs(:profile).returns(c)
    folder = create(Folder, :profile => c, :name => 'test folder')
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    article = profile.articles.create!(:name => 'something interesting', :body => 'ruby on rails')
    t = ApproveArticle.create!(:name => 'test name', :article => article, :target => c, :requestor => profile)

    post :close, :tasks => {t.id => {:decision => 'finish', :task => {:name => 'new_name', :article_parent_id => folder.id}}}
    assert_equal folder, c.articles.find_by(name: 'new_name').parent
  end

  should 'be highlighted if asked when approving a published article' do
    c = fast_create(Community)
    c.update(:moderated_articles => false)
    @controller.stubs(:profile).returns(c)
    folder = create(Article, :profile => c, :name => 'test folder', :type => 'Folder')
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    article = profile.articles.create!(:name => 'something interesting', :body => 'ruby on rails')
    t = ApproveArticle.create!(:article => article, :target => c, :requestor => profile)

    post :close, :tasks => {t.id => {:decision => 'finish', :task => {:name => 'new_name', :article_parent_id => folder.id, :highlighted => true}}}
    assert_equal true, c.articles.find_by(name: 'new_name').highlighted
  end

  should 'create article of same class after choosing root folder on approve article task' do
    c = fast_create(Community)
    c.update(:moderated_articles => false)
    @controller.stubs(:profile).returns(c)
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    article = profile.articles.create!(:name => 'something interesting', :body => 'ruby on rails')
    t = ApproveArticle.create!(:article => article, :target => c, :requestor => profile)

    post :close, :tasks => {t.id => {:decision => 'finish', :task => {:name => 'new_name', :article_parent_id => ""}}}
    assert_not_nil c.articles.find_by(name: 'new_name')
  end

  should 'handle blank names for published articles' do
    c = fast_create(Community)
    @controller.stubs(:profile).returns(c)
    c.affiliate(profile, Profile::Roles.all_roles(c.environment))
    person = create_user('test_user').person
    c.add_member(person)
    p_blog = Blog.create!(:profile => person, :name => 'Blog')
    c_blog1 = Blog.create!(:profile => c, :name => 'Blog')
    c_blog2 = Blog.new(:profile => c); c_blog2.name = 'blog2'; c_blog2.save!

    article = person.articles.create!(:name => 'test article', :parent => p_blog)
    a = ApproveArticle.create!(:article => article, :target => c, :requestor => person)
    assert_includes c.tasks, a

    assert_difference 'article.class.count' do
      post :close, :tasks => {a.id => {:decision => 'finish', :task => {:name => "", :highlighted => "0", :article_parent_id => c_blog2.id.to_s}}}
    end
    assert p_article = article.class.find_by(reference_article_id: article.id)
    assert_includes c_blog2.children(true), p_article
  end

  should 'display error if there is an enterprise with the same identifier and keep the task active' do
    e = Environment.default
    e.add_admin(profile)
    task = CreateEnterprise.create!(:name => "My Enterprise", :identifier => "my-enterprise", :requestor => profile, :target => e)
    enterprise = fast_create(Enterprise, :name => "My Enterprise", :identifier => "my-enterprise")

    assert_nothing_raised do
      post :close, :tasks => {task.id => {:decision => "finish"}}
    end

    assert_match /Validation.failed/, @response.body

    task.reload
    assert_equal Task::Status::ACTIVE, task.status
  end

  should 'render TinyMce Editor when approving suggested article task' do
    Task.destroy_all
    c = fast_create(Community)
    c.add_admin profile
    @controller.stubs(:profile).returns(c)
    t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c)

    get :index
    assert_tag :tag => 'textarea', :content => /test abstract/, :attributes => { :name => /tasks\[#{t.id}\]\[task\]\[article\]\[abstract\]/, :class => Article::Editor::TINY_MCE }
    assert_tag :tag => 'textarea', :content => /test body/, :attributes => { :name => /tasks\[#{t.id}\]\[task\]\[article\]\[body\]/, :class => Article::Editor::TINY_MCE }
  end

  should 'create TextArticle article after finish approve suggested article task' do
    TextArticle.destroy_all
    c = fast_create(Community)
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    @controller.stubs(:profile).returns(c)
    t = SuggestArticle.create!(:article => {:name => 'test name', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c)

    post :close, :tasks => {t.id => { :task => {}, :decision => "finish"}}
    assert_not_nil TextArticle.first
  end

  should "change the article's attributes on suggested article task approval" do
    TextArticle.destroy_all
    c = fast_create(Community)
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    @controller.stubs(:profile).returns(c)
    t = SuggestArticle.new
    t.article = {:name => 'test name', :body => 'test body', :source => 'http://test.com', :source_name => 'some source name'}
    t.name = 'some name'
    t.email = 'test@localhost.com'
    t.target = c
    t.save!

    post :close, :tasks => {t.id => { :task => {:article => {:name => 'new article name', :body => 'new body', :source => 'http://www.noosfero.com', :source_name => 'new source'}, :name => 'new name'}, :decision => "finish"}}
    assert_equal 'new article name', TextArticle.first.name
    assert_equal 'new name', TextArticle.first.author_name
    assert_equal 'new body', TextArticle.first.body
    assert_equal 'http://www.noosfero.com', TextArticle.first.source
    assert_equal 'new source', TextArticle.first.source_name
  end

  should "display name from article suggestion when requestor was not setted" do
    Task.destroy_all
    c = fast_create(Community)
    c.add_admin profile
    @controller.stubs(:profile).returns(c)
    t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c)

    get :index
    assert_select "#tasks_#{t.id}_task_name"
  end

  should "not crash when article suggestion task fails" do
    TextArticle.destroy_all
    c = fast_create(Community)
    c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id))
    @controller.stubs(:profile).returns(c)
    t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :name => 'some name', :email => 'test@localhost.com', :target => c)

    SuggestArticle.any_instance.stubs(:perform).raises('erro')
    assert_nothing_raised do
      post :close, :tasks => {t.id => { :task => {}, :decision => "finish"}}
    end
  end

  should "append hidden tag with type value from article suggestion" do
    Task.destroy_all
    c = fast_create(Community)
    c.add_admin profile
    @controller.stubs(:profile).returns(c)
    t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body', :type => 'TextArticle'}, :name => 'some name', :email => 'test@localhost.com', :target => c)

    get :index
    assert_select "#tasks_#{t.id}_task_article_type[value=TextArticle]"
  end

  should "display parent_id selection from article suggestion with predefined value" do
    Task.destroy_all
    c = fast_create(Community)
    c.add_admin profile
    @controller.stubs(:profile).returns(c)
    parent = fast_create(Folder, :profile_id => c.id)
    t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body', :parent_id => parent.id}, :name => 'some name', :email => 'test@localhost.com', :target => c)

    get :index
    assert_select "#tasks_#{t.id}_task_article_parent_id option[value=#{parent.id}]"
  end

  should "not display name from article suggestion when requestor was setted" do
    Task.destroy_all
    c = fast_create(Community)
    c.add_admin profile
    @controller.stubs(:profile).returns(c)
    t = SuggestArticle.create!(:article => {:name => 'test name', :abstract => 'test abstract', :body => 'test body'}, :requestor => fast_create(Person), :target => c)

    get :index
    assert_select "#tasks_#{t.id}_task_name", 0
  end

  should "not crash if accessing close without tasks parameter" do
    assert_nothing_raised do
      post :close
    end
  end

  should 'close create enterprise if trying to cancel even if there is already an existing identifier' do
    identifier = "common-identifier"
    task = CreateEnterprise.create!(:identifier => identifier, :name => identifier, :requestor => profile, :target => profile)
    fast_create(Profile, :identifier => identifier)

    assert_nothing_raised do
      post :close, :tasks => {task.id => {:task => {:reject_explanation => "Some explanation"}, :decision => 'cancel'}}
    end

    task.reload
    assert_equal Task::Status::CANCELLED, task.status
  end

  should 'filter tasks by type' do
    class CleanHouse < Task; end
    class FeedDog < Task; end
    Task.stubs(:per_page).returns(3)
    requestor = fast_create(Person)
    t1 = CleanHouse.create!(:requestor => requestor, :target => profile)
    t2 = CleanHouse.create!(:requestor => requestor, :target => profile)
    t3 = FeedDog.create!(:requestor => requestor, :target => profile)

    post :index, :filter_type => t1.type

    assert_includes assigns(:tasks), t1
    assert_includes assigns(:tasks), t2
    assert_not_includes assigns(:tasks), t3

    post :index

    assert_includes assigns(:tasks), t1
    assert_includes assigns(:tasks), t2
    assert_includes assigns(:tasks), t3
  end

  should 'filter tasks by type and data content' do
    class CleanHouse < Task; end
    class FeedDog < Task; end
    Task.stubs(:per_page).returns(3)
    requestor = fast_create(Person)
    t1 = CleanHouse.create!(:requestor => requestor, :target => profile, :data => {:name => 'Task Test'})
    t2 = CleanHouse.create!(:requestor => requestor, :target => profile)
    t3 = FeedDog.create!(:requestor => requestor, :target => profile)

    post :index, :filter_type => t1.type, :filter_text => 'test'

    assert_includes assigns(:tasks), t1
    assert_not_includes assigns(:tasks), t2
    assert_not_includes assigns(:tasks), t3

    post :index

    assert_includes assigns(:tasks), t1
    assert_includes assigns(:tasks), t2
    assert_includes assigns(:tasks), t3
  end

  should 'return tasks ordered accordingly and limited by pages' do
    time = Time.now
    person = fast_create(Person)
    t1 = create(Task, :status => Task::Status::ACTIVE, :target => profile, :requestor => person, :created_at => time)
    t2 = create(Task, :status => Task::Status::ACTIVE, :target => profile, :requestor => person, :created_at => time + 1.second)
    t3 = create(Task, :status => Task::Status::ACTIVE, :target => profile, :requestor => person, :created_at => time + 2.seconds)
    t4 = create(Task, :status => Task::Status::ACTIVE, :target => profile, :requestor => person, :created_at => time + 3.seconds)

    Task.stubs(:per_page).returns(2)

    post :index, :page => 1
    assert_equal [t1,t2], assigns(:tasks)

    Task.stubs(:per_page).returns(3)
    post :index, :page => 2
    assert_equal [t4], assigns(:tasks)
  end

  should 'filter tasks by responsible' do
    Task.stubs(:per_page).returns(3)
    requestor = fast_create(Person)
    responsible = fast_create(Person)
    t1 = Task.create!(:requestor => requestor, :target => profile, :responsible => responsible)
    t2 = Task.create!(:requestor => requestor, :target => profile, :responsible => responsible)
    t3 = Task.create!(:requestor => requestor, :target => profile)

    get :index, :filter_responsible => responsible.id

    assert_includes assigns(:tasks), t1
    assert_includes assigns(:tasks), t2
    assert_not_includes assigns(:tasks), t3

    get :index

    assert_includes assigns(:tasks), t1
    assert_includes assigns(:tasks), t2
    assert_includes assigns(:tasks), t3
  end

  should 'do not display responsible assignment if profile is not an organization' do
    profile = create_user('personprofile').person
    t1 = Task.create!(:requestor => profile, :target => profile)
    @controller.stubs(:profile).returns(profile)
    login_as profile.user.login
    get :index

    assert_select "#task-#{t1.id}"
    assert_select '.task_responsible', 0
  end

  should 'do not display responsible assignment filter if profile is not an organization' do
    profile = create_user('personprofile').person
    @controller.stubs(:profile).returns(profile)
    login_as profile.user.login
    get :index

    assert_select '.filter_responsible', 0
  end

  should 'display responsible assignment if profile is an organization' do
    profile = fast_create(Community)
    person1 = create_user('person1').person
    person2 = create_user('person2').person
    person3 = create_user('person3').person
    profile.add_admin(person1)
    profile.add_admin(person2)
    profile.add_member(person3)
    Task.create!(:requestor => person3, :target => profile)
    @controller.stubs(:profile).returns(profile)

    login_as person1.user.login
    get :index
    assert_equivalent [person1, person2], assigns(:responsible_candidates)
    assert_select '.task_responsible'
  end

  should 'change task responsible' do
    profile = fast_create(Community)
    @controller.stubs(:profile).returns(profile)
    person = create_user('person1').person
    profile.add_admin(person)
    task = Task.create!(:requestor => person, :target => profile)

    assert_equal nil, task.responsible
    login_as person.user.login
    post :change_responsible, :task_id => task.id, :responsible_id => person.id
    assert_equal person, task.reload.responsible
  end

  should 'not change task responsible if old responsible is not the current' do
    profile = fast_create(Community)
    @controller.stubs(:profile).returns(profile)
    person1 = create_user('person1').person
    person2 = create_user('person2').person
    profile.add_admin(person1)
    task = Task.create!(:requestor => person1, :target => profile, :responsible => person1)

    login_as person1.user.login
    post :change_responsible, :task_id => task.id, :responsible_id => person2.id, :old_responsible => nil
    assert_equal person1, task.reload.responsible
    json_response = ActiveSupport::JSON.decode(response.body)
    refute json_response['success']
  end

  should 'list tasks for user with only view_tasks permission' do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskviewer', 'view_tasks', community)
    login_as person.user.login
    get :index
    assert_response :success
    assert assigns(:view_only)
  end

  should 'forbid user with only view_tasks permission to close a task' do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskviewer', 'view_tasks', community)
    login_as person.user.login
    post :close
    assert_response 403
  end

  should 'hide tasks actions when user has only view_tasks permission' do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskviewer', 'view_tasks', community)
    login_as person.user.login

    Task.create!(:requestor => person, :target => community)
    get :index

    assert_select '.task-actions', 0
  end

  should 'display tasks actions when user has perform_task permission' do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskperformer', 'perform_task', community)
    login_as person.user.login

    Task.create!(:requestor => person, :target => community)
    get :index

    assert_select '.task-actions', 2
  end

  should 'hide decision selector when user has only view_tasks permission' do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskviewer', 'view_tasks', community)
    login_as person.user.login

    Task.create!(:requestor => person, :target => community)
    get :index

    assert_select '#up-set-all-tasks-to', 0
    assert_select '#down-set-all-tasks-to', 0
  end

  should 'display decision selector when user has perform_task permission' do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskperformer', 'perform_task', community)
    login_as person.user.login

    Task.create!(:requestor => person, :target => community)
    get :index

    assert_select '#up-set-all-tasks-to'
    assert_select '#down-set-all-tasks-to'
  end

  should 'hide decision buttons when user has only view_tasks permission' do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskviewer', 'view_tasks', community)
    login_as person.user.login

    task = Task.create!(:requestor => person, :target => community)
    get :index

    assert_select "#decision-finish-#{task.id}", 0
    assert_select "#decision-cancel-#{task.id}", 0
    assert_select "#decision-skip-#{task.id}", 0
  end

  should 'display decision buttons when user has perform_task permission' do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskperformer', 'perform_task', community)
    login_as person.user.login

    task = Task.create!(:requestor => person, :target => community)
    get :index

    assert_select "#decision-finish-#{task.id}"
    assert_select "#decision-cancel-#{task.id}"
    assert_select "#decision-skip-#{task.id}"
  end

  should 'hide responsive selection when user has only view_tasks permission' do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskviewer', 'view_tasks', community)
    login_as person.user.login

    task = Task.create!(:requestor => person, :target => community, :responsible => person)
    get :index

    assert_select ".task_responsible select", 0
    assert_select ".task_responsible .value"
  end

  should 'store the person who closes a task' do
    t = profile.tasks.build; t.save!
    post :close, :tasks => {t.id => {:decision => 'finish', :task => {}}}
    assert_equal profile, t.reload.closed_by
  end

  should 'list custom field details in moderation user tasks when moderation_tasks is true' do
    person_custom_field = CustomField.create(:name => "great_field", :format=>"string", :default_value => "value for person", :customized_type=>"Person", :active => true, :environment => Environment.default, :moderation_task => true, :required => true)
    p1 = create_user("great_person").person
    p1.custom_values = {"great_field" => "new_value!"}
    p1.save!
    p1.reload
    admin = create_user("admin").person
    Environment.default.add_admin(admin)
    @controller.stubs(:profile).returns(admin)
    login_as "admin"

    ModerateUserRegistration.create!(:requestor => p1, :name => "great_person", :email => "alo@alo.alo", :target => Environment.default)

    get :index

    assert_tag :tag=> 'div', :attributes => { :class => 'field-name' }, :content => /great_field: new_value!/
  end

  should 'list custom fields in moderation email if moderation tasks if true' do
    person_custom_field = CustomField.create(:name => "registration_reason", :format=>"string", :default_value => "because i want to", :customized_type=>"Person", :active => true, :environment => Environment.default, :moderation_task => true, :required => true)
    p1 = create_user("great_person").person
    p1.custom_values = {"registration_reason" => "I want to send my TCC"}
    p1.save!
    p1.reload
    admin = create_user("admin").person
    Environment.default.add_admin(admin)

    task = ModerateUserRegistration.create!(:requestor => p1, :name => "great_person", :email => "alo@alo.alo", :target => Environment.default)
    email = ActionMailer::Base.deliveries.last
    assert_match /#{task.target_custom_fields}/, email.body.to_s
  end

  should 'not list custom fields in moderation email if moderation tasks is false' do
    person_custom_field = CustomField.create(:name => "registration_reason", :format=>"string", :default_value => "because i want to", :customized_type=>"Person", :active => true, :environment => Environment.default, :moderation_task => false, :required => true)
    p1 = create_user("great_person").person
    p1.custom_values = {"registration_reason" => "I want to send my TCC"}
    p1.save!
    p1.reload
    admin = create_user("admin").person
    Environment.default.add_admin(admin)

    task = ModerateUserRegistration.create!(:requestor => p1, :name => "great_person", :email => "alo@alo.alo", :target => Environment.default)
    email = ActionMailer::Base.deliveries.last
    assert_no_match /#{person_custom_field.name}/, email.body.to_s
  end


  should 'list custom field details in moderation of community creation tasks when moderation_tasks is true' do
    community_custom_field = CustomField.create(:name => "great_field", :format=>"string", :default_value => "value for community", :customized_type=>"Community", :active => true, :environment => Environment.default, :moderation_task => true, :required => true)
    p1 = create_user("great_person").person
    p1.save!
    admin = create_user("admin").person
    Environment.default.add_admin(admin)
    @controller.stubs(:profile).returns(admin)
    login_as "admin"

    CreateCommunity.create!(:requestor => p1, :name => "great_community", :target => Environment.default, :custom_values => {"great_field" => {"value" => "new value for community!"}})

    get :index

    assert_tag :tag=> 'div', :attributes => { :class => 'field-name' }, :content => /great_field: new value for community!/
  end

  should "display email template selection when accept a task" do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskviewer', 'view_tasks', community)
    login_as person.user.login

    email_template = EmailTemplate.create!(:name => 'template', :owner => community, :template_type => :task_acceptance)
    task = ApproveArticle.create!(:requestor => person, :target => community, :responsible => person)
    get :index
    assert_select "#on-accept-information-#{task.id} .template-selection"
    assert_equal [email_template], assigns(:acceptance_email_templates)
  end

  should "display email template selection when reject a task" do
    community = fast_create(Community)
    @controller.stubs(:profile).returns(community)
    person = create_user_with_permission('taskviewer', 'view_tasks', community)
    login_as person.user.login

    email_template = EmailTemplate.create!(:name => 'template', :owner => community, :template_type => :task_rejection)
    task = ApproveArticle.create!(:requestor => person, :target => community, :responsible => person)
    get :index
    assert_select "#on-reject-information-#{task.id} .template-selection"
    assert_equal [email_template], assigns(:rejection_email_templates)
  end

  should 'filter processed tasks by all filters' do
    requestor = fast_create(Person)
    closed_by = fast_create(Person)
    class AnotherTask < Task; end

    created_date = DateTime.now
    processed_date = DateTime.now

    task_params = {:status => Task::Status::FINISHED, :requestor => requestor, :target => profile, :created_at => created_date, :end_date => processed_date, :closed_by => closed_by, :data => {:field => 'some data field'}}

    task = create(AnotherTask, task_params)
    create(Task, task_params)
    create(AnotherTask, task_params.clone.merge(:status => Task::Status::CANCELLED))
    create(AnotherTask, task_params.clone.merge(:created_at => created_date - 1.day))
    create(AnotherTask, task_params.clone.merge(:created_at => created_date + 1.day))
    create(AnotherTask, task_params.clone.merge(:end_date => processed_date - 1.day))
    create(AnotherTask, task_params.clone.merge(:end_date => processed_date + 1.day))
    create(AnotherTask, task_params.clone.merge(:requestor => fast_create(Person, :name => 'another-requestor')))
    create(AnotherTask, task_params.clone.merge(:closed_by => fast_create(Person, :name => 'another-closer')))
    create(AnotherTask, task_params.clone.merge(:data => {:field => "other data field"}))

    get :processed, :filter => {:type => AnotherTask, :status => Task::Status::FINISHED, :created_from => created_date, :created_until => created_date, :closed_from => processed_date, :closed_until => processed_date, :requestor => requestor.name, :closed_by => closed_by.name, :text => "some data field"}
    assert_response :success
    assert_equal [task], assigns(:tasks)
  end

end