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/edit", :method => :patch },
162 { :controller => "trace", :action => "edit", :id => "1" }
165 { :path => "/trace/1/delete", :method => :post },
166 { :controller => "trace", :action => "delete", :id => "1" }
170 # Check that the list of traces is displayed
173 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
174 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
175 create(:tracetag, :trace => trace, :tag => "London")
177 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
178 create(:tracetag, :trace => trace, :tag => "Birmingham")
180 trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
181 create(:tracetag, :trace => trace, :tag => "London")
183 trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
184 create(:tracetag, :trace => trace, :tag => "Birmingham")
187 # First with the public list
189 check_trace_list [trace_b, trace_a]
191 # Restrict traces to those with a given tag
192 get :list, :tag => "London"
193 check_trace_list [trace_a]
195 # Should see more when we are logged in
196 get :list, {}, { :user => user }
197 check_trace_list [trace_d, trace_c, trace_b, trace_a]
199 # Again, we should see more when we are logged in
200 get :list, { :tag => "London" }, { :user => user }
201 check_trace_list [trace_c, trace_a]
204 # Check that I can get mine
207 create(:trace, :visibility => "public") do |trace|
208 create(:tracetag, :trace => trace, :tag => "Birmingham")
210 trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
211 create(:tracetag, :trace => trace, :tag => "London")
214 # First try to get it when not logged in
216 assert_redirected_to :controller => "user", :action => "login", :referer => "/traces/mine"
218 # Now try when logged in
219 get :mine, {}, { :user => user }
220 assert_redirected_to :controller => "trace", :action => "list", :display_name => user.display_name
222 # Fetch the actual list
223 get :list, { :display_name => user.display_name }, { :user => user }
224 check_trace_list [trace_b]
227 # Check the list of traces for a specific user
230 second_user = create(:user)
231 third_user = create(:user)
233 trace_b = create(:trace, :visibility => "public", :user => user)
234 trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
235 create(:tracetag, :trace => trace, :tag => "London")
238 # Test a user with no traces
239 get :list, :display_name => second_user.display_name
242 # Test the user with the traces - should see only public ones
243 get :list, :display_name => user.display_name
244 check_trace_list [trace_b]
246 # Should still see only public ones when authenticated as another user
247 get :list, { :display_name => user.display_name }, { :user => third_user }
248 check_trace_list [trace_b]
250 # Should see all traces when authenticated as the target user
251 get :list, { :display_name => user.display_name }, { :user => user }
252 check_trace_list [trace_c, trace_b]
254 # Should only see traces with the correct tag when a tag is specified
255 get :list, { :display_name => user.display_name, :tag => "London" }, { :user => user }
256 check_trace_list [trace_c]
258 # Should get an error if the user does not exist
259 get :list, :display_name => "UnknownUser"
260 assert_response :not_found
261 assert_template "user/no_such_user"
264 # Check that the rss loads
268 # First with the public feed
269 get :georss, :format => :rss
270 check_trace_feed Trace.visible_to_all
272 # Restrict traces to those with a given tag
273 get :georss, :tag => "London", :format => :rss
274 check_trace_feed Trace.tagged("London").visible_to_all
276 # Restrict traces to those for a given user
277 get :georss, :display_name => user.display_name, :format => :rss
278 check_trace_feed user.traces.visible_to_all
280 # Restrict traces to those for a given user with a tiven tag
281 get :georss, :display_name => user.display_name, :tag => "Birmingham", :format => :rss
282 check_trace_feed user.traces.tagged("Birmingham").visible_to_all
285 # Test viewing a trace
287 public_trace_file = create(:trace, :visibility => "public")
289 # First with no auth, which should work since the trace is public
290 get :view, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
291 check_trace_view public_trace_file
293 # Now with some other user, which should work since the trace is public
294 get :view, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
295 check_trace_view public_trace_file
297 # And finally we should be able to do it with the owner of the trace
298 get :view, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
299 check_trace_view public_trace_file
302 # Check an anonymous trace can't be viewed by another user
304 anon_trace_file = create(:trace, :visibility => "private")
307 get :view, :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id
308 assert_response :redirect
309 assert_redirected_to :action => :list
311 # Now with some other user, which should not work since the trace is anon
312 get :view, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => create(:user) }
313 assert_response :redirect
314 assert_redirected_to :action => :list
316 # And finally we should be able to do it with the owner of the trace
317 get :view, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => anon_trace_file.user }
318 check_trace_view anon_trace_file
321 # Test viewing a trace that doesn't exist
322 def test_view_not_found
323 deleted_trace_file = create(:trace, :deleted)
326 get :view, :display_name => deleted_trace_file.user.display_name, :id => 0
327 assert_response :redirect
328 assert_redirected_to :action => :list
330 # Now with some other user
331 get :view, { :display_name => deleted_trace_file.user.display_name, :id => 0 }, { :user => create(:user) }
332 assert_response :redirect
333 assert_redirected_to :action => :list
335 # And finally we should not be able to view a deleted trace
336 get :view, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
337 assert_response :redirect
338 assert_redirected_to :action => :list
341 # Test downloading a trace
343 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
345 # First with no auth, which should work since the trace is public
346 get :data, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
347 check_trace_data public_trace_file
349 # Now with some other user, which should work since the trace is public
350 get :data, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
351 check_trace_data public_trace_file
353 # And finally we should be able to do it with the owner of the trace
354 get :data, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
355 check_trace_data public_trace_file
358 # Test downloading a compressed trace
359 def test_data_compressed
360 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
362 # First get the data as is
363 get :data, :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id
364 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
366 # Now ask explicitly for XML format
367 get :data, :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml"
368 check_trace_data identifiable_trace_file, "application/xml", "xml"
370 # Now ask explicitly for GPX format
371 get :data, :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx"
372 check_trace_data identifiable_trace_file
375 # Check an anonymous trace can't be downloaded by another user
377 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
380 get :data, :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id
381 assert_response :not_found
383 # Now with some other user, which shouldn't work since the trace is anon
384 get :data, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => create(:user) }
385 assert_response :not_found
387 # And finally we should be able to do it with the owner of the trace
388 get :data, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => anon_trace_file.user }
389 check_trace_data anon_trace_file
392 # Test downloading a trace that doesn't exist
393 def test_data_not_found
394 deleted_trace_file = create(:trace, :deleted)
396 # First with no auth and a trace that has never existed
397 get :data, :display_name => deleted_trace_file.user.display_name, :id => 0
398 assert_response :not_found
400 # Now with a trace that has never existed
401 get :data, { :display_name => deleted_trace_file.user.display_name, :id => 0 }, { :user => deleted_trace_file.user }
402 assert_response :not_found
404 # Now with a trace that has been deleted
405 get :data, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
406 assert_response :not_found
409 # Test downloading the picture for a trace
411 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
413 # First with no auth, which should work since the trace is public
414 get :picture, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
415 check_trace_picture public_trace_file
417 # Now with some other user, which should work since the trace is public
418 get :picture, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
419 check_trace_picture public_trace_file
421 # And finally we should be able to do it with the owner of the trace
422 get :picture, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
423 check_trace_picture public_trace_file
426 # Check the picture for an anonymous trace can't be downloaded by another user
427 def test_picture_anon
428 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
431 get :picture, :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id
432 assert_response :forbidden
434 # Now with some other user, which shouldn't work since the trace is anon
435 get :picture, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => create(:user) }
436 assert_response :forbidden
438 # And finally we should be able to do it with the owner of the trace
439 get :picture, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => anon_trace_file.user }
440 check_trace_picture anon_trace_file
443 # Test downloading the picture for a trace that doesn't exist
444 def test_picture_not_found
445 # First with no auth, which should work since the trace is public
446 get :picture, :display_name => create(:user).display_name, :id => 0
447 assert_response :not_found
449 # Now with some other user, which should work since the trace is public
450 get :picture, { :display_name => create(:user).display_name, :id => 0 }, { :user => create(:user) }
451 assert_response :not_found
453 # And finally we should not be able to do it with a deleted trace
454 deleted_trace_file = create(:trace, :deleted)
455 get :picture, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
456 assert_response :not_found
459 # Test downloading the icon for a trace
461 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
463 # First with no auth, which should work since the trace is public
464 get :icon, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
465 check_trace_icon public_trace_file
467 # Now with some other user, which should work since the trace is public
468 get :icon, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
469 check_trace_icon public_trace_file
471 # And finally we should be able to do it with the owner of the trace
472 get :icon, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
473 check_trace_icon public_trace_file
476 # Check the icon for an anonymous trace can't be downloaded by another user
478 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
481 get :icon, :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id
482 assert_response :forbidden
484 # Now with some other user, which shouldn't work since the trace is anon
485 get :icon, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => create(:user) }
486 assert_response :forbidden
488 # And finally we should be able to do it with the owner of the trace
489 get :icon, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => anon_trace_file.user }
490 check_trace_icon anon_trace_file
493 # Test downloading the icon for a trace that doesn't exist
494 def test_icon_not_found
496 get :icon, :display_name => create(:user).display_name, :id => 0
497 assert_response :not_found
499 # Now with some other user
500 get :icon, { :display_name => create(:user).display_name, :id => 0 }, { :user => create(:user) }
501 assert_response :not_found
503 # And finally we should not be able to do it with a deleted trace
504 deleted_trace_file = create(:trace, :deleted)
505 get :icon, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
506 assert_response :not_found
509 # Test fetching the create page
513 assert_response :redirect
514 assert_redirected_to :controller => :user, :action => :login, :referer => trace_create_path
516 # Now authenticated as a user with gps.trace.visibility set
518 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
519 get :create, {}, { :user => user }
520 assert_response :success
521 assert_template :create
522 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
524 # Now authenticated as a user with gps.trace.public set
525 second_user = create(:user)
526 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
527 get :create, {}, { :user => second_user }
528 assert_response :success
529 assert_template :create
530 assert_select "select#trace_visibility option[value=public][selected]", 1
532 # Now authenticated as a user with no preferences
533 third_user = create(:user)
534 get :create, {}, { :user => third_user }
535 assert_response :success
536 assert_template :create
537 assert_select "select#trace_visibility option[value=private][selected]", 1
540 # Test creating a trace
543 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
544 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
548 post :create, :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" }
549 assert_response :forbidden
552 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
553 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
554 post :create, { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, { :user => user }
555 assert_response :redirect
556 assert_redirected_to :action => :list, :display_name => user.display_name
557 assert_match /file has been uploaded/, flash[:notice]
558 trace = Trace.order(:id => :desc).first
559 assert_equal "a.gpx", trace.name
560 assert_equal "New Trace", trace.description
561 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
562 assert_equal "trackable", trace.visibility
563 assert_equal false, trace.inserted
564 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
566 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
569 # Test fetching the edit page for a trace using GET
571 public_trace_file = create(:trace, :visibility => "public")
572 deleted_trace_file = create(:trace, :deleted)
575 get :edit, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
576 assert_response :redirect
577 assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)
579 # Now with some other user, which should fail
580 get :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
581 assert_response :forbidden
583 # Now with a trace which doesn't exist
584 get :edit, { :display_name => create(:user).display_name, :id => 0 }, { :user => create(:user) }
585 assert_response :not_found
587 # Now with a trace which has been deleted
588 get :edit, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
589 assert_response :not_found
591 # Finally with a trace that we are allowed to edit
592 get :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
593 assert_response :success
596 # Test fetching the edit page for a trace using POST
597 def test_edit_post_no_details
598 public_trace_file = create(:trace, :visibility => "public")
599 deleted_trace_file = create(:trace, :deleted)
602 post :edit, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
603 assert_response :forbidden
605 # Now with some other user, which should fail
606 post :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
607 assert_response :forbidden
609 # Now with a trace which doesn't exist
610 post :edit, { :display_name => create(:user).display_name, :id => 0 }, { :user => create(:user) }
611 assert_response :not_found
613 # Now with a trace which has been deleted
614 post :edit, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
615 assert_response :not_found
617 # Finally with a trace that we are allowed to edit
618 post :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
619 assert_response :success
622 # Test saving edits to a trace
623 def test_edit_post_with_details
624 public_trace_file = create(:trace, :visibility => "public")
625 deleted_trace_file = create(:trace, :deleted)
628 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
631 post :edit, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details
632 assert_response :forbidden
634 # Now with some other user, which should fail
635 post :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, { :user => create(:user) }
636 assert_response :forbidden
638 # Now with a trace which doesn't exist
639 post :edit, { :display_name => create(:user).display_name, :id => 0 }, { :user => create(:user), :trace => new_details }
640 assert_response :not_found
642 # Now with a trace which has been deleted
643 post :edit, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id, :trace => new_details }, { :user => deleted_trace_file.user }
644 assert_response :not_found
646 # Finally with a trace that we are allowed to edit
647 post :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, { :user => public_trace_file.user }
648 assert_response :redirect
649 assert_redirected_to :action => :view, :display_name => public_trace_file.user.display_name
650 trace = Trace.find(public_trace_file.id)
651 assert_equal new_details[:description], trace.description
652 assert_equal new_details[:tagstring], trace.tagstring
653 assert_equal new_details[:visibility], trace.visibility
656 # Test deleting a trace
658 public_trace_file = create(:trace, :visibility => "public")
659 deleted_trace_file = create(:trace, :deleted)
662 post :delete, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
663 assert_response :forbidden
665 # Now with some other user, which should fail
666 post :delete, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
667 assert_response :forbidden
669 # Now with a trace which doesn't exist
670 post :delete, { :display_name => create(:user).display_name, :id => 0 }, { :user => create(:user) }
671 assert_response :not_found
673 # Now with a trace has already been deleted
674 post :delete, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
675 assert_response :not_found
677 # Finally with a trace that we are allowed to delete
678 post :delete, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
679 assert_response :redirect
680 assert_redirected_to :action => :list, :display_name => public_trace_file.user.display_name
681 trace = Trace.find(public_trace_file.id)
682 assert_equal false, trace.visible
685 # Check getting a specific trace through the api
687 public_trace_file = create(:trace, :visibility => "public")
690 get :api_read, :id => public_trace_file.id
691 assert_response :unauthorized
693 # Now with some other user, which should work since the trace is public
694 basic_authorization(create(:user).display_name, "test")
695 get :api_read, :id => public_trace_file.id
696 assert_response :success
698 # And finally we should be able to do it with the owner of the trace
699 basic_authorization(public_trace_file.user.display_name, "test")
700 get :api_read, :id => public_trace_file.id
701 assert_response :success
704 # Check an anoymous trace can't be specifically fetched by another user
705 def test_api_read_anon
706 anon_trace_file = create(:trace, :visibility => "private")
709 get :api_read, :id => anon_trace_file.id
710 assert_response :unauthorized
712 # Now try with another user, which shouldn't work since the trace is anon
713 basic_authorization(create(:user).display_name, "test")
714 get :api_read, :id => anon_trace_file.id
715 assert_response :forbidden
717 # And finally we should be able to get the trace details with the trace owner
718 basic_authorization(anon_trace_file.user.display_name, "test")
719 get :api_read, :id => anon_trace_file.id
720 assert_response :success
723 # Check the api details for a trace that doesn't exist
724 def test_api_read_not_found
725 deleted_trace_file = create(:trace, :deleted)
727 # Try first with no auth, as it should require it
728 get :api_read, :id => 0
729 assert_response :unauthorized
731 # Login, and try again
732 basic_authorization(deleted_trace_file.user.display_name, "test")
733 get :api_read, :id => 0
734 assert_response :not_found
736 # Now try a trace which did exist but has been deleted
737 basic_authorization(deleted_trace_file.user.display_name, "test")
738 get :api_read, :id => deleted_trace_file.id
739 assert_response :not_found
742 # Test downloading a trace through the api
744 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
747 get :api_data, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
748 assert_response :unauthorized
750 # Now with some other user, which should work since the trace is public
751 basic_authorization(create(:user).display_name, "test")
752 get :api_data, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
753 check_trace_data public_trace_file
755 # And finally we should be able to do it with the owner of the trace
756 basic_authorization(public_trace_file.user.display_name, "test")
757 get :api_data, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
758 check_trace_data public_trace_file
761 # Test downloading a compressed trace through the api
762 def test_api_data_compressed
763 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
765 # Authenticate as the owner of the trace we will be using
766 basic_authorization(identifiable_trace_file.user.display_name, "test")
768 # First get the data as is
769 get :api_data, :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id
770 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
772 # Now ask explicitly for XML format
773 get :api_data, :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml"
774 check_trace_data identifiable_trace_file, "application/xml", "xml"
776 # Now ask explicitly for GPX format
777 get :api_data, :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx"
778 check_trace_data identifiable_trace_file
781 # Check an anonymous trace can't be downloaded by another user through the api
782 def test_api_data_anon
783 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
786 get :api_data, :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id
787 assert_response :unauthorized
789 # Now with some other user, which shouldn't work since the trace is anon
790 basic_authorization(create(:user).display_name, "test")
791 get :api_data, :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id
792 assert_response :forbidden
794 # And finally we should be able to do it with the owner of the trace
795 basic_authorization(anon_trace_file.user.display_name, "test")
796 get :api_data, :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id
797 check_trace_data anon_trace_file
800 # Test downloading a trace that doesn't exist through the api
801 def test_api_data_not_found
803 get :api_data, :display_name => create(:user).display_name, :id => 0
804 assert_response :unauthorized
806 # Now with a trace that has never existed
807 basic_authorization(create(:user).display_name, "test")
808 get :api_data, :display_name => create(:user).display_name, :id => 0
809 assert_response :not_found
811 # Now with a trace that has been deleted
812 deleted_trace_file = create(:trace, :deleted)
813 basic_authorization(deleted_trace_file.user.display_name, "test")
814 get :api_data, :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id
815 assert_response :not_found
818 # Test creating a trace through the api
821 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
822 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
826 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
827 assert_response :unauthorized
830 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
831 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
832 basic_authorization(user.display_name, "test")
833 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
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 "trackable", trace.visibility
840 assert_equal false, trace.inserted
841 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
843 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
848 # Now authenticated, with the legacy public flag
849 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
850 basic_authorization(user.display_name, "test")
851 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 1
852 assert_response :success
853 trace = Trace.find(response.body.to_i)
854 assert_equal "a.gpx", trace.name
855 assert_equal "New Trace", trace.description
856 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
857 assert_equal "public", trace.visibility
858 assert_equal false, trace.inserted
859 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
861 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
866 # Now authenticated, with the legacy private flag
867 second_user = create(:user)
868 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
869 basic_authorization(second_user.display_name, "test")
870 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 0
871 assert_response :success
872 trace = Trace.find(response.body.to_i)
873 assert_equal "a.gpx", trace.name
874 assert_equal "New Trace", trace.description
875 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
876 assert_equal "private", trace.visibility
877 assert_equal false, trace.inserted
878 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
880 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
883 # Check updating a trace through the api
885 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
886 deleted_trace_file = create(:trace, :deleted)
887 anon_trace_file = create(:trace, :visibility => "private")
890 content public_trace_file.to_xml
891 put :api_update, :id => public_trace_file.id
892 assert_response :unauthorized
894 # Now with some other user, which should fail
895 basic_authorization(create(:user).display_name, "test")
896 content public_trace_file.to_xml
897 put :api_update, :id => public_trace_file.id
898 assert_response :forbidden
900 # Now with a trace which doesn't exist
901 basic_authorization(create(:user).display_name, "test")
902 content public_trace_file.to_xml
903 put :api_update, :id => 0
904 assert_response :not_found
906 # Now with a trace which did exist but has been deleted
907 basic_authorization(deleted_trace_file.user.display_name, "test")
908 content deleted_trace_file.to_xml
909 put :api_update, :id => deleted_trace_file.id
910 assert_response :not_found
912 # Now try an update with the wrong ID
913 basic_authorization(public_trace_file.user.display_name, "test")
914 content anon_trace_file.to_xml
915 put :api_update, :id => public_trace_file.id
916 assert_response :bad_request,
917 "should not be able to update a trace with a different ID from the XML"
919 # And finally try an update that should work
920 basic_authorization(public_trace_file.user.display_name, "test")
921 t = public_trace_file
922 t.description = "Changed description"
923 t.visibility = "private"
925 put :api_update, :id => t.id
926 assert_response :success
927 nt = Trace.find(t.id)
928 assert_equal nt.description, t.description
929 assert_equal nt.visibility, t.visibility
932 # Check deleting a trace through the api
934 public_trace_file = create(:trace, :visibility => "public")
937 delete :api_delete, :id => public_trace_file.id
938 assert_response :unauthorized
940 # Now with some other user, which should fail
941 basic_authorization(create(:user).display_name, "test")
942 delete :api_delete, :id => public_trace_file.id
943 assert_response :forbidden
945 # Now with a trace which doesn't exist
946 basic_authorization(create(:user).display_name, "test")
947 delete :api_delete, :id => 0
948 assert_response :not_found
950 # And finally we should be able to do it with the owner of the trace
951 basic_authorization(public_trace_file.user.display_name, "test")
952 delete :api_delete, :id => public_trace_file.id
953 assert_response :success
955 # Try it a second time, which should fail
956 basic_authorization(public_trace_file.user.display_name, "test")
957 delete :api_delete, :id => public_trace_file.id
958 assert_response :not_found
963 def check_trace_feed(traces)
964 assert_response :success
965 assert_template "georss"
966 assert_equal "application/rss+xml", @response.content_type
967 assert_select "rss", :count => 1 do
968 assert_select "channel", :count => 1 do
969 assert_select "title"
970 assert_select "description"
972 assert_select "image"
973 assert_select "item", :count => traces.visible.count do |items|
974 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
975 assert_select item, "title", trace.name
976 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
977 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
978 assert_select item, "description"
979 # assert_select item, "dc:creator", trace.user.display_name
980 assert_select item, "pubDate", trace.timestamp.rfc822
987 def check_trace_list(traces)
988 assert_response :success
989 assert_template "list"
992 assert_select "table#trace_list tbody", :count => 1 do
993 assert_select "tr", :count => traces.length do |rows|
994 traces.zip(rows).each do |trace, row|
995 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
996 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
997 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
998 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
1003 assert_select "h4", /Nothing here yet/
1007 def check_trace_view(trace)
1008 assert_response :success
1009 assert_template "view"
1011 assert_select "table", :count => 1 do
1012 assert_select "td", /^#{Regexp.quote(trace.name)} /
1013 assert_select "td", trace.user.display_name
1014 assert_select "td", trace.description
1018 def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
1019 assert_response :success
1020 assert_equal content_type, response.content_type
1021 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
1024 def check_trace_picture(trace)
1025 assert_response :success
1026 assert_equal "image/gif", response.content_type
1027 assert_equal trace.large_picture, response.body
1030 def check_trace_icon(trace)
1031 assert_response :success
1032 assert_equal "image/gif", response.content_type
1033 assert_equal trace.icon_picture, response.body