4 class TracesControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/gpx", :method => :post },
10 { :controller => "api/traces", :action => "create" }
13 { :controller => "api/traces", :action => "create" },
14 { :path => "/api/0.6/gpx/create", :method => :post }
17 { :path => "/api/0.6/gpx/1", :method => :get },
18 { :controller => "api/traces", :action => "show", :id => "1" }
21 { :path => "/api/0.6/gpx/1", :method => :put },
22 { :controller => "api/traces", :action => "update", :id => "1" }
25 { :path => "/api/0.6/gpx/1", :method => :delete },
26 { :controller => "api/traces", :action => "destroy", :id => "1" }
29 { :controller => "api/traces", :action => "show", :id => "1" },
30 { :path => "/api/0.6/gpx/1/details", :method => :get }
33 { :path => "/api/0.6/gpx/1/data", :method => :get },
34 { :controller => "api/traces", :action => "data", :id => "1" }
37 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
38 { :controller => "api/traces", :action => "data", :id => "1", :format => "xml" }
42 # Check getting a specific trace through the api
44 public_trace_file = create(:trace, :visibility => "public")
47 get api_trace_path(public_trace_file)
48 assert_response :unauthorized
50 # Now with some other user, which should work since the trace is public
51 auth_header = bearer_authorization_header
52 get api_trace_path(public_trace_file), :headers => auth_header
53 assert_response :success
55 # And finally we should be able to do it with the owner of the trace
56 auth_header = bearer_authorization_header public_trace_file.user
57 get api_trace_path(public_trace_file), :headers => auth_header
58 assert_response :success
59 assert_select "gpx_file[id='#{public_trace_file.id}'][uid='#{public_trace_file.user.id}']", 1
62 # Check an anonymous trace can't be specifically fetched by another user
64 anon_trace_file = create(:trace, :visibility => "private")
67 get api_trace_path(anon_trace_file)
68 assert_response :unauthorized
70 # Now try with another user, which shouldn't work since the trace is anon
71 auth_header = bearer_authorization_header
72 get api_trace_path(anon_trace_file), :headers => auth_header
73 assert_response :forbidden
75 # And finally we should be able to get the trace details with the trace owner
76 auth_header = bearer_authorization_header anon_trace_file.user
77 get api_trace_path(anon_trace_file), :headers => auth_header
78 assert_response :success
81 # Check the api details for a trace that doesn't exist
82 def test_show_not_found
83 deleted_trace_file = create(:trace, :deleted)
85 # Try first with no auth, as it should require it
86 get api_trace_path(:id => 0)
87 assert_response :unauthorized
89 # Login, and try again
90 auth_header = bearer_authorization_header deleted_trace_file.user
91 get api_trace_path(:id => 0), :headers => auth_header
92 assert_response :not_found
94 # Now try a trace which did exist but has been deleted
95 auth_header = bearer_authorization_header deleted_trace_file.user
96 get api_trace_path(deleted_trace_file), :headers => auth_header
97 assert_response :not_found
100 # Test downloading a trace through the api
102 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
105 get api_trace_data_path(public_trace_file)
106 assert_response :unauthorized
108 # Now with some other user, which should work since the trace is public
109 auth_header = bearer_authorization_header
110 get api_trace_data_path(public_trace_file), :headers => auth_header
113 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
115 # And finally we should be able to do it with the owner of the trace
116 auth_header = bearer_authorization_header public_trace_file.user
117 get api_trace_data_path(public_trace_file), :headers => auth_header
120 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
123 # Test downloading a compressed trace through the api
124 def test_data_compressed
125 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
127 # Authenticate as the owner of the trace we will be using
128 auth_header = bearer_authorization_header identifiable_trace_file.user
130 # First get the data as is
131 get api_trace_data_path(identifiable_trace_file), :headers => auth_header
134 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
136 # Now ask explicitly for XML format
137 get api_trace_data_path(identifiable_trace_file, :format => "xml"), :headers => auth_header
138 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
140 # Now ask explicitly for GPX format
141 get api_trace_data_path(identifiable_trace_file, :format => "gpx"), :headers => auth_header
142 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
145 # Check an anonymous trace can't be downloaded by another user through the api
147 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
150 get api_trace_data_path(anon_trace_file)
151 assert_response :unauthorized
153 # Now with some other user, which shouldn't work since the trace is anon
154 auth_header = bearer_authorization_header
155 get api_trace_data_path(anon_trace_file), :headers => auth_header
156 assert_response :forbidden
158 # And finally we should be able to do it with the owner of the trace
159 auth_header = bearer_authorization_header anon_trace_file.user
160 get api_trace_data_path(anon_trace_file), :headers => auth_header
163 check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
166 # Test downloading a trace that doesn't exist through the api
167 def test_data_not_found
168 deleted_trace_file = create(:trace, :deleted)
170 # Try first with no auth, as it should require it
171 get api_trace_data_path(:id => 0)
172 assert_response :unauthorized
174 # Login, and try again
175 auth_header = bearer_authorization_header
176 get api_trace_data_path(:id => 0), :headers => auth_header
177 assert_response :not_found
179 # Now try a trace which did exist but has been deleted
180 auth_header = bearer_authorization_header deleted_trace_file.user
181 get api_trace_data_path(deleted_trace_file), :headers => auth_header
182 assert_response :not_found
185 # Test creating a trace through the api
188 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
189 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
193 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
194 assert_response :unauthorized
200 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
201 assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
203 auth_header = bearer_authorization_header user
205 # Create trace and import tracepoints in background job
206 perform_enqueued_jobs do
207 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
210 assert_response :success
212 trace = Trace.find(response.body.to_i)
213 assert_equal "a.gpx", trace.name
214 assert_equal "New Trace", trace.description
215 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
216 assert_equal "trackable", trace.visibility
217 assert trace.inserted
218 assert_equal File.new(fixture).read, trace.file.blob.download
220 # Validate tracepoints
221 assert_equal 1, trace.points.size
222 tp = trace.points.first
223 assert_equal 10000000, tp.latitude
224 assert_equal 10000000, tp.longitude
225 assert_equal 3221331576, tp.tile
226 assert_equal 0, tp.trackid
227 assert_in_delta(134.0, tp.altitude)
228 assert_equal DateTime.parse("2008-10-01T10:10:10.000Z"), tp.timestamp
231 assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
236 # Now authenticated, with the legacy public flag
237 assert_not_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
238 auth_header = bearer_authorization_header user
239 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
240 assert_response :success
241 trace = Trace.find(response.body.to_i)
242 assert_equal "a.gpx", trace.name
243 assert_equal "New Trace", trace.description
244 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
245 assert_equal "public", trace.visibility
246 assert_not trace.inserted
247 assert_equal File.new(fixture).read, trace.file.blob.download
249 assert_equal "public", user.preferences.find_by(:k => "gps.trace.visibility").v
254 # Now authenticated, with the legacy private flag
255 second_user = create(:user)
256 assert_nil second_user.preferences.find_by(:k => "gps.trace.visibility")
257 auth_header = bearer_authorization_header second_user
258 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
259 assert_response :success
260 trace = Trace.find(response.body.to_i)
261 assert_equal "a.gpx", trace.name
262 assert_equal "New Trace", trace.description
263 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
264 assert_equal "private", trace.visibility
265 assert_not trace.inserted
266 assert_equal File.new(fixture).read, trace.file.blob.download
268 assert_equal "private", second_user.preferences.find_by(:k => "gps.trace.visibility").v
271 # Check updating a trace through the api
273 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
274 deleted_trace_file = create(:trace, :deleted)
275 anon_trace_file = create(:trace, :visibility => "private")
278 put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file)
279 assert_response :unauthorized
281 # Now with some other user, which should fail
282 auth_header = bearer_authorization_header
283 put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file), :headers => auth_header
284 assert_response :forbidden
286 # Now with a trace which doesn't exist
287 auth_header = bearer_authorization_header
288 put api_trace_path(:id => 0), :params => create_trace_xml(public_trace_file), :headers => auth_header
289 assert_response :not_found
291 # Now with a trace which did exist but has been deleted
292 auth_header = bearer_authorization_header deleted_trace_file.user
293 put api_trace_path(deleted_trace_file), :params => create_trace_xml(deleted_trace_file), :headers => auth_header
294 assert_response :not_found
296 # Now try an update with the wrong ID
297 auth_header = bearer_authorization_header public_trace_file.user
298 put api_trace_path(public_trace_file), :params => create_trace_xml(anon_trace_file), :headers => auth_header
299 assert_response :bad_request,
300 "should not be able to update a trace with a different ID from the XML"
302 # And finally try an update that should work
303 auth_header = bearer_authorization_header public_trace_file.user
304 t = public_trace_file
305 t.description = "Changed description"
306 t.visibility = "private"
307 put api_trace_path(t), :params => create_trace_xml(t), :headers => auth_header
308 assert_response :success
309 nt = Trace.find(t.id)
310 assert_equal nt.description, t.description
311 assert_equal nt.visibility, t.visibility
314 # Test that updating a trace doesn't duplicate the tags
316 tracetag = create(:tracetag)
317 trace = tracetag.trace
318 auth_header = bearer_authorization_header trace.user
320 put api_trace_path(trace), :params => create_trace_xml(trace), :headers => auth_header
321 assert_response :success
323 updated = Trace.find(trace.id)
324 # Ensure there's only one tag in the database after updating
325 assert_equal(1, Tracetag.count)
326 # The new tag object might have a different id, so check the string representation
327 assert_equal trace.tagstring, updated.tagstring
330 # Check deleting a trace through the api
332 public_trace_file = create(:trace, :visibility => "public")
335 delete api_trace_path(public_trace_file)
336 assert_response :unauthorized
338 # Now with some other user, which should fail
339 auth_header = bearer_authorization_header
340 delete api_trace_path(public_trace_file), :headers => auth_header
341 assert_response :forbidden
343 # Now with a trace which doesn't exist
344 auth_header = bearer_authorization_header
345 delete api_trace_path(:id => 0), :headers => auth_header
346 assert_response :not_found
348 # And finally we should be able to do it with the owner of the trace
349 auth_header = bearer_authorization_header public_trace_file.user
350 delete api_trace_path(public_trace_file), :headers => auth_header
351 assert_response :success
353 # Try it a second time, which should fail
354 auth_header = bearer_authorization_header public_trace_file.user
355 delete api_trace_path(public_trace_file), :headers => auth_header
356 assert_response :not_found
361 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
362 assert_response :success
363 assert_equal digest, Digest::MD5.hexdigest(response.body)
364 assert_equal content_type, response.media_type
365 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
369 # build XML for traces
370 # this builds a minimum viable XML for the tests in this suite
371 def create_trace_xml(trace)
372 root = XML::Document.new
373 root.root = XML::Node.new "osm"
374 trc = XML::Node.new "gpx_file"
375 trc["id"] = trace.id.to_s
376 trc["visibility"] = trace.visibility
377 trc["visible"] = trace.visible.to_s
378 desc = XML::Node.new "description"
379 desc << trace.description
381 trace.tags.each do |tag|
382 t = XML::Node.new "tag"