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 anonymous 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
124 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
126 # And finally we should be able to do it with the owner of the trace
127 auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
128 get api_trace_data_path(public_trace_file), :headers => auth_header
131 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
134 # Test downloading a compressed trace through the api
135 def test_data_compressed
136 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
138 # Authenticate as the owner of the trace we will be using
139 auth_header = basic_authorization_header identifiable_trace_file.user.display_name, "test"
141 # First get the data as is
142 get api_trace_data_path(identifiable_trace_file), :headers => auth_header
145 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
147 # Now ask explicitly for XML format
148 get api_trace_data_path(identifiable_trace_file, :format => "xml"), :headers => auth_header
149 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
151 # Now ask explicitly for GPX format
152 get api_trace_data_path(identifiable_trace_file, :format => "gpx"), :headers => auth_header
153 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
156 # Check an anonymous trace can't be downloaded by another user through the api
158 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
161 get api_trace_data_path(anon_trace_file)
162 assert_response :unauthorized
164 # Now with some other user, which shouldn't work since the trace is anon
165 auth_header = basic_authorization_header create(:user).display_name, "test"
166 get api_trace_data_path(anon_trace_file), :headers => auth_header
167 assert_response :forbidden
169 # And finally we should be able to do it with the owner of the trace
170 auth_header = basic_authorization_header anon_trace_file.user.display_name, "test"
171 get api_trace_data_path(anon_trace_file), :headers => auth_header
174 check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
177 # Test downloading a trace that doesn't exist through the api
178 def test_data_not_found
179 deleted_trace_file = create(:trace, :deleted)
181 # Try first with no auth, as it should require it
182 get api_trace_data_path(:id => 0)
183 assert_response :unauthorized
185 # Login, and try again
186 auth_header = basic_authorization_header create(:user).display_name, "test"
187 get api_trace_data_path(:id => 0), :headers => auth_header
188 assert_response :not_found
190 # Now try a trace which did exist but has been deleted
191 auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
192 get api_trace_data_path(deleted_trace_file), :headers => auth_header
193 assert_response :not_found
196 # Test creating a trace through the api
199 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
200 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
204 post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
205 assert_response :unauthorized
211 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
212 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
213 auth_header = basic_authorization_header user.display_name, "test"
214 post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
215 assert_response :success
216 trace = Trace.find(response.body.to_i)
217 assert_equal "a.gpx", trace.name
218 assert_equal "New Trace", trace.description
219 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
220 assert_equal "trackable", trace.visibility
221 assert_not trace.inserted
222 assert_equal File.new(fixture).read, trace.file.blob.download
224 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
229 # Now authenticated, with the legacy public flag
230 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
231 auth_header = basic_authorization_header user.display_name, "test"
232 post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
233 assert_response :success
234 trace = Trace.find(response.body.to_i)
235 assert_equal "a.gpx", trace.name
236 assert_equal "New Trace", trace.description
237 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
238 assert_equal "public", trace.visibility
239 assert_not trace.inserted
240 assert_equal File.new(fixture).read, trace.file.blob.download
242 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
247 # Now authenticated, with the legacy private flag
248 second_user = create(:user)
249 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
250 auth_header = basic_authorization_header second_user.display_name, "test"
251 post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
252 assert_response :success
253 trace = Trace.find(response.body.to_i)
254 assert_equal "a.gpx", trace.name
255 assert_equal "New Trace", trace.description
256 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
257 assert_equal "private", trace.visibility
258 assert_not trace.inserted
259 assert_equal File.new(fixture).read, trace.file.blob.download
261 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
264 # Check updating a trace through the api
266 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
267 deleted_trace_file = create(:trace, :deleted)
268 anon_trace_file = create(:trace, :visibility => "private")
271 put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file)
272 assert_response :unauthorized
274 # Now with some other user, which should fail
275 auth_header = basic_authorization_header create(:user).display_name, "test"
276 put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file), :headers => auth_header
277 assert_response :forbidden
279 # Now with a trace which doesn't exist
280 auth_header = basic_authorization_header create(:user).display_name, "test"
281 put api_trace_path(:id => 0), :params => create_trace_xml(public_trace_file), :headers => auth_header
282 assert_response :not_found
284 # Now with a trace which did exist but has been deleted
285 auth_header = basic_authorization_header deleted_trace_file.user.display_name, "test"
286 put api_trace_path(deleted_trace_file), :params => create_trace_xml(deleted_trace_file), :headers => auth_header
287 assert_response :not_found
289 # Now try an update with the wrong ID
290 auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
291 put api_trace_path(public_trace_file), :params => create_trace_xml(anon_trace_file), :headers => auth_header
292 assert_response :bad_request,
293 "should not be able to update a trace with a different ID from the XML"
295 # And finally try an update that should work
296 auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
297 t = public_trace_file
298 t.description = "Changed description"
299 t.visibility = "private"
300 put api_trace_path(t), :params => create_trace_xml(t), :headers => auth_header
301 assert_response :success
302 nt = Trace.find(t.id)
303 assert_equal nt.description, t.description
304 assert_equal nt.visibility, t.visibility
307 # Test that updating a trace doesn't duplicate the tags
309 tracetag = create(:tracetag)
310 trace = tracetag.trace
311 auth_header = basic_authorization_header trace.user.display_name, "test"
313 put api_trace_path(trace), :params => create_trace_xml(trace), :headers => auth_header
314 assert_response :success
316 updated = Trace.find(trace.id)
317 # Ensure there's only one tag in the database after updating
318 assert_equal(1, Tracetag.count)
319 # The new tag object might have a different id, so check the string representation
320 assert_equal trace.tagstring, updated.tagstring
323 # Check deleting a trace through the api
325 public_trace_file = create(:trace, :visibility => "public")
328 delete api_trace_path(public_trace_file)
329 assert_response :unauthorized
331 # Now with some other user, which should fail
332 auth_header = basic_authorization_header create(:user).display_name, "test"
333 delete api_trace_path(public_trace_file), :headers => auth_header
334 assert_response :forbidden
336 # Now with a trace which doesn't exist
337 auth_header = basic_authorization_header create(:user).display_name, "test"
338 delete api_trace_path(:id => 0), :headers => auth_header
339 assert_response :not_found
341 # And finally we should be able to do it with the owner of the trace
342 auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
343 delete api_trace_path(public_trace_file), :headers => auth_header
344 assert_response :success
346 # Try it a second time, which should fail
347 auth_header = basic_authorization_header public_trace_file.user.display_name, "test"
348 delete api_trace_path(public_trace_file), :headers => auth_header
349 assert_response :not_found
354 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
355 assert_response :success
356 assert_equal digest, Digest::MD5.hexdigest(response.body)
357 assert_equal content_type, response.media_type
358 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
362 # build XML for traces
363 # this builds a minimum viable XML for the tests in this suite
364 def create_trace_xml(trace)
365 root = XML::Document.new
366 root.root = XML::Node.new "osm"
367 trc = XML::Node.new "gpx_file"
368 trc["id"] = trace.id.to_s
369 trc["visibility"] = trace.visibility
370 trc["visible"] = trace.visible.to_s
371 desc = XML::Node.new "description"
372 desc << trace.description
374 trace.tags.each do |tag|
375 t = XML::Node.new "tag"