comment_test.rb
24.7 KB
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
require_relative "../test_helper"
class CommentTest < ActiveSupport::TestCase
def setup
end
should 'have a name but not require it' do
assert_optional(Comment.new, :title)
end
should 'have a body and require it' do
assert_mandatory(Comment.new, :body)
end
should 'have a polymorphic relationship with source' do
c = Comment.new
assert_nothing_raised do
c.source = Article.new
end
assert_nothing_raised do
c.source = ActionTracker::Record.new
end
end
should 'record authenticated author' do
c = Comment.new
assert_raise ActiveRecord::AssociationTypeMismatch do
c.author = 1
end
assert_raise ActiveRecord::AssociationTypeMismatch do
c.author = Profile
end
assert_nothing_raised do
c.author = Person.new
end
end
should 'record unauthenticated author' do
assert_optional Comment.new, :name
assert_optional Comment.new, :email
# if given name, require email
c1 = Comment.new
c1.name = 'My Name'
assert_mandatory c1, :email, 'my@email.com'
# if given email, require name
c2 = Comment.new
c2.email = 'my@email.com'
assert_mandatory c2, :name
end
should 'accept either an authenticated or unauthenticated author' do
assert_mandatory Comment.new, :author_id
c1 = Comment.new
c1.author = create_user('someperson').person
c1.name = 'my name'
c1.valid?
assert c1.errors[:name.to_s].present?
assert_no_match /\{fn\}/, c1.errors[:name].first
end
should 'update counter cache in article' do
owner = create_user('testuser').person
art = create(TextileArticle, :profile_id => owner.id)
cc = art.comments_count
comment = create(Comment, :source => art, :author_id => owner.id)
assert_equal cc + 1, Article.find(art.id).comments_count
end
should 'update counter cache in article activity' do
owner = create_user('testuser').person
article = create(TextileArticle, :profile_id => owner.id)
action = article.activity
cc = action.comments_count
comment = create(Comment, :source => action, :author_id => owner.id)
assert_equal cc + 1, ActionTracker::Record.find(action.id).comments_count
end
should 'update counter cache in general activity when add a comment' do
person = fast_create(Person)
community = fast_create(Community)
activity = create ActionTracker::Record, :user => person, :target => community, :verb => 'add_member_in_community'
cc = activity.comments_count
comment = create(Comment, :source => activity, :author_id => person.id)
assert_equal cc + 1, ActionTracker::Record.find(activity.id).comments_count
end
should 'provide author name for authenticated authors' do
owner = create_user('testuser').person
assert_equal 'testuser', build(Comment, :author => owner).author_name
end
should 'provide author name for unauthenticated author' do
assert_equal 'anonymous coward', build(Comment, :name => 'anonymous coward').author_name
end
should 'provide empty text for author name if user was removed ' do
assert_equal '', build(Comment, :author_id => 9999).author_name
end
should "provide author e-mail for athenticated authors" do
owner = create_user('testuser').person
assert_equal owner.email, build(Comment, :author => owner).author_email
end
should "provide author e-mail for unauthenticated author" do
assert_equal 'my@email.com', build(Comment, :email => 'my@email.com').author_email
end
should 'provide author link for authenticated author' do
author = Person.new
author.expects(:url).returns('http://blabla.net/author')
assert_equal 'http://blabla.net/author', build(Comment, :author => author).author_link
end
should 'provide author e-mail as author link for unauthenticated author' do
assert_equal 'my@email.com', build(Comment, :email => 'my@email.com').author_link
end
should 'provide url to comment' do
art = Article.new
art.expects(:url).returns({ :controller => 'lala', :action => 'something' })
comment = build(Comment, :article => art)
comment.expects(:id).returns(9876)
assert_equal({ :controller => 'lala', :action => 'something', :anchor => 'comment-9876'}, comment.url)
end
should 'provide anchor' do
comment = Comment.new
comment.expects(:id).returns(4321)
assert_equal 'comment-4321', comment.anchor
end
should 'be able to find recent comments' do
Comment.delete_all
owner = create_user('testuser').person
art = owner.articles.build(:name => 'ytest'); art.save!
comments = []
3.times do
comments.unshift create(Comment, :article => art, :title => 'a test comment', :body => 'bla', :author => owner)
end
assert_equal comments, Comment.recent
end
should 'be able to find recent comments with limit' do
Comment.delete_all
owner = create_user('testuser').person
art = owner.articles.build(:name => 'ytest'); art.save!
comments = []
3.times do
comments.unshift create(Comment, :article => art, :title => 'a test comment', :body => 'bla', :author => owner)
end
comments.pop
assert_equal comments, Comment.recent(2)
end
should 'not accept invalid email' do
c = build(Comment, :name => 'My Name', :email => 'my@invalid')
c.valid?
assert c.errors[:email.to_s].present?
end
should 'generate links to comments on images with view set to true' do
owner = create_user('testuser').person
image = create(UploadedFile, :profile => owner, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
comment = create(Comment, :article => image, :author => owner, :title => 'a random comment', :body => 'just another comment')
assert comment.url[:view]
end
should 'not fill fields with javascript' do
owner = create_user('testuser').person
article = owner.articles.create!(:name => 'test', :body => '...')
javascript = "<script>alert('XSS')</script>"
comment = create(Comment, :article => article, :name => javascript, :title => javascript, :body => javascript, :email => 'cracker@test.org')
assert_no_match(/<script>/, comment.name)
end
should 'sanitize required fields before validation' do
owner = create_user('testuser').person
article = owner.articles.create(:name => 'test', :body => '...')
comment = build(Comment, :article => article, :title => '<h1 title </h1>', :body => '<h1 body </h1>', :name => '<h1 name </h1>', :email => 'cracker@test.org')
comment.valid?
assert comment.errors[:name.to_s].present?
assert comment.errors[:body.to_s].present?
end
should 'escape malformed html tags' do
owner = create_user('testuser').person
article = owner.articles.create(:name => 'test', :body => '...')
comment = build(Comment, :article => article, :title => '<h1 title </h1>>> sd f <<', :body => '<h1>> sdf><asd>< body </h1>', :name => '<h1 name </h1>>><<dfsf<sd', :email => 'cracker@test.org')
comment.valid?
assert_no_match /[<>]/, comment.title
assert_no_match /[<>]/, comment.body
assert_no_match /[<>]/, comment.name
end
should 'use an existing image for deleted comments' do
image = Comment.new.removed_user_image[1..-1]
assert File.exists?(Rails.root.join('public', image)), "#{image} does not exist."
end
should 'have the action_tracker_target defined' do
assert Comment.method_defined?(:action_tracker_target)
end
should "get children of a comment" do
c = fast_create(Comment)
c1 = fast_create(Comment, :reply_of_id => c.id)
c2 = fast_create(Comment)
c3 = fast_create(Comment, :reply_of_id => c.id)
assert_equal [c1,c3], c.children
end
should "get parent of a comment" do
c = fast_create(Comment)
c1 = fast_create(Comment, :reply_of_id => c.id)
c2 = fast_create(Comment, :reply_of_id => c1.id)
c3 = fast_create(Comment, :reply_of_id => c.id)
c4 = fast_create(Comment)
assert_equal c, c1.reply_of
assert_equal c, c3.reply_of
assert_equal c1, c2.reply_of
assert_nil c4.reply_of
end
should 'destroy replies when comment is removed' do
Comment.delete_all
owner = create_user('testuser').person
article = owner.articles.create!(:name => 'test', :body => '...')
c = create(Comment, :article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org')
c1 = create(Comment, :article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org', :reply_of_id => c.id)
c2 = create(Comment, :article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org')
c3 = create(Comment, :article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org', :reply_of_id => c.id)
assert_equal 4, Comment.count
c.destroy
assert_equal [c2], Comment.all
end
should "get children if replies are not loaded" do
c = fast_create(Comment)
c1 = fast_create(Comment, :reply_of_id => c.id)
c2 = fast_create(Comment)
c3 = fast_create(Comment, :reply_of_id => c.id)
assert_nil c.instance_variable_get('@replies')
assert_equal [c1,c3], c.replies
end
should "get replies if they are loaded" do
c = fast_create(Comment)
c1 = fast_create(Comment, :reply_of_id => c.id)
c2 = fast_create(Comment)
c3 = fast_create(Comment, :reply_of_id => c.id)
c.replies = [c2]
assert_not_nil c.instance_variable_get('@replies')
assert_equal [c2], c.replies
end
should "set replies" do
c = fast_create(Comment)
c1 = fast_create(Comment, :reply_of_id => c.id)
c2 = fast_create(Comment)
c3 = fast_create(Comment, :reply_of_id => c.id)
c.replies = []
c.replies << c2
assert_equal [c2], c.instance_variable_get('@replies')
assert_equal [c2], c.replies
assert_equal [c1,c3], c.reload.children
end
should "return activities comments as a thread" do
person = fast_create(Person)
a = TextileArticle.create!(:profile => person, :name => 'My article', :body => 'Article body')
c0 = Comment.create!(:source => a, :body => 'My comment', :author => person)
c1 = Comment.create!(:reply_of_id => c0.id, :source => a, :body => 'bla', :author => person)
c2 = Comment.create!(:reply_of_id => c1.id, :source => a, :body => 'bla', :author => person)
c3 = Comment.create!(:reply_of_id => c0.id, :source => a, :body => 'bla', :author => person)
c4 = Comment.create!(:source => a, :body => 'My comment', :author => person)
result = a.activity.comments
assert_equal c0, result[0]
assert_equal [c1, c3], result[0].replies
assert_equal [c2], result[0].replies[0].replies
assert_equal c4, result[1]
assert result[1].replies.empty?
end
should "return activities comments when some comment on thread is spam and not display its replies" do
person = fast_create(Person)
a = TextileArticle.create!(:profile => person, :name => 'My article', :body => 'Article body')
c0 = Comment.create(:source => a, :body => 'Root comment', :author => person)
c1 = Comment.create(:reply_of_id => c0.id, :source => a, :body => 'c1', :author => person)
c2 = Comment.create(:source => a, :body => 'c2', :author => person)
spam = create(Comment, :spam => true, :reply_of_id => c2.id, :source => a, :body => 'spam', :author => person)
spam_reply = Comment.create(:reply_of_id => spam.id, :source => a, :body => 'spam reply', :author => person)
result = a.activity.comments
assert_equal c0, result[0]
assert_equal [c1], result[0].replies.without_spam
assert_equal c2, result[1]
assert_equal [], result[1].replies.without_spam
end
should 'provide author url for authenticated user' do
author = Person.new
author.expects(:url).returns('http://blabla.net/author')
assert_equal 'http://blabla.net/author', build(Comment, :author => author).author_url
end
should 'not provide author url for unauthenticated user' do
assert_nil build(Comment, :email => 'my@email.com').author_url
end
should 'be able to reject a comment' do
c = Comment.new
assert !c.rejected?
c.reject!
assert c.rejected?
end
should 'subscribe user as follower of an article on new comment' do
owner = create_user('owner_of_article').person
person = create_user('follower').person
article = fast_create(Article, :profile_id => owner.id)
assert_not_includes article.followers, person.email
create(Comment, :source => article, :author => person, :title => 'new comment', :body => 'new comment')
assert_includes article.reload.followers, person.email
end
should 'subscribe guest user as follower of an article on new comment' do
article = fast_create(Article, :profile_id => create_user('article_owner').person.id)
assert_not_includes article.followers, 'follower@example.com'
create(Comment, :source => article, :name => 'follower', :email => 'follower@example.com', :title => 'new comment', :body => 'new comment')
assert_includes article.reload.followers, 'follower@example.com'
end
should 'keep unique emails in list of followers' do
article = fast_create(Article, :profile_id => create_user('article_owner').person.id)
create(Comment, :source => article, :name => 'follower one', :email => 'follower@example.com', :title => 'new comment', :body => 'new comment')
create(Comment, :source => article, :name => 'follower two', :email => 'follower@example.com', :title => 'another comment', :body => 'new comment')
assert_equal 1, article.reload.followers.select{|v| v == 'follower@example.com'}.count
end
should 'not subscribe owner as follower of an article on new comment' do
owner = create_user('owner_of_article').person
article = fast_create(Article, :profile_id => owner.id)
create(Comment, :source => article, :author => owner, :title => 'new comment', :body => 'new comment')
assert_not_includes article.reload.followers, owner.email
end
should 'not subscribe admins as follower of an article on new comment' do
owner = fast_create(Community)
follower = create_user('follower').person
admin = create_user('admin_of_community').person
owner.add_admin(admin)
article = fast_create(Article, :profile_id => owner.id)
create(Comment, :source => article, :author => follower, :title => 'new comment', :body => 'new comment')
create(Comment, :source => article, :author => admin, :title => 'new comment', :body => 'new comment')
assert_not_includes article.reload.followers, admin.email
assert_includes article.followers, follower.email
end
should 'update article activity when add a comment' do
now = Time.now
Time.stubs(:now).returns(now)
profile = create_user('testuser').person
article = create(TinyMceArticle, :profile => profile)
ActionTracker::Record.record_timestamps = false
article.activity.update_attribute(:updated_at, Time.now - 1.day)
ActionTracker::Record.record_timestamps = true
time = article.activity.updated_at
comment = create(Comment, :source => article, :author => profile)
assert_equal time + 1.day, article.activity.updated_at
end
should 'create a new activity when add a comment and the activity was removed' do
profile = create_user('testuser').person
article = create(TinyMceArticle, :profile => profile)
article.activity.destroy
assert_nil article.activity
comment = create(Comment, :source => article, :author => profile)
assert_not_nil article.activity
end
should 'be able to mark comments as spam/ham/unknown' do
c = Comment.new
c.spam = true
assert c.spam?
assert !c.ham?
c.spam = false
assert c.ham?
assert !c.spam?
c.spam = nil
assert !c.spam?
assert !c.ham?
end
should 'be able to select non-spam comments' do
Comment.destroy_all
c1 = fast_create(Comment)
c2 = fast_create(Comment, :spam => false)
c3 = fast_create(Comment, :spam => true)
assert_equivalent [c1,c2], Comment.without_spam
end
should 'be able to mark as spam atomically' do
c1 = create_comment
c1.spam!
c1.reload
assert c1.spam?
end
should 'be able to select spammy comments' do
c1 = fast_create(Comment)
c2 = fast_create(Comment, :spam => false)
c3 = fast_create(Comment, :spam => true)
assert_equivalent [c3], Comment.spam
end
should 'be able to mark as ham atomically' do
c1 = create_comment
c1.ham!
c1.reload
assert c1.ham?
end
should 'notify by email' do
c1 = create_comment
c1.expects(:notify_by_mail)
c1.verify_and_notify
end
should 'not notify by email when comment is spam' do
c1 = create_comment(:spam => true)
c1.expects(:notify_by_mail).never
c1.verify_and_notify
end
class EverythingIsSpam < Noosfero::Plugin
def check_for_spam(comment)
comment.spam!
end
end
should 'delegate spam detection to plugins' do
Noosfero::Plugin.stubs(:all).returns(['CommentTest::EverythingIsSpam'])
Environment.default.enable_plugin(EverythingIsSpam)
c1 = create_comment
c1.expects(:notify_by_mail).never
c1.verify_and_notify
end
class SpamNotification < Noosfero::Plugin
class << self
attr_accessor :marked_as_spam
attr_accessor :marked_as_ham
end
def marked_as_spam(c)
self.class.marked_as_spam = c
end
def marked_as_ham(c)
self.class.marked_as_ham = c
end
end
should 'notify plugins of comments being marked as spam' do
Noosfero::Plugin.stubs(:all).returns(['CommentTest::SpamNotification'])
Environment.default.enable_plugin(SpamNotification)
c = create_comment
c.spam!
process_delayed_job_queue
assert_equal c, SpamNotification.marked_as_spam
end
should 'notify plugins of comments being marked as ham' do
Noosfero::Plugin.stubs(:all).returns(['CommentTest::SpamNotification'])
Environment.default.enable_plugin(SpamNotification)
c = create_comment
c.ham!
process_delayed_job_queue
assert_equal c, SpamNotification.marked_as_ham
end
should 'store User-Agent' do
c = build(Comment, :user_agent => 'foo')
assert_equal 'foo', c.user_agent
end
should 'store referrer' do
c = build(Comment, :referrer => 'bar')
assert_equal 'bar', c.referrer
end
should 'delegate environment to article' do
profile = fast_create(Profile, :environment_id => Environment.default)
article = fast_create(Article, :profile_id => profile.id)
comment = fast_create(Comment, :source_id => article.id, :source_type => 'Article')
assert_equal Environment.default, comment.environment
end
should 'log spammer ip after marking comment as spam' do
comment = create_comment(:ip_address => '192.168.0.1')
comment.spam!
log = File.open('log/test_spammers.log')
assert_match "Comment-id: #{comment.id} IP: 192.168.0.1", log.read
end
should 'not need moderation if article is not moderated' do
article = Article.new
comment = build(Comment, :article => article)
assert !comment.need_moderation?
end
should 'not need moderation if the comment author is the article author' do
author = Person.new
article = Article.new
article.stubs(:author).returns(author)
article.moderate_comments = true
comment = build(Comment, :article => article)
comment.stubs(:author).returns(author)
assert !comment.need_moderation?
end
should 'need moderation if article is moderated and the comment has no author' do
article = Article.new
article.stubs(:moderate_comments?).returns(true)
comment = build(Comment, :article => article)
assert comment.need_moderation?
end
should 'need moderation if article is moderated and the comment author is different from article author' do
article_author = Person.new
comment_author = Person.new
article = Article.new
article.stubs(:author).returns(article_author)
article.stubs(:moderate_comments?).returns(true)
comment = build(Comment, :article => article)
comment.stubs(:author).returns(comment_author)
assert comment.need_moderation?
end
should 'not be able to destroy comment without user' do
comment = Comment.new
assert !comment.can_be_destroyed_by?(nil)
end
should 'not be able to destroy comment' do
user = Person.new
profile = Profile.new
article = build(Article, :profile => profile)
comment = build(Comment, :article => article)
user.expects(:has_permission?).with(:moderate_comments, profile).returns(false)
assert !comment.can_be_destroyed_by?(user)
end
should 'be able to destroy comment if is the author' do
user = Person.new
comment = build(Comment, :author => user)
assert comment.can_be_destroyed_by?(user)
end
should 'be able to destroy comment if is the profile' do
user = Person.new
article = build(Article, :profile => user)
comment = build(Comment, :article => article)
assert comment.can_be_destroyed_by?(user)
end
should 'be able to destroy comment if can moderate_comments on the profile' do
user = Person.new
profile = Profile.new
article = build(Article, :profile => profile)
comment = build(Comment, :article => article)
user.expects(:has_permission?).with(:moderate_comments, profile).returns(true)
assert comment.can_be_destroyed_by?(user)
end
should 'not be able to mark comment as spam without user' do
comment = Comment.new
assert !comment.can_be_marked_as_spam_by?(nil)
end
should 'not be able to mark comment as spam' do
user = Person.new
profile = Profile.new
article = build(Article, :profile => profile)
comment = build(Comment, :article => article)
user.expects(:has_permission?).with(:moderate_comments, profile).returns(false)
assert !comment.can_be_marked_as_spam_by?(user)
end
should 'be able to mark comment as spam if is the profile' do
user = Person.new
article = build(Article, :profile => user)
comment = build(Comment, :article => article)
assert comment.can_be_marked_as_spam_by?(user)
end
should 'be able to mark comment as spam if can moderate_comments on the profile' do
user = Person.new
profile = Profile.new
article = build(Article, :profile => profile)
comment = build(Comment, :article => article)
user.expects(:has_permission?).with(:moderate_comments, profile).returns(true)
assert comment.can_be_marked_as_spam_by?(user)
end
should 'not be able to update comment without user' do
comment = Comment.new
assert !comment.can_be_updated_by?(nil)
end
should 'not be able to update comment' do
user = Person.new
comment = Comment.new
assert !comment.can_be_updated_by?(user)
end
should 'be able to update comment if is the author' do
user = Person.new
comment = build(Comment, :author => user)
assert comment.can_be_updated_by?(user)
end
should 'get comment root' do
c1 = Comment.new
c2 = build(Comment, :reply_of => c1)
c3 = build(Comment, :reply_of => c2)
assert_equal c1, c3.comment_root
assert_equal c1, c2.comment_root
assert_equal c1, c1.comment_root
end
should 'be able to select non-reply comments' do
Comment.destroy_all
c1 = fast_create(Comment)
c2 = fast_create(Comment, :reply_of_id => c1.id)
c3 = fast_create(Comment, :reply_of_id => c2.id)
c4 = fast_create(Comment)
assert_equivalent [c1,c4], Comment.without_reply
end
should 'vote in a comment' do
comment = create_comment
person = create_user('voter').person
person.vote(comment, 5)
assert_equal 1, comment.voters_who_voted.length
assert_equal 5, comment.votes_total
end
should 'like a comment' do
comment = create_comment
person = create_user('voter').person
assert !comment.voted_by?(person, true)
person.vote_for(comment)
assert comment.voted_by?(person, true)
assert !comment.voted_by?(person, false)
end
should 'count voters for' do
comment = create_comment
person = create_user('voter').person
person2 = create_user('voter2').person
person3 = create_user('voter3').person
person.vote_for(comment)
person2.vote_for(comment)
person3.vote_against(comment)
assert_equal 2, comment.votes_for
end
should 'count votes againts' do
comment = create_comment
person = create_user('voter').person
person2 = create_user('voter2').person
person3 = create_user('voter3').person
person.vote_against(comment)
person2.vote_against(comment)
person3.vote_for(comment)
assert_equal 2, comment.votes_against
end
should 'be able to remove a voted comment' do
comment = create_comment
person = create_user('voter').person
person.vote(comment, 5)
comment.destroy
end
private
def create_comment(args = {})
owner = create_user('testuser').person
article = create(TextileArticle, :profile_id => owner.id)
create(Comment, { :name => 'foo', :email => 'foo@example.com', :source => article }.merge(args))
end
end