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 second_user = create(:user)
176 third_user = create(:user)
178 trace_b = create(:trace, :visibility => "public", :user => user)
179 trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
180 create(:tracetag, :trace => trace, :tag => "London")
183 # Test a user with no traces
184 get traces_path(:display_name => second_user.display_name)
187 # Test the user with the traces - should see only public ones
188 get traces_path(:display_name => user.display_name)
189 check_trace_index [trace_b]
191 session_for(third_user)
193 # Should still see only public ones when authenticated as another user
194 get traces_path(:display_name => user.display_name)
195 check_trace_index [trace_b]
199 # Should see all traces when authenticated as the target user
200 get traces_path(:display_name => user.display_name)
201 check_trace_index [trace_c, trace_b]
203 # Should only see traces with the correct tag when a tag is specified
204 get traces_path(:display_name => user.display_name, :tag => "London")
205 check_trace_index [trace_c]
207 # Should get an error if the user does not exist
208 get traces_path(:display_name => "UnknownUser")
209 assert_response :not_found
210 assert_template "users/no_such_user"
213 # Check a multi-page index
215 # Create several pages worth of traces
216 create_list(:trace, 50)
218 # Try and get the index
220 assert_response :success
221 assert_select "table#trace_list tbody", :count => 1 do
222 assert_select "tr", :count => 20
224 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
225 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
227 # Try and get the second page
228 get css_select("li.page-item a.page-link").last["href"]
229 assert_response :success
230 assert_select "table#trace_list tbody", :count => 1 do
231 assert_select "tr", :count => 20
233 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
234 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
236 # Try and get the third page
237 get css_select("li.page-item a.page-link").last["href"]
238 assert_response :success
239 assert_select "table#trace_list tbody", :count => 1 do
240 assert_select "tr", :count => 10
242 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
243 assert_select "li.page-item.disabled span.page-link", :text => "Older Traces", :count => 2
245 # Go back to the second page
246 get css_select("li.page-item a.page-link").first["href"]
247 assert_response :success
248 assert_select "table#trace_list tbody", :count => 1 do
249 assert_select "tr", :count => 20
251 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
252 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
254 # Go back to the first page
255 get css_select("li.page-item a.page-link").first["href"]
256 assert_response :success
257 assert_select "table#trace_list tbody", :count => 1 do
258 assert_select "tr", :count => 20
260 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
261 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
264 # Check a multi-page index of tagged traces
265 def test_index_tagged_paged
266 # Create several pages worth of traces
267 create_list(:trace, 100) do |trace, index|
268 create(:tracetag, :trace => trace, :tag => "London") if index.even?
271 # Try and get the index
272 get traces_path(:tag => "London")
273 assert_response :success
274 assert_select "table#trace_list tbody", :count => 1 do
275 assert_select "tr", :count => 20
277 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
278 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
280 # Try and get the second page
281 get css_select("li.page-item a.page-link").last["href"]
282 assert_response :success
283 assert_select "table#trace_list tbody", :count => 1 do
284 assert_select "tr", :count => 20
286 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
287 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
289 # Try and get the third page
290 get css_select("li.page-item a.page-link").last["href"]
291 assert_response :success
292 assert_select "table#trace_list tbody", :count => 1 do
293 assert_select "tr", :count => 10
295 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
296 assert_select "li.page-item.disabled span.page-link", :text => "Older Traces", :count => 2
298 # Go back to the second page
299 get css_select("li.page-item a.page-link").first["href"]
300 assert_response :success
301 assert_select "table#trace_list tbody", :count => 1 do
302 assert_select "tr", :count => 20
304 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
305 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
307 # Go back to the first page
308 get css_select("li.page-item a.page-link").first["href"]
309 assert_response :success
310 assert_select "table#trace_list tbody", :count => 1 do
311 assert_select "tr", :count => 20
313 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
314 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
320 # The fourth test below is surprisingly sensitive to timestamp ordering when the timestamps are equal.
321 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
322 create(:tracetag, :trace => trace, :tag => "London")
324 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
325 create(:tracetag, :trace => trace, :tag => "Birmingham")
327 create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
328 create(:tracetag, :trace => trace, :tag => "London")
330 create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
331 create(:tracetag, :trace => trace, :tag => "Birmingham")
334 # First with the public feed
336 check_trace_feed [trace_b, trace_a]
338 # Restrict traces to those with a given tag
339 get traces_rss_path(:tag => "London")
340 check_trace_feed [trace_a]
343 # Check the RSS feed for a specific user
346 second_user = create(:user)
349 trace_b = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago, :user => user)
350 trace_c = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago, :user => user) do |trace|
351 create(:tracetag, :trace => trace, :tag => "London")
353 create(:trace, :visibility => "private")
355 # Test a user with no traces
356 get traces_rss_path(:display_name => second_user.display_name)
359 # Test the user with the traces - should see only public ones
360 get traces_rss_path(:display_name => user.display_name)
361 check_trace_feed [trace_c, trace_b]
363 # Should only see traces with the correct tag when a tag is specified
364 get traces_rss_path(:display_name => user.display_name, :tag => "London")
365 check_trace_feed [trace_c]
367 # Should no traces if the user does not exist
368 get traces_rss_path(:display_name => "UnknownUser")
372 # Test showing a trace
374 public_trace_file = create(:trace, :visibility => "public")
376 # First with no auth, which should work since the trace is public
377 get show_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
378 check_trace_show public_trace_file
380 # Now with some other user, which should work since the trace is public
381 session_for(create(:user))
382 get show_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
383 check_trace_show public_trace_file
385 # And finally we should be able to do it with the owner of the trace
386 session_for(public_trace_file.user)
387 get show_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
388 check_trace_show public_trace_file
391 # Check an anonymous trace can't be viewed by another user
393 anon_trace_file = create(:trace, :visibility => "private")
396 get show_trace_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
397 assert_response :redirect
398 assert_redirected_to :action => :index
400 # Now with some other user, which should not work since the trace is anon
401 session_for(create(:user))
402 get show_trace_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
403 assert_response :redirect
404 assert_redirected_to :action => :index
406 # And finally we should be able to do it with the owner of the trace
407 session_for(anon_trace_file.user)
408 get show_trace_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
409 check_trace_show anon_trace_file
412 # Test showing a trace that doesn't exist
413 def test_show_not_found
414 deleted_trace_file = create(:trace, :deleted)
416 # First with a trace that has never existed
417 get show_trace_path(:display_name => create(:user).display_name, :id => 0)
418 assert_response :redirect
419 assert_redirected_to :action => :index
421 # Now with a trace that has been deleted
422 session_for(deleted_trace_file.user)
423 get show_trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
424 assert_response :redirect
425 assert_redirected_to :action => :index
428 # Test downloading a trace
430 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
432 # First with no auth, which should work since the trace is public
433 get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
436 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
438 # Now with some other user, which should work since the trace is public
439 session_for(create(:user))
440 get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
443 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
445 # And finally we should be able to do it with the owner of the trace
446 session_for(public_trace_file.user)
447 get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
450 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
453 # Test downloading a compressed trace
454 def test_data_compressed
455 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
457 # First get the data as is
458 get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file)
461 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
463 # Now ask explicitly for XML format
464 get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml")
465 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
467 # Now ask explicitly for GPX format
468 get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx")
469 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
472 # Check an anonymous trace can't be downloaded by another user
474 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
477 get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
478 assert_response :not_found
480 # Now with some other user, which shouldn't work since the trace is anon
481 session_for(create(:user))
482 get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
483 assert_response :not_found
485 # And finally we should be able to do it with the owner of the trace
486 session_for(anon_trace_file.user)
487 get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
490 check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
493 # Test downloading a trace that doesn't exist
494 def test_data_not_found
495 deleted_trace_file = create(:trace, :deleted)
497 # First with a trace that has never existed
498 get trace_data_path(:display_name => create(:user).display_name, :id => 0)
499 assert_response :not_found
501 # Now with a trace that has been deleted
502 session_for(deleted_trace_file.user)
503 get trace_data_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
504 assert_response :not_found
507 # Test downloading the picture for a trace
509 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
511 # First with no auth, which should work since the trace is public
512 get trace_picture_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
513 check_trace_picture public_trace_file
515 # Now with some other user, which should work since the trace is public
516 session_for(create(:user))
517 get trace_picture_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
518 check_trace_picture public_trace_file
520 # And finally we should be able to do it with the owner of the trace
521 session_for(public_trace_file.user)
522 get trace_picture_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
523 check_trace_picture public_trace_file
526 # Check the picture for an anonymous trace can't be downloaded by another user
527 def test_picture_anon
528 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
531 get trace_picture_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
532 assert_response :forbidden
534 # Now with some other user, which shouldn't work since the trace is anon
535 session_for(create(:user))
536 get trace_picture_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
537 assert_response :forbidden
539 # And finally we should be able to do it with the owner of the trace
540 session_for(anon_trace_file.user)
541 get trace_picture_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
542 check_trace_picture anon_trace_file
545 # Test downloading the picture for a trace that doesn't exist
546 def test_picture_not_found
547 deleted_trace_file = create(:trace, :deleted)
549 # First with a trace that has never existed
550 get trace_picture_path(:display_name => create(:user).display_name, :id => 0)
551 assert_response :not_found
553 # Now with a trace that has been deleted
554 session_for(deleted_trace_file.user)
555 get trace_picture_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
556 assert_response :not_found
559 # Test downloading the icon for a trace
561 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
563 # First with no auth, which should work since the trace is public
564 get trace_icon_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
565 check_trace_icon public_trace_file
567 # Now with some other user, which should work since the trace is public
568 session_for(create(:user))
569 get trace_icon_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
570 check_trace_icon public_trace_file
572 # And finally we should be able to do it with the owner of the trace
573 session_for(public_trace_file.user)
574 get trace_icon_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
575 check_trace_icon public_trace_file
578 # Check the icon for an anonymous trace can't be downloaded by another user
580 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
583 get trace_icon_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
584 assert_response :forbidden
586 # Now with some other user, which shouldn't work since the trace is anon
587 session_for(create(:user))
588 get trace_icon_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
589 assert_response :forbidden
591 # And finally we should be able to do it with the owner of the trace
592 session_for(anon_trace_file.user)
593 get trace_icon_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
594 check_trace_icon anon_trace_file
597 # Test downloading the icon for a trace that doesn't exist
598 def test_icon_not_found
599 deleted_trace_file = create(:trace, :deleted)
601 # First with a trace that has never existed
602 get trace_icon_path(:display_name => create(:user).display_name, :id => 0)
603 assert_response :not_found
605 # Now with a trace that has been deleted
606 session_for(deleted_trace_file.user)
607 get trace_icon_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
608 assert_response :not_found
611 # Test fetching the new trace page
615 assert_response :redirect
616 assert_redirected_to login_path(:referer => new_trace_path)
618 # Now authenticated as a user with gps.trace.visibility set
620 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
623 assert_response :success
625 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
627 # Now authenticated as a user with gps.trace.public set
628 second_user = create(:user)
629 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
630 session_for(second_user)
632 assert_response :success
634 assert_select "select#trace_visibility option[value=public][selected]", 1
636 # Now authenticated as a user with no preferences
637 third_user = create(:user)
638 session_for(third_user)
640 assert_response :success
642 assert_select "select#trace_visibility option[value=private][selected]", 1
645 # Test creating a trace
648 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
649 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
653 post traces_path(:trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" })
654 assert_response :forbidden
660 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
661 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
663 post traces_path, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
664 assert_response :redirect
665 assert_redirected_to :action => :index, :display_name => user.display_name
666 assert_match(/file has been uploaded/, flash[:notice])
667 trace = Trace.order(:id => :desc).first
668 assert_equal "a.gpx", trace.name
669 assert_equal "New Trace", trace.description
670 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
671 assert_equal "trackable", trace.visibility
672 assert_not trace.inserted
673 assert_equal File.new(fixture).read, trace.file.blob.download
675 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
678 # Test creating a trace with validation errors
679 def test_create_post_with_validation_errors
681 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
682 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
686 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
687 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
689 post traces_path, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }
691 assert_match "is too short (minimum is 1 character)", response.body
694 # Test fetching the edit page for a trace using GET
696 public_trace_file = create(:trace, :visibility => "public")
697 deleted_trace_file = create(:trace, :deleted)
700 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
701 assert_response :redirect
702 assert_redirected_to login_path(:referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id))
704 # Now with some other user, which should fail
705 session_for(create(:user))
706 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
707 assert_response :forbidden
709 # Now with a trace which doesn't exist
710 session_for(create(:user))
711 get edit_trace_path(:display_name => create(:user).display_name, :id => 0)
712 assert_response :not_found
714 # Now with a trace which has been deleted
715 session_for(deleted_trace_file.user)
716 get edit_trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
717 assert_response :not_found
719 # Finally with a trace that we are allowed to edit
720 session_for(public_trace_file.user)
721 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
722 assert_response :success
725 # Test saving edits to a trace
727 public_trace_file = create(:trace, :visibility => "public")
728 deleted_trace_file = create(:trace, :deleted)
731 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
734 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
735 assert_response :forbidden
737 # Now with some other user, which should fail
738 session_for(create(:user))
739 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
740 assert_response :forbidden
742 # Now with a trace which doesn't exist
743 session_for(create(:user))
744 put trace_path(:display_name => create(:user).display_name, :id => 0, :trace => new_details)
745 assert_response :not_found
747 # Now with a trace which has been deleted
748 session_for(deleted_trace_file.user)
749 put trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file, :trace => new_details)
750 assert_response :not_found
752 # Finally with a trace that we are allowed to edit
753 session_for(public_trace_file.user)
754 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
755 assert_response :redirect
756 assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
757 trace = Trace.find(public_trace_file.id)
758 assert_equal new_details[:description], trace.description
759 assert_equal new_details[:tagstring], trace.tagstring
760 assert_equal new_details[:visibility], trace.visibility
763 # Test destroying a trace
765 public_trace_file = create(:trace, :visibility => "public")
766 deleted_trace_file = create(:trace, :deleted)
769 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
770 assert_response :forbidden
772 # Now with some other user, which should fail
773 session_for(create(:user))
774 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
775 assert_response :forbidden
777 # Now with a trace which doesn't exist
778 session_for(create(:user))
779 delete trace_path(:display_name => create(:user).display_name, :id => 0)
780 assert_response :not_found
782 # Now with a trace has already been deleted
783 session_for(deleted_trace_file.user)
784 delete trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
785 assert_response :not_found
787 # Now with a trace that we are allowed to delete
788 session_for(public_trace_file.user)
789 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
790 assert_response :redirect
791 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
792 trace = Trace.find(public_trace_file.id)
793 assert_not trace.visible
795 # Finally with a trace that is destroyed by an admin
796 public_trace_file = create(:trace, :visibility => "public")
797 admin = create(:administrator_user)
799 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
800 assert_response :redirect
801 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
802 trace = Trace.find(public_trace_file.id)
803 assert_not trace.visible
808 def check_trace_feed(traces)
809 assert_response :success
810 assert_template "georss"
811 assert_equal "application/rss+xml", @response.media_type
812 assert_select "rss", :count => 1 do
813 assert_select "channel", :count => 1 do
814 assert_select "title"
815 assert_select "description"
817 assert_select "image"
818 assert_select "item", :count => traces.length do |items|
819 traces.zip(items).each do |trace, item|
820 assert_select item, "title", trace.name
821 assert_select item, "link", "http://www.example.com/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
822 assert_select item, "guid", "http://www.example.com/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
823 assert_select item, "description"
824 # assert_select item, "dc:creator", trace.user.display_name
825 assert_select item, "pubDate", trace.timestamp.rfc822
832 def check_trace_index(traces)
833 assert_response :success
834 assert_template "index"
837 assert_select "h2", /Nothing here yet/
839 assert_select "table#trace_list tbody", :count => 1 do
840 assert_select "tr", :count => traces.length do |rows|
841 traces.zip(rows).each do |trace, row|
842 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
843 assert_select row, "li", Regexp.new(Regexp.escape("#{trace.size} points")) if trace.inserted?
844 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
845 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
852 def check_trace_show(trace)
853 assert_response :success
854 assert_template "show"
856 assert_select "table", :count => 1 do
857 assert_select "td", /^#{Regexp.quote(trace.name)} /
858 assert_select "td", trace.user.display_name
859 assert_select "td", trace.description
863 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
864 assert_equal digest, Digest::MD5.hexdigest(response.body)
865 assert_equal content_type, response.media_type
866 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
869 def check_trace_picture(trace)
872 assert_response :success
873 assert_equal "image/gif", response.media_type
874 assert_equal trace.large_picture, response.body
877 def check_trace_icon(trace)
880 assert_response :success
881 assert_equal "image/gif", response.media_type
882 assert_equal trace.icon_picture, response.body