X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/0a10ac2e9be4246987951eea86ae201f14c65fc1..HEAD:/test/controllers/diary_entries_controller_test.rb diff --git a/test/controllers/diary_entries_controller_test.rb b/test/controllers/diary_entries_controller_test.rb index e2300e27b..7d543250b 100644 --- a/test/controllers/diary_entries_controller_test.rb +++ b/test/controllers/diary_entries_controller_test.rb @@ -49,15 +49,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest { :controller => "diary_entries", :action => "rss", :display_name => "username", :format => :rss } ) - assert_routing( - { :path => "/user/username/diary/comments", :method => :get }, - { :controller => "diary_entries", :action => "comments", :display_name => "username" } - ) - assert_routing( - { :path => "/user/username/diary/comments/1", :method => :get }, - { :controller => "diary_entries", :action => "comments", :display_name => "username", :page => "1" } - ) - assert_routing( { :path => "/diary/new", :method => :get }, { :controller => "diary_entries", :action => "new" } @@ -78,10 +69,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest { :path => "/user/username/diary/1", :method => :put }, { :controller => "diary_entries", :action => "update", :display_name => "username", :id => "1" } ) - assert_routing( - { :path => "/user/username/diary/1/newcomment", :method => :post }, - { :controller => "diary_entries", :action => "comment", :display_name => "username", :id => "1" } - ) assert_routing( { :path => "/user/username/diary/1/hide", :method => :post }, { :controller => "diary_entries", :action => "hide", :display_name => "username", :id => "1" } @@ -90,14 +77,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest { :path => "/user/username/diary/1/unhide", :method => :post }, { :controller => "diary_entries", :action => "unhide", :display_name => "username", :id => "1" } ) - assert_routing( - { :path => "/user/username/diary/1/hidecomment/2", :method => :post }, - { :controller => "diary_entries", :action => "hidecomment", :display_name => "username", :id => "1", :comment => "2" } - ) - assert_routing( - { :path => "/user/username/diary/1/unhidecomment/2", :method => :post }, - { :controller => "diary_entries", :action => "unhidecomment", :display_name => "username", :id => "1", :comment => "2" } - ) assert_routing( { :path => "/user/username/diary/1/subscribe", :method => :get }, { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" } @@ -140,9 +119,9 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1 assert_select "input[name=commit][type=submit][value=Publish]", :count => 1 - assert_select "input[name=commit][type=submit][value=Edit]", :count => 1 - assert_select "input[name=commit][type=submit][value=Preview]", :count => 1 - assert_select "input", :count => 6 + assert_select "button[type=button]", :text => "Edit", :count => 1 + assert_select "button[type=button]", :text => "Preview", :count => 1 + assert_select "input", :count => 4 end end end @@ -184,7 +163,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest :longitude => "2.2", :language_code => "en" }) end assert_redirected_to :action => :index, :display_name => user.display_name - entry = DiaryEntry.order(:id).last + entry = DiaryEntry.last assert_equal user.id, entry.user_id assert_equal "New Title", entry.title assert_equal "This is a new body for the diary entry", entry.body @@ -210,7 +189,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest :longitude => "2.2", :language_code => "de" }) end assert_redirected_to :action => :index, :display_name => user.display_name - entry = DiaryEntry.order(:id).last + entry = DiaryEntry.last assert_equal user.id, entry.user_id assert_equal "New Title", entry.title assert_equal "This is a new body for the diary entry", entry.body @@ -238,7 +217,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" }) end assert_redirected_to :action => :index, :display_name => user.display_name - entry = DiaryEntry.order(:id).last + entry = DiaryEntry.last assert_equal user.id, entry.user_id assert_equal spammy_title, entry.title assert_equal spammy_body, entry.body @@ -258,20 +237,20 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Make sure that you are redirected to the login page when you are # not logged in, without and with the id of the entry you want to edit - get edit_diary_entry_path(:display_name => entry.user.display_name, :id => entry) + get edit_diary_entry_path(entry.user, entry) assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit") session_for(other_user) # Verify that you get redirected to show if you are not the user # that created the entry - get edit_diary_entry_path(:display_name => entry.user.display_name, :id => entry) + get edit_diary_entry_path(entry.user, entry) assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id session_for(entry.user) # Verify that you get a not found error, when you pass a bogus id - get edit_diary_entry_path(:display_name => entry.user.display_name, :id => 9999) + get edit_diary_entry_path(entry.user, :id => 9999) assert_response :not_found assert_select "div.content-heading", :count => 1 do assert_select "h1", :text => "No entry with the id: 9999", :count => 1 @@ -279,7 +258,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Now pass the id, and check that you can edit it, when using the same # user as the person who created the entry - get edit_diary_entry_path(:display_name => entry.user.display_name, :id => entry) + get edit_diary_entry_path(entry.user, entry) assert_response :success assert_select "title", :text => /Edit Diary Entry/, :count => 1 assert_select "div.content-heading", :count => 1 do @@ -293,9 +272,9 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest assert_select "input#latitude[name='diary_entry[latitude]']", :count => 1 assert_select "input#longitude[name='diary_entry[longitude]']", :count => 1 assert_select "input[name=commit][type=submit][value=Update]", :count => 1 - assert_select "input[name=commit][type=submit][value=Edit]", :count => 1 - assert_select "input[name=commit][type=submit][value=Preview]", :count => 1 - assert_select "input", :count => 7 + assert_select "button[type=button]", :text => "Edit", :count => 1 + assert_select "button[type=button]", :text => "Preview", :count => 1 + assert_select "input", :count => 5 end end @@ -305,13 +284,13 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest new_latitude = "1.1" new_longitude = "2.2" new_language_code = "en" - put diary_entry_path(:display_name => entry.user.display_name, :id => entry, :commit => "save", - :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude, - :longitude => new_longitude, :language_code => new_language_code }) + put diary_entry_path(entry.user, entry, :commit => "save", + :diary_entry => { :title => new_title, :body => new_body, :latitude => new_latitude, + :longitude => new_longitude, :language_code => new_language_code }) assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id # Now check that the new data is rendered, when logged in - get diary_entry_path(:display_name => entry.user.display_name, :id => entry) + get diary_entry_path(entry.user, entry) assert_response :success assert_template "show" assert_select "title", :text => /Users' Diaries | /, :count => 1 @@ -330,7 +309,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # and when not logged in as the user who wrote the entry session_for(create(:user)) - get diary_entry_path(:display_name => entry.user.display_name, :id => entry) + get diary_entry_path(entry.user, entry) assert_response :success assert_template "show" assert_select "title", :text => /Users' Diaries | /, :count => 1 @@ -352,116 +331,32 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest user = create(:user) diary_entry = create(:diary_entry, :language_code => "en", :user => user) session_for(user) - get edit_diary_entry_path(:display_name => user.display_name, :id => diary_entry) + get edit_diary_entry_path(user, diary_entry) assert_response :success assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry" end - def test_comment + def test_update user = create(:user) other_user = create(:user) - entry = create(:diary_entry, :user => user) - create(:diary_entry_subscription, :diary_entry => entry, :user => user) + diary_entry = create(:diary_entry, :language_code => "en", :user => user, :title => "Original Title") - # Make sure that you are denied when you are not logged in - post comment_diary_entry_path(:display_name => entry.user.display_name, :id => entry) + put diary_entry_path(user, diary_entry, :diary_entry => { :title => "Updated Title" }) assert_response :forbidden + diary_entry.reload + assert_equal "Original Title", diary_entry.title session_for(other_user) + put diary_entry_path(user, diary_entry, :diary_entry => { :title => "Updated Title" }) + assert_redirected_to diary_entry_path(user, diary_entry) + diary_entry.reload + assert_equal "Original Title", diary_entry.title - # Verify that you get a not found error, when you pass a bogus id - post comment_diary_entry_path(:display_name => entry.user.display_name, :id => 9999) - assert_response :not_found - assert_select "div.content-heading", :count => 1 do - assert_select "h1", :text => "No entry with the id: 9999", :count => 1 - end - - # 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_diary_entry_path(:display_name => entry.user.display_name, :id => entry, :diary_comment => { :body => "" }) - 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_diary_entry_path(:display_name => entry.user.display_name, :id => entry, :diary_comment => { :body => "New comment" }) - end - end - end - end - 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 diary_entry_path(:display_name => entry.user.display_name, :id => entry) - 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) - entry = create(:diary_entry, :user => user) - create(:diary_entry_subscription, :diary_entry => entry, :user => user) - - session_for(other_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_diary_entry_path(:display_name => entry.user.display_name, :id => entry, :diary_comment => { :body => spammy_text }) - end - end - end - 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 diary_entries_path(:display_name => user.display_name) - assert_redirected_to :controller => :users, :action => :suspended - - # Now show the diary entry, and check the new comment is not present - get diary_entry_path(:display_name => entry.user.display_name, :id => entry) - assert_response :success - assert_select ".diary-comment", :count => 0 + session_for(user) + put diary_entry_path(user, diary_entry, :diary_entry => { :title => "Updated Title" }) + assert_redirected_to diary_entry_path(user, diary_entry) + diary_entry.reload + assert_equal "Updated Title", diary_entry.title end def test_index_all @@ -591,6 +486,17 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1 end + def test_index_invalid_paged + # Try some invalid paged accesses + %w[-1 0 fred].each do |id| + get diary_entries_path(:before => id) + assert_redirected_to :controller => :errors, :action => :bad_request + + get diary_entries_path(:after => id) + assert_redirected_to :controller => :errors, :action => :bad_request + end + end + def test_rss create(:language, :code => "de") create(:diary_entry, :language_code => "en") @@ -685,7 +591,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Try a normal entry that should work diary_entry = create(:diary_entry, :user => user) - get diary_entry_path(:display_name => user.display_name, :id => diary_entry) + get diary_entry_path(user, diary_entry) assert_response :success assert_template :show @@ -696,28 +602,28 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Try a deleted entry diary_entry_deleted = create(:diary_entry, :user => user, :visible => false) - get diary_entry_path(:display_name => user.display_name, :id => diary_entry_deleted) + get diary_entry_path(user, diary_entry_deleted) assert_response :not_found # Try an entry by a suspended user diary_entry_suspended_user = create(:diary_entry, :user => suspended_user) - get diary_entry_path(:display_name => suspended_user.display_name, :id => diary_entry_suspended_user) + get diary_entry_path(suspended_user, diary_entry_suspended_user) assert_response :not_found # Try an entry by a deleted user diary_entry_deleted_user = create(:diary_entry, :user => deleted_user) - get diary_entry_path(:display_name => deleted_user.display_name, :id => diary_entry_deleted_user) + get diary_entry_path(deleted_user, diary_entry_deleted_user) assert_response :not_found # Now try as a moderator session_for(create(:moderator_user)) - get diary_entry_path(:display_name => user.display_name, :id => diary_entry_deleted) + get diary_entry_path(user, diary_entry_deleted) assert_response :success assert_template :show # Finally try as an administrator session_for(create(:administrator_user)) - get diary_entry_path(:display_name => user.display_name, :id => diary_entry_deleted) + get diary_entry_path(user, diary_entry_deleted) assert_response :success assert_template :show end @@ -731,7 +637,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest deleted_user_comment = create(:diary_comment, :diary_entry => diary_entry, :user => create(:user, :deleted)) hidden_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false) - get diary_entry_path(:display_name => user.display_name, :id => diary_entry) + get diary_entry_path(user, diary_entry) assert_response :success assert_template :show assert_select "div.comments" do @@ -742,24 +648,164 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest end end + def test_show_og_title + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :title => "The Important Blog Post") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:title']" do + assert_dom "> @content", "The Important Blog Post" + end + end + + def test_show_og_image_with_no_image + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "nothing") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:image']" do + assert_dom "> @content", ActionController::Base.helpers.image_url("osm_logo_256.png", :host => root_url) + end + assert_dom "head meta[property='og:image:alt']" do + assert_dom "> @content", "OpenStreetMap logo" + end + end + + def test_show_og_image + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/picture.jpg)") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:image']" do + assert_dom "> @content", "https://example.com/picture.jpg" + end + assert_dom "head meta[property='og:image:alt']" do + assert_dom "> @content", "some picture" + end + end + + def test_show_og_image_with_relative_uri + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/picture.jpg)") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:image']" do + assert_dom "> @content", "#{root_url}picture.jpg" + end + assert_dom "head meta[property='og:image:alt']" do + assert_dom "> @content", "some local picture" + end + end + + def test_show_og_image_with_spaces + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "![some picture](https://example.com/the picture.jpg)") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:image']" do + assert_dom "> @content", "https://example.com/the%20picture.jpg" + end + assert_dom "head meta[property='og:image:alt']" do + assert_dom "> @content", "some picture" + end + end + + def test_show_og_image_with_relative_uri_and_spaces + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "![some local picture](/the picture.jpg)") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:image']" do + assert_dom "> @content", "#{root_url}the%20picture.jpg" + end + assert_dom "head meta[property='og:image:alt']" do + assert_dom "> @content", "some local picture" + end + end + + def test_show_og_image_with_invalid_uri + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "![](:)") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:image']" do + assert_dom "> @content", ActionController::Base.helpers.image_url("osm_logo_256.png", :host => root_url) + end + assert_dom "head meta[property='og:image:alt']" do + assert_dom "> @content", "OpenStreetMap logo" + end + end + + def test_show_og_image_without_alt + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:image']" do + assert_dom "> @content", "https://example.com/no_alt.gif" + end + assert_dom "head meta[property='og:image:alt']", :count => 0 + end + + def test_show_no_og_description + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "![nope](https://example.com/nope.jpg)") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:description']" do + assert_dom "> @content", I18n.t("layouts.intro_text") + end + end + + def test_show_og_description + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :body => "# Hello\n\n![hello](https://example.com/hello.jpg)\n\nFirst paragraph.\n\nSecond paragraph.") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='og:description']" do + assert_dom "> @content", "First paragraph." + end + end + + def test_show_article_published_time + user = create(:user) + diary_entry = create(:diary_entry, :user => user, :created_at => "2020-03-04") + + get diary_entry_path(user, diary_entry) + assert_response :success + assert_dom "head meta[property='article:published_time']" do + assert_dom "> @content", "2020-03-04T00:00:00Z" + end + end + def test_hide user = create(:user) diary_entry = create(:diary_entry, :user => user) # Try without logging in - post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) + post hide_diary_entry_path(user, diary_entry) assert_response :forbidden assert DiaryEntry.find(diary_entry.id).visible # Now try as a normal user session_for(user) - post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) + post hide_diary_entry_path(user, diary_entry) assert_redirected_to :controller => :errors, :action => :forbidden assert DiaryEntry.find(diary_entry.id).visible # Now try as a moderator session_for(create(:moderator_user)) - post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) + post hide_diary_entry_path(user, diary_entry) assert_redirected_to :action => :index, :display_name => user.display_name assert_not DiaryEntry.find(diary_entry.id).visible @@ -768,7 +814,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Finally try as an administrator session_for(create(:administrator_user)) - post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) + post hide_diary_entry_path(user, diary_entry) assert_redirected_to :action => :index, :display_name => user.display_name assert_not DiaryEntry.find(diary_entry.id).visible end @@ -778,19 +824,19 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Try without logging in diary_entry = create(:diary_entry, :user => user, :visible => false) - post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) + post unhide_diary_entry_path(user, diary_entry) assert_response :forbidden assert_not DiaryEntry.find(diary_entry.id).visible # Now try as a normal user session_for(user) - post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) + post unhide_diary_entry_path(user, diary_entry) assert_redirected_to :controller => :errors, :action => :forbidden assert_not DiaryEntry.find(diary_entry.id).visible # Now try as a moderator session_for(create(:moderator_user)) - post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) + post unhide_diary_entry_path(user, diary_entry) assert_redirected_to :action => :index, :display_name => user.display_name assert DiaryEntry.find(diary_entry.id).visible @@ -799,111 +845,16 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Finally try as an administrator session_for(create(:administrator_user)) - post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) + post unhide_diary_entry_path(user, diary_entry) assert_redirected_to :action => :index, :display_name => user.display_name assert DiaryEntry.find(diary_entry.id).visible end - def test_hidecomment - user = create(:user) - diary_entry = create(:diary_entry, :user => user) - diary_comment = create(:diary_comment, :diary_entry => diary_entry) - - # Try without logging in - post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment) - assert_response :forbidden - assert DiaryComment.find(diary_comment.id).visible - - # Now try as a normal user - session_for(user) - post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment) - assert_redirected_to :controller => :errors, :action => :forbidden - assert DiaryComment.find(diary_comment.id).visible - - # Try as a moderator - session_for(create(:moderator_user)) - post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment) - assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id - assert_not DiaryComment.find(diary_comment.id).visible - - # Reset - diary_comment.reload.update(:visible => true) - - # Finally try as an administrator - session_for(create(:administrator_user)) - post hide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment) - assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id - assert_not DiaryComment.find(diary_comment.id).visible - end - - def test_unhidecomment - user = create(:user) - diary_entry = create(:diary_entry, :user => user) - diary_comment = create(:diary_comment, :diary_entry => diary_entry, :visible => false) - - # Try without logging in - post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment) - assert_response :forbidden - assert_not DiaryComment.find(diary_comment.id).visible - - # Now try as a normal user - session_for(user) - post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment) - assert_redirected_to :controller => :errors, :action => :forbidden - assert_not DiaryComment.find(diary_comment.id).visible - - # Now try as a moderator - session_for(create(:moderator_user)) - post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment) - assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id - assert DiaryComment.find(diary_comment.id).visible - - # Reset - diary_comment.reload.update(:visible => true) - - # Finally try as an administrator - session_for(create(:administrator_user)) - post unhide_diary_comment_path(:display_name => user.display_name, :id => diary_entry, :comment => diary_comment) - assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id - assert DiaryComment.find(diary_comment.id).visible - end - - def test_comments - user = create(:user) - other_user = create(:user) - suspended_user = create(:user, :suspended) - deleted_user = create(:user, :deleted) - - # Test a user with no comments - get diary_comments_path(:display_name => user.display_name) - assert_response :success - assert_template :comments - assert_select "h4", :html => "No diary comments" - - # Test a user with a comment - create(:diary_comment, :user => other_user) - - get diary_comments_path(:display_name => other_user.display_name) - assert_response :success - assert_template :comments - assert_select "table.table-striped" do - assert_select "tr", :count => 2 # header and one comment - end - - # Test a suspended user - get diary_comments_path(:display_name => suspended_user.display_name) - assert_response :not_found - - # Test a deleted user - get diary_comments_path(:display_name => deleted_user.display_name) - assert_response :not_found - end - def test_subscribe_page user = create(:user) other_user = create(:user) diary_entry = create(:diary_entry, :user => user) - path = diary_entry_subscribe_path(:id => diary_entry, :display_name => user.display_name) + path = diary_entry_subscribe_path(user, diary_entry) get path assert_redirected_to login_path(:referer => path) @@ -912,7 +863,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest get path assert_response :success assert_dom ".content-body" do - assert_dom "a[href='#{diary_entry_path(:id => diary_entry, :display_name => user.display_name)}']", :text => diary_entry.title + assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title assert_dom "a[href='#{user_path(user)}']", :text => user.display_name end end @@ -924,7 +875,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest session_for(other_user) assert_difference "diary_entry.subscribers.count", 1 do - post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name) + post diary_entry_subscribe_path(user, diary_entry) end assert_response :redirect end @@ -937,20 +888,20 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # not signed in assert_no_difference "diary_entry.subscribers.count" do - post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name) + post diary_entry_subscribe_path(user, diary_entry) end assert_response :forbidden session_for(other_user) # bad diary id - post diary_entry_subscribe_path(:id => 999111, :display_name => "username") + post diary_entry_subscribe_path("username", 999111) assert_response :not_found # trying to subscribe when already subscribed - post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name) + post diary_entry_subscribe_path(user, diary_entry) assert_no_difference "diary_entry.subscribers.count" do - post diary_entry_subscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name) + post diary_entry_subscribe_path(user, diary_entry) end end @@ -958,7 +909,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest user = create(:user) other_user = create(:user) diary_entry = create(:diary_entry, :user => user) - path = diary_entry_unsubscribe_path(:id => diary_entry, :display_name => user.display_name) + path = diary_entry_unsubscribe_path(user, diary_entry) get path assert_redirected_to login_path(:referer => path) @@ -967,7 +918,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest get path assert_response :success assert_dom ".content-body" do - assert_dom "a[href='#{diary_entry_path(:id => diary_entry, :display_name => user.display_name)}']", :text => diary_entry.title + assert_dom "a[href='#{diary_entry_path(user, diary_entry)}']", :text => diary_entry.title assert_dom "a[href='#{user_path(user)}']", :text => user.display_name end end @@ -981,7 +932,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest session_for(other_user) assert_difference "diary_entry.subscribers.count", -1 do - post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name) + post diary_entry_unsubscribe_path(user, diary_entry) end assert_response :redirect end @@ -994,19 +945,19 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # not signed in assert_no_difference "diary_entry.subscribers.count" do - post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name) + post diary_entry_unsubscribe_path(user, diary_entry) end assert_response :forbidden session_for(other_user) # bad diary id - post diary_entry_unsubscribe_path(:id => 999111, :display_name => "username") + post diary_entry_unsubscribe_path("username", 999111) assert_response :not_found # trying to unsubscribe when not subscribed assert_no_difference "diary_entry.subscribers.count" do - post diary_entry_unsubscribe_path(:id => diary_entry, :display_name => diary_entry.user.display_name) + post diary_entry_unsubscribe_path(user, diary_entry) end end