4 class TracesControllerTest < ActionController::TestCase
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 :show, :params => { :id => public_trace_file.id }
60 assert_response :unauthorized
62 # Now with some other user, which should work since the trace is public
63 basic_authorization create(:user).display_name, "test"
64 get :show, :params => { :id => public_trace_file.id }
65 assert_response :success
67 # And finally we should be able to do it with the owner of the trace
68 basic_authorization public_trace_file.user.display_name, "test"
69 get :show, :params => { :id => public_trace_file.id }
70 assert_response :success
73 # Check an anoymous trace can't be specifically fetched by another user
75 anon_trace_file = create(:trace, :visibility => "private")
78 get :show, :params => { :id => anon_trace_file.id }
79 assert_response :unauthorized
81 # Now try with another user, which shouldn't work since the trace is anon
82 basic_authorization create(:user).display_name, "test"
83 get :show, :params => { :id => anon_trace_file.id }
84 assert_response :forbidden
86 # And finally we should be able to get the trace details with the trace owner
87 basic_authorization anon_trace_file.user.display_name, "test"
88 get :show, :params => { :id => anon_trace_file.id }
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 :show, :params => { :id => 0 }
98 assert_response :unauthorized
100 # Login, and try again
101 basic_authorization deleted_trace_file.user.display_name, "test"
102 get :show, :params => { :id => 0 }
103 assert_response :not_found
105 # Now try a trace which did exist but has been deleted
106 basic_authorization deleted_trace_file.user.display_name, "test"
107 get :show, :params => { :id => deleted_trace_file.id }
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 :data, :params => { :id => public_trace_file.id }
117 assert_response :unauthorized
119 # Now with some other user, which should work since the trace is public
120 basic_authorization create(:user).display_name, "test"
121 get :data, :params => { :id => public_trace_file.id }
122 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
124 # And finally we should be able to do it with the owner of the trace
125 basic_authorization public_trace_file.user.display_name, "test"
126 get :data, :params => { :id => public_trace_file.id }
127 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
130 # Test downloading a compressed trace through the api
131 def test_data_compressed
132 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
134 # Authenticate as the owner of the trace we will be using
135 basic_authorization identifiable_trace_file.user.display_name, "test"
137 # First get the data as is
138 get :data, :params => { :id => identifiable_trace_file.id }
139 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
141 # Now ask explicitly for XML format
142 get :data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
143 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
145 # Now ask explicitly for GPX format
146 get :data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
147 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
150 # Check an anonymous trace can't be downloaded by another user through the api
152 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
155 get :data, :params => { :id => anon_trace_file.id }
156 assert_response :unauthorized
158 # Now with some other user, which shouldn't work since the trace is anon
159 basic_authorization create(:user).display_name, "test"
160 get :data, :params => { :id => anon_trace_file.id }
161 assert_response :forbidden
163 # And finally we should be able to do it with the owner of the trace
164 basic_authorization anon_trace_file.user.display_name, "test"
165 get :data, :params => { :id => anon_trace_file.id }
166 check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
169 # Test downloading a trace that doesn't exist through the api
170 def test_data_not_found
171 deleted_trace_file = create(:trace, :deleted)
173 # Try first with no auth, as it should require it
174 get :data, :params => { :id => 0 }
175 assert_response :unauthorized
177 # Login, and try again
178 basic_authorization create(:user).display_name, "test"
179 get :data, :params => { :id => 0 }
180 assert_response :not_found
182 # Now try a trace which did exist but has been deleted
183 basic_authorization deleted_trace_file.user.display_name, "test"
184 get :data, :params => { :id => deleted_trace_file.id }
185 assert_response :not_found
188 # Test creating a trace through the api
191 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
192 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
196 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
197 assert_response :unauthorized
203 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
204 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
205 basic_authorization user.display_name, "test"
206 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
207 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_equal false, trace.inserted
214 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
216 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
221 # Now authenticated, with the legacy public flag
222 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
223 basic_authorization user.display_name, "test"
224 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }
225 assert_response :success
226 trace = Trace.find(response.body.to_i)
227 assert_equal "a.gpx", trace.name
228 assert_equal "New Trace", trace.description
229 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
230 assert_equal "public", trace.visibility
231 assert_equal false, trace.inserted
232 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
234 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
239 # Now authenticated, with the legacy private flag
240 second_user = create(:user)
241 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
242 basic_authorization second_user.display_name, "test"
243 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }
244 assert_response :success
245 trace = Trace.find(response.body.to_i)
246 assert_equal "a.gpx", trace.name
247 assert_equal "New Trace", trace.description
248 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
249 assert_equal "private", trace.visibility
250 assert_equal false, trace.inserted
251 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
253 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
256 # Check updating a trace through the api
258 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
259 deleted_trace_file = create(:trace, :deleted)
260 anon_trace_file = create(:trace, :visibility => "private")
263 put :update, :params => { :id => public_trace_file.id }, :body => create_trace_xml(public_trace_file)
264 assert_response :unauthorized
266 # Now with some other user, which should fail
267 basic_authorization create(:user).display_name, "test"
268 put :update, :params => { :id => public_trace_file.id }, :body => create_trace_xml(public_trace_file)
269 assert_response :forbidden
271 # Now with a trace which doesn't exist
272 basic_authorization create(:user).display_name, "test"
273 put :update, :params => { :id => 0 }, :body => create_trace_xml(public_trace_file)
274 assert_response :not_found
276 # Now with a trace which did exist but has been deleted
277 basic_authorization deleted_trace_file.user.display_name, "test"
278 put :update, :params => { :id => deleted_trace_file.id }, :body => create_trace_xml(deleted_trace_file)
279 assert_response :not_found
281 # Now try an update with the wrong ID
282 basic_authorization public_trace_file.user.display_name, "test"
283 put :update, :params => { :id => public_trace_file.id }, :body => create_trace_xml(anon_trace_file)
284 assert_response :bad_request,
285 "should not be able to update a trace with a different ID from the XML"
287 # And finally try an update that should work
288 basic_authorization public_trace_file.user.display_name, "test"
289 t = public_trace_file
290 t.description = "Changed description"
291 t.visibility = "private"
292 put :update, :params => { :id => t.id }, :body => create_trace_xml(t)
293 assert_response :success
294 nt = Trace.find(t.id)
295 assert_equal nt.description, t.description
296 assert_equal nt.visibility, t.visibility
299 # Test that updating a trace doesn't duplicate the tags
301 tracetag = create(:tracetag)
302 trace = tracetag.trace
303 basic_authorization trace.user.display_name, "test"
305 put :update, :params => { :id => trace.id }, :body => create_trace_xml(trace)
306 assert_response :success
308 updated = Trace.find(trace.id)
309 # Ensure there's only one tag in the database after updating
310 assert_equal Tracetag.count, 1
311 # The new tag object might have a different id, so check the string representation
312 assert_equal trace.tagstring, updated.tagstring
315 # Check deleting a trace through the api
317 public_trace_file = create(:trace, :visibility => "public")
320 delete :destroy, :params => { :id => public_trace_file.id }
321 assert_response :unauthorized
323 # Now with some other user, which should fail
324 basic_authorization create(:user).display_name, "test"
325 delete :destroy, :params => { :id => public_trace_file.id }
326 assert_response :forbidden
328 # Now with a trace which doesn't exist
329 basic_authorization create(:user).display_name, "test"
330 delete :destroy, :params => { :id => 0 }
331 assert_response :not_found
333 # And finally we should be able to do it with the owner of the trace
334 basic_authorization public_trace_file.user.display_name, "test"
335 delete :destroy, :params => { :id => public_trace_file.id }
336 assert_response :success
338 # Try it a second time, which should fail
339 basic_authorization public_trace_file.user.display_name, "test"
340 delete :destroy, :params => { :id => public_trace_file.id }
341 assert_response :not_found
346 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
347 assert_response :success
348 assert_equal digest, Digest::MD5.hexdigest(response.body)
349 assert_equal content_type, response.media_type
350 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
354 # build XML for traces
355 # this builds a minimum viable XML for the tests in this suite
356 def create_trace_xml(trace)
357 root = XML::Document.new
358 root.root = XML::Node.new "osm"
359 trc = XML::Node.new "gpx_file"
360 trc["id"] = trace.id.to_s
361 trc["visibility"] = trace.visibility
362 trc["visible"] = trace.visible.to_s
363 desc = XML::Node.new "description"
364 desc << trace.description
366 trace.tags.each do |tag|
367 t = XML::Node.new "tag"