4   class TracesControllerTest < ActionDispatch::IntegrationTest
 
   6     # test all routes which lead to this controller
 
   9         { :path => "/api/0.6/gpx/create", :method => :post },
 
  10         { :controller => "api/traces", :action => "create" }
 
  13         { :path => "/api/0.6/gpx/1", :method => :get },
 
  14         { :controller => "api/traces", :action => "show", :id => "1" }
 
  17         { :path => "/api/0.6/gpx/1", :method => :put },
 
  18         { :controller => "api/traces", :action => "update", :id => "1" }
 
  21         { :path => "/api/0.6/gpx/1", :method => :delete },
 
  22         { :controller => "api/traces", :action => "destroy", :id => "1" }
 
  25         { :controller => "api/traces", :action => "show", :id => "1" },
 
  26         { :path => "/api/0.6/gpx/1/details", :method => :get }
 
  29         { :path => "/api/0.6/gpx/1/data", :method => :get },
 
  30         { :controller => "api/traces", :action => "data", :id => "1" }
 
  33         { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
 
  34         { :controller => "api/traces", :action => "data", :id => "1", :format => "xml" }
 
  38     # Check getting a specific trace through the api
 
  40       public_trace_file = create(:trace, :visibility => "public")
 
  43       get api_trace_path(public_trace_file)
 
  44       assert_response :unauthorized
 
  46       # Now with some other user, which should work since the trace is public
 
  47       auth_header = basic_authorization_header create(:user).display_name, "test"
 
  48       get api_trace_path(public_trace_file), :headers => auth_header
 
  49       assert_response :success
 
  51       # And finally we should be able to do it with the owner of the trace
 
  52       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
  53       get api_trace_path(public_trace_file), :headers => auth_header
 
  54       assert_response :success
 
  55       assert_select "gpx_file[id='#{public_trace_file.id}'][uid='#{public_trace_file.user.id}']", 1
 
  58     # Check an anonymous trace can't be specifically fetched by another user
 
  60       anon_trace_file = create(:trace, :visibility => "private")
 
  63       get api_trace_path(anon_trace_file)
 
  64       assert_response :unauthorized
 
  66       # Now try with another user, which shouldn't work since the trace is anon
 
  67       auth_header = basic_authorization_header create(:user).display_name, "test"
 
  68       get api_trace_path(anon_trace_file), :headers => auth_header
 
  69       assert_response :forbidden
 
  71       # And finally we should be able to get the trace details with the trace owner
 
  72       auth_header = basic_authorization_header anon_trace_file.user.display_name, "test"
 
  73       get api_trace_path(anon_trace_file), :headers => auth_header
 
  74       assert_response :success
 
  77     # Check the api details for a trace that doesn't exist
 
  78     def test_show_not_found
 
  79       deleted_trace_file = create(:trace, :deleted)
 
  81       # Try first with no auth, as it should require it
 
  82       get api_trace_path(:id => 0)
 
  83       assert_response :unauthorized
 
  85       # Login, and try again
 
  86       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
 
  87       get api_trace_path(:id => 0), :headers => auth_header
 
  88       assert_response :not_found
 
  90       # Now try a trace which did exist but has been deleted
 
  91       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
 
  92       get api_trace_path(deleted_trace_file), :headers => auth_header
 
  93       assert_response :not_found
 
  96     # Test downloading a trace through the api
 
  98       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
 
 101       get api_trace_data_path(public_trace_file)
 
 102       assert_response :unauthorized
 
 104       # Now with some other user, which should work since the trace is public
 
 105       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 106       get api_trace_data_path(public_trace_file), :headers => auth_header
 
 109       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
 
 111       # And finally we should be able to do it with the owner of the trace
 
 112       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
 113       get api_trace_data_path(public_trace_file), :headers => auth_header
 
 116       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
 
 119     # Test downloading a compressed trace through the api
 
 120     def test_data_compressed
 
 121       identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
 
 123       # Authenticate as the owner of the trace we will be using
 
 124       auth_header = basic_authorization_header identifiable_trace_file.user.display_name, "test"
 
 126       # First get the data as is
 
 127       get api_trace_data_path(identifiable_trace_file), :headers => auth_header
 
 130       check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
 
 132       # Now ask explicitly for XML format
 
 133       get api_trace_data_path(identifiable_trace_file, :format => "xml"), :headers => auth_header
 
 134       check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
 
 136       # Now ask explicitly for GPX format
 
 137       get api_trace_data_path(identifiable_trace_file, :format => "gpx"), :headers => auth_header
 
 138       check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
 
 141     # Check an anonymous trace can't be downloaded by another user through the api
 
 143       anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
 
 146       get api_trace_data_path(anon_trace_file)
 
 147       assert_response :unauthorized
 
 149       # Now with some other user, which shouldn't work since the trace is anon
 
 150       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 151       get api_trace_data_path(anon_trace_file), :headers => auth_header
 
 152       assert_response :forbidden
 
 154       # And finally we should be able to do it with the owner of the trace
 
 155       auth_header = basic_authorization_header anon_trace_file.user.display_name, "test"
 
 156       get api_trace_data_path(anon_trace_file), :headers => auth_header
 
 159       check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
 
 162     # Test downloading a trace that doesn't exist through the api
 
 163     def test_data_not_found
 
 164       deleted_trace_file = create(:trace, :deleted)
 
 166       # Try first with no auth, as it should require it
 
 167       get api_trace_data_path(:id => 0)
 
 168       assert_response :unauthorized
 
 170       # Login, and try again
 
 171       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 172       get api_trace_data_path(:id => 0), :headers => auth_header
 
 173       assert_response :not_found
 
 175       # Now try a trace which did exist but has been deleted
 
 176       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
 
 177       get api_trace_data_path(deleted_trace_file), :headers => auth_header
 
 178       assert_response :not_found
 
 181     # Test creating a trace through the api
 
 184       fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
 
 185       file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
 
 189       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
 
 190       assert_response :unauthorized
 
 196       create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
 
 197       assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
 
 198       auth_header = basic_authorization_header user.display_name, "test"
 
 199       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
 
 200       assert_response :success
 
 201       trace = Trace.find(response.body.to_i)
 
 202       assert_equal "a.gpx", trace.name
 
 203       assert_equal "New Trace", trace.description
 
 204       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
 
 205       assert_equal "trackable", trace.visibility
 
 206       assert_not trace.inserted
 
 207       assert_equal File.new(fixture).read, trace.file.blob.download
 
 209       assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
 
 214       # Now authenticated, with the legacy public flag
 
 215       assert_not_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
 
 216       auth_header = basic_authorization_header user.display_name, "test"
 
 217       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
 
 218       assert_response :success
 
 219       trace = Trace.find(response.body.to_i)
 
 220       assert_equal "a.gpx", trace.name
 
 221       assert_equal "New Trace", trace.description
 
 222       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
 
 223       assert_equal "public", trace.visibility
 
 224       assert_not trace.inserted
 
 225       assert_equal File.new(fixture).read, trace.file.blob.download
 
 227       assert_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
 
 232       # Now authenticated, with the legacy private flag
 
 233       second_user = create(:user)
 
 234       assert_nil second_user.preferences.find_by(:k => "gps.trace.visibility")
 
 235       auth_header = basic_authorization_header second_user.display_name, "test"
 
 236       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
 
 237       assert_response :success
 
 238       trace = Trace.find(response.body.to_i)
 
 239       assert_equal "a.gpx", trace.name
 
 240       assert_equal "New Trace", trace.description
 
 241       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
 
 242       assert_equal "private", trace.visibility
 
 243       assert_not trace.inserted
 
 244       assert_equal File.new(fixture).read, trace.file.blob.download
 
 246       assert_equal "private", second_user.preferences.find_by(:k => "gps.trace.visibility").v
 
 249     # Check updating a trace through the api
 
 251       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
 
 252       deleted_trace_file = create(:trace, :deleted)
 
 253       anon_trace_file = create(:trace, :visibility => "private")
 
 256       put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file)
 
 257       assert_response :unauthorized
 
 259       # Now with some other user, which should fail
 
 260       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 261       put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file), :headers => auth_header
 
 262       assert_response :forbidden
 
 264       # Now with a trace which doesn't exist
 
 265       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 266       put api_trace_path(:id => 0), :params => create_trace_xml(public_trace_file), :headers => auth_header
 
 267       assert_response :not_found
 
 269       # Now with a trace which did exist but has been deleted
 
 270       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
 
 271       put api_trace_path(deleted_trace_file), :params => create_trace_xml(deleted_trace_file), :headers => auth_header
 
 272       assert_response :not_found
 
 274       # Now try an update with the wrong ID
 
 275       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
 276       put api_trace_path(public_trace_file), :params => create_trace_xml(anon_trace_file), :headers => auth_header
 
 277       assert_response :bad_request,
 
 278                       "should not be able to update a trace with a different ID from the XML"
 
 280       # And finally try an update that should work
 
 281       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
 282       t = public_trace_file
 
 283       t.description = "Changed description"
 
 284       t.visibility = "private"
 
 285       put api_trace_path(t), :params => create_trace_xml(t), :headers => auth_header
 
 286       assert_response :success
 
 287       nt = Trace.find(t.id)
 
 288       assert_equal nt.description, t.description
 
 289       assert_equal nt.visibility, t.visibility
 
 292     # Test that updating a trace doesn't duplicate the tags
 
 294       tracetag = create(:tracetag)
 
 295       trace = tracetag.trace
 
 296       auth_header = basic_authorization_header trace.user.display_name, "test"
 
 298       put api_trace_path(trace), :params => create_trace_xml(trace), :headers => auth_header
 
 299       assert_response :success
 
 301       updated = Trace.find(trace.id)
 
 302       # Ensure there's only one tag in the database after updating
 
 303       assert_equal(1, Tracetag.count)
 
 304       # The new tag object might have a different id, so check the string representation
 
 305       assert_equal trace.tagstring, updated.tagstring
 
 308     # Check deleting a trace through the api
 
 310       public_trace_file = create(:trace, :visibility => "public")
 
 313       delete api_trace_path(public_trace_file)
 
 314       assert_response :unauthorized
 
 316       # Now with some other user, which should fail
 
 317       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 318       delete api_trace_path(public_trace_file), :headers => auth_header
 
 319       assert_response :forbidden
 
 321       # Now with a trace which doesn't exist
 
 322       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 323       delete api_trace_path(:id => 0), :headers => auth_header
 
 324       assert_response :not_found
 
 326       # And finally we should be able to do it with the owner of the trace
 
 327       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
 328       delete api_trace_path(public_trace_file), :headers => auth_header
 
 329       assert_response :success
 
 331       # Try it a second time, which should fail
 
 332       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
 333       delete api_trace_path(public_trace_file), :headers => auth_header
 
 334       assert_response :not_found
 
 339     def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
 
 340       assert_response :success
 
 341       assert_equal digest, Digest::MD5.hexdigest(response.body)
 
 342       assert_equal content_type, response.media_type
 
 343       assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
 
 347     # build XML for traces
 
 348     # this builds a minimum viable XML for the tests in this suite
 
 349     def create_trace_xml(trace)
 
 350       root = XML::Document.new
 
 351       root.root = XML::Node.new "osm"
 
 352       trc = XML::Node.new "gpx_file"
 
 353       trc["id"] = trace.id.to_s
 
 354       trc["visibility"] = trace.visibility
 
 355       trc["visible"] = trace.visible.to_s
 
 356       desc = XML::Node.new "description"
 
 357       desc << trace.description
 
 359       trace.tags.each do |tag|
 
 360         t = XML::Node.new "tag"