X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/2d09b9408b4f3f9a7c7a5782098c6c5b81ae1206..17bea98e199f204b22847356fc64beaa81a9373a:/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 d0453bf08..aad759b5b 100644 --- a/test/controllers/diary_entries_controller_test.rb +++ b/test/controllers/diary_entries_controller_test.rb @@ -69,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" } @@ -123,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 @@ -167,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 @@ -193,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 @@ -221,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 @@ -276,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 @@ -340,111 +336,27 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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(entry.user, 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(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 - 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(entry.user, 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(entry.user, 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(entry.user, 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(entry.user, 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(entry.user, 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 @@ -745,6 +657,9 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 @@ -756,6 +671,9 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 @@ -767,6 +685,9 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 @@ -778,6 +699,9 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 @@ -789,6 +713,9 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 @@ -800,6 +727,54 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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