2 require "minitest/mock"
5 class TracesControllerTest < ActionController::TestCase
6 # Use temporary directories with unique names for each test
7 # This allows the tests to be run in parallel.
9 @gpx_trace_dir_orig = Settings.gpx_trace_dir
10 @gpx_image_dir_orig = Settings.gpx_image_dir
11 Settings.gpx_trace_dir = Dir.mktmpdir("trace", Rails.root.join("test/gpx"))
12 Settings.gpx_image_dir = Dir.mktmpdir("image", Rails.root.join("test/gpx"))
16 FileUtils.remove_dir(Settings.gpx_trace_dir)
17 FileUtils.remove_dir(Settings.gpx_image_dir)
18 Settings.gpx_trace_dir = @gpx_trace_dir_orig
19 Settings.gpx_image_dir = @gpx_image_dir_orig
23 # test all routes which lead to this controller
26 { :path => "/api/0.6/gpx/create", :method => :post },
27 { :controller => "api/traces", :action => "create" }
30 { :path => "/api/0.6/gpx/1", :method => :get },
31 { :controller => "api/traces", :action => "show", :id => "1" }
34 { :path => "/api/0.6/gpx/1", :method => :put },
35 { :controller => "api/traces", :action => "update", :id => "1" }
38 { :path => "/api/0.6/gpx/1", :method => :delete },
39 { :controller => "api/traces", :action => "destroy", :id => "1" }
42 { :controller => "api/traces", :action => "show", :id => "1" },
43 { :path => "/api/0.6/gpx/1/details", :method => :get }
46 { :path => "/api/0.6/gpx/1/data", :method => :get },
47 { :controller => "api/traces", :action => "data", :id => "1" }
50 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
51 { :controller => "api/traces", :action => "data", :id => "1", :format => "xml" }
55 # Check getting a specific trace through the api
57 public_trace_file = create(:trace, :visibility => "public")
60 get :show, :params => { :id => public_trace_file.id }
61 assert_response :unauthorized
63 # Now with some other user, which should work since the trace is public
64 basic_authorization create(:user).display_name, "test"
65 get :show, :params => { :id => public_trace_file.id }
66 assert_response :success
68 # And finally we should be able to do it with the owner of the trace
69 basic_authorization public_trace_file.user.display_name, "test"
70 get :show, :params => { :id => public_trace_file.id }
71 assert_response :success
74 # Check an anoymous trace can't be specifically fetched by another user
76 anon_trace_file = create(:trace, :visibility => "private")
79 get :show, :params => { :id => anon_trace_file.id }
80 assert_response :unauthorized
82 # Now try with another user, which shouldn't work since the trace is anon
83 basic_authorization create(:user).display_name, "test"
84 get :show, :params => { :id => anon_trace_file.id }
85 assert_response :forbidden
87 # And finally we should be able to get the trace details with the trace owner
88 basic_authorization anon_trace_file.user.display_name, "test"
89 get :show, :params => { :id => anon_trace_file.id }
90 assert_response :success
93 # Check the api details for a trace that doesn't exist
94 def test_show_not_found
95 deleted_trace_file = create(:trace, :deleted)
97 # Try first with no auth, as it should require it
98 get :show, :params => { :id => 0 }
99 assert_response :unauthorized
101 # Login, and try again
102 basic_authorization deleted_trace_file.user.display_name, "test"
103 get :show, :params => { :id => 0 }
104 assert_response :not_found
106 # Now try a trace which did exist but has been deleted
107 basic_authorization deleted_trace_file.user.display_name, "test"
108 get :show, :params => { :id => deleted_trace_file.id }
109 assert_response :not_found
112 # Test downloading a trace through the api
114 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
117 get :data, :params => { :id => public_trace_file.id }
118 assert_response :unauthorized
120 # Now with some other user, which should work since the trace is public
121 basic_authorization create(:user).display_name, "test"
122 get :data, :params => { :id => public_trace_file.id }
123 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
125 # And finally we should be able to do it with the owner of the trace
126 basic_authorization public_trace_file.user.display_name, "test"
127 get :data, :params => { :id => public_trace_file.id }
128 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
131 # Test downloading a compressed trace through the api
132 def test_data_compressed
133 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
135 # Authenticate as the owner of the trace we will be using
136 basic_authorization identifiable_trace_file.user.display_name, "test"
138 # First get the data as is
139 get :data, :params => { :id => identifiable_trace_file.id }
140 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
142 # Now ask explicitly for XML format
143 get :data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
144 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
146 # Now ask explicitly for GPX format
147 get :data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
148 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
151 # Check an anonymous trace can't be downloaded by another user through the api
153 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
156 get :data, :params => { :id => anon_trace_file.id }
157 assert_response :unauthorized
159 # Now with some other user, which shouldn't work since the trace is anon
160 basic_authorization create(:user).display_name, "test"
161 get :data, :params => { :id => anon_trace_file.id }
162 assert_response :forbidden
164 # And finally we should be able to do it with the owner of the trace
165 basic_authorization anon_trace_file.user.display_name, "test"
166 get :data, :params => { :id => anon_trace_file.id }
167 check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
170 # Test downloading a trace that doesn't exist through the api
171 def test_data_not_found
172 deleted_trace_file = create(:trace, :deleted)
174 # Try first with no auth, as it should require it
175 get :data, :params => { :id => 0 }
176 assert_response :unauthorized
178 # Login, and try again
179 basic_authorization create(:user).display_name, "test"
180 get :data, :params => { :id => 0 }
181 assert_response :not_found
183 # Now try a trace which did exist but has been deleted
184 basic_authorization deleted_trace_file.user.display_name, "test"
185 get :data, :params => { :id => deleted_trace_file.id }
186 assert_response :not_found
189 # Test creating a trace through the api
192 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
193 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
197 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
198 assert_response :unauthorized
204 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
205 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
206 basic_authorization user.display_name, "test"
207 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
208 assert_response :success
209 trace = Trace.find(response.body.to_i)
210 assert_equal "a.gpx", trace.name
211 assert_equal "New Trace", trace.description
212 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
213 assert_equal "trackable", trace.visibility
214 assert_equal false, trace.inserted
215 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
217 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
222 # Now authenticated, with the legacy public flag
223 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
224 basic_authorization user.display_name, "test"
225 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }
226 assert_response :success
227 trace = Trace.find(response.body.to_i)
228 assert_equal "a.gpx", trace.name
229 assert_equal "New Trace", trace.description
230 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
231 assert_equal "public", trace.visibility
232 assert_equal false, trace.inserted
233 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
235 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
240 # Now authenticated, with the legacy private flag
241 second_user = create(:user)
242 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
243 basic_authorization second_user.display_name, "test"
244 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }
245 assert_response :success
246 trace = Trace.find(response.body.to_i)
247 assert_equal "a.gpx", trace.name
248 assert_equal "New Trace", trace.description
249 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
250 assert_equal "private", trace.visibility
251 assert_equal false, trace.inserted
252 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
254 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
257 # Check updating a trace through the api
259 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
260 deleted_trace_file = create(:trace, :deleted)
261 anon_trace_file = create(:trace, :visibility => "private")
264 put :update, :params => { :id => public_trace_file.id }, :body => create_trace_xml(public_trace_file)
265 assert_response :unauthorized
267 # Now with some other user, which should fail
268 basic_authorization create(:user).display_name, "test"
269 put :update, :params => { :id => public_trace_file.id }, :body => create_trace_xml(public_trace_file)
270 assert_response :forbidden
272 # Now with a trace which doesn't exist
273 basic_authorization create(:user).display_name, "test"
274 put :update, :params => { :id => 0 }, :body => create_trace_xml(public_trace_file)
275 assert_response :not_found
277 # Now with a trace which did exist but has been deleted
278 basic_authorization deleted_trace_file.user.display_name, "test"
279 put :update, :params => { :id => deleted_trace_file.id }, :body => create_trace_xml(deleted_trace_file)
280 assert_response :not_found
282 # Now try an update with the wrong ID
283 basic_authorization public_trace_file.user.display_name, "test"
284 put :update, :params => { :id => public_trace_file.id }, :body => create_trace_xml(anon_trace_file)
285 assert_response :bad_request,
286 "should not be able to update a trace with a different ID from the XML"
288 # And finally try an update that should work
289 basic_authorization public_trace_file.user.display_name, "test"
290 t = public_trace_file
291 t.description = "Changed description"
292 t.visibility = "private"
293 put :update, :params => { :id => t.id }, :body => create_trace_xml(t)
294 assert_response :success
295 nt = Trace.find(t.id)
296 assert_equal nt.description, t.description
297 assert_equal nt.visibility, t.visibility
300 # Test that updating a trace doesn't duplicate the tags
302 tracetag = create(:tracetag)
303 trace = tracetag.trace
304 basic_authorization trace.user.display_name, "test"
306 put :update, :params => { :id => trace.id }, :body => create_trace_xml(trace)
307 assert_response :success
309 updated = Trace.find(trace.id)
310 # Ensure there's only one tag in the database after updating
311 assert_equal Tracetag.count, 1
312 # The new tag object might have a different id, so check the string representation
313 assert_equal trace.tagstring, updated.tagstring
316 # Check deleting a trace through the api
318 public_trace_file = create(:trace, :visibility => "public")
321 delete :destroy, :params => { :id => public_trace_file.id }
322 assert_response :unauthorized
324 # Now with some other user, which should fail
325 basic_authorization create(:user).display_name, "test"
326 delete :destroy, :params => { :id => public_trace_file.id }
327 assert_response :forbidden
329 # Now with a trace which doesn't exist
330 basic_authorization create(:user).display_name, "test"
331 delete :destroy, :params => { :id => 0 }
332 assert_response :not_found
334 # And finally we should be able to do it with the owner of the trace
335 basic_authorization public_trace_file.user.display_name, "test"
336 delete :destroy, :params => { :id => public_trace_file.id }
337 assert_response :success
339 # Try it a second time, which should fail
340 basic_authorization public_trace_file.user.display_name, "test"
341 delete :destroy, :params => { :id => public_trace_file.id }
342 assert_response :not_found
347 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
348 assert_response :success
349 assert_equal digest, Digest::MD5.hexdigest(response.body)
350 assert_equal content_type, response.media_type
351 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
355 # build XML for traces
356 # this builds a minimum viable XML for the tests in this suite
357 def create_trace_xml(trace)
358 root = XML::Document.new
359 root.root = XML::Node.new "osm"
360 trc = XML::Node.new "gpx_file"
361 trc["id"] = trace.id.to_s
362 trc["visibility"] = trace.visibility
363 trc["visible"] = trace.visible.to_s
364 desc = XML::Node.new "description"
365 desc << trace.description
367 trace.tags.each do |tag|
368 t = XML::Node.new "tag"