1 # frozen_string_literal: true
 
   6   class DataControllerTest < ActionDispatch::IntegrationTest
 
   8     # test all routes which lead to this controller
 
  11         { :path => "/traces/1/data", :method => :get },
 
  12         { :controller => "traces/data", :action => "show", :trace_id => "1" }
 
  15         { :path => "/traces/1/data.xml", :method => :get },
 
  16         { :controller => "traces/data", :action => "show", :trace_id => "1", :format => "xml" }
 
  20       assert_redirected_to "/traces/1/data"
 
  22       get "/trace/1/data.xml"
 
  23       assert_redirected_to "/traces/1/data.xml"
 
  26     # Test downloading a trace
 
  28       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
 
  30       # First with no auth, which should work since the trace is public
 
  31       get trace_data_path(public_trace_file)
 
  34       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
 
  36       # Now with some other user, which should work since the trace is public
 
  37       session_for(create(:user))
 
  38       get trace_data_path(public_trace_file)
 
  41       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
 
  43       # And finally we should be able to do it with the owner of the trace
 
  44       session_for(public_trace_file.user)
 
  45       get trace_data_path(public_trace_file)
 
  48       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
 
  51     # Test downloading a compressed trace
 
  52     def test_show_compressed
 
  53       identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
 
  55       # First get the data as is
 
  56       get trace_data_path(identifiable_trace_file)
 
  59       check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
 
  61       # Now ask explicitly for XML format
 
  62       get trace_data_path(identifiable_trace_file, :format => "xml")
 
  63       check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
 
  65       # Now ask explicitly for GPX format
 
  66       get trace_data_path(identifiable_trace_file, :format => "gpx")
 
  67       check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
 
  70     # Check an anonymous trace can't be downloaded by another user
 
  72       anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
 
  75       get trace_data_path(anon_trace_file)
 
  76       assert_response :not_found
 
  78       # Now with some other user, which shouldn't work since the trace is anon
 
  79       session_for(create(:user))
 
  80       get trace_data_path(anon_trace_file)
 
  81       assert_response :not_found
 
  83       # And finally we should be able to do it with the owner of the trace
 
  84       session_for(anon_trace_file.user)
 
  85       get trace_data_path(anon_trace_file)
 
  88       check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
 
  91     # Test downloading a trace that doesn't exist
 
  92     def test_show_not_found
 
  93       deleted_trace_file = create(:trace, :deleted)
 
  95       # First with a trace that has never existed
 
  96       get trace_data_path(0)
 
  97       assert_response :not_found
 
  99       # Now with a trace that has been deleted
 
 100       session_for(deleted_trace_file.user)
 
 101       get trace_data_path(deleted_trace_file)
 
 102       assert_response :not_found
 
 105     def test_show_offline
 
 106       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
 
 107       with_settings(:status => "gpx_offline") do
 
 108         get trace_data_path(public_trace_file)
 
 109         assert_response :success
 
 110         assert_template :offline
 
 116     def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
 
 117       assert_equal digest, Digest::MD5.hexdigest(response.body)
 
 118       assert_equal content_type, response.media_type
 
 119       assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]