]> git.openstreetmap.org Git - rails.git/blob - test/controllers/traces/data_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5537'
[rails.git] / test / controllers / traces / data_controller_test.rb
1 require "test_helper"
2
3 module Traces
4   class DataControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/traces/1/data", :method => :get },
10         { :controller => "traces/data", :action => "show", :trace_id => "1" }
11       )
12       assert_routing(
13         { :path => "/traces/1/data.xml", :method => :get },
14         { :controller => "traces/data", :action => "show", :trace_id => "1", :format => "xml" }
15       )
16
17       get "/trace/1/data"
18       assert_redirected_to "/traces/1/data"
19
20       get "/trace/1/data.xml"
21       assert_redirected_to "/traces/1/data.xml"
22     end
23
24     # Test downloading a trace
25     def test_show
26       public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
27
28       # First with no auth, which should work since the trace is public
29       get trace_data_path(public_trace_file)
30       follow_redirect!
31       follow_redirect!
32       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
33
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)
37       follow_redirect!
38       follow_redirect!
39       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
40
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)
44       follow_redirect!
45       follow_redirect!
46       check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
47     end
48
49     # Test downloading a compressed trace
50     def test_show_compressed
51       identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
52
53       # First get the data as is
54       get trace_data_path(identifiable_trace_file)
55       follow_redirect!
56       follow_redirect!
57       check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
58
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"
62
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"
66     end
67
68     # Check an anonymous trace can't be downloaded by another user
69     def test_show_anon
70       anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
71
72       # First with no auth
73       get trace_data_path(anon_trace_file)
74       assert_response :not_found
75
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
80
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)
84       follow_redirect!
85       follow_redirect!
86       check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
87     end
88
89     # Test downloading a trace that doesn't exist
90     def test_show_not_found
91       deleted_trace_file = create(:trace, :deleted)
92
93       # First with a trace that has never existed
94       get trace_data_path(0)
95       assert_response :not_found
96
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
101     end
102
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
109       end
110     end
111
112     private
113
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"]
118     end
119   end
120 end