X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/46875fdde3ecdc37bb69e892b15509132a3bf9cf..bde2faac581d7c16582e1012ba036f8c535773f2:/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 b1e9b72f2..e2300e27b 100644 --- a/test/controllers/diary_entries_controller_test.rb +++ b/test/controllers/diary_entries_controller_test.rb @@ -98,10 +98,18 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest { :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" } + ) assert_routing( { :path => "/user/username/diary/1/subscribe", :method => :post }, { :controller => "diary_entries", :action => "subscribe", :display_name => "username", :id => "1" } ) + assert_routing( + { :path => "/user/username/diary/1/unsubscribe", :method => :get }, + { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" } + ) assert_routing( { :path => "/user/username/diary/1/unsubscribe", :method => :post }, { :controller => "diary_entries", :action => "unsubscribe", :display_name => "username", :id => "1" } @@ -112,7 +120,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Make sure that you are redirected to the login page when you # are not logged in get new_diary_entry_path - assert_response :redirect assert_redirected_to login_path(:referer => "/diary/new") end @@ -135,7 +142,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 => 7 + assert_select "input", :count => 6 end end end @@ -164,7 +171,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_template :new - assert_nil UserPreference.where(:user_id => user.id, :k => "diary.default_language").first + assert_nil UserPreference.find_by(:user => user, :k => "diary.default_language") end def test_create @@ -176,7 +183,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1", :longitude => "2.2", :language_code => "en" }) end - assert_response :redirect assert_redirected_to :action => :index, :display_name => user.display_name entry = DiaryEntry.order(:id).last assert_equal user.id, entry.user_id @@ -189,7 +195,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # checks if user was subscribed assert_equal 1, entry.subscribers.length - assert_equal "en", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v + assert_equal "en", UserPreference.find_by(:user => user, :k => "diary.default_language").v end def test_create_german @@ -203,7 +209,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest :diary_entry => { :title => "New Title", :body => "This is a new body for the diary entry", :latitude => "1.1", :longitude => "2.2", :language_code => "de" }) end - assert_response :redirect assert_redirected_to :action => :index, :display_name => user.display_name entry = DiaryEntry.order(:id).last assert_equal user.id, entry.user_id @@ -216,7 +221,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # checks if user was subscribed assert_equal 1, entry.subscribers.length - assert_equal "de", UserPreference.where(:user_id => user.id, :k => "diary.default_language").first.v + assert_equal "de", UserPreference.find_by(:user => user, :k => "diary.default_language").v end def test_new_spammy @@ -232,7 +237,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest post diary_entries_path(:commit => "save", :diary_entry => { :title => spammy_title, :body => spammy_body, :language_code => "en" }) end - assert_response :redirect assert_redirected_to :action => :index, :display_name => user.display_name entry = DiaryEntry.order(:id).last assert_equal user.id, entry.user_id @@ -243,7 +247,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Follow the redirect get diary_entries_path(:display_name => user.display_name) - assert_response :redirect assert_redirected_to :controller => :users, :action => :suspended end @@ -256,7 +259,6 @@ 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) - assert_response :redirect assert_redirected_to login_path(:referer => "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit") session_for(other_user) @@ -264,7 +266,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # 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) - assert_response :redirect assert_redirected_to :action => :show, :display_name => entry.user.display_name, :id => entry.id session_for(entry.user) @@ -294,7 +295,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 => 8 + assert_select "input", :count => 7 end end @@ -307,7 +308,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 }) - assert_response :redirect 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 @@ -325,7 +325,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest assert_select "p", :text => /#{new_body}/, :count => 1 assert_select "abbr[class='geo'][title='#{number_with_precision(new_latitude, :precision => 4)}; #{number_with_precision(new_longitude, :precision => 4)}']", :count => 1 # As we're not logged in, check that you cannot edit - # print @response.body assert_select "a[href='/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}/edit']", :text => "Edit this entry", :count => 1 end @@ -400,7 +399,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 @@ -443,7 +441,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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 @@ -459,7 +456,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Follow the redirect get diary_entries_path(:display_name => user.display_name) - assert_response :redirect assert_redirected_to :controller => :users, :action => :suspended # Now show the diary entry, and check the new comment is not present @@ -505,7 +501,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Try a list of diary entries for your friends when not logged in get friends_diary_entries_path - assert_response :redirect assert_redirected_to login_path(:referer => "/diary/friends") # Try a list of diary entries for your friends when logged in @@ -525,7 +520,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Try a list of diary entries for nearby users when not logged in get nearby_diary_entries_path - assert_response :redirect assert_redirected_to login_path(:referer => "/diary/nearby") # Try a list of diary entries for nearby users when logged in @@ -564,12 +558,37 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Try and get the index get diary_entries_path assert_response :success - assert_select "div.diary_post", :count => 20 + assert_select "article.diary_post", :count => 20 + assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1 + assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1 # Try and get the second page - get diary_entries_path(:page => 2) + get css_select("li.page-item .page-link").last["href"] + assert_response :success + assert_select "article.diary_post", :count => 20 + assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1 + assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1 + + # Try and get the third page + get css_select("li.page-item .page-link").last["href"] assert_response :success - assert_select "div.diary_post", :count => 20 + assert_select "article.diary_post", :count => 10 + assert_select "li.page-item.disabled span.page-link", :text => "Older Entries", :count => 1 + assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1 + + # Go back to the second page + get css_select("li.page-item .page-link").first["href"] + assert_response :success + assert_select "article.diary_post", :count => 20 + assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1 + assert_select "li.page-item a.page-link", :text => "Newer Entries", :count => 1 + + # Go back to the first page + get css_select("li.page-item .page-link").first["href"] + assert_response :success + assert_select "article.diary_post", :count => 20 + assert_select "li.page-item a.page-link", :text => "Older Entries", :count => 1 + assert_select "li.page-item.disabled span.page-link", :text => "Newer Entries", :count => 1 end def test_rss @@ -653,7 +672,7 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest get diary_rss_path assert_select "rss>channel>item", :count => 2 - with_diary_feed_delay(6) do + with_settings(:diary_feed_delay => 6) do get diary_rss_path assert_select "rss>channel>item", :count => 1 end @@ -670,20 +689,37 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_template :show + # Try a non-integer ID + get "/user/#{CGI.escape(user.display_name)}/diary/#{diary_entry.id})" + assert_response :not_found + assert_template "rescues/routing_error" + # 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) assert_response :not_found # Try an entry by a suspended user - diary_entry_suspended = create(:diary_entry, :user => suspended_user) - get diary_entry_path(:display_name => suspended_user.display_name, :id => diary_entry_suspended) + 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) assert_response :not_found # Try an entry by a deleted user - diary_entry_deleted = create(:diary_entry, :user => deleted_user) - get diary_entry_path(:display_name => deleted_user.display_name, :id => diary_entry_deleted) + 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) 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) + 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) + assert_response :success + assert_template :show end def test_show_hidden_comments @@ -718,14 +754,12 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Now try as a normal user session_for(user) post hide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) - assert_response :redirect 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) - assert_response :redirect assert_redirected_to :action => :index, :display_name => user.display_name assert_not DiaryEntry.find(diary_entry.id).visible @@ -735,7 +769,6 @@ 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) - assert_response :redirect assert_redirected_to :action => :index, :display_name => user.display_name assert_not DiaryEntry.find(diary_entry.id).visible end @@ -752,21 +785,21 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # Now try as a normal user session_for(user) post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) - assert_response :redirect 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) - assert_response :redirect - assert_redirected_to :controller => :errors, :action => :forbidden - assert_not DiaryEntry.find(diary_entry.id).visible + assert_redirected_to :action => :index, :display_name => user.display_name + assert DiaryEntry.find(diary_entry.id).visible + + # Reset + diary_entry.reload.update(:visible => true) # Finally try as an administrator session_for(create(:administrator_user)) post unhide_diary_entry_path(:display_name => user.display_name, :id => diary_entry) - assert_response :redirect assert_redirected_to :action => :index, :display_name => user.display_name assert DiaryEntry.find(diary_entry.id).visible end @@ -784,14 +817,12 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # 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_response :redirect 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_response :redirect assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id assert_not DiaryComment.find(diary_comment.id).visible @@ -801,7 +832,6 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # 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_response :redirect assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id assert_not DiaryComment.find(diary_comment.id).visible end @@ -819,21 +849,21 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest # 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_response :redirect 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_response :redirect - assert_redirected_to :controller => :errors, :action => :forbidden - assert_not DiaryComment.find(diary_comment.id).visible + 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_response :redirect assert_redirected_to :action => :show, :display_name => user.display_name, :id => diary_entry.id assert DiaryComment.find(diary_comment.id).visible end @@ -869,6 +899,24 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest 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) + + get path + assert_redirected_to login_path(:referer => path) + + session_for(other_user) + 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='#{user_path(user)}']", :text => user.display_name + end + end + def test_subscribe_success user = create(:user) other_user = create(:user) @@ -906,6 +954,24 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest end end + def test_unsubscribe_page + 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) + + get path + assert_redirected_to login_path(:referer => path) + + session_for(other_user) + 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='#{user_path(user)}']", :text => user.display_name + end + end + def test_unsubscribe_success user = create(:user) other_user = create(:user) @@ -950,19 +1016,10 @@ class DiaryEntriesControllerTest < ActionDispatch::IntegrationTest assert_response :success assert_template "index" assert_no_missing_translations - assert_select "div.diary_post", entries.count + assert_select "article.diary_post", entries.count entries.each do |entry| assert_select "a[href=?]", "/user/#{ERB::Util.u(entry.user.display_name)}/diary/#{entry.id}" end end - - def with_diary_feed_delay(value) - diary_feed_delay = Settings.diary_feed_delay - Settings.diary_feed_delay = value - - yield - - Settings.diary_feed_delay = diary_feed_delay - end end