2 require "minitest/mock"
5 class TracesControllerTest < ActionController::TestCase
7 @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
8 Object.const_set("GPX_TRACE_DIR", Rails.root.join("test", "gpx", "traces"))
10 @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
11 Object.const_set("GPX_IMAGE_DIR", Rails.root.join("test", "gpx", "images"))
15 File.unlink(*Dir.glob(File.join(GPX_TRACE_DIR, "*.gpx")))
16 File.unlink(*Dir.glob(File.join(GPX_IMAGE_DIR, "*.gif")))
18 Object.send("remove_const", "GPX_TRACE_DIR")
19 Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
21 Object.send("remove_const", "GPX_IMAGE_DIR")
22 Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
26 # test all routes which lead to this controller
29 { :path => "/api/0.6/gpx/create", :method => :post },
30 { :controller => "api/traces", :action => "api_create" }
33 { :path => "/api/0.6/gpx/1", :method => :get },
34 { :controller => "api/traces", :action => "api_read", :id => "1" }
37 { :path => "/api/0.6/gpx/1", :method => :put },
38 { :controller => "api/traces", :action => "api_update", :id => "1" }
41 { :path => "/api/0.6/gpx/1", :method => :delete },
42 { :controller => "api/traces", :action => "api_delete", :id => "1" }
45 { :controller => "api/traces", :action => "api_read", :id => "1" },
46 { :path => "/api/0.6/gpx/1/details", :method => :get }
49 { :path => "/api/0.6/gpx/1/data", :method => :get },
50 { :controller => "api/traces", :action => "api_data", :id => "1" }
53 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
54 { :controller => "api/traces", :action => "api_data", :id => "1", :format => "xml" }
58 # Check getting a specific trace through the api
60 public_trace_file = create(:trace, :visibility => "public")
63 get :api_read, :params => { :id => public_trace_file.id }
64 assert_response :unauthorized
66 # Now with some other user, which should work since the trace is public
67 basic_authorization create(:user).display_name, "test"
68 get :api_read, :params => { :id => public_trace_file.id }
69 assert_response :success
71 # And finally we should be able to do it with the owner of the trace
72 basic_authorization public_trace_file.user.display_name, "test"
73 get :api_read, :params => { :id => public_trace_file.id }
74 assert_response :success
77 # Check an anoymous trace can't be specifically fetched by another user
78 def test_api_read_anon
79 anon_trace_file = create(:trace, :visibility => "private")
82 get :api_read, :params => { :id => anon_trace_file.id }
83 assert_response :unauthorized
85 # Now try with another user, which shouldn't work since the trace is anon
86 basic_authorization create(:user).display_name, "test"
87 get :api_read, :params => { :id => anon_trace_file.id }
88 assert_response :forbidden
90 # And finally we should be able to get the trace details with the trace owner
91 basic_authorization anon_trace_file.user.display_name, "test"
92 get :api_read, :params => { :id => anon_trace_file.id }
93 assert_response :success
96 # Check the api details for a trace that doesn't exist
97 def test_api_read_not_found
98 deleted_trace_file = create(:trace, :deleted)
100 # Try first with no auth, as it should require it
101 get :api_read, :params => { :id => 0 }
102 assert_response :unauthorized
104 # Login, and try again
105 basic_authorization deleted_trace_file.user.display_name, "test"
106 get :api_read, :params => { :id => 0 }
107 assert_response :not_found
109 # Now try a trace which did exist but has been deleted
110 basic_authorization deleted_trace_file.user.display_name, "test"
111 get :api_read, :params => { :id => deleted_trace_file.id }
112 assert_response :not_found
115 # Test downloading a trace through the api
117 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
120 get :api_data, :params => { :id => public_trace_file.id }
121 assert_response :unauthorized
123 # Now with some other user, which should work since the trace is public
124 basic_authorization create(:user).display_name, "test"
125 get :api_data, :params => { :id => public_trace_file.id }
126 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
128 # And finally we should be able to do it with the owner of the trace
129 basic_authorization public_trace_file.user.display_name, "test"
130 get :api_data, :params => { :id => public_trace_file.id }
131 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
134 # Test downloading a compressed trace through the api
135 def test_api_data_compressed
136 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
138 # Authenticate as the owner of the trace we will be using
139 basic_authorization identifiable_trace_file.user.display_name, "test"
141 # First get the data as is
142 get :api_data, :params => { :id => identifiable_trace_file.id }
143 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
145 # Now ask explicitly for XML format
146 get :api_data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
147 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
149 # Now ask explicitly for GPX format
150 get :api_data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
151 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
154 # Check an anonymous trace can't be downloaded by another user through the api
155 def test_api_data_anon
156 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
159 get :api_data, :params => { :id => anon_trace_file.id }
160 assert_response :unauthorized
162 # Now with some other user, which shouldn't work since the trace is anon
163 basic_authorization create(:user).display_name, "test"
164 get :api_data, :params => { :id => anon_trace_file.id }
165 assert_response :forbidden
167 # And finally we should be able to do it with the owner of the trace
168 basic_authorization anon_trace_file.user.display_name, "test"
169 get :api_data, :params => { :id => anon_trace_file.id }
170 check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
173 # Test downloading a trace that doesn't exist through the api
174 def test_api_data_not_found
175 deleted_trace_file = create(:trace, :deleted)
177 # Try first with no auth, as it should require it
178 get :api_data, :params => { :id => 0 }
179 assert_response :unauthorized
181 # Login, and try again
182 basic_authorization create(:user).display_name, "test"
183 get :api_data, :params => { :id => 0 }
184 assert_response :not_found
186 # Now try a trace which did exist but has been deleted
187 basic_authorization deleted_trace_file.user.display_name, "test"
188 get :api_data, :params => { :id => deleted_trace_file.id }
189 assert_response :not_found
192 # Test creating a trace through the api
195 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
196 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
200 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
201 assert_response :unauthorized
207 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
208 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
209 basic_authorization user.display_name, "test"
210 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
211 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_equal false, trace.inserted
218 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
220 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
225 # Now authenticated, with the legacy public flag
226 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
227 basic_authorization user.display_name, "test"
228 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }
229 assert_response :success
230 trace = Trace.find(response.body.to_i)
231 assert_equal "a.gpx", trace.name
232 assert_equal "New Trace", trace.description
233 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
234 assert_equal "public", trace.visibility
235 assert_equal false, trace.inserted
236 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
238 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
243 # Now authenticated, with the legacy private flag
244 second_user = create(:user)
245 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
246 basic_authorization second_user.display_name, "test"
247 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }
248 assert_response :success
249 trace = Trace.find(response.body.to_i)
250 assert_equal "a.gpx", trace.name
251 assert_equal "New Trace", trace.description
252 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
253 assert_equal "private", trace.visibility
254 assert_equal false, trace.inserted
255 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
257 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
260 # Check updating a trace through the api
262 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
263 deleted_trace_file = create(:trace, :deleted)
264 anon_trace_file = create(:trace, :visibility => "private")
267 put :api_update, :params => { :id => public_trace_file.id }, :body => public_trace_file.to_xml.to_s
268 assert_response :unauthorized
270 # Now with some other user, which should fail
271 basic_authorization create(:user).display_name, "test"
272 put :api_update, :params => { :id => public_trace_file.id }, :body => public_trace_file.to_xml.to_s
273 assert_response :forbidden
275 # Now with a trace which doesn't exist
276 basic_authorization create(:user).display_name, "test"
277 put :api_update, :params => { :id => 0 }, :body => public_trace_file.to_xml.to_s
278 assert_response :not_found
280 # Now with a trace which did exist but has been deleted
281 basic_authorization deleted_trace_file.user.display_name, "test"
282 put :api_update, :params => { :id => deleted_trace_file.id }, :body => deleted_trace_file.to_xml.to_s
283 assert_response :not_found
285 # Now try an update with the wrong ID
286 basic_authorization public_trace_file.user.display_name, "test"
287 put :api_update, :params => { :id => public_trace_file.id }, :body => anon_trace_file.to_xml.to_s
288 assert_response :bad_request,
289 "should not be able to update a trace with a different ID from the XML"
291 # And finally try an update that should work
292 basic_authorization public_trace_file.user.display_name, "test"
293 t = public_trace_file
294 t.description = "Changed description"
295 t.visibility = "private"
296 put :api_update, :params => { :id => t.id }, :body => t.to_xml.to_s
297 assert_response :success
298 nt = Trace.find(t.id)
299 assert_equal nt.description, t.description
300 assert_equal nt.visibility, t.visibility
303 # Test that updating a trace doesn't duplicate the tags
304 def test_api_update_tags
305 tracetag = create(:tracetag)
306 trace = tracetag.trace
307 basic_authorization trace.user.display_name, "test"
309 put :api_update, :params => { :id => trace.id }, :body => trace.to_xml.to_s
310 assert_response :success
312 updated = Trace.find(trace.id)
313 # Ensure there's only one tag in the database after updating
314 assert_equal Tracetag.count, 1
315 # The new tag object might have a different id, so check the string representation
316 assert_equal trace.tagstring, updated.tagstring
319 # Check deleting a trace through the api
321 public_trace_file = create(:trace, :visibility => "public")
324 delete :api_delete, :params => { :id => public_trace_file.id }
325 assert_response :unauthorized
327 # Now with some other user, which should fail
328 basic_authorization create(:user).display_name, "test"
329 delete :api_delete, :params => { :id => public_trace_file.id }
330 assert_response :forbidden
332 # Now with a trace which doesn't exist
333 basic_authorization create(:user).display_name, "test"
334 delete :api_delete, :params => { :id => 0 }
335 assert_response :not_found
337 # And finally we should be able to do it with the owner of the trace
338 basic_authorization public_trace_file.user.display_name, "test"
339 delete :api_delete, :params => { :id => public_trace_file.id }
340 assert_response :success
342 # Try it a second time, which should fail
343 basic_authorization public_trace_file.user.display_name, "test"
344 delete :api_delete, :params => { :id => public_trace_file.id }
345 assert_response :not_found
350 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
351 assert_response :success
352 assert_equal digest, Digest::MD5.hexdigest(response.body)
353 assert_equal content_type, response.content_type
354 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]