4 class TracesControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/gpx/create", :method => :post },
10 { :controller => "api/traces", :action => "create" }
13 { :path => "/api/0.6/gpx/1", :method => :get },
14 { :controller => "api/traces", :action => "show", :id => "1" }
17 { :path => "/api/0.6/gpx/1", :method => :put },
18 { :controller => "api/traces", :action => "update", :id => "1" }
21 { :path => "/api/0.6/gpx/1", :method => :delete },
22 { :controller => "api/traces", :action => "destroy", :id => "1" }
25 { :controller => "api/traces", :action => "show", :id => "1" },
26 { :path => "/api/0.6/gpx/1/details", :method => :get }
29 { :path => "/api/0.6/gpx/1/data", :method => :get },
30 { :controller => "api/traces", :action => "data", :id => "1" }
33 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
34 { :controller => "api/traces", :action => "data", :id => "1", :format => "xml" }
38 # Check getting a specific trace through the api
40 public_trace_file = create(:trace, :visibility => "public")
43 get api_trace_path(public_trace_file)
44 assert_response :unauthorized
46 # Now with some other user, which should work since the trace is public
47 auth_header = bearer_authorization_header
48 get api_trace_path(public_trace_file), :headers => auth_header
49 assert_response :success
51 # And finally we should be able to do it with the owner of the trace
52 auth_header = bearer_authorization_header public_trace_file.user
53 get api_trace_path(public_trace_file), :headers => auth_header
54 assert_response :success
55 assert_select "gpx_file[id='#{public_trace_file.id}'][uid='#{public_trace_file.user.id}']", 1
58 # Check an anonymous trace can't be specifically fetched by another user
60 anon_trace_file = create(:trace, :visibility => "private")
63 get api_trace_path(anon_trace_file)
64 assert_response :unauthorized
66 # Now try with another user, which shouldn't work since the trace is anon
67 auth_header = bearer_authorization_header
68 get api_trace_path(anon_trace_file), :headers => auth_header
69 assert_response :forbidden
71 # And finally we should be able to get the trace details with the trace owner
72 auth_header = bearer_authorization_header anon_trace_file.user
73 get api_trace_path(anon_trace_file), :headers => auth_header
74 assert_response :success
77 # Check the api details for a trace that doesn't exist
78 def test_show_not_found
79 deleted_trace_file = create(:trace, :deleted)
81 # Try first with no auth, as it should require it
82 get api_trace_path(:id => 0)
83 assert_response :unauthorized
85 # Login, and try again
86 auth_header = bearer_authorization_header deleted_trace_file.user
87 get api_trace_path(:id => 0), :headers => auth_header
88 assert_response :not_found
90 # Now try a trace which did exist but has been deleted
91 auth_header = bearer_authorization_header deleted_trace_file.user
92 get api_trace_path(deleted_trace_file), :headers => auth_header
93 assert_response :not_found
96 # Test downloading a trace through the api
98 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
101 get api_trace_data_path(public_trace_file)
102 assert_response :unauthorized
104 # Now with some other user, which should work since the trace is public
105 auth_header = bearer_authorization_header
106 get api_trace_data_path(public_trace_file), :headers => auth_header
109 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
111 # And finally we should be able to do it with the owner of the trace
112 auth_header = bearer_authorization_header public_trace_file.user
113 get api_trace_data_path(public_trace_file), :headers => auth_header
116 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
119 # Test downloading a compressed trace through the api
120 def test_data_compressed
121 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
123 # Authenticate as the owner of the trace we will be using
124 auth_header = bearer_authorization_header identifiable_trace_file.user
126 # First get the data as is
127 get api_trace_data_path(identifiable_trace_file), :headers => auth_header
130 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
132 # Now ask explicitly for XML format
133 get api_trace_data_path(identifiable_trace_file, :format => "xml"), :headers => auth_header
134 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
136 # Now ask explicitly for GPX format
137 get api_trace_data_path(identifiable_trace_file, :format => "gpx"), :headers => auth_header
138 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
141 # Check an anonymous trace can't be downloaded by another user through the api
143 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
146 get api_trace_data_path(anon_trace_file)
147 assert_response :unauthorized
149 # Now with some other user, which shouldn't work since the trace is anon
150 auth_header = bearer_authorization_header
151 get api_trace_data_path(anon_trace_file), :headers => auth_header
152 assert_response :forbidden
154 # And finally we should be able to do it with the owner of the trace
155 auth_header = bearer_authorization_header anon_trace_file.user
156 get api_trace_data_path(anon_trace_file), :headers => auth_header
159 check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
162 # Test downloading a trace that doesn't exist through the api
163 def test_data_not_found
164 deleted_trace_file = create(:trace, :deleted)
166 # Try first with no auth, as it should require it
167 get api_trace_data_path(:id => 0)
168 assert_response :unauthorized
170 # Login, and try again
171 auth_header = bearer_authorization_header
172 get api_trace_data_path(:id => 0), :headers => auth_header
173 assert_response :not_found
175 # Now try a trace which did exist but has been deleted
176 auth_header = bearer_authorization_header deleted_trace_file.user
177 get api_trace_data_path(deleted_trace_file), :headers => auth_header
178 assert_response :not_found
181 # Test creating a trace through the api
184 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
185 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
189 post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
190 assert_response :unauthorized
196 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
197 assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
199 auth_header = bearer_authorization_header user
201 # Create trace and import tracepoints in background job
202 perform_enqueued_jobs do
203 post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
206 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 trace.inserted
214 assert_equal File.new(fixture).read, trace.file.blob.download
216 # Validate tracepoints
217 assert_equal 1, trace.points.size
218 tp = trace.points.first
219 assert_equal 10000000, tp.latitude
220 assert_equal 10000000, tp.longitude
221 assert_equal 3221331576, tp.tile
222 assert_equal 0, tp.trackid
223 assert_in_delta(134.0, tp.altitude)
224 assert_equal DateTime.parse("2008-10-01T10:10:10.000Z"), tp.timestamp
227 assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
232 # Now authenticated, with the legacy public flag
233 assert_not_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
234 auth_header = bearer_authorization_header user
235 post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
236 assert_response :success
237 trace = Trace.find(response.body.to_i)
238 assert_equal "a.gpx", trace.name
239 assert_equal "New Trace", trace.description
240 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
241 assert_equal "public", trace.visibility
242 assert_not trace.inserted
243 assert_equal File.new(fixture).read, trace.file.blob.download
245 assert_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
250 # Now authenticated, with the legacy private flag
251 second_user = create(:user)
252 assert_nil second_user.preferences.find_by(:k => "gps.trace.visibility")
253 auth_header = bearer_authorization_header second_user
254 post gpx_create_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
255 assert_response :success
256 trace = Trace.find(response.body.to_i)
257 assert_equal "a.gpx", trace.name
258 assert_equal "New Trace", trace.description
259 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
260 assert_equal "private", trace.visibility
261 assert_not trace.inserted
262 assert_equal File.new(fixture).read, trace.file.blob.download
264 assert_equal "private", second_user.preferences.find_by(:k => "gps.trace.visibility").v
267 # Check updating a trace through the api
269 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
270 deleted_trace_file = create(:trace, :deleted)
271 anon_trace_file = create(:trace, :visibility => "private")
274 put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file)
275 assert_response :unauthorized
277 # Now with some other user, which should fail
278 auth_header = bearer_authorization_header
279 put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file), :headers => auth_header
280 assert_response :forbidden
282 # Now with a trace which doesn't exist
283 auth_header = bearer_authorization_header
284 put api_trace_path(:id => 0), :params => create_trace_xml(public_trace_file), :headers => auth_header
285 assert_response :not_found
287 # Now with a trace which did exist but has been deleted
288 auth_header = bearer_authorization_header deleted_trace_file.user
289 put api_trace_path(deleted_trace_file), :params => create_trace_xml(deleted_trace_file), :headers => auth_header
290 assert_response :not_found
292 # Now try an update with the wrong ID
293 auth_header = bearer_authorization_header public_trace_file.user
294 put api_trace_path(public_trace_file), :params => create_trace_xml(anon_trace_file), :headers => auth_header
295 assert_response :bad_request,
296 "should not be able to update a trace with a different ID from the XML"
298 # And finally try an update that should work
299 auth_header = bearer_authorization_header public_trace_file.user
300 t = public_trace_file
301 t.description = "Changed description"
302 t.visibility = "private"
303 put api_trace_path(t), :params => create_trace_xml(t), :headers => auth_header
304 assert_response :success
305 nt = Trace.find(t.id)
306 assert_equal nt.description, t.description
307 assert_equal nt.visibility, t.visibility
310 # Test that updating a trace doesn't duplicate the tags
312 tracetag = create(:tracetag)
313 trace = tracetag.trace
314 auth_header = bearer_authorization_header trace.user
316 put api_trace_path(trace), :params => create_trace_xml(trace), :headers => auth_header
317 assert_response :success
319 updated = Trace.find(trace.id)
320 # Ensure there's only one tag in the database after updating
321 assert_equal(1, Tracetag.count)
322 # The new tag object might have a different id, so check the string representation
323 assert_equal trace.tagstring, updated.tagstring
326 # Check deleting a trace through the api
328 public_trace_file = create(:trace, :visibility => "public")
331 delete api_trace_path(public_trace_file)
332 assert_response :unauthorized
334 # Now with some other user, which should fail
335 auth_header = bearer_authorization_header
336 delete api_trace_path(public_trace_file), :headers => auth_header
337 assert_response :forbidden
339 # Now with a trace which doesn't exist
340 auth_header = bearer_authorization_header
341 delete api_trace_path(:id => 0), :headers => auth_header
342 assert_response :not_found
344 # And finally we should be able to do it with the owner of the trace
345 auth_header = bearer_authorization_header public_trace_file.user
346 delete api_trace_path(public_trace_file), :headers => auth_header
347 assert_response :success
349 # Try it a second time, which should fail
350 auth_header = bearer_authorization_header public_trace_file.user
351 delete api_trace_path(public_trace_file), :headers => auth_header
352 assert_response :not_found
357 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
358 assert_response :success
359 assert_equal digest, Digest::MD5.hexdigest(response.body)
360 assert_equal content_type, response.media_type
361 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
365 # build XML for traces
366 # this builds a minimum viable XML for the tests in this suite
367 def create_trace_xml(trace)
368 root = XML::Document.new
369 root.root = XML::Node.new "osm"
370 trc = XML::Node.new "gpx_file"
371 trc["id"] = trace.id.to_s
372 trc["visibility"] = trace.visibility
373 trc["visible"] = trace.visible.to_s
374 desc = XML::Node.new "description"
375 desc << trace.description
377 trace.tags.each do |tag|
378 t = XML::Node.new "tag"