- # Verify that you get a not found error, when you pass a bogus id
- post :comment,
- :params => { :display_name => entry.user.display_name, :id => 9999 },
- :session => { :user => other_user }
- assert_response :not_found
- assert_select "div.content-heading", :count => 1 do
- assert_select "h2", :text => "No entry with the id: 9999", :count => 1
- end
-
- post :subscribe,
- :params => { :id => entry.id, :display_name => entry.user.display_name },
- :session => { :user => user }
-
- # Now try an invalid comment with an empty body
- assert_no_difference "ActionMailer::Base.deliveries.size" do
- assert_no_difference "DiaryComment.count" do
- assert_no_difference "entry.subscribers.count" do
- perform_enqueued_jobs do
- post :comment,
- :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "" } },
- :session => { :user => other_user }
- end
- end
- end
- end
- assert_response :success
- assert_template :show
-
- # Now try again with the right id
- assert_difference "ActionMailer::Base.deliveries.size", entry.subscribers.count do
- assert_difference "DiaryComment.count", 1 do
- assert_difference "entry.subscribers.count", 1 do
- perform_enqueued_jobs do
- post :comment,
- :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => "New comment" } },
- :session => { :user => other_user }
- end
- end
- end
- end
- assert_response :redirect
- assert_redirected_to :action => :show, :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(/New comment/, email.text_part.decoded)
- assert_match(/New comment/, 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 "New comment", comment.body
-
- # Now show the diary entry, and check the new comment is present
- get :show,
- :params => { :display_name => entry.user.display_name, :id => entry.id }
- assert_response :success
- assert_select ".diary-comment", :count => 1 do
- assert_select "#comment#{comment.id}", :count => 1 do
- assert_select "a[href='/user/#{ERB::Util.u(other_user.display_name)}']", :text => other_user.display_name, :count => 1
- end
- assert_select ".richtext", :text => /New comment/, :count => 1
- end
- end
-
- 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
- perform_enqueued_jobs do
- post :comment,
- :params => { :display_name => entry.user.display_name, :id => entry.id, :diary_comment => { :body => spammy_text } },
- :session => { :user => other_user }
- end
- end
- end
- assert_response :redirect
- assert_redirected_to :action => :show, :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 :index,
- :params => { :display_name => user.display_name },
- :session => { :user => other_user }
- assert_response :redirect
- assert_redirected_to :controller => :users, :action => :suspended