+ def test_comment_spammy
+ user = create(:user)
+ other_user = create(:user)
+
+ # Find the entry to comment on
+ entry = create(:diary_entry, :user => user)
+ post :subscribe,
+ :params => { :id => entry.id, :display_name => entry.user.display_name },
+ :session => { :user => user }
+
+ # Generate some spammy content
+ spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
+
+ # Try creating a spammy comment
+ assert_difference "ActionMailer::Base.deliveries.size", 1 do
+ assert_difference "DiaryComment.count", 1 do
+ post :comment,
+ :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } },
+ :session => { :user => other_user }
+ end
+ end
+ assert_response :redirect
+ assert_redirected_to :action => :view, :display_name => entry.user.display_name, :id => entry.id
+ email = ActionMailer::Base.deliveries.first
+ assert_equal [user.email], email.to
+ assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
+ assert_match %r{http://example.com/spam}, email.text_part.decoded
+ assert_match %r{http://example.com/spam}, email.html_part.decoded
+ ActionMailer::Base.deliveries.clear
+ comment = DiaryComment.order(:id).last
+ assert_equal entry.id, comment.diary_entry_id
+ assert_equal other_user.id, comment.user_id
+ assert_equal spammy_text, comment.body
+ assert_equal "suspended", User.find(other_user.id).status
+
+ # Follow the redirect
+ get :list,
+ :params => { :display_name => user.display_name },
+ :session => { :user => other_user }
+ assert_response :redirect
+ assert_redirected_to :controller => :user, :action => :suspended
+
+ # Now view the diary entry, and check the new comment is not present
+ get :view,
+ :params => { :display_name => entry.user.display_name, :id => entry.id }
+ assert_response :success
+ assert_select ".diary-comment", :count => 0
+ end
+