3 class TracesControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/traces", :method => :get },
9 { :controller => "traces", :action => "index" }
12 { :path => "/traces/tag/tagname", :method => :get },
13 { :controller => "traces", :action => "index", :tag => "tagname" }
16 { :path => "/user/username/traces", :method => :get },
17 { :controller => "traces", :action => "index", :display_name => "username" }
20 { :path => "/user/username/traces/tag/tagname", :method => :get },
21 { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
25 { :path => "/traces/mine", :method => :get },
26 { :controller => "traces", :action => "mine" }
29 { :path => "/traces/mine/tag/tagname", :method => :get },
30 { :controller => "traces", :action => "mine", :tag => "tagname" }
34 { :path => "/traces/rss", :method => :get },
35 { :controller => "traces", :action => "georss", :format => :rss }
38 { :path => "/traces/tag/tagname/rss", :method => :get },
39 { :controller => "traces", :action => "georss", :tag => "tagname", :format => :rss }
42 { :path => "/user/username/traces/rss", :method => :get },
43 { :controller => "traces", :action => "georss", :display_name => "username", :format => :rss }
46 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
47 { :controller => "traces", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
51 { :path => "/user/username/traces/1", :method => :get },
52 { :controller => "traces", :action => "show", :display_name => "username", :id => "1" }
55 { :path => "/user/username/traces/1/picture", :method => :get },
56 { :controller => "traces", :action => "picture", :display_name => "username", :id => "1" }
59 { :path => "/user/username/traces/1/icon", :method => :get },
60 { :controller => "traces", :action => "icon", :display_name => "username", :id => "1" }
64 { :path => "/traces/new", :method => :get },
65 { :controller => "traces", :action => "new" }
68 { :path => "/traces", :method => :post },
69 { :controller => "traces", :action => "create" }
72 { :path => "/trace/1/data", :method => :get },
73 { :controller => "traces", :action => "data", :id => "1" }
76 { :path => "/trace/1/data.xml", :method => :get },
77 { :controller => "traces", :action => "data", :id => "1", :format => "xml" }
80 { :path => "/traces/1/edit", :method => :get },
81 { :controller => "traces", :action => "edit", :id => "1" }
84 { :path => "/traces/1", :method => :put },
85 { :controller => "traces", :action => "update", :id => "1" }
88 { :path => "/traces/1", :method => :delete },
89 { :controller => "traces", :action => "destroy", :id => "1" }
93 assert_redirected_to "/traces"
95 get "/traces/tag/tagname/page/1"
96 assert_redirected_to "/traces/tag/tagname"
98 get "/user/username/traces/page/1"
99 assert_redirected_to "/user/username/traces"
101 get "/user/username/traces/tag/tagname/page/1"
102 assert_redirected_to "/user/username/traces/tag/tagname"
104 get "/traces/mine/page/1"
105 assert_redirected_to "/traces/mine"
107 get "/traces/mine/tag/tagname/page/1"
108 assert_redirected_to "/traces/mine/tag/tagname"
111 # Check that the index of traces is displayed
114 # The fourth test below is surprisingly sensitive to timestamp ordering when the timestamps are equal.
115 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
116 create(:tracetag, :trace => trace, :tag => "London")
118 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
119 create(:tracetag, :trace => trace, :tag => "Birmingham")
121 trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
122 create(:tracetag, :trace => trace, :tag => "London")
124 trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
125 create(:tracetag, :trace => trace, :tag => "Birmingham")
128 # First with the public index
130 check_trace_index [trace_b, trace_a]
132 # Restrict traces to those with a given tag
133 get traces_path(:tag => "London")
134 check_trace_index [trace_a]
138 # Should see more when we are logged in
140 check_trace_index [trace_d, trace_c, trace_b, trace_a]
142 # Again, we should see more when we are logged in
143 get traces_path(:tag => "London")
144 check_trace_index [trace_c, trace_a]
147 # Check that I can get mine
150 create(:trace, :visibility => "public") do |trace|
151 create(:tracetag, :trace => trace, :tag => "Birmingham")
153 trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
154 create(:tracetag, :trace => trace, :tag => "London")
157 # First try to get it when not logged in
159 assert_redirected_to login_path(:referer => "/traces/mine")
163 # Now try when logged in
165 assert_redirected_to :action => "index", :display_name => user.display_name
167 # Fetch the actual index
168 get traces_path(:display_name => user.display_name)
169 check_trace_index [trace_b]
172 # Check the index of traces for a specific user
175 checked_user_traces_path = url_for :only_path => true, :controller => "traces", :action => "index", :display_name => user.display_name
176 second_user = create(:user)
177 third_user = create(:user)
179 trace_b = create(:trace, :visibility => "public", :user => user)
180 trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
181 create(:tracetag, :trace => trace, :tag => "London")
184 # Test a user with no traces
185 get traces_path(:display_name => second_user.display_name)
188 # Test the user with the traces - should see only public ones
189 get traces_path(:display_name => user.display_name)
190 check_trace_index [trace_b]
191 assert_dom ".nav-tabs" do
192 assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
193 assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 0
194 assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 1
197 session_for(third_user)
199 # Should still see only public ones when authenticated as another user
200 get traces_path(:display_name => user.display_name)
201 check_trace_index [trace_b]
202 assert_dom ".nav-tabs" do
203 assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
204 assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 1
205 assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 1
210 # Should see all traces when authenticated as the target user
211 get traces_path(:display_name => user.display_name)
212 check_trace_index [trace_c, trace_b]
213 assert_dom ".nav-tabs" do
214 assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
215 assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 1
216 assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 0
219 # Should only see traces with the correct tag when a tag is specified
220 get traces_path(:display_name => user.display_name, :tag => "London")
221 check_trace_index [trace_c]
223 # Should get an error if the user does not exist
224 get traces_path(:display_name => "UnknownUser")
225 assert_response :not_found
226 assert_template "users/no_such_user"
229 # Check a multi-page index
231 # Create several pages worth of traces
232 create_list(:trace, 50)
234 # Try and get the index
236 assert_response :success
237 assert_select "table#trace_list tbody", :count => 1 do
238 assert_select "tr", :count => 20
240 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
241 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
243 # Try and get the second page
244 get css_select("li.page-item a.page-link").last["href"]
245 assert_response :success
246 assert_select "table#trace_list tbody", :count => 1 do
247 assert_select "tr", :count => 20
249 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
250 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
252 # Try and get the third page
253 get css_select("li.page-item a.page-link").last["href"]
254 assert_response :success
255 assert_select "table#trace_list tbody", :count => 1 do
256 assert_select "tr", :count => 10
258 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
259 assert_select "li.page-item.disabled span.page-link", :text => "Older Traces", :count => 2
261 # Go back to the second page
262 get css_select("li.page-item a.page-link").first["href"]
263 assert_response :success
264 assert_select "table#trace_list tbody", :count => 1 do
265 assert_select "tr", :count => 20
267 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
268 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
270 # Go back to the first page
271 get css_select("li.page-item a.page-link").first["href"]
272 assert_response :success
273 assert_select "table#trace_list tbody", :count => 1 do
274 assert_select "tr", :count => 20
276 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
277 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
280 # Check a multi-page index of tagged traces
281 def test_index_tagged_paged
282 # Create several pages worth of traces
283 create_list(:trace, 100) do |trace, index|
284 create(:tracetag, :trace => trace, :tag => "London") if index.even?
287 # Try and get the index
288 get traces_path(:tag => "London")
289 assert_response :success
290 assert_select "table#trace_list tbody", :count => 1 do
291 assert_select "tr", :count => 20
293 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
294 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
296 # Try and get the second page
297 get css_select("li.page-item a.page-link").last["href"]
298 assert_response :success
299 assert_select "table#trace_list tbody", :count => 1 do
300 assert_select "tr", :count => 20
302 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
303 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
305 # Try and get the third page
306 get css_select("li.page-item a.page-link").last["href"]
307 assert_response :success
308 assert_select "table#trace_list tbody", :count => 1 do
309 assert_select "tr", :count => 10
311 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
312 assert_select "li.page-item.disabled span.page-link", :text => "Older Traces", :count => 2
314 # Go back to the second page
315 get css_select("li.page-item a.page-link").first["href"]
316 assert_response :success
317 assert_select "table#trace_list tbody", :count => 1 do
318 assert_select "tr", :count => 20
320 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
321 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
323 # Go back to the first page
324 get css_select("li.page-item a.page-link").first["href"]
325 assert_response :success
326 assert_select "table#trace_list tbody", :count => 1 do
327 assert_select "tr", :count => 20
329 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
330 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
336 # The fourth test below is surprisingly sensitive to timestamp ordering when the timestamps are equal.
337 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
338 create(:tracetag, :trace => trace, :tag => "London")
340 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
341 create(:tracetag, :trace => trace, :tag => "Birmingham")
343 create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
344 create(:tracetag, :trace => trace, :tag => "London")
346 create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
347 create(:tracetag, :trace => trace, :tag => "Birmingham")
350 # First with the public feed
352 check_trace_feed [trace_b, trace_a]
354 # Restrict traces to those with a given tag
355 get traces_rss_path(:tag => "London")
356 check_trace_feed [trace_a]
359 # Check the RSS feed for a specific user
362 second_user = create(:user)
365 trace_b = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago, :user => user)
366 trace_c = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago, :user => user) do |trace|
367 create(:tracetag, :trace => trace, :tag => "London")
369 create(:trace, :visibility => "private")
371 # Test a user with no traces
372 get traces_rss_path(:display_name => second_user.display_name)
375 # Test the user with the traces - should see only public ones
376 get traces_rss_path(:display_name => user.display_name)
377 check_trace_feed [trace_c, trace_b]
379 # Should only see traces with the correct tag when a tag is specified
380 get traces_rss_path(:display_name => user.display_name, :tag => "London")
381 check_trace_feed [trace_c]
383 # Should no traces if the user does not exist
384 get traces_rss_path(:display_name => "UnknownUser")
388 # Test showing a trace
390 public_trace_file = create(:trace, :visibility => "public")
392 # First with no auth, which should work since the trace is public
393 get show_trace_path(public_trace_file.user, public_trace_file)
394 check_trace_show public_trace_file
396 # Now with some other user, which should work since the trace is public
397 session_for(create(:user))
398 get show_trace_path(public_trace_file.user, public_trace_file)
399 check_trace_show public_trace_file
401 # And finally we should be able to do it with the owner of the trace
402 session_for(public_trace_file.user)
403 get show_trace_path(public_trace_file.user, public_trace_file)
404 check_trace_show public_trace_file
407 # Check an anonymous trace can't be viewed by another user
409 anon_trace_file = create(:trace, :visibility => "private")
412 get show_trace_path(anon_trace_file.user, anon_trace_file)
413 assert_redirected_to :action => :index
415 # Now with some other user, which should not work since the trace is anon
416 session_for(create(:user))
417 get show_trace_path(anon_trace_file.user, anon_trace_file)
418 assert_redirected_to :action => :index
420 # And finally we should be able to do it with the owner of the trace
421 session_for(anon_trace_file.user)
422 get show_trace_path(anon_trace_file.user, anon_trace_file)
423 check_trace_show anon_trace_file
426 # Test showing a trace that doesn't exist
427 def test_show_not_found
428 deleted_trace_file = create(:trace, :deleted)
430 # First with a trace that has never existed
431 get show_trace_path(create(:user), 0)
432 assert_redirected_to :action => :index
434 # Now with a trace that has been deleted
435 session_for(deleted_trace_file.user)
436 get show_trace_path(deleted_trace_file.user, deleted_trace_file)
437 assert_redirected_to :action => :index
440 # Test downloading a trace
442 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
444 # First with no auth, which should work since the trace is public
445 get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
448 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
450 # Now with some other user, which should work since the trace is public
451 session_for(create(:user))
452 get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
455 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
457 # And finally we should be able to do it with the owner of the trace
458 session_for(public_trace_file.user)
459 get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
462 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
465 # Test downloading a compressed trace
466 def test_data_compressed
467 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
469 # First get the data as is
470 get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file)
473 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
475 # Now ask explicitly for XML format
476 get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml")
477 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
479 # Now ask explicitly for GPX format
480 get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx")
481 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
484 # Check an anonymous trace can't be downloaded by another user
486 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
489 get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
490 assert_response :not_found
492 # Now with some other user, which shouldn't work since the trace is anon
493 session_for(create(:user))
494 get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
495 assert_response :not_found
497 # And finally we should be able to do it with the owner of the trace
498 session_for(anon_trace_file.user)
499 get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
502 check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
505 # Test downloading a trace that doesn't exist
506 def test_data_not_found
507 deleted_trace_file = create(:trace, :deleted)
509 # First with a trace that has never existed
510 get trace_data_path(:display_name => create(:user).display_name, :id => 0)
511 assert_response :not_found
513 # Now with a trace that has been deleted
514 session_for(deleted_trace_file.user)
515 get trace_data_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
516 assert_response :not_found
519 # Test downloading the picture for a trace
521 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
523 # First with no auth, which should work since the trace is public
524 get trace_picture_path(public_trace_file.user, public_trace_file)
525 check_trace_picture public_trace_file
527 # Now with some other user, which should work since the trace is public
528 session_for(create(:user))
529 get trace_picture_path(public_trace_file.user, public_trace_file)
530 check_trace_picture public_trace_file
532 # And finally we should be able to do it with the owner of the trace
533 session_for(public_trace_file.user)
534 get trace_picture_path(public_trace_file.user, public_trace_file)
535 check_trace_picture public_trace_file
538 # Check the picture for an anonymous trace can't be downloaded by another user
539 def test_picture_anon
540 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
543 get trace_picture_path(anon_trace_file.user, anon_trace_file)
544 assert_response :forbidden
546 # Now with some other user, which shouldn't work since the trace is anon
547 session_for(create(:user))
548 get trace_picture_path(anon_trace_file.user, anon_trace_file)
549 assert_response :forbidden
551 # And finally we should be able to do it with the owner of the trace
552 session_for(anon_trace_file.user)
553 get trace_picture_path(anon_trace_file.user, anon_trace_file)
554 check_trace_picture anon_trace_file
557 # Test downloading the picture for a trace that doesn't exist
558 def test_picture_not_found
559 deleted_trace_file = create(:trace, :deleted)
561 # First with a trace that has never existed
562 get trace_picture_path(create(:user), 0)
563 assert_response :not_found
565 # Now with a trace that has been deleted
566 session_for(deleted_trace_file.user)
567 get trace_picture_path(deleted_trace_file.user, deleted_trace_file)
568 assert_response :not_found
571 # Test downloading the icon for a trace
573 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
575 # First with no auth, which should work since the trace is public
576 get trace_icon_path(public_trace_file.user, public_trace_file)
577 check_trace_icon public_trace_file
579 # Now with some other user, which should work since the trace is public
580 session_for(create(:user))
581 get trace_icon_path(public_trace_file.user, public_trace_file)
582 check_trace_icon public_trace_file
584 # And finally we should be able to do it with the owner of the trace
585 session_for(public_trace_file.user)
586 get trace_icon_path(public_trace_file.user, public_trace_file)
587 check_trace_icon public_trace_file
590 # Check the icon for an anonymous trace can't be downloaded by another user
592 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
595 get trace_icon_path(anon_trace_file.user, anon_trace_file)
596 assert_response :forbidden
598 # Now with some other user, which shouldn't work since the trace is anon
599 session_for(create(:user))
600 get trace_icon_path(anon_trace_file.user, anon_trace_file)
601 assert_response :forbidden
603 # And finally we should be able to do it with the owner of the trace
604 session_for(anon_trace_file.user)
605 get trace_icon_path(anon_trace_file.user, anon_trace_file)
606 check_trace_icon anon_trace_file
609 # Test downloading the icon for a trace that doesn't exist
610 def test_icon_not_found
611 deleted_trace_file = create(:trace, :deleted)
613 # First with a trace that has never existed
614 get trace_icon_path(create(:user), 0)
615 assert_response :not_found
617 # Now with a trace that has been deleted
618 session_for(deleted_trace_file.user)
619 get trace_icon_path(deleted_trace_file.user, deleted_trace_file)
620 assert_response :not_found
623 # Test fetching the new trace page
627 assert_redirected_to login_path(:referer => new_trace_path)
629 # Now authenticated as a user with gps.trace.visibility set
631 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
634 assert_response :success
636 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
638 # Now authenticated as a user with gps.trace.public set
639 second_user = create(:user)
640 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
641 session_for(second_user)
643 assert_response :success
645 assert_select "select#trace_visibility option[value=public][selected]", 1
647 # Now authenticated as a user with no preferences
648 third_user = create(:user)
649 session_for(third_user)
651 assert_response :success
653 assert_select "select#trace_visibility option[value=private][selected]", 1
656 # Test creating a trace
659 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
660 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
664 post traces_path(:trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" })
665 assert_response :forbidden
671 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
672 assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
674 post traces_path, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
675 assert_redirected_to :action => :index, :display_name => user.display_name
676 assert_match(/file has been uploaded/, flash[:notice])
677 trace = Trace.order(:id => :desc).first
678 assert_equal "a.gpx", trace.name
679 assert_equal "New Trace", trace.description
680 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
681 assert_equal "trackable", trace.visibility
682 assert_not trace.inserted
683 assert_equal File.new(fixture).read, trace.file.blob.download
685 assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
688 # Test creating a trace with validation errors
689 def test_create_post_with_validation_errors
691 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
692 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
696 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
697 assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
699 post traces_path, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }
701 assert_match "is too short (minimum is 1 character)", response.body
704 # Test fetching the edit page for a trace using GET
706 public_trace_file = create(:trace, :visibility => "public")
707 deleted_trace_file = create(:trace, :deleted)
710 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
711 assert_redirected_to login_path(:referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id))
713 # Now with some other user, which should fail
714 session_for(create(:user))
715 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
716 assert_response :forbidden
718 # Now with a trace which doesn't exist
719 session_for(create(:user))
720 get edit_trace_path(:display_name => create(:user).display_name, :id => 0)
721 assert_response :not_found
723 # Now with a trace which has been deleted
724 session_for(deleted_trace_file.user)
725 get edit_trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
726 assert_response :not_found
728 # Finally with a trace that we are allowed to edit
729 session_for(public_trace_file.user)
730 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
731 assert_response :success
734 # Test saving edits to a trace
736 public_trace_file = create(:trace, :visibility => "public")
737 deleted_trace_file = create(:trace, :deleted)
740 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
743 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
744 assert_response :forbidden
746 # Now with some other user, which should fail
747 session_for(create(:user))
748 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
749 assert_response :forbidden
751 # Now with a trace which doesn't exist
752 session_for(create(:user))
753 put trace_path(:display_name => create(:user).display_name, :id => 0, :trace => new_details)
754 assert_response :not_found
756 # Now with a trace which has been deleted
757 session_for(deleted_trace_file.user)
758 put trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file, :trace => new_details)
759 assert_response :not_found
761 # Finally with a trace that we are allowed to edit
762 session_for(public_trace_file.user)
763 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
764 assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
765 trace = Trace.find(public_trace_file.id)
766 assert_equal new_details[:description], trace.description
767 assert_equal new_details[:tagstring], trace.tagstring
768 assert_equal new_details[:visibility], trace.visibility
771 # Test destroying a trace
773 public_trace_file = create(:trace, :visibility => "public")
774 deleted_trace_file = create(:trace, :deleted)
777 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
778 assert_response :forbidden
780 # Now with some other user, which should fail
781 session_for(create(:user))
782 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
783 assert_response :forbidden
785 # Now with a trace which doesn't exist
786 session_for(create(:user))
787 delete trace_path(:display_name => create(:user).display_name, :id => 0)
788 assert_response :not_found
790 # Now with a trace has already been deleted
791 session_for(deleted_trace_file.user)
792 delete trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
793 assert_response :not_found
795 # Now with a trace that we are allowed to delete
796 session_for(public_trace_file.user)
797 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
798 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
799 trace = Trace.find(public_trace_file.id)
800 assert_not trace.visible
802 # Finally with a trace that is destroyed by an admin
803 public_trace_file = create(:trace, :visibility => "public")
804 admin = create(:administrator_user)
806 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
807 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
808 trace = Trace.find(public_trace_file.id)
809 assert_not trace.visible
814 def check_trace_feed(traces)
815 assert_response :success
816 assert_template "georss"
817 assert_equal "application/rss+xml", @response.media_type
818 assert_select "rss", :count => 1 do
819 assert_select "channel", :count => 1 do
820 assert_select "title"
821 assert_select "description"
823 assert_select "image"
824 assert_select "item", :count => traces.length do |items|
825 traces.zip(items).each do |trace, item|
826 assert_select item, "title", trace.name
827 assert_select item, "link", "http://www.example.com/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
828 assert_select item, "guid", "http://www.example.com/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
829 assert_select item, "description" do
830 assert_dom_encoded do
831 assert_select "img[src='#{trace_icon_url trace.user, trace}']"
834 # assert_select item, "dc:creator", trace.user.display_name
835 assert_select item, "pubDate", trace.timestamp.rfc822
842 def check_trace_index(traces)
843 assert_response :success
844 assert_template "index"
847 assert_select "h2", /Nothing here yet/
849 assert_select "table#trace_list tbody", :count => 1 do
850 assert_select "tr", :count => traces.length do |rows|
851 traces.zip(rows).each do |trace, row|
852 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
853 assert_select row, "li", Regexp.new(Regexp.escape("#{trace.size} points")) if trace.inserted?
854 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
855 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
856 assert_select row, "a[href='#{user_path trace.user}']", :text => trace.user.display_name
863 def check_trace_show(trace)
864 assert_response :success
865 assert_template "show"
867 assert_select "table", :count => 1 do
868 assert_select "td", /^#{Regexp.quote(trace.name)} /
869 assert_select "td a[href='#{user_path trace.user}']", :text => trace.user.display_name
870 assert_select "td", trace.description
874 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
875 assert_equal digest, Digest::MD5.hexdigest(response.body)
876 assert_equal content_type, response.media_type
877 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
880 def check_trace_picture(trace)
883 assert_response :success
884 assert_equal "image/gif", response.media_type
885 assert_equal trace.large_picture, response.body
888 def check_trace_icon(trace)
891 assert_response :success
892 assert_equal "image/gif", response.media_type
893 assert_equal trace.icon_picture, response.body