1 # frozen_string_literal: true
 
   5 class DiaryCommentsControllerTest < ActionDispatch::IntegrationTest
 
   8     # Create the default language for diary entries
 
   9     create(:language, :code => "en")
 
  14       { :path => "/user/username/diary/1/comments", :method => :post },
 
  15       { :controller => "diary_comments", :action => "create", :display_name => "username", :id => "1" }
 
  18       { :path => "/diary_comments/2/hide", :method => :post },
 
  19       { :controller => "diary_comments", :action => "hide", :comment => "2" }
 
  22       { :path => "/diary_comments/2/unhide", :method => :post },
 
  23       { :controller => "diary_comments", :action => "unhide", :comment => "2" }
 
  29     other_user = create(:user)
 
  30     entry = create(:diary_entry, :user => user)
 
  31     create(:diary_entry_subscription, :diary_entry => entry, :user => user)
 
  33     # Make sure that you are denied when you are not logged in
 
  34     post comment_diary_entry_path(entry.user, entry)
 
  35     assert_response :forbidden
 
  37     session_for(other_user)
 
  39     # Verify that you get a not found error, when you pass a bogus id
 
  40     post comment_diary_entry_path(entry.user, :id => 9999)
 
  41     assert_response :not_found
 
  42     assert_select "div.content-heading", :count => 1 do
 
  43       assert_select "h1", :text => "No entry with the id: 9999", :count => 1
 
  46     # Now try an invalid comment with an empty body
 
  47     assert_no_difference "ActionMailer::Base.deliveries.size" do
 
  48       assert_no_difference "DiaryComment.count" do
 
  49         assert_no_difference "entry.subscribers.count" do
 
  50           perform_enqueued_jobs do
 
  51             post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "" })
 
  56     assert_response :success
 
  58     assert_match(/img-src \* data:;/, @response.headers["Content-Security-Policy-Report-Only"])
 
  60     # Now try again with the right id
 
  61     assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
 
  62       assert_difference "DiaryComment.count", 1 do
 
  63         assert_difference "entry.subscribers.count", 1 do
 
  64           perform_enqueued_jobs do
 
  65             post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => "New comment" })
 
  70     comment = DiaryComment.last
 
  71     assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
 
  72     email = ActionMailer::Base.deliveries.first
 
  73     assert_equal [user.email], email.to
 
  74     assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
 
  75     assert_match(/New comment/, email.text_part.decoded)
 
  76     assert_match(/New comment/, email.html_part.decoded)
 
  77     assert_equal entry.id, comment.diary_entry_id
 
  78     assert_equal other_user.id, comment.user_id
 
  79     assert_equal "New comment", comment.body
 
  81     # Now show the diary entry, and check the new comment is present
 
  82     get diary_entry_path(entry.user, entry)
 
  83     assert_response :success
 
  84     assert_select ".diary-comment", :count => 1 do
 
  85       assert_select "#comment#{comment.id}", :count => 1 do
 
  86         assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
 
  88       assert_select ".richtext", :text => /New comment/, :count => 1
 
  92   def test_create_spammy
 
  94     other_user = create(:user)
 
  95     entry = create(:diary_entry, :user => user)
 
  96     create(:diary_entry_subscription, :diary_entry => entry, :user => user)
 
  98     session_for(other_user)
 
 100     # Generate some spammy content
 
 101     spammy_text = 1.upto(50).map { |n| "http://example.com/spam#{n}" }.join(" ")
 
 103     # Try creating a spammy comment
 
 104     assert_difference "ActionMailer::Base.deliveries.size", 1 do
 
 105       assert_difference "DiaryComment.count", 1 do
 
 106         perform_enqueued_jobs do
 
 107           post comment_diary_entry_path(entry.user, entry, :diary_comment => { :body => spammy_text })
 
 111     comment = DiaryComment.last
 
 112     assert_redirected_to diary_entry_path(entry.user, entry, :anchor => "comment#{comment.id}")
 
 113     email = ActionMailer::Base.deliveries.first
 
 114     assert_equal [user.email], email.to
 
 115     assert_equal "[OpenStreetMap] #{other_user.display_name} commented on a diary entry", email.subject
 
 116     assert_match %r{http://example.com/spam}, email.text_part.decoded
 
 117     assert_match %r{http://example.com/spam}, email.html_part.decoded
 
 118     assert_equal entry.id, comment.diary_entry_id
 
 119     assert_equal other_user.id, comment.user_id
 
 120     assert_equal spammy_text, comment.body
 
 121     assert_equal "suspended", User.find(other_user.id).status
 
 124     assert_redirected_to :controller => :users, :action => :suspended
 
 126     # Now show the diary entry, and check the new comment is not present
 
 127     get diary_entry_path(entry.user, entry)
 
 128     assert_response :success
 
 129     assert_select ".diary-comment", :count => 0
 
 134     diary_entry = create(:diary_entry, :user => user)
 
 135     diary_comment = create(:diary_comment, :diary_entry => diary_entry)
 
 137     # Try without logging in
 
 138     post hide_diary_comment_path(diary_comment)
 
 139     assert_response :forbidden
 
 140     assert DiaryComment.find(diary_comment.id).visible
 
 142     # Now try as a normal user
 
 144     post hide_diary_comment_path(diary_comment)
 
 145     assert_redirected_to :controller => :errors, :action => :forbidden
 
 146     assert DiaryComment.find(diary_comment.id).visible
 
 149     session_for(create(:moderator_user))
 
 150     post hide_diary_comment_path(diary_comment)
 
 151     assert_redirected_to diary_entry_path(user, diary_entry)
 
 152     assert_not DiaryComment.find(diary_comment.id).visible
 
 155     diary_comment.reload.update(:visible => true)
 
 157     # Finally try as an administrator
 
 158     session_for(create(:administrator_user))
 
 159     post hide_diary_comment_path(diary_comment)
 
 160     assert_redirected_to diary_entry_path(user, diary_entry)
 
 161     assert_not DiaryComment.find(diary_comment.id).visible
 
 166     diary_entry = create(:diary_entry, :user => user)
 
 167     diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false)
 
 169     # Try without logging in
 
 170     post unhide_diary_comment_path(diary_comment)
 
 171     assert_response :forbidden
 
 172     assert_not DiaryComment.find(diary_comment.id).visible
 
 174     # Now try as a normal user
 
 176     post unhide_diary_comment_path(diary_comment)
 
 177     assert_redirected_to :controller => :errors, :action => :forbidden
 
 178     assert_not DiaryComment.find(diary_comment.id).visible
 
 180     # Now try as a moderator
 
 181     session_for(create(:moderator_user))
 
 182     post unhide_diary_comment_path(diary_comment)
 
 183     assert_redirected_to diary_entry_path(user, diary_entry)
 
 184     assert DiaryComment.find(diary_comment.id).visible
 
 187     diary_comment.reload.update(:visible => true)
 
 189     # Finally try as an administrator
 
 190     session_for(create(:administrator_user))
 
 191     post unhide_diary_comment_path(diary_comment)
 
 192     assert_redirected_to diary_entry_path(user, diary_entry)
 
 193     assert DiaryComment.find(diary_comment.id).visible