4 class DataControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/traces/1/data", :method => :get },
10 { :controller => "traces/data", :action => "show", :trace_id => "1" }
13 { :path => "/traces/1/data.xml", :method => :get },
14 { :controller => "traces/data", :action => "show", :trace_id => "1", :format => "xml" }
18 assert_redirected_to "/traces/1/data"
20 get "/trace/1/data.xml"
21 assert_redirected_to "/traces/1/data.xml"
24 # Test downloading a trace
26 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
28 # First with no auth, which should work since the trace is public
29 get trace_data_path(public_trace_file)
32 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
34 # Now with some other user, which should work since the trace is public
35 session_for(create(:user))
36 get trace_data_path(public_trace_file)
39 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
41 # And finally we should be able to do it with the owner of the trace
42 session_for(public_trace_file.user)
43 get trace_data_path(public_trace_file)
46 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
49 # Test downloading a compressed trace
50 def test_show_compressed
51 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
53 # First get the data as is
54 get trace_data_path(identifiable_trace_file)
57 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
59 # Now ask explicitly for XML format
60 get trace_data_path(identifiable_trace_file, :format => "xml")
61 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
63 # Now ask explicitly for GPX format
64 get trace_data_path(identifiable_trace_file, :format => "gpx")
65 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
68 # Check an anonymous trace can't be downloaded by another user
70 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
73 get trace_data_path(anon_trace_file)
74 assert_response :not_found
76 # Now with some other user, which shouldn't work since the trace is anon
77 session_for(create(:user))
78 get trace_data_path(anon_trace_file)
79 assert_response :not_found
81 # And finally we should be able to do it with the owner of the trace
82 session_for(anon_trace_file.user)
83 get trace_data_path(anon_trace_file)
86 check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
89 # Test downloading a trace that doesn't exist
90 def test_show_not_found
91 deleted_trace_file = create(:trace, :deleted)
93 # First with a trace that has never existed
94 get trace_data_path(0)
95 assert_response :not_found
97 # Now with a trace that has been deleted
98 session_for(deleted_trace_file.user)
99 get trace_data_path(deleted_trace_file)
100 assert_response :not_found
103 def test_show_offline
104 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
105 with_settings(:status => "gpx_offline") do
106 get trace_data_path(public_trace_file)
107 assert_response :success
108 assert_template :offline
114 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
115 assert_equal digest, Digest::MD5.hexdigest(response.body)
116 assert_equal content_type, response.media_type
117 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]