2 require "minitest/mock"
4 class TraceControllerTest < ActionController::TestCase
6 @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
7 Object.const_set("GPX_TRACE_DIR", Rails.root.join("test", "gpx", "traces"))
9 @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
10 Object.const_set("GPX_IMAGE_DIR", Rails.root.join("test", "gpx", "images"))
14 File.unlink(*Dir.glob(File.join(GPX_TRACE_DIR, "*.gpx")))
15 File.unlink(*Dir.glob(File.join(GPX_IMAGE_DIR, "*.gif")))
17 Object.send("remove_const", "GPX_TRACE_DIR")
18 Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
20 Object.send("remove_const", "GPX_IMAGE_DIR")
21 Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
25 # test all routes which lead to this controller
28 { :path => "/api/0.6/gpx/create", :method => :post },
29 { :controller => "trace", :action => "api_create" }
32 { :path => "/api/0.6/gpx/1", :method => :get },
33 { :controller => "trace", :action => "api_read", :id => "1" }
36 { :path => "/api/0.6/gpx/1", :method => :put },
37 { :controller => "trace", :action => "api_update", :id => "1" }
40 { :path => "/api/0.6/gpx/1", :method => :delete },
41 { :controller => "trace", :action => "api_delete", :id => "1" }
44 { :controller => "trace", :action => "api_read", :id => "1" },
45 { :path => "/api/0.6/gpx/1/details", :method => :get }
48 { :path => "/api/0.6/gpx/1/data", :method => :get },
49 { :controller => "trace", :action => "api_data", :id => "1" }
52 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
53 { :controller => "trace", :action => "api_data", :id => "1", :format => "xml" }
57 { :path => "/traces", :method => :get },
58 { :controller => "trace", :action => "list" }
61 { :path => "/traces/page/1", :method => :get },
62 { :controller => "trace", :action => "list", :page => "1" }
65 { :path => "/traces/tag/tagname", :method => :get },
66 { :controller => "trace", :action => "list", :tag => "tagname" }
69 { :path => "/traces/tag/tagname/page/1", :method => :get },
70 { :controller => "trace", :action => "list", :tag => "tagname", :page => "1" }
73 { :path => "/user/username/traces", :method => :get },
74 { :controller => "trace", :action => "list", :display_name => "username" }
77 { :path => "/user/username/traces/page/1", :method => :get },
78 { :controller => "trace", :action => "list", :display_name => "username", :page => "1" }
81 { :path => "/user/username/traces/tag/tagname", :method => :get },
82 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname" }
85 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
86 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname", :page => "1" }
90 { :path => "/traces/mine", :method => :get },
91 { :controller => "trace", :action => "mine" }
94 { :path => "/traces/mine/page/1", :method => :get },
95 { :controller => "trace", :action => "mine", :page => "1" }
98 { :path => "/traces/mine/tag/tagname", :method => :get },
99 { :controller => "trace", :action => "mine", :tag => "tagname" }
102 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
103 { :controller => "trace", :action => "mine", :tag => "tagname", :page => "1" }
107 { :path => "/traces/rss", :method => :get },
108 { :controller => "trace", :action => "georss", :format => :rss }
111 { :path => "/traces/tag/tagname/rss", :method => :get },
112 { :controller => "trace", :action => "georss", :tag => "tagname", :format => :rss }
115 { :path => "/user/username/traces/rss", :method => :get },
116 { :controller => "trace", :action => "georss", :display_name => "username", :format => :rss }
119 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
120 { :controller => "trace", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
124 { :path => "/user/username/traces/1", :method => :get },
125 { :controller => "trace", :action => "view", :display_name => "username", :id => "1" }
128 { :path => "/user/username/traces/1/picture", :method => :get },
129 { :controller => "trace", :action => "picture", :display_name => "username", :id => "1" }
132 { :path => "/user/username/traces/1/icon", :method => :get },
133 { :controller => "trace", :action => "icon", :display_name => "username", :id => "1" }
137 { :path => "/trace/create", :method => :get },
138 { :controller => "trace", :action => "create" }
141 { :path => "/trace/create", :method => :post },
142 { :controller => "trace", :action => "create" }
145 { :path => "/trace/1/data", :method => :get },
146 { :controller => "trace", :action => "data", :id => "1" }
149 { :path => "/trace/1/data.xml", :method => :get },
150 { :controller => "trace", :action => "data", :id => "1", :format => "xml" }
153 { :path => "/trace/1/edit", :method => :get },
154 { :controller => "trace", :action => "edit", :id => "1" }
157 { :path => "/trace/1/edit", :method => :post },
158 { :controller => "trace", :action => "edit", :id => "1" }
161 { :path => "/trace/1/delete", :method => :post },
162 { :controller => "trace", :action => "delete", :id => "1" }
166 # Check that the list of traces is displayed
169 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
170 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
171 create(:tracetag, :trace => trace, :tag => "London")
173 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
174 create(:tracetag, :trace => trace, :tag => "Birmingham")
176 trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
177 create(:tracetag, :trace => trace, :tag => "London")
179 trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
180 create(:tracetag, :trace => trace, :tag => "Birmingham")
183 # First with the public list
185 check_trace_list [trace_b, trace_a]
187 # Restrict traces to those with a given tag
188 get :list, :params => { :tag => "London" }
189 check_trace_list [trace_a]
191 # Should see more when we are logged in
192 get :list, :session => { :user => user }
193 check_trace_list [trace_d, trace_c, trace_b, trace_a]
195 # Again, we should see more when we are logged in
196 get :list, :params => { :tag => "London" }, :session => { :user => user }
197 check_trace_list [trace_c, trace_a]
200 # Check that I can get mine
203 create(:trace, :visibility => "public") do |trace|
204 create(:tracetag, :trace => trace, :tag => "Birmingham")
206 trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
207 create(:tracetag, :trace => trace, :tag => "London")
210 # First try to get it when not logged in
212 assert_redirected_to :controller => "user", :action => "login", :referer => "/traces/mine"
214 # Now try when logged in
215 get :mine, :session => { :user => user }
216 assert_redirected_to :controller => "trace", :action => "list", :display_name => user.display_name
218 # Fetch the actual list
219 get :list, :params => { :display_name => user.display_name }, :session => { :user => user }
220 check_trace_list [trace_b]
223 # Check the list of traces for a specific user
226 second_user = create(:user)
227 third_user = create(:user)
229 trace_b = create(:trace, :visibility => "public", :user => user)
230 trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
231 create(:tracetag, :trace => trace, :tag => "London")
234 # Test a user with no traces
235 get :list, :params => { :display_name => second_user.display_name }
238 # Test the user with the traces - should see only public ones
239 get :list, :params => { :display_name => user.display_name }
240 check_trace_list [trace_b]
242 # Should still see only public ones when authenticated as another user
243 get :list, :params => { :display_name => user.display_name }, :session => { :user => third_user }
244 check_trace_list [trace_b]
246 # Should see all traces when authenticated as the target user
247 get :list, :params => { :display_name => user.display_name }, :session => { :user => user }
248 check_trace_list [trace_c, trace_b]
250 # Should only see traces with the correct tag when a tag is specified
251 get :list, :params => { :display_name => user.display_name, :tag => "London" }, :session => { :user => user }
252 check_trace_list [trace_c]
254 # Should get an error if the user does not exist
255 get :list, :params => { :display_name => "UnknownUser" }
256 assert_response :not_found
257 assert_template "user/no_such_user"
260 # Check that the rss loads
264 # First with the public feed
265 get :georss, :params => { :format => :rss }
266 check_trace_feed Trace.visible_to_all
268 # Restrict traces to those with a given tag
269 get :georss, :params => { :tag => "London", :format => :rss }
270 check_trace_feed Trace.tagged("London").visible_to_all
272 # Restrict traces to those for a given user
273 get :georss, :params => { :display_name => user.display_name, :format => :rss }
274 check_trace_feed user.traces.visible_to_all
276 # Restrict traces to those for a given user with a tiven tag
277 get :georss, :params => { :display_name => user.display_name, :tag => "Birmingham", :format => :rss }
278 check_trace_feed user.traces.tagged("Birmingham").visible_to_all
281 # Test viewing a trace
283 public_trace_file = create(:trace, :visibility => "public")
285 # First with no auth, which should work since the trace is public
286 get :view, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
287 check_trace_view public_trace_file
289 # Now with some other user, which should work since the trace is public
290 get :view, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
291 check_trace_view public_trace_file
293 # And finally we should be able to do it with the owner of the trace
294 get :view, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
295 check_trace_view public_trace_file
298 # Check an anonymous trace can't be viewed by another user
300 anon_trace_file = create(:trace, :visibility => "private")
303 get :view, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
304 assert_response :redirect
305 assert_redirected_to :action => :list
307 # Now with some other user, which should not work since the trace is anon
308 get :view, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
309 assert_response :redirect
310 assert_redirected_to :action => :list
312 # And finally we should be able to do it with the owner of the trace
313 get :view, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
314 check_trace_view anon_trace_file
317 # Test viewing a trace that doesn't exist
318 def test_view_not_found
319 deleted_trace_file = create(:trace, :deleted)
321 # First with a trace that has never existed
322 get :view, :params => { :display_name => create(:user).display_name, :id => 0 }
323 assert_response :redirect
324 assert_redirected_to :action => :list
326 # Now with a trace that has been deleted
327 get :view, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
328 assert_response :redirect
329 assert_redirected_to :action => :list
332 # Test downloading a trace
334 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
336 # First with no auth, which should work since the trace is public
337 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
338 check_trace_data public_trace_file
340 # Now with some other user, which should work since the trace is public
341 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
342 check_trace_data public_trace_file
344 # And finally we should be able to do it with the owner of the trace
345 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
346 check_trace_data public_trace_file
349 # Test downloading a compressed trace
350 def test_data_compressed
351 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
353 # First get the data as is
354 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id }
355 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
357 # Now ask explicitly for XML format
358 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" }
359 check_trace_data identifiable_trace_file, "application/xml", "xml"
361 # Now ask explicitly for GPX format
362 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" }
363 check_trace_data identifiable_trace_file
366 # Check an anonymous trace can't be downloaded by another user
368 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
371 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
372 assert_response :not_found
374 # Now with some other user, which shouldn't work since the trace is anon
375 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
376 assert_response :not_found
378 # And finally we should be able to do it with the owner of the trace
379 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
380 check_trace_data anon_trace_file
383 # Test downloading a trace that doesn't exist
384 def test_data_not_found
385 deleted_trace_file = create(:trace, :deleted)
387 # First with a trace that has never existed
388 get :data, :params => { :display_name => create(:user).display_name, :id => 0 }
389 assert_response :not_found
391 # Now with a trace that has been deleted
392 get :data, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
393 assert_response :not_found
396 # Test downloading the picture for a trace
398 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
400 # First with no auth, which should work since the trace is public
401 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
402 check_trace_picture public_trace_file
404 # Now with some other user, which should work since the trace is public
405 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
406 check_trace_picture public_trace_file
408 # And finally we should be able to do it with the owner of the trace
409 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
410 check_trace_picture public_trace_file
413 # Check the picture for an anonymous trace can't be downloaded by another user
414 def test_picture_anon
415 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
418 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
419 assert_response :forbidden
421 # Now with some other user, which shouldn't work since the trace is anon
422 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
423 assert_response :forbidden
425 # And finally we should be able to do it with the owner of the trace
426 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
427 check_trace_picture anon_trace_file
430 # Test downloading the picture for a trace that doesn't exist
431 def test_picture_not_found
432 deleted_trace_file = create(:trace, :deleted)
434 # First with a trace that has never existed
435 get :picture, :params => { :display_name => create(:user).display_name, :id => 0 }
436 assert_response :not_found
438 # Now with a trace that has been deleted
439 get :picture, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
440 assert_response :not_found
443 # Test downloading the icon for a trace
445 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
447 # First with no auth, which should work since the trace is public
448 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
449 check_trace_icon public_trace_file
451 # Now with some other user, which should work since the trace is public
452 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
453 check_trace_icon public_trace_file
455 # And finally we should be able to do it with the owner of the trace
456 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
457 check_trace_icon public_trace_file
460 # Check the icon for an anonymous trace can't be downloaded by another user
462 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
465 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
466 assert_response :forbidden
468 # Now with some other user, which shouldn't work since the trace is anon
469 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
470 assert_response :forbidden
472 # And finally we should be able to do it with the owner of the trace
473 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
474 check_trace_icon anon_trace_file
477 # Test downloading the icon for a trace that doesn't exist
478 def test_icon_not_found
479 deleted_trace_file = create(:trace, :deleted)
481 # First with a trace that has never existed
482 get :icon, :params => { :display_name => create(:user).display_name, :id => 0 }
483 assert_response :not_found
485 # Now with a trace that has been deleted
486 get :icon, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
487 assert_response :not_found
490 # Test fetching the create page
494 assert_response :redirect
495 assert_redirected_to :controller => :user, :action => :login, :referer => trace_create_path
497 # Now authenticated as a user with gps.trace.visibility set
499 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
500 get :create, :session => { :user => user }
501 assert_response :success
502 assert_template :create
503 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
505 # Now authenticated as a user with gps.trace.public set
506 second_user = create(:user)
507 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
508 get :create, :session => { :user => second_user }
509 assert_response :success
510 assert_template :create
511 assert_select "select#trace_visibility option[value=public][selected]", 1
513 # Now authenticated as a user with no preferences
514 third_user = create(:user)
515 get :create, :session => { :user => third_user }
516 assert_response :success
517 assert_template :create
518 assert_select "select#trace_visibility option[value=private][selected]", 1
521 # Test creating a trace
524 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
525 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
529 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
530 assert_response :forbidden
533 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
534 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
535 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
536 assert_response :redirect
537 assert_redirected_to :action => :list, :display_name => user.display_name
538 assert_match /file has been uploaded/, flash[:notice]
539 trace = Trace.order(:id => :desc).first
540 assert_equal "a.gpx", trace.name
541 assert_equal "New Trace", trace.description
542 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
543 assert_equal "trackable", trace.visibility
544 assert_equal false, trace.inserted
545 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
547 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
550 # Test fetching the edit page for a trace using GET
552 public_trace_file = create(:trace, :visibility => "public")
553 deleted_trace_file = create(:trace, :deleted)
556 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
557 assert_response :redirect
558 assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)
560 # Now with some other user, which should fail
561 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
562 assert_response :forbidden
564 # Now with a trace which doesn't exist
565 get :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
566 assert_response :not_found
568 # Now with a trace which has been deleted
569 get :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
570 assert_response :not_found
572 # Finally with a trace that we are allowed to edit
573 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
574 assert_response :success
577 # Test fetching the edit page for a trace using POST
578 def test_edit_post_no_details
579 public_trace_file = create(:trace, :visibility => "public")
580 deleted_trace_file = create(:trace, :deleted)
583 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
584 assert_response :forbidden
586 # Now with some other user, which should fail
587 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
588 assert_response :forbidden
590 # Now with a trace which doesn't exist
591 post :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
592 assert_response :not_found
594 # Now with a trace which has been deleted
595 post :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
596 assert_response :not_found
598 # Finally with a trace that we are allowed to edit
599 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
600 assert_response :success
603 # Test saving edits to a trace
604 def test_edit_post_with_details
605 public_trace_file = create(:trace, :visibility => "public")
606 deleted_trace_file = create(:trace, :deleted)
609 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
612 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }
613 assert_response :forbidden
615 # Now with some other user, which should fail
616 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => create(:user) }
617 assert_response :forbidden
619 # Now with a trace which doesn't exist
620 post :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user), :trace => new_details }
621 assert_response :not_found
623 # Now with a trace which has been deleted
624 post :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id, :trace => new_details }, :session => { :user => deleted_trace_file.user }
625 assert_response :not_found
627 # Finally with a trace that we are allowed to edit
628 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => public_trace_file.user }
629 assert_response :redirect
630 assert_redirected_to :action => :view, :display_name => public_trace_file.user.display_name
631 trace = Trace.find(public_trace_file.id)
632 assert_equal new_details[:description], trace.description
633 assert_equal new_details[:tagstring], trace.tagstring
634 assert_equal new_details[:visibility], trace.visibility
637 # Test deleting a trace
639 public_trace_file = create(:trace, :visibility => "public")
640 deleted_trace_file = create(:trace, :deleted)
643 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
644 assert_response :forbidden
646 # Now with some other user, which should fail
647 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
648 assert_response :forbidden
650 # Now with a trace which doesn't exist
651 post :delete, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
652 assert_response :not_found
654 # Now with a trace has already been deleted
655 post :delete, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
656 assert_response :not_found
658 # Finally with a trace that we are allowed to delete
659 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
660 assert_response :redirect
661 assert_redirected_to :action => :list, :display_name => public_trace_file.user.display_name
662 trace = Trace.find(public_trace_file.id)
663 assert_equal false, trace.visible
666 # Check getting a specific trace through the api
668 public_trace_file = create(:trace, :visibility => "public")
671 get :api_read, :params => { :id => public_trace_file.id }
672 assert_response :unauthorized
674 # Now with some other user, which should work since the trace is public
675 basic_authorization(create(:user).display_name, "test")
676 get :api_read, :params => { :id => public_trace_file.id }
677 assert_response :success
679 # And finally we should be able to do it with the owner of the trace
680 basic_authorization(public_trace_file.user.display_name, "test")
681 get :api_read, :params => { :id => public_trace_file.id }
682 assert_response :success
685 # Check an anoymous trace can't be specifically fetched by another user
686 def test_api_read_anon
687 anon_trace_file = create(:trace, :visibility => "private")
690 get :api_read, :params => { :id => anon_trace_file.id }
691 assert_response :unauthorized
693 # Now try with another user, which shouldn't work since the trace is anon
694 basic_authorization(create(:user).display_name, "test")
695 get :api_read, :params => { :id => anon_trace_file.id }
696 assert_response :forbidden
698 # And finally we should be able to get the trace details with the trace owner
699 basic_authorization(anon_trace_file.user.display_name, "test")
700 get :api_read, :params => { :id => anon_trace_file.id }
701 assert_response :success
704 # Check the api details for a trace that doesn't exist
705 def test_api_read_not_found
706 deleted_trace_file = create(:trace, :deleted)
708 # Try first with no auth, as it should require it
709 get :api_read, :params => { :id => 0 }
710 assert_response :unauthorized
712 # Login, and try again
713 basic_authorization(deleted_trace_file.user.display_name, "test")
714 get :api_read, :params => { :id => 0 }
715 assert_response :not_found
717 # Now try a trace which did exist but has been deleted
718 basic_authorization(deleted_trace_file.user.display_name, "test")
719 get :api_read, :params => { :id => deleted_trace_file.id }
720 assert_response :not_found
723 # Test downloading a trace through the api
725 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
728 get :api_data, :params => { :id => public_trace_file.id }
729 assert_response :unauthorized
731 # Now with some other user, which should work since the trace is public
732 basic_authorization(create(:user).display_name, "test")
733 get :api_data, :params => { :id => public_trace_file.id }
734 check_trace_data public_trace_file
736 # And finally we should be able to do it with the owner of the trace
737 basic_authorization(public_trace_file.user.display_name, "test")
738 get :api_data, :params => { :id => public_trace_file.id }
739 check_trace_data public_trace_file
742 # Test downloading a compressed trace through the api
743 def test_api_data_compressed
744 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
746 # Authenticate as the owner of the trace we will be using
747 basic_authorization(identifiable_trace_file.user.display_name, "test")
749 # First get the data as is
750 get :api_data, :params => { :id => identifiable_trace_file.id }
751 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
753 # Now ask explicitly for XML format
754 get :api_data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
755 check_trace_data identifiable_trace_file, "application/xml", "xml"
757 # Now ask explicitly for GPX format
758 get :api_data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
759 check_trace_data identifiable_trace_file
762 # Check an anonymous trace can't be downloaded by another user through the api
763 def test_api_data_anon
764 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
767 get :api_data, :params => { :id => anon_trace_file.id }
768 assert_response :unauthorized
770 # Now with some other user, which shouldn't work since the trace is anon
771 basic_authorization(create(:user).display_name, "test")
772 get :api_data, :params => { :id => anon_trace_file.id }
773 assert_response :forbidden
775 # And finally we should be able to do it with the owner of the trace
776 basic_authorization(anon_trace_file.user.display_name, "test")
777 get :api_data, :params => { :id => anon_trace_file.id }
778 check_trace_data anon_trace_file
781 # Test downloading a trace that doesn't exist through the api
782 def test_api_data_not_found
783 deleted_trace_file = create(:trace, :deleted)
785 # Try first with no auth, as it should require it
786 get :api_data, :params => { :id => 0 }
787 assert_response :unauthorized
789 # Login, and try again
790 basic_authorization(create(:user).display_name, "test")
791 get :api_data, :params => { :id => 0 }
792 assert_response :not_found
794 # Now try a trace which did exist but has been deleted
795 basic_authorization(deleted_trace_file.user.display_name, "test")
796 get :api_data, :params => { :id => deleted_trace_file.id }
797 assert_response :not_found
800 # Test creating a trace through the api
803 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
804 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
808 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
809 assert_response :unauthorized
812 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
813 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
814 basic_authorization(user.display_name, "test")
815 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
816 assert_response :success
817 trace = Trace.find(response.body.to_i)
818 assert_equal "a.gpx", trace.name
819 assert_equal "New Trace", trace.description
820 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
821 assert_equal "trackable", trace.visibility
822 assert_equal false, trace.inserted
823 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
825 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
830 # Now authenticated, with the legacy public flag
831 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
832 basic_authorization(user.display_name, "test")
833 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }
834 assert_response :success
835 trace = Trace.find(response.body.to_i)
836 assert_equal "a.gpx", trace.name
837 assert_equal "New Trace", trace.description
838 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
839 assert_equal "public", trace.visibility
840 assert_equal false, trace.inserted
841 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
843 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
848 # Now authenticated, with the legacy private flag
849 second_user = create(:user)
850 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
851 basic_authorization(second_user.display_name, "test")
852 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }
853 assert_response :success
854 trace = Trace.find(response.body.to_i)
855 assert_equal "a.gpx", trace.name
856 assert_equal "New Trace", trace.description
857 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
858 assert_equal "private", trace.visibility
859 assert_equal false, trace.inserted
860 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
862 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
865 # Check updating a trace through the api
867 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
868 deleted_trace_file = create(:trace, :deleted)
869 anon_trace_file = create(:trace, :visibility => "private")
872 content public_trace_file.to_xml
873 put :api_update, :params => { :id => public_trace_file.id }
874 assert_response :unauthorized
876 # Now with some other user, which should fail
877 basic_authorization(create(:user).display_name, "test")
878 content public_trace_file.to_xml
879 put :api_update, :params => { :id => public_trace_file.id }
880 assert_response :forbidden
882 # Now with a trace which doesn't exist
883 basic_authorization(create(:user).display_name, "test")
884 content public_trace_file.to_xml
885 put :api_update, :params => { :id => 0 }
886 assert_response :not_found
888 # Now with a trace which did exist but has been deleted
889 basic_authorization(deleted_trace_file.user.display_name, "test")
890 content deleted_trace_file.to_xml
891 put :api_update, :params => { :id => deleted_trace_file.id }
892 assert_response :not_found
894 # Now try an update with the wrong ID
895 basic_authorization(public_trace_file.user.display_name, "test")
896 content anon_trace_file.to_xml
897 put :api_update, :params => { :id => public_trace_file.id }
898 assert_response :bad_request,
899 "should not be able to update a trace with a different ID from the XML"
901 # And finally try an update that should work
902 basic_authorization(public_trace_file.user.display_name, "test")
903 t = public_trace_file
904 t.description = "Changed description"
905 t.visibility = "private"
907 put :api_update, :params => { :id => t.id }
908 assert_response :success
909 nt = Trace.find(t.id)
910 assert_equal nt.description, t.description
911 assert_equal nt.visibility, t.visibility
914 # Check deleting a trace through the api
916 public_trace_file = create(:trace, :visibility => "public")
919 delete :api_delete, :params => { :id => public_trace_file.id }
920 assert_response :unauthorized
922 # Now with some other user, which should fail
923 basic_authorization(create(:user).display_name, "test")
924 delete :api_delete, :params => { :id => public_trace_file.id }
925 assert_response :forbidden
927 # Now with a trace which doesn't exist
928 basic_authorization(create(:user).display_name, "test")
929 delete :api_delete, :params => { :id => 0 }
930 assert_response :not_found
932 # And finally we should be able to do it with the owner of the trace
933 basic_authorization(public_trace_file.user.display_name, "test")
934 delete :api_delete, :params => { :id => public_trace_file.id }
935 assert_response :success
937 # Try it a second time, which should fail
938 basic_authorization(public_trace_file.user.display_name, "test")
939 delete :api_delete, :params => { :id => public_trace_file.id }
940 assert_response :not_found
945 def check_trace_feed(traces)
946 assert_response :success
947 assert_template "georss"
948 assert_equal "application/rss+xml", @response.content_type
949 assert_select "rss", :count => 1 do
950 assert_select "channel", :count => 1 do
951 assert_select "title"
952 assert_select "description"
954 assert_select "image"
955 assert_select "item", :count => traces.visible.count do |items|
956 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
957 assert_select item, "title", trace.name
958 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
959 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
960 assert_select item, "description"
961 # assert_select item, "dc:creator", trace.user.display_name
962 assert_select item, "pubDate", trace.timestamp.rfc822
969 def check_trace_list(traces)
970 assert_response :success
971 assert_template "list"
974 assert_select "table#trace_list tbody", :count => 1 do
975 assert_select "tr", :count => traces.length do |rows|
976 traces.zip(rows).each do |trace, row|
977 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
978 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
979 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
980 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
985 assert_select "h4", /Nothing here yet/
989 def check_trace_view(trace)
990 assert_response :success
991 assert_template "view"
993 assert_select "table", :count => 1 do
994 assert_select "td", /^#{Regexp.quote(trace.name)} /
995 assert_select "td", trace.user.display_name
996 assert_select "td", trace.description
1000 def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
1001 assert_response :success
1002 assert_equal content_type, response.content_type
1003 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
1006 def check_trace_picture(trace)
1007 assert_response :success
1008 assert_equal "image/gif", response.content_type
1009 assert_equal trace.large_picture, response.body
1012 def check_trace_icon(trace)
1013 assert_response :success
1014 assert_equal "image/gif", response.content_type
1015 assert_equal trace.icon_picture, response.body