X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/03408b6294e0aa56d46823c1f44d47aaf69e226b..646dcb62fca67b6dd52d8a46fef5fc260baa4b83:/test/controllers/trace_controller_test.rb diff --git a/test/controllers/trace_controller_test.rb b/test/controllers/trace_controller_test.rb index 1e7642e36..a9008bc97 100644 --- a/test/controllers/trace_controller_test.rb +++ b/test/controllers/trace_controller_test.rb @@ -1,4 +1,5 @@ require "test_helper" +require "digest" require "minitest/mock" class TraceControllerTest < ActionController::TestCase @@ -257,6 +258,26 @@ class TraceControllerTest < ActionController::TestCase assert_template "user/no_such_user" end + # Check a multi-page list + def test_list_paged + # Create several pages worth of traces + create_list(:trace, 50) + + # Try and get the list + get :list + assert_response :success + assert_select "table#trace_list tbody", :count => 1 do + assert_select "tr", :count => 20 + end + + # Try and get the second page + get :list, :params => { :page => 2 } + assert_response :success + assert_select "table#trace_list tbody", :count => 1 do + assert_select "tr", :count => 20 + end + end + # Check that the rss loads def test_rss user = create(:user) @@ -335,15 +356,15 @@ class TraceControllerTest < ActionController::TestCase # First with no auth, which should work since the trace is public get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id } - check_trace_data public_trace_file + check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9" # Now with some other user, which should work since the trace is public get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) } - check_trace_data public_trace_file + check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9" # And finally we should be able to do it with the owner of the trace get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user } - check_trace_data public_trace_file + check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9" end # Test downloading a compressed trace @@ -352,15 +373,15 @@ class TraceControllerTest < ActionController::TestCase # First get the data as is get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id } - check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz" + check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz" # Now ask explicitly for XML format get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" } - check_trace_data identifiable_trace_file, "application/xml", "xml" + check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml" # Now ask explicitly for GPX format get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" } - check_trace_data identifiable_trace_file + check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d" end # Check an anonymous trace can't be downloaded by another user @@ -377,7 +398,7 @@ class TraceControllerTest < ActionController::TestCase # And finally we should be able to do it with the owner of the trace get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user } - check_trace_data anon_trace_file + check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732" end # Test downloading a trace that doesn't exist @@ -529,6 +550,9 @@ class TraceControllerTest < ActionController::TestCase post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } } assert_response :forbidden + # Rewind the file + file.rewind + # Now authenticated create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable") assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v @@ -655,12 +679,22 @@ class TraceControllerTest < ActionController::TestCase post :delete, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user } assert_response :not_found - # Finally with a trace that we are allowed to delete + # Now with a trace that we are allowed to delete post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user } assert_response :redirect assert_redirected_to :action => :list, :display_name => public_trace_file.user.display_name trace = Trace.find(public_trace_file.id) assert_equal false, trace.visible + + # Finally with a trace that is deleted by an admin + public_trace_file = create(:trace, :visibility => "public") + admin = create(:administrator_user) + + post :delete, :params => { :display_name => admin.display_name, :id => public_trace_file.id }, :session => { :user => admin } + assert_response :redirect + assert_redirected_to :action => :list, :display_name => admin.display_name + trace = Trace.find(public_trace_file.id) + assert_equal false, trace.visible end # Check getting a specific trace through the api @@ -672,12 +706,12 @@ class TraceControllerTest < ActionController::TestCase assert_response :unauthorized # Now with some other user, which should work since the trace is public - basic_authorization(create(:user).display_name, "test") + basic_authorization create(:user).display_name, "test" get :api_read, :params => { :id => public_trace_file.id } assert_response :success # And finally we should be able to do it with the owner of the trace - basic_authorization(public_trace_file.user.display_name, "test") + basic_authorization public_trace_file.user.display_name, "test" get :api_read, :params => { :id => public_trace_file.id } assert_response :success end @@ -691,12 +725,12 @@ class TraceControllerTest < ActionController::TestCase assert_response :unauthorized # Now try with another user, which shouldn't work since the trace is anon - basic_authorization(create(:user).display_name, "test") + basic_authorization create(:user).display_name, "test" get :api_read, :params => { :id => anon_trace_file.id } assert_response :forbidden # And finally we should be able to get the trace details with the trace owner - basic_authorization(anon_trace_file.user.display_name, "test") + basic_authorization anon_trace_file.user.display_name, "test" get :api_read, :params => { :id => anon_trace_file.id } assert_response :success end @@ -710,12 +744,12 @@ class TraceControllerTest < ActionController::TestCase assert_response :unauthorized # Login, and try again - basic_authorization(deleted_trace_file.user.display_name, "test") + basic_authorization deleted_trace_file.user.display_name, "test" get :api_read, :params => { :id => 0 } assert_response :not_found # Now try a trace which did exist but has been deleted - basic_authorization(deleted_trace_file.user.display_name, "test") + basic_authorization deleted_trace_file.user.display_name, "test" get :api_read, :params => { :id => deleted_trace_file.id } assert_response :not_found end @@ -729,14 +763,14 @@ class TraceControllerTest < ActionController::TestCase assert_response :unauthorized # Now with some other user, which should work since the trace is public - basic_authorization(create(:user).display_name, "test") + basic_authorization create(:user).display_name, "test" get :api_data, :params => { :id => public_trace_file.id } - check_trace_data public_trace_file + check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9" # And finally we should be able to do it with the owner of the trace - basic_authorization(public_trace_file.user.display_name, "test") + basic_authorization public_trace_file.user.display_name, "test" get :api_data, :params => { :id => public_trace_file.id } - check_trace_data public_trace_file + check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9" end # Test downloading a compressed trace through the api @@ -744,19 +778,19 @@ class TraceControllerTest < ActionController::TestCase identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d") # Authenticate as the owner of the trace we will be using - basic_authorization(identifiable_trace_file.user.display_name, "test") + basic_authorization identifiable_trace_file.user.display_name, "test" # First get the data as is get :api_data, :params => { :id => identifiable_trace_file.id } - check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz" + check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz" # Now ask explicitly for XML format get :api_data, :params => { :id => identifiable_trace_file.id, :format => "xml" } - check_trace_data identifiable_trace_file, "application/xml", "xml" + check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml" # Now ask explicitly for GPX format get :api_data, :params => { :id => identifiable_trace_file.id, :format => "gpx" } - check_trace_data identifiable_trace_file + check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d" end # Check an anonymous trace can't be downloaded by another user through the api @@ -768,14 +802,14 @@ class TraceControllerTest < ActionController::TestCase assert_response :unauthorized # Now with some other user, which shouldn't work since the trace is anon - basic_authorization(create(:user).display_name, "test") + basic_authorization create(:user).display_name, "test" get :api_data, :params => { :id => anon_trace_file.id } assert_response :forbidden # And finally we should be able to do it with the owner of the trace - basic_authorization(anon_trace_file.user.display_name, "test") + basic_authorization anon_trace_file.user.display_name, "test" get :api_data, :params => { :id => anon_trace_file.id } - check_trace_data anon_trace_file + check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732" end # Test downloading a trace that doesn't exist through the api @@ -787,12 +821,12 @@ class TraceControllerTest < ActionController::TestCase assert_response :unauthorized # Login, and try again - basic_authorization(create(:user).display_name, "test") + basic_authorization create(:user).display_name, "test" get :api_data, :params => { :id => 0 } assert_response :not_found # Now try a trace which did exist but has been deleted - basic_authorization(deleted_trace_file.user.display_name, "test") + basic_authorization deleted_trace_file.user.display_name, "test" get :api_data, :params => { :id => deleted_trace_file.id } assert_response :not_found end @@ -808,10 +842,13 @@ class TraceControllerTest < ActionController::TestCase post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" } assert_response :unauthorized + # Rewind the file + file.rewind + # Now authenticated create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable") assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v - basic_authorization(user.display_name, "test") + basic_authorization user.display_name, "test" post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" } assert_response :success trace = Trace.find(response.body.to_i) @@ -829,7 +866,7 @@ class TraceControllerTest < ActionController::TestCase # Now authenticated, with the legacy public flag assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v - basic_authorization(user.display_name, "test") + basic_authorization user.display_name, "test" post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 } assert_response :success trace = Trace.find(response.body.to_i) @@ -848,7 +885,7 @@ class TraceControllerTest < ActionController::TestCase # Now authenticated, with the legacy private flag second_user = create(:user) assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first - basic_authorization(second_user.display_name, "test") + basic_authorization second_user.display_name, "test" post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 } assert_response :success trace = Trace.find(response.body.to_i) @@ -874,32 +911,32 @@ class TraceControllerTest < ActionController::TestCase assert_response :unauthorized # Now with some other user, which should fail - basic_authorization(create(:user).display_name, "test") + basic_authorization create(:user).display_name, "test" content public_trace_file.to_xml put :api_update, :params => { :id => public_trace_file.id } assert_response :forbidden # Now with a trace which doesn't exist - basic_authorization(create(:user).display_name, "test") + basic_authorization create(:user).display_name, "test" content public_trace_file.to_xml put :api_update, :params => { :id => 0 } assert_response :not_found # Now with a trace which did exist but has been deleted - basic_authorization(deleted_trace_file.user.display_name, "test") + basic_authorization deleted_trace_file.user.display_name, "test" content deleted_trace_file.to_xml put :api_update, :params => { :id => deleted_trace_file.id } assert_response :not_found # Now try an update with the wrong ID - basic_authorization(public_trace_file.user.display_name, "test") + basic_authorization public_trace_file.user.display_name, "test" content anon_trace_file.to_xml put :api_update, :params => { :id => public_trace_file.id } assert_response :bad_request, "should not be able to update a trace with a different ID from the XML" # And finally try an update that should work - basic_authorization(public_trace_file.user.display_name, "test") + basic_authorization public_trace_file.user.display_name, "test" t = public_trace_file t.description = "Changed description" t.visibility = "private" @@ -911,6 +948,23 @@ class TraceControllerTest < ActionController::TestCase assert_equal nt.visibility, t.visibility end + # Test that updating a trace doesn't duplicate the tags + def test_api_update_tags + tracetag = create(:tracetag) + trace = tracetag.trace + basic_authorization trace.user.display_name, "test" + + content trace.to_xml + put :api_update, :params => { :id => trace.id } + assert_response :success + + updated = Trace.find(trace.id) + # Ensure there's only one tag in the database after updating + assert_equal Tracetag.count, 1 + # The new tag object might have a different id, so check the string representation + assert_equal trace.tagstring, updated.tagstring + end + # Check deleting a trace through the api def test_api_delete public_trace_file = create(:trace, :visibility => "public") @@ -920,22 +974,22 @@ class TraceControllerTest < ActionController::TestCase assert_response :unauthorized # Now with some other user, which should fail - basic_authorization(create(:user).display_name, "test") + basic_authorization create(:user).display_name, "test" delete :api_delete, :params => { :id => public_trace_file.id } assert_response :forbidden # Now with a trace which doesn't exist - basic_authorization(create(:user).display_name, "test") + basic_authorization create(:user).display_name, "test" delete :api_delete, :params => { :id => 0 } assert_response :not_found # And finally we should be able to do it with the owner of the trace - basic_authorization(public_trace_file.user.display_name, "test") + basic_authorization public_trace_file.user.display_name, "test" delete :api_delete, :params => { :id => public_trace_file.id } assert_response :success # Try it a second time, which should fail - basic_authorization(public_trace_file.user.display_name, "test") + basic_authorization public_trace_file.user.display_name, "test" delete :api_delete, :params => { :id => public_trace_file.id } assert_response :not_found end @@ -997,8 +1051,9 @@ class TraceControllerTest < ActionController::TestCase end end - def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx") + def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx") assert_response :success + assert_equal digest, Digest::MD5.hexdigest(response.body) assert_equal content_type, response.content_type assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"] end