Commit dae370c61cbdde34329dbd46608ce082d09b18e0

Authored by Rodrigo Souto
1 parent 29f6b68e

rails3: fix comment tests

PS: still failing due to ActionTracker::Record#current_user_from_model
Showing 1 changed file with 60 additions and 60 deletions   Show diff stats
test/unit/comment_test.rb
... ... @@ -60,7 +60,7 @@ class CommentTest < ActiveSupport::TestCase
60 60 c1.name = 'my name'
61 61 c1.valid?
62 62 assert c1.errors[:name.to_s].present?
63   - assert_no_match /\{fn\}/, c1.errors.on(:name)
  63 + assert_no_match /\{fn\}/, c1.errors[:name].first
64 64 end
65 65  
66 66 should 'update counter cache in article' do
... ... @@ -86,7 +86,7 @@ class CommentTest < ActiveSupport::TestCase
86 86 person = fast_create(Person)
87 87 community = fast_create(Community)
88 88  
89   - activity = ActionTracker::Record.create :user => person, :target => community, :verb => 'add_member_in_community'
  89 + activity = create ActionTracker::Record, :user => person, :target => community, :verb => 'add_member_in_community'
90 90  
91 91 cc = activity.comments_count
92 92  
... ... @@ -96,40 +96,40 @@ class CommentTest < ActiveSupport::TestCase
96 96  
97 97 should 'provide author name for authenticated authors' do
98 98 owner = create_user('testuser').person
99   - assert_equal 'testuser', Comment.new(:author => owner).author_name
  99 + assert_equal 'testuser', build(Comment, :author => owner).author_name
100 100 end
101 101  
102 102 should 'provide author name for unauthenticated author' do
103   - assert_equal 'anonymous coward', Comment.new(:name => 'anonymous coward').author_name
  103 + assert_equal 'anonymous coward', build(Comment, :name => 'anonymous coward').author_name
104 104 end
105 105  
106 106 should 'provide empty text for author name if user was removed ' do
107   - assert_equal '', Comment.new(:author_id => 9999).author_name
  107 + assert_equal '', build(Comment, :author_id => 9999).author_name
108 108 end
109 109  
110 110 should "provide author e-mail for athenticated authors" do
111 111 owner = create_user('testuser').person
112   - assert_equal owner.email, Comment.new(:author => owner).author_email
  112 + assert_equal owner.email, build(Comment, :author => owner).author_email
113 113 end
114 114  
115 115 should "provide author e-mail for unauthenticated author" do
116   - assert_equal 'my@email.com', Comment.new(:email => 'my@email.com').author_email
  116 + assert_equal 'my@email.com', build(Comment, :email => 'my@email.com').author_email
117 117 end
118 118  
119 119 should 'provide author link for authenticated author' do
120 120 author = Person.new
121 121 author.expects(:url).returns('http://blabla.net/author')
122   - assert_equal 'http://blabla.net/author', Comment.new(:author => author).author_link
  122 + assert_equal 'http://blabla.net/author', build(Comment, :author => author).author_link
123 123 end
124 124  
125 125 should 'provide author e-mail as author link for unauthenticated author' do
126   - assert_equal 'my@email.com', Comment.new(:email => 'my@email.com').author_link
  126 + assert_equal 'my@email.com', build(Comment, :email => 'my@email.com').author_link
127 127 end
128 128  
129 129 should 'provide url to comment' do
130 130 art = Article.new
131 131 art.expects(:url).returns({ :controller => 'lala', :action => 'something' })
132   - comment = Comment.new(:article => art)
  132 + comment = build(Comment, :article => art)
133 133 comment.expects(:id).returns(9876)
134 134  
135 135 assert_equal({ :controller => 'lala', :action => 'something', :anchor => 'comment-9876'}, comment.url)
... ... @@ -148,7 +148,7 @@ class CommentTest < ActiveSupport::TestCase
148 148 art = owner.articles.build(:name => 'ytest'); art.save!
149 149 comments = []
150 150 3.times do
151   - comments.unshift art.comments.create!(:title => 'a test comment', :body => 'bla', :author => owner)
  151 + comments.unshift create(Comment, :article => art, :title => 'a test comment', :body => 'bla', :author => owner)
152 152 end
153 153  
154 154 assert_equal comments, Comment.recent
... ... @@ -161,7 +161,7 @@ class CommentTest < ActiveSupport::TestCase
161 161 art = owner.articles.build(:name => 'ytest'); art.save!
162 162 comments = []
163 163 3.times do
164   - comments.unshift art.comments.create!(:title => 'a test comment', :body => 'bla', :author => owner)
  164 + comments.unshift create(Comment, :article => art, :title => 'a test comment', :body => 'bla', :author => owner)
165 165 end
166 166  
167 167 comments.pop
... ... @@ -170,15 +170,15 @@ class CommentTest < ActiveSupport::TestCase
170 170 end
171 171  
172 172 should 'not accept invalid email' do
173   - c = Comment.new(:name => 'My Name', :email => 'my@invalid')
  173 + c = build(Comment, :name => 'My Name', :email => 'my@invalid')
174 174 c.valid?
175 175 assert c.errors[:email.to_s].present?
176 176 end
177 177  
178 178 should 'generate links to comments on images with view set to true' do
179 179 owner = create_user('testuser').person
180   - image = UploadedFile.create!(:profile => owner, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
181   - comment = image.comments.create!(:article => image, :author => owner, :title => 'a random comment', :body => 'just another comment')
  180 + image = create(UploadedFile, :profile => owner, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
  181 + comment = create(Comment, :article => image, :author => owner, :title => 'a random comment', :body => 'just another comment')
182 182  
183 183 assert comment.url[:view]
184 184 end
... ... @@ -187,14 +187,14 @@ class CommentTest < ActiveSupport::TestCase
187 187 owner = create_user('testuser').person
188 188 article = owner.articles.create!(:name => 'test', :body => '...')
189 189 javascript = "<script>alert('XSS')</script>"
190   - comment = article.comments.create!(:article => article, :name => javascript, :title => javascript, :body => javascript, :email => 'cracker@test.org')
  190 + comment = create(Comment, :article => article, :name => javascript, :title => javascript, :body => javascript, :email => 'cracker@test.org')
191 191 assert_no_match(/<script>/, comment.name)
192 192 end
193 193  
194 194 should 'sanitize required fields before validation' do
195 195 owner = create_user('testuser').person
196 196 article = owner.articles.create(:name => 'test', :body => '...')
197   - comment = article.comments.new(:title => '<h1 title </h1>', :body => '<h1 body </h1>', :name => '<h1 name </h1>', :email => 'cracker@test.org')
  197 + comment = build(Comment, :article => article, :title => '<h1 title </h1>', :body => '<h1 body </h1>', :name => '<h1 name </h1>', :email => 'cracker@test.org')
198 198 comment.valid?
199 199  
200 200 assert comment.errors[:name.to_s].present?
... ... @@ -204,7 +204,7 @@ class CommentTest &lt; ActiveSupport::TestCase
204 204 should 'escape malformed html tags' do
205 205 owner = create_user('testuser').person
206 206 article = owner.articles.create(:name => 'test', :body => '...')
207   - comment = article.comments.new(:title => '<h1 title </h1>>> sd f <<', :body => '<h1>> sdf><asd>< body </h1>', :name => '<h1 name </h1>>><<dfsf<sd', :email => 'cracker@test.org')
  207 + 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')
208 208 comment.valid?
209 209  
210 210 assert_no_match /[<>]/, comment.title
... ... @@ -213,7 +213,7 @@ class CommentTest &lt; ActiveSupport::TestCase
213 213 end
214 214  
215 215 should 'use an existing image for deleted comments' do
216   - image = Comment.new.removed_user_image
  216 + image = Comment.new.removed_user_image[1..-1]
217 217 assert File.exists?(Rails.root.join('public', image)), "#{image} does not exist."
218 218 end
219 219  
... ... @@ -245,10 +245,10 @@ class CommentTest &lt; ActiveSupport::TestCase
245 245 Comment.delete_all
246 246 owner = create_user('testuser').person
247 247 article = owner.articles.create!(:name => 'test', :body => '...')
248   - c = article.comments.create!(:article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org')
249   - c1 = article.comments.create!(:article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org', :reply_of_id => c.id)
250   - c2 = article.comments.create!(:article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org')
251   - c3 = article.comments.create!(:article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org', :reply_of_id => c.id)
  248 + c = create(Comment, :article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org')
  249 + c1 = create(Comment, :article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org', :reply_of_id => c.id)
  250 + c2 = create(Comment, :article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org')
  251 + c3 = create(Comment, :article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org', :reply_of_id => c.id)
252 252 assert_equal 4, Comment.count
253 253 c.destroy
254 254 assert_equal [c2], Comment.all
... ... @@ -287,12 +287,12 @@ class CommentTest &lt; ActiveSupport::TestCase
287 287  
288 288 should "return activities comments as a thread" do
289 289 person = fast_create(Person)
290   - a = TextileArticle.create!(:profile => person, :name => 'My article', :body => 'Article body')
291   - c0 = Comment.create!(:source => a, :body => 'My comment', :author => person)
292   - c1 = Comment.create!(:reply_of_id => c0.id, :source => a, :body => 'bla', :author => person)
293   - c2 = Comment.create!(:reply_of_id => c1.id, :source => a, :body => 'bla', :author => person)
294   - c3 = Comment.create!(:reply_of_id => c0.id, :source => a, :body => 'bla', :author => person)
295   - c4 = Comment.create!(:source => a, :body => 'My comment', :author => person)
  290 + a = create(TextileArticle, :profile => person, :name => 'My article', :body => 'Article body')
  291 + c0 = create(Comment, :source => a, :body => 'My comment', :author => person)
  292 + c1 = create(Comment, :reply_of_id => c0.id, :source => a, :body => 'bla', :author => person)
  293 + c2 = create(Comment, :reply_of_id => c1.id, :source => a, :body => 'bla', :author => person)
  294 + c3 = create(Comment, :reply_of_id => c0.id, :source => a, :body => 'bla', :author => person)
  295 + c4 = create(Comment, :source => a, :body => 'My comment', :author => person)
296 296 result = a.activity.comments_as_thread
297 297 assert_equal c0, result[0]
298 298 assert_equal [c1, c3], result[0].replies
... ... @@ -304,11 +304,11 @@ class CommentTest &lt; ActiveSupport::TestCase
304 304 should 'provide author url for authenticated user' do
305 305 author = Person.new
306 306 author.expects(:url).returns('http://blabla.net/author')
307   - assert_equal 'http://blabla.net/author', Comment.new(:author => author).author_url
  307 + assert_equal 'http://blabla.net/author', build(Comment, :author => author).author_url
308 308 end
309 309  
310 310 should 'not provide author url for unauthenticated user' do
311   - assert_nil Comment.new(:email => 'my@email.com').author_url
  311 + assert_nil build(Comment, :email => 'my@email.com').author_url
312 312 end
313 313  
314 314 should 'be able to reject a comment' do
... ... @@ -324,28 +324,28 @@ class CommentTest &lt; ActiveSupport::TestCase
324 324 person = create_user('follower').person
325 325 article = fast_create(Article, :profile_id => owner.id)
326 326 assert_not_includes article.followers, person.email
327   - article.comments.create!(:source => article, :author => person, :title => 'new comment', :body => 'new comment')
  327 + create(Comment, :source => article, :author => person, :title => 'new comment', :body => 'new comment')
328 328 assert_includes article.reload.followers, person.email
329 329 end
330 330  
331 331 should 'subscribe guest user as follower of an article on new comment' do
332 332 article = fast_create(Article, :profile_id => create_user('article_owner').person.id)
333 333 assert_not_includes article.followers, 'follower@example.com'
334   - article.comments.create!(:source => article, :name => 'follower', :email => 'follower@example.com', :title => 'new comment', :body => 'new comment')
  334 + create(Comment, :source => article, :name => 'follower', :email => 'follower@example.com', :title => 'new comment', :body => 'new comment')
335 335 assert_includes article.reload.followers, 'follower@example.com'
336 336 end
337 337  
338 338 should 'keep unique emails in list of followers' do
339 339 article = fast_create(Article, :profile_id => create_user('article_owner').person.id)
340   - article.comments.create!(:source => article, :name => 'follower one', :email => 'follower@example.com', :title => 'new comment', :body => 'new comment')
341   - article.comments.create!(:source => article, :name => 'follower two', :email => 'follower@example.com', :title => 'another comment', :body => 'new comment')
  340 + create(Comment, :source => article, :name => 'follower one', :email => 'follower@example.com', :title => 'new comment', :body => 'new comment')
  341 + create(Comment, :source => article, :name => 'follower two', :email => 'follower@example.com', :title => 'another comment', :body => 'new comment')
342 342 assert_equal 1, article.reload.followers.select{|v| v == 'follower@example.com'}.count
343 343 end
344 344  
345 345 should 'not subscribe owner as follower of an article on new comment' do
346 346 owner = create_user('owner_of_article').person
347 347 article = fast_create(Article, :profile_id => owner.id)
348   - article.comments.create!(:source => article, :author => owner, :title => 'new comment', :body => 'new comment')
  348 + create(Comment, :source => article, :author => owner, :title => 'new comment', :body => 'new comment')
349 349 assert_not_includes article.reload.followers, owner.email
350 350 end
351 351  
... ... @@ -355,8 +355,8 @@ class CommentTest &lt; ActiveSupport::TestCase
355 355 admin = create_user('admin_of_community').person
356 356 owner.add_admin(admin)
357 357 article = fast_create(Article, :profile_id => owner.id)
358   - article.comments.create!(:source => article, :author => follower, :title => 'new comment', :body => 'new comment')
359   - article.comments.create!(:source => article, :author => admin, :title => 'new comment', :body => 'new comment')
  358 + create(Comment, :source => article, :author => follower, :title => 'new comment', :body => 'new comment')
  359 + create(Comment, :source => article, :author => admin, :title => 'new comment', :body => 'new comment')
360 360 assert_not_includes article.reload.followers, admin.email
361 361 assert_includes article.followers, follower.email
362 362 end
... ... @@ -501,12 +501,12 @@ class CommentTest &lt; ActiveSupport::TestCase
501 501 end
502 502  
503 503 should 'store User-Agent' do
504   - c = Comment.new(:user_agent => 'foo')
  504 + c = build(Comment, :user_agent => 'foo')
505 505 assert_equal 'foo', c.user_agent
506 506 end
507 507  
508 508 should 'store referrer' do
509   - c = Comment.new(:referrer => 'bar')
  509 + c = build(Comment, :referrer => 'bar')
510 510 assert_equal 'bar', c.referrer
511 511 end
512 512  
... ... @@ -528,7 +528,7 @@ class CommentTest &lt; ActiveSupport::TestCase
528 528  
529 529 should 'not need moderation if article is not moderated' do
530 530 article = Article.new
531   - comment = Comment.new(:article => article)
  531 + comment = build(Comment, :article => article)
532 532  
533 533 assert !comment.need_moderation?
534 534 end
... ... @@ -540,7 +540,7 @@ class CommentTest &lt; ActiveSupport::TestCase
540 540 article.stubs(:author).returns(author)
541 541 article.moderate_comments = true
542 542  
543   - comment = Comment.new(:article => article)
  543 + comment = build(Comment, :article => article)
544 544 comment.stubs(:author).returns(author)
545 545  
546 546 assert !comment.need_moderation?
... ... @@ -550,7 +550,7 @@ class CommentTest &lt; ActiveSupport::TestCase
550 550 article = Article.new
551 551 article.stubs(:moderate_comments?).returns(true)
552 552  
553   - comment = Comment.new(:article => article)
  553 + comment = build(Comment, :article => article)
554 554  
555 555 assert comment.need_moderation?
556 556 end
... ... @@ -563,7 +563,7 @@ class CommentTest &lt; ActiveSupport::TestCase
563 563 article.stubs(:author).returns(article_author)
564 564 article.stubs(:moderate_comments?).returns(true)
565 565  
566   - comment = Comment.new(:article => article)
  566 + comment = build(Comment, :article => article)
567 567 comment.stubs(:author).returns(comment_author)
568 568  
569 569 assert comment.need_moderation?
... ... @@ -578,8 +578,8 @@ class CommentTest &lt; ActiveSupport::TestCase
578 578 should 'not be able to destroy comment' do
579 579 user = Person.new
580 580 profile = Profile.new
581   - article = Article.new(:profile => profile)
582   - comment = Comment.new(:article => article)
  581 + article = build(Article, :profile => profile)
  582 + comment = build(Comment, :article => article)
583 583 user.expects(:has_permission?).with(:moderate_comments, profile).returns(false)
584 584  
585 585 assert !comment.can_be_destroyed_by?(user)
... ... @@ -587,15 +587,15 @@ class CommentTest &lt; ActiveSupport::TestCase
587 587  
588 588 should 'be able to destroy comment if is the author' do
589 589 user = Person.new
590   - comment = Comment.new(:author => user)
  590 + comment = build(Comment, :author => user)
591 591  
592 592 assert comment.can_be_destroyed_by?(user)
593 593 end
594 594  
595 595 should 'be able to destroy comment if is the profile' do
596 596 user = Person.new
597   - article = Article.new(:profile => user)
598   - comment = Comment.new(:article => article)
  597 + article = build(Article, :profile => user)
  598 + comment = build(Comment, :article => article)
599 599  
600 600 assert comment.can_be_destroyed_by?(user)
601 601 end
... ... @@ -603,8 +603,8 @@ class CommentTest &lt; ActiveSupport::TestCase
603 603 should 'be able to destroy comment if can moderate_comments on the profile' do
604 604 user = Person.new
605 605 profile = Profile.new
606   - article = Article.new(:profile => profile)
607   - comment = Comment.new(:article => article)
  606 + article = build(Article, :profile => profile)
  607 + comment = build(Comment, :article => article)
608 608  
609 609 user.expects(:has_permission?).with(:moderate_comments, profile).returns(true)
610 610  
... ... @@ -620,8 +620,8 @@ class CommentTest &lt; ActiveSupport::TestCase
620 620 should 'not be able to mark comment as spam' do
621 621 user = Person.new
622 622 profile = Profile.new
623   - article = Article.new(:profile => profile)
624   - comment = Comment.new(:article => article)
  623 + article = build(Article, :profile => profile)
  624 + comment = build(Comment, :article => article)
625 625 user.expects(:has_permission?).with(:moderate_comments, profile).returns(false)
626 626  
627 627 assert !comment.can_be_marked_as_spam_by?(user)
... ... @@ -629,8 +629,8 @@ class CommentTest &lt; ActiveSupport::TestCase
629 629  
630 630 should 'be able to mark comment as spam if is the profile' do
631 631 user = Person.new
632   - article = Article.new(:profile => user)
633   - comment = Comment.new(:article => article)
  632 + article = build(Article, :profile => user)
  633 + comment = build(Comment, :article => article)
634 634  
635 635 assert comment.can_be_marked_as_spam_by?(user)
636 636 end
... ... @@ -638,8 +638,8 @@ class CommentTest &lt; ActiveSupport::TestCase
638 638 should 'be able to mark comment as spam if can moderate_comments on the profile' do
639 639 user = Person.new
640 640 profile = Profile.new
641   - article = Article.new(:profile => profile)
642   - comment = Comment.new(:article => article)
  641 + article = build(Article, :profile => profile)
  642 + comment = build(Comment, :article => article)
643 643  
644 644 user.expects(:has_permission?).with(:moderate_comments, profile).returns(true)
645 645  
... ... @@ -661,15 +661,15 @@ class CommentTest &lt; ActiveSupport::TestCase
661 661  
662 662 should 'be able to update comment if is the author' do
663 663 user = Person.new
664   - comment = Comment.new(:author => user)
  664 + comment = build(Comment, :author => user)
665 665  
666 666 assert comment.can_be_updated_by?(user)
667 667 end
668 668  
669 669 should 'get comment root' do
670 670 c1 = Comment.new
671   - c2 = Comment.new(:reply_of => c1)
672   - c3 = Comment.new(:reply_of => c2)
  671 + c2 = build(Comment, :reply_of => c1)
  672 + c3 = build(Comment, :reply_of => c2)
673 673  
674 674 assert_equal c1, c3.comment_root
675 675 assert_equal c1, c2.comment_root
... ...