2 require "minitest/mock"
5 class TracesControllerTest < ActionController::TestCase
7 File.unlink(*Dir.glob(File.join(Settings.gpx_trace_dir, "*.gpx")))
8 File.unlink(*Dir.glob(File.join(Settings.gpx_image_dir, "*.gif")))
12 # test all routes which lead to this controller
15 { :path => "/api/0.6/gpx/create", :method => :post },
16 { :controller => "api/traces", :action => "create" }
19 { :path => "/api/0.6/gpx/1", :method => :get },
20 { :controller => "api/traces", :action => "show", :id => "1" }
23 { :path => "/api/0.6/gpx/1", :method => :put },
24 { :controller => "api/traces", :action => "update", :id => "1" }
27 { :path => "/api/0.6/gpx/1", :method => :delete },
28 { :controller => "api/traces", :action => "destroy", :id => "1" }
31 { :controller => "api/traces", :action => "show", :id => "1" },
32 { :path => "/api/0.6/gpx/1/details", :method => :get }
35 { :path => "/api/0.6/gpx/1/data", :method => :get },
36 { :controller => "api/traces", :action => "data", :id => "1" }
39 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
40 { :controller => "api/traces", :action => "data", :id => "1", :format => "xml" }
44 # Check getting a specific trace through the api
46 public_trace_file = create(:trace, :visibility => "public")
49 get :show, :params => { :id => public_trace_file.id }
50 assert_response :unauthorized
52 # Now with some other user, which should work since the trace is public
53 basic_authorization create(:user).display_name, "test"
54 get :show, :params => { :id => public_trace_file.id }
55 assert_response :success
57 # And finally we should be able to do it with the owner of the trace
58 basic_authorization public_trace_file.user.display_name, "test"
59 get :show, :params => { :id => public_trace_file.id }
60 assert_response :success
63 # Check an anoymous trace can't be specifically fetched by another user
65 anon_trace_file = create(:trace, :visibility => "private")
68 get :show, :params => { :id => anon_trace_file.id }
69 assert_response :unauthorized
71 # Now try with another user, which shouldn't work since the trace is anon
72 basic_authorization create(:user).display_name, "test"
73 get :show, :params => { :id => anon_trace_file.id }
74 assert_response :forbidden
76 # And finally we should be able to get the trace details with the trace owner
77 basic_authorization anon_trace_file.user.display_name, "test"
78 get :show, :params => { :id => anon_trace_file.id }
79 assert_response :success
82 # Check the api details for a trace that doesn't exist
83 def test_show_not_found
84 deleted_trace_file = create(:trace, :deleted)
86 # Try first with no auth, as it should require it
87 get :show, :params => { :id => 0 }
88 assert_response :unauthorized
90 # Login, and try again
91 basic_authorization deleted_trace_file.user.display_name, "test"
92 get :show, :params => { :id => 0 }
93 assert_response :not_found
95 # Now try a trace which did exist but has been deleted
96 basic_authorization deleted_trace_file.user.display_name, "test"
97 get :show, :params => { :id => deleted_trace_file.id }
98 assert_response :not_found
101 # Test downloading a trace through the api
103 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
106 get :data, :params => { :id => public_trace_file.id }
107 assert_response :unauthorized
109 # Now with some other user, which should work since the trace is public
110 basic_authorization create(:user).display_name, "test"
111 get :data, :params => { :id => public_trace_file.id }
112 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
114 # And finally we should be able to do it with the owner of the trace
115 basic_authorization public_trace_file.user.display_name, "test"
116 get :data, :params => { :id => public_trace_file.id }
117 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
120 # Test downloading a compressed trace through the api
121 def test_data_compressed
122 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
124 # Authenticate as the owner of the trace we will be using
125 basic_authorization identifiable_trace_file.user.display_name, "test"
127 # First get the data as is
128 get :data, :params => { :id => identifiable_trace_file.id }
129 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
131 # Now ask explicitly for XML format
132 get :data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
133 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
135 # Now ask explicitly for GPX format
136 get :data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
137 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
140 # Check an anonymous trace can't be downloaded by another user through the api
142 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
145 get :data, :params => { :id => anon_trace_file.id }
146 assert_response :unauthorized
148 # Now with some other user, which shouldn't work since the trace is anon
149 basic_authorization create(:user).display_name, "test"
150 get :data, :params => { :id => anon_trace_file.id }
151 assert_response :forbidden
153 # And finally we should be able to do it with the owner of the trace
154 basic_authorization anon_trace_file.user.display_name, "test"
155 get :data, :params => { :id => anon_trace_file.id }
156 check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
159 # Test downloading a trace that doesn't exist through the api
160 def test_data_not_found
161 deleted_trace_file = create(:trace, :deleted)
163 # Try first with no auth, as it should require it
164 get :data, :params => { :id => 0 }
165 assert_response :unauthorized
167 # Login, and try again
168 basic_authorization create(:user).display_name, "test"
169 get :data, :params => { :id => 0 }
170 assert_response :not_found
172 # Now try a trace which did exist but has been deleted
173 basic_authorization deleted_trace_file.user.display_name, "test"
174 get :data, :params => { :id => deleted_trace_file.id }
175 assert_response :not_found
178 # Test creating a trace through the api
181 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
182 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
186 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
187 assert_response :unauthorized
193 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
194 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
195 basic_authorization user.display_name, "test"
196 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
197 assert_response :success
198 trace = Trace.find(response.body.to_i)
199 assert_equal "a.gpx", trace.name
200 assert_equal "New Trace", trace.description
201 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
202 assert_equal "trackable", trace.visibility
203 assert_equal false, trace.inserted
204 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
206 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
211 # Now authenticated, with the legacy public flag
212 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
213 basic_authorization user.display_name, "test"
214 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }
215 assert_response :success
216 trace = Trace.find(response.body.to_i)
217 assert_equal "a.gpx", trace.name
218 assert_equal "New Trace", trace.description
219 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
220 assert_equal "public", trace.visibility
221 assert_equal false, trace.inserted
222 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
224 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
229 # Now authenticated, with the legacy private flag
230 second_user = create(:user)
231 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
232 basic_authorization second_user.display_name, "test"
233 post :create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }
234 assert_response :success
235 trace = Trace.find(response.body.to_i)
236 assert_equal "a.gpx", trace.name
237 assert_equal "New Trace", trace.description
238 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
239 assert_equal "private", trace.visibility
240 assert_equal false, trace.inserted
241 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
243 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
246 # Check updating a trace through the api
248 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
249 deleted_trace_file = create(:trace, :deleted)
250 anon_trace_file = create(:trace, :visibility => "private")
253 put :update, :params => { :id => public_trace_file.id }, :body => public_trace_file.to_xml.to_s
254 assert_response :unauthorized
256 # Now with some other user, which should fail
257 basic_authorization create(:user).display_name, "test"
258 put :update, :params => { :id => public_trace_file.id }, :body => public_trace_file.to_xml.to_s
259 assert_response :forbidden
261 # Now with a trace which doesn't exist
262 basic_authorization create(:user).display_name, "test"
263 put :update, :params => { :id => 0 }, :body => public_trace_file.to_xml.to_s
264 assert_response :not_found
266 # Now with a trace which did exist but has been deleted
267 basic_authorization deleted_trace_file.user.display_name, "test"
268 put :update, :params => { :id => deleted_trace_file.id }, :body => deleted_trace_file.to_xml.to_s
269 assert_response :not_found
271 # Now try an update with the wrong ID
272 basic_authorization public_trace_file.user.display_name, "test"
273 put :update, :params => { :id => public_trace_file.id }, :body => anon_trace_file.to_xml.to_s
274 assert_response :bad_request,
275 "should not be able to update a trace with a different ID from the XML"
277 # And finally try an update that should work
278 basic_authorization public_trace_file.user.display_name, "test"
279 t = public_trace_file
280 t.description = "Changed description"
281 t.visibility = "private"
282 put :update, :params => { :id => t.id }, :body => t.to_xml.to_s
283 assert_response :success
284 nt = Trace.find(t.id)
285 assert_equal nt.description, t.description
286 assert_equal nt.visibility, t.visibility
289 # Test that updating a trace doesn't duplicate the tags
291 tracetag = create(:tracetag)
292 trace = tracetag.trace
293 basic_authorization trace.user.display_name, "test"
295 put :update, :params => { :id => trace.id }, :body => trace.to_xml.to_s
296 assert_response :success
298 updated = Trace.find(trace.id)
299 # Ensure there's only one tag in the database after updating
300 assert_equal Tracetag.count, 1
301 # The new tag object might have a different id, so check the string representation
302 assert_equal trace.tagstring, updated.tagstring
305 # Check deleting a trace through the api
307 public_trace_file = create(:trace, :visibility => "public")
310 delete :destroy, :params => { :id => public_trace_file.id }
311 assert_response :unauthorized
313 # Now with some other user, which should fail
314 basic_authorization create(:user).display_name, "test"
315 delete :destroy, :params => { :id => public_trace_file.id }
316 assert_response :forbidden
318 # Now with a trace which doesn't exist
319 basic_authorization create(:user).display_name, "test"
320 delete :destroy, :params => { :id => 0 }
321 assert_response :not_found
323 # And finally we should be able to do it with the owner of the trace
324 basic_authorization public_trace_file.user.display_name, "test"
325 delete :destroy, :params => { :id => public_trace_file.id }
326 assert_response :success
328 # Try it a second time, which should fail
329 basic_authorization public_trace_file.user.display_name, "test"
330 delete :destroy, :params => { :id => public_trace_file.id }
331 assert_response :not_found
336 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
337 assert_response :success
338 assert_equal digest, Digest::MD5.hexdigest(response.body)
339 assert_equal content_type, response.content_type
340 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]