4   class TracesControllerTest < ActionDispatch::IntegrationTest
 
   5     # Use temporary directories with unique names for each test
 
   6     # This allows the tests to be run in parallel.
 
   8       @gpx_trace_dir_orig = Settings.gpx_trace_dir
 
   9       @gpx_image_dir_orig = Settings.gpx_image_dir
 
  10       Settings.gpx_trace_dir = Dir.mktmpdir("trace", Rails.root.join("test/gpx"))
 
  11       Settings.gpx_image_dir = Dir.mktmpdir("image", Rails.root.join("test/gpx"))
 
  15       FileUtils.remove_dir(Settings.gpx_trace_dir)
 
  16       FileUtils.remove_dir(Settings.gpx_image_dir)
 
  17       Settings.gpx_trace_dir = @gpx_trace_dir_orig
 
  18       Settings.gpx_image_dir = @gpx_image_dir_orig
 
  22     # test all routes which lead to this controller
 
  25         { :path => "/api/0.6/gpx/create", :method => :post },
 
  26         { :controller => "api/traces", :action => "create" }
 
  29         { :path => "/api/0.6/gpx/1", :method => :get },
 
  30         { :controller => "api/traces", :action => "show", :id => "1" }
 
  33         { :path => "/api/0.6/gpx/1", :method => :put },
 
  34         { :controller => "api/traces", :action => "update", :id => "1" }
 
  37         { :path => "/api/0.6/gpx/1", :method => :delete },
 
  38         { :controller => "api/traces", :action => "destroy", :id => "1" }
 
  41         { :controller => "api/traces", :action => "show", :id => "1" },
 
  42         { :path => "/api/0.6/gpx/1/details", :method => :get }
 
  45         { :path => "/api/0.6/gpx/1/data", :method => :get },
 
  46         { :controller => "api/traces", :action => "data", :id => "1" }
 
  49         { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
 
  50         { :controller => "api/traces", :action => "data", :id => "1", :format => "xml" }
 
  54     # Check getting a specific trace through the api
 
  56       public_trace_file = create(:trace, :visibility => "public")
 
  59       get api_trace_path(public_trace_file)
 
  60       assert_response :unauthorized
 
  62       # Now with some other user, which should work since the trace is public
 
  63       auth_header = basic_authorization_header create(:user).display_name, "test"
 
  64       get api_trace_path(public_trace_file), :headers => auth_header
 
  65       assert_response :success
 
  67       # And finally we should be able to do it with the owner of the trace
 
  68       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
  69       get api_trace_path(public_trace_file), :headers => auth_header
 
  70       assert_response :success
 
  73     # Check an anoymous trace can't be specifically fetched by another user
 
  75       anon_trace_file = create(:trace, :visibility => "private")
 
  78       get api_trace_path(anon_trace_file)
 
  79       assert_response :unauthorized
 
  81       # Now try with another user, which shouldn't work since the trace is anon
 
  82       auth_header = basic_authorization_header create(:user).display_name, "test"
 
  83       get api_trace_path(anon_trace_file), :headers => auth_header
 
  84       assert_response :forbidden
 
  86       # And finally we should be able to get the trace details with the trace owner
 
  87       auth_header = basic_authorization_header anon_trace_file.user.display_name, "test"
 
  88       get api_trace_path(anon_trace_file), :headers => auth_header
 
  89       assert_response :success
 
  92     # Check the api details for a trace that doesn't exist
 
  93     def test_show_not_found
 
  94       deleted_trace_file = create(:trace, :deleted)
 
  96       # Try first with no auth, as it should require it
 
  97       get api_trace_path(:id => 0)
 
  98       assert_response :unauthorized
 
 100       # Login, and try again
 
 101       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
 
 102       get api_trace_path(:id => 0), :headers => auth_header
 
 103       assert_response :not_found
 
 105       # Now try a trace which did exist but has been deleted
 
 106       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
 
 107       get api_trace_path(deleted_trace_file), :headers => auth_header
 
 108       assert_response :not_found
 
 111     # Test downloading a trace through the api
 
 113       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
 
 116       get api_trace_data_path(public_trace_file)
 
 117       assert_response :unauthorized
 
 119       # Now with some other user, which should work since the trace is public
 
 120       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 121       get api_trace_data_path(public_trace_file), :headers => auth_header
 
 122       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
 
 124       # And finally we should be able to do it with the owner of the trace
 
 125       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
 126       get api_trace_data_path(public_trace_file), :headers => auth_header
 
 127       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
 
 130     # Test downloading a compressed trace through the api
 
 131     def test_data_compressed
 
 132       identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
 
 134       # Authenticate as the owner of the trace we will be using
 
 135       auth_header = basic_authorization_header identifiable_trace_file.user.display_name, "test"
 
 137       # First get the data as is
 
 138       get api_trace_data_path(identifiable_trace_file), :headers => auth_header
 
 139       check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
 
 141       # Now ask explicitly for XML format
 
 142       get api_trace_data_path(identifiable_trace_file, :format => "xml"), :headers => auth_header
 
 143       check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
 
 145       # Now ask explicitly for GPX format
 
 146       get api_trace_data_path(identifiable_trace_file, :format => "gpx"), :headers => auth_header
 
 147       check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
 
 150     # Check an anonymous trace can't be downloaded by another user through the api
 
 152       anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
 
 155       get api_trace_data_path(anon_trace_file)
 
 156       assert_response :unauthorized
 
 158       # Now with some other user, which shouldn't work since the trace is anon
 
 159       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 160       get api_trace_data_path(anon_trace_file), :headers => auth_header
 
 161       assert_response :forbidden
 
 163       # And finally we should be able to do it with the owner of the trace
 
 164       auth_header = basic_authorization_header anon_trace_file.user.display_name, "test"
 
 165       get api_trace_data_path(anon_trace_file), :headers => auth_header
 
 166       check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
 
 169     # Test downloading a trace that doesn't exist through the api
 
 170     def test_data_not_found
 
 171       deleted_trace_file = create(:trace, :deleted)
 
 173       # Try first with no auth, as it should require it
 
 174       get api_trace_data_path(:id => 0)
 
 175       assert_response :unauthorized
 
 177       # Login, and try again
 
 178       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 179       get api_trace_data_path(:id => 0), :headers => auth_header
 
 180       assert_response :not_found
 
 182       # Now try a trace which did exist but has been deleted
 
 183       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
 
 184       get api_trace_data_path(deleted_trace_file), :headers => auth_header
 
 185       assert_response :not_found
 
 188     # Test creating a trace through the api
 
 191       fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
 
 192       file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
 
 196       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
 
 197       assert_response :unauthorized
 
 203       create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
 
 204       assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
 
 205       auth_header = basic_authorization_header user.display_name, "test"
 
 206       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
 
 207       assert_response :success
 
 208       trace = Trace.find(response.body.to_i)
 
 209       assert_equal "a.gpx", trace.name
 
 210       assert_equal "New Trace", trace.description
 
 211       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
 
 212       assert_equal "trackable", trace.visibility
 
 213       assert_not trace.inserted
 
 214       assert_equal File.new(fixture).read, File.new(trace.trace_name).read
 
 216       assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
 
 221       # Now authenticated, with the legacy public flag
 
 222       assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
 
 223       auth_header = basic_authorization_header user.display_name, "test"
 
 224       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
 
 225       assert_response :success
 
 226       trace = Trace.find(response.body.to_i)
 
 227       assert_equal "a.gpx", trace.name
 
 228       assert_equal "New Trace", trace.description
 
 229       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
 
 230       assert_equal "public", trace.visibility
 
 231       assert_not trace.inserted
 
 232       assert_equal File.new(fixture).read, File.new(trace.trace_name).read
 
 234       assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
 
 239       # Now authenticated, with the legacy private flag
 
 240       second_user = create(:user)
 
 241       assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
 
 242       auth_header = basic_authorization_header second_user.display_name, "test"
 
 243       post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
 
 244       assert_response :success
 
 245       trace = Trace.find(response.body.to_i)
 
 246       assert_equal "a.gpx", trace.name
 
 247       assert_equal "New Trace", trace.description
 
 248       assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
 
 249       assert_equal "private", trace.visibility
 
 250       assert_not trace.inserted
 
 251       assert_equal File.new(fixture).read, File.new(trace.trace_name).read
 
 253       assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
 
 256     # Check updating a trace through the api
 
 258       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
 
 259       deleted_trace_file = create(:trace, :deleted)
 
 260       anon_trace_file = create(:trace, :visibility => "private")
 
 263       put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file)
 
 264       assert_response :unauthorized
 
 266       # Now with some other user, which should fail
 
 267       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 268       put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file), :headers => auth_header
 
 269       assert_response :forbidden
 
 271       # Now with a trace which doesn't exist
 
 272       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 273       put api_trace_path(:id => 0), :params => create_trace_xml(public_trace_file), :headers => auth_header
 
 274       assert_response :not_found
 
 276       # Now with a trace which did exist but has been deleted
 
 277       auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
 
 278       put api_trace_path(deleted_trace_file), :params => create_trace_xml(deleted_trace_file), :headers => auth_header
 
 279       assert_response :not_found
 
 281       # Now try an update with the wrong ID
 
 282       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
 283       put api_trace_path(public_trace_file), :params => create_trace_xml(anon_trace_file), :headers => auth_header
 
 284       assert_response :bad_request,
 
 285                       "should not be able to update a trace with a different ID from the XML"
 
 287       # And finally try an update that should work
 
 288       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
 289       t = public_trace_file
 
 290       t.description = "Changed description"
 
 291       t.visibility = "private"
 
 292       put api_trace_path(t), :params => create_trace_xml(t), :headers => auth_header
 
 293       assert_response :success
 
 294       nt = Trace.find(t.id)
 
 295       assert_equal nt.description, t.description
 
 296       assert_equal nt.visibility, t.visibility
 
 299     # Test that updating a trace doesn't duplicate the tags
 
 301       tracetag = create(:tracetag)
 
 302       trace = tracetag.trace
 
 303       auth_header = basic_authorization_header trace.user.display_name, "test"
 
 305       put api_trace_path(trace), :params => create_trace_xml(trace), :headers => auth_header
 
 306       assert_response :success
 
 308       updated = Trace.find(trace.id)
 
 309       # Ensure there's only one tag in the database after updating
 
 310       assert_equal(1, Tracetag.count)
 
 311       # The new tag object might have a different id, so check the string representation
 
 312       assert_equal trace.tagstring, updated.tagstring
 
 315     # Check deleting a trace through the api
 
 317       public_trace_file = create(:trace, :visibility => "public")
 
 320       delete api_trace_path(public_trace_file)
 
 321       assert_response :unauthorized
 
 323       # Now with some other user, which should fail
 
 324       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 325       delete api_trace_path(public_trace_file), :headers => auth_header
 
 326       assert_response :forbidden
 
 328       # Now with a trace which doesn't exist
 
 329       auth_header = basic_authorization_header create(:user).display_name, "test"
 
 330       delete api_trace_path(:id => 0), :headers => auth_header
 
 331       assert_response :not_found
 
 333       # And finally we should be able to do it with the owner of the trace
 
 334       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
 335       delete api_trace_path(public_trace_file), :headers => auth_header
 
 336       assert_response :success
 
 338       # Try it a second time, which should fail
 
 339       auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
 
 340       delete api_trace_path(public_trace_file), :headers => auth_header
 
 341       assert_response :not_found
 
 346     def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
 
 347       assert_response :success
 
 348       assert_equal digest, Digest::MD5.hexdigest(response.body)
 
 349       assert_equal content_type, response.media_type
 
 350       assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
 
 354     # build XML for traces
 
 355     # this builds a minimum viable XML for the tests in this suite
 
 356     def create_trace_xml(trace)
 
 357       root = XML::Document.new
 
 358       root.root = XML::Node.new "osm"
 
 359       trc = XML::Node.new "gpx_file"
 
 360       trc["id"] = trace.id.to_s
 
 361       trc["visibility"] = trace.visibility
 
 362       trc["visible"] = trace.visible.to_s
 
 363       desc = XML::Node.new "description"
 
 364       desc << trace.description
 
 366       trace.tags.each do |tag|
 
 367         t = XML::Node.new "tag"