1 # frozen_string_literal: true
6 class TracesControllerTest < ActionDispatch::IntegrationTest
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/gpx", :method => :post },
12 { :controller => "api/traces", :action => "create" }
15 { :controller => "api/traces", :action => "create" },
16 { :path => "/api/0.6/gpx/create", :method => :post }
19 { :path => "/api/0.6/gpx/1", :method => :get },
20 { :controller => "api/traces", :action => "show", :id => "1" }
23 { :path => "/api/0.6/gpx/1.json", :method => :get },
24 { :controller => "api/traces", :action => "show", :id => "1", :format => "json" }
27 { :path => "/api/0.6/gpx/1", :method => :put },
28 { :controller => "api/traces", :action => "update", :id => "1" }
31 { :path => "/api/0.6/gpx/1", :method => :delete },
32 { :controller => "api/traces", :action => "destroy", :id => "1" }
35 { :controller => "api/traces", :action => "show", :id => "1" },
36 { :path => "/api/0.6/gpx/1/details", :method => :get }
40 # Check getting a specific trace through the api
42 public_trace_file = create(:trace, :visibility => "identifiable")
45 get api_trace_path(public_trace_file)
46 assert_response :unauthorized
48 # Now with some other user, which should work since the trace is public
49 auth_header = bearer_authorization_header
50 get api_trace_path(public_trace_file), :headers => auth_header
51 assert_response :success
53 # We should be able to do it with the owner of the trace
54 auth_header = bearer_authorization_header public_trace_file.user
55 get api_trace_path(public_trace_file), :headers => auth_header
56 assert_response :success
57 assert_select "gpx_file[id='#{public_trace_file.id}'][uid='#{public_trace_file.user.id}']", 1
59 # We should be able to do it with the owner of the trace with json format
60 auth_header = bearer_authorization_header public_trace_file.user
61 get api_trace_path(public_trace_file, :format => "json"), :headers => auth_header
62 assert_response :success
63 assert_equal "application/json", response.media_type
64 js = ActiveSupport::JSON.decode(@response.body)
66 assert_equal public_trace_file.id, js["trace"]["id"]
67 assert_equal public_trace_file.user.id, js["trace"]["uid"]
70 # Check an anonymous trace can't be specifically fetched by another user
72 anon_trace_file = create(:trace, :visibility => "trackable")
75 get api_trace_path(anon_trace_file)
76 assert_response :unauthorized
78 # Now try with another user, which shouldn't work since the trace is anon
79 auth_header = bearer_authorization_header
80 get api_trace_path(anon_trace_file), :headers => auth_header
81 assert_response :forbidden
83 # And finally we should be able to get the trace details with the trace owner
84 auth_header = bearer_authorization_header anon_trace_file.user
85 get api_trace_path(anon_trace_file), :headers => auth_header
86 assert_response :success
89 # Check the api details for a trace that doesn't exist
90 def test_show_not_found
91 deleted_trace_file = create(:trace, :deleted)
93 # Try first with no auth, as it should require it
94 get api_trace_path(:id => 0)
95 assert_response :unauthorized
97 # Login, and try again
98 auth_header = bearer_authorization_header deleted_trace_file.user
99 get api_trace_path(:id => 0), :headers => auth_header
100 assert_response :not_found
102 # Now try a trace which did exist but has been deleted
103 auth_header = bearer_authorization_header deleted_trace_file.user
104 get api_trace_path(deleted_trace_file), :headers => auth_header
105 assert_response :not_found
108 # Check that getting a trace through the api is not possible when the traces feature is disabled
109 def test_show_disabled
110 public_trace_file = create(:trace, :visibility => "identifiable")
111 auth_header = bearer_authorization_header public_trace_file.user
113 with_settings(:traces_disabled => true) do
114 get api_trace_path(public_trace_file), :headers => auth_header
115 assert_response :not_found
119 # Test creating a trace through the api
122 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
123 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
127 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
128 assert_response :unauthorized
134 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
135 assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
137 auth_header = bearer_authorization_header user
139 # Create trace and import tracepoints in background job
140 perform_enqueued_jobs do
141 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }, :headers => auth_header
144 assert_response :success
146 trace = Trace.find(response.body.to_i)
147 assert_equal "a.gpx", trace.name
148 assert_equal "New Trace", trace.description
149 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
150 assert_equal "trackable", trace.visibility
151 assert trace.inserted
152 assert_equal File.new(fixture).read, trace.file.blob.download
154 # Validate tracepoints
155 assert_equal 1, trace.points.size
156 tp = trace.points.order(:timestamp).first
157 assert_equal 10000000, tp.latitude
158 assert_equal 10000000, tp.longitude
159 assert_equal 3221331576, tp.tile
160 assert_equal 0, tp.trackid
161 assert_in_delta(134.0, tp.altitude)
162 assert_equal DateTime.parse("2008-10-01T10:10:10.000Z"), tp.timestamp
165 assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
168 # The legacy public flag is no longer supported and returns bad request
169 def test_create_legacy_public_flag
170 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
171 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
173 auth_header = bearer_authorization_header user
175 assert_no_difference "Trace.count" do
176 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }, :headers => auth_header
178 assert_response :bad_request
182 assert_no_difference "Trace.count" do
183 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }, :headers => auth_header
185 assert_response :bad_request
188 # A legacy visibility (public or private) is no longer accepted on upload
189 def test_create_legacy_visibility
190 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
191 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
193 auth_header = bearer_authorization_header user
195 assert_no_difference "Trace.count" do
196 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "public" }, :headers => auth_header
198 assert_response :bad_request
202 assert_no_difference "Trace.count" do
203 post api_traces_path, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "private" }, :headers => auth_header
205 assert_response :bad_request
208 # Check updating a trace through the api
210 public_trace_file = create(:trace, :visibility => "identifiable", :fixture => "a")
211 deleted_trace_file = create(:trace, :deleted)
212 anon_trace_file = create(:trace, :visibility => "trackable")
215 put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file)
216 assert_response :unauthorized
218 # Now with some other user, which should fail
219 auth_header = bearer_authorization_header
220 put api_trace_path(public_trace_file), :params => create_trace_xml(public_trace_file), :headers => auth_header
221 assert_response :forbidden
223 # Now with a trace which doesn't exist
224 auth_header = bearer_authorization_header
225 put api_trace_path(:id => 0), :params => create_trace_xml(public_trace_file), :headers => auth_header
226 assert_response :not_found
228 # Now with a trace which did exist but has been deleted
229 auth_header = bearer_authorization_header deleted_trace_file.user
230 put api_trace_path(deleted_trace_file), :params => create_trace_xml(deleted_trace_file), :headers => auth_header
231 assert_response :not_found
233 # Now try an update with the wrong ID
234 auth_header = bearer_authorization_header public_trace_file.user
235 put api_trace_path(public_trace_file), :params => create_trace_xml(anon_trace_file), :headers => auth_header
236 assert_response :bad_request,
237 "should not be able to update a trace with a different ID from the XML"
239 # And finally try an update that should work
240 auth_header = bearer_authorization_header public_trace_file.user
241 t = public_trace_file
242 t.description = "Changed description"
243 t.visibility = "trackable"
244 put api_trace_path(t), :params => create_trace_xml(t), :headers => auth_header
245 assert_response :success
246 nt = Trace.find(t.id)
247 assert_equal nt.description, t.description
248 assert_equal nt.visibility, t.visibility
251 # Test that updating a trace doesn't duplicate the tags
253 tracetag = create(:tracetag)
254 trace = tracetag.trace
255 auth_header = bearer_authorization_header trace.user
257 put api_trace_path(trace), :params => create_trace_xml(trace), :headers => auth_header
258 assert_response :success
260 updated = Trace.find(trace.id)
261 # Ensure there's only one tag in the database after updating
262 assert_equal(1, Tracetag.count)
263 # The new tag object might have a different id, so check the string representation
264 assert_equal trace.tagstring, updated.tagstring
267 # Check deleting a trace through the api
269 public_trace_file = create(:trace, :visibility => "identifiable")
272 delete api_trace_path(public_trace_file)
273 assert_response :unauthorized
275 # Now with some other user, which should fail
276 auth_header = bearer_authorization_header
277 delete api_trace_path(public_trace_file), :headers => auth_header
278 assert_response :forbidden
280 # Now with a trace which doesn't exist
281 auth_header = bearer_authorization_header
282 delete api_trace_path(:id => 0), :headers => auth_header
283 assert_response :not_found
285 # And finally we should be able to do it with the owner of the trace
286 auth_header = bearer_authorization_header public_trace_file.user
287 delete api_trace_path(public_trace_file), :headers => auth_header
288 assert_response :success
290 # Try it a second time, which should fail
291 auth_header = bearer_authorization_header public_trace_file.user
292 delete api_trace_path(public_trace_file), :headers => auth_header
293 assert_response :not_found
299 # build XML for traces
300 # this builds a minimum viable XML for the tests in this suite
301 def create_trace_xml(trace)
302 root = XML::Document.new
303 root.root = XML::Node.new "osm"
304 trc = XML::Node.new "gpx_file"
305 trc["id"] = trace.id.to_s
306 trc["visibility"] = trace.visibility
307 trc["visible"] = trace.visible.to_s
308 desc = XML::Node.new "description"
309 desc << trace.description
311 trace.tags.each do |tag|
312 t = XML::Node.new "tag"