]> git.openstreetmap.org Git - rails.git/blob - test/controllers/traces_controller_test.rb
Use a custom cop to check controller action names
[rails.git] / test / controllers / traces_controller_test.rb
1 require "test_helper"
2
3 class TracesControllerTest < ActionDispatch::IntegrationTest
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/traces", :method => :get },
9       { :controller => "traces", :action => "index" }
10     )
11     assert_routing(
12       { :path => "/traces/tag/tagname", :method => :get },
13       { :controller => "traces", :action => "index", :tag => "tagname" }
14     )
15     assert_routing(
16       { :path => "/user/username/traces", :method => :get },
17       { :controller => "traces", :action => "index", :display_name => "username" }
18     )
19     assert_routing(
20       { :path => "/user/username/traces/tag/tagname", :method => :get },
21       { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
22     )
23
24     assert_routing(
25       { :path => "/traces/mine", :method => :get },
26       { :controller => "traces", :action => "mine" }
27     )
28     assert_routing(
29       { :path => "/traces/mine/tag/tagname", :method => :get },
30       { :controller => "traces", :action => "mine", :tag => "tagname" }
31     )
32
33     assert_routing(
34       { :path => "/user/username/traces/1", :method => :get },
35       { :controller => "traces", :action => "show", :display_name => "username", :id => "1" }
36     )
37
38     assert_routing(
39       { :path => "/traces/new", :method => :get },
40       { :controller => "traces", :action => "new" }
41     )
42     assert_routing(
43       { :path => "/traces", :method => :post },
44       { :controller => "traces", :action => "create" }
45     )
46     assert_routing(
47       { :path => "/trace/1/data", :method => :get },
48       { :controller => "traces", :action => "data", :id => "1" }
49     )
50     assert_routing(
51       { :path => "/trace/1/data.xml", :method => :get },
52       { :controller => "traces", :action => "data", :id => "1", :format => "xml" }
53     )
54     assert_routing(
55       { :path => "/traces/1/edit", :method => :get },
56       { :controller => "traces", :action => "edit", :id => "1" }
57     )
58     assert_routing(
59       { :path => "/traces/1", :method => :put },
60       { :controller => "traces", :action => "update", :id => "1" }
61     )
62     assert_routing(
63       { :path => "/traces/1", :method => :delete },
64       { :controller => "traces", :action => "destroy", :id => "1" }
65     )
66
67     get "/traces/page/1"
68     assert_redirected_to "/traces"
69
70     get "/traces/tag/tagname/page/1"
71     assert_redirected_to "/traces/tag/tagname"
72
73     get "/user/username/traces/page/1"
74     assert_redirected_to "/user/username/traces"
75
76     get "/user/username/traces/tag/tagname/page/1"
77     assert_redirected_to "/user/username/traces/tag/tagname"
78
79     get "/traces/mine/page/1"
80     assert_redirected_to "/traces/mine"
81
82     get "/traces/mine/tag/tagname/page/1"
83     assert_redirected_to "/traces/mine/tag/tagname"
84   end
85
86   # Check that the index of traces is displayed
87   def test_index
88     user = create(:user)
89     # The fourth test below is surprisingly sensitive to timestamp ordering when the timestamps are equal.
90     trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
91       create(:tracetag, :trace => trace, :tag => "London")
92     end
93     trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
94       create(:tracetag, :trace => trace, :tag => "Birmingham")
95     end
96     trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
97       create(:tracetag, :trace => trace, :tag => "London")
98     end
99     trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
100       create(:tracetag, :trace => trace, :tag => "Birmingham")
101     end
102
103     # First with the public index
104     get traces_path
105     check_trace_index [trace_b, trace_a]
106
107     # Restrict traces to those with a given tag
108     get traces_path(:tag => "London")
109     check_trace_index [trace_a]
110
111     session_for(user)
112
113     # Should see more when we are logged in
114     get traces_path
115     check_trace_index [trace_d, trace_c, trace_b, trace_a]
116
117     # Again, we should see more when we are logged in
118     get traces_path(:tag => "London")
119     check_trace_index [trace_c, trace_a]
120   end
121
122   # Check that I can get mine
123   def test_index_mine
124     user = create(:user)
125     create(:trace, :visibility => "public") do |trace|
126       create(:tracetag, :trace => trace, :tag => "Birmingham")
127     end
128     trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
129       create(:tracetag, :trace => trace, :tag => "London")
130     end
131
132     # First try to get it when not logged in
133     get traces_mine_path
134     assert_redirected_to login_path(:referer => "/traces/mine")
135
136     session_for(user)
137
138     # Now try when logged in
139     get traces_mine_path
140     assert_redirected_to :action => "index", :display_name => user.display_name
141
142     # Fetch the actual index
143     get traces_path(:display_name => user.display_name)
144     check_trace_index [trace_b]
145   end
146
147   # Check the index of traces for a specific user
148   def test_index_user
149     user = create(:user)
150     checked_user_traces_path = url_for :only_path => true, :controller => "traces", :action => "index", :display_name => user.display_name
151     second_user = create(:user)
152     third_user = create(:user)
153     create(:trace)
154     trace_b = create(:trace, :visibility => "public", :user => user)
155     trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
156       create(:tracetag, :trace => trace, :tag => "London")
157     end
158
159     # Test a user with no traces
160     get traces_path(:display_name => second_user.display_name)
161     check_trace_index []
162
163     # Test the user with the traces - should see only public ones
164     get traces_path(:display_name => user.display_name)
165     check_trace_index [trace_b]
166     assert_dom ".nav-tabs" do
167       assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
168       assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 0
169       assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 1
170     end
171
172     session_for(third_user)
173
174     # Should still see only public ones when authenticated as another user
175     get traces_path(:display_name => user.display_name)
176     check_trace_index [trace_b]
177     assert_dom ".nav-tabs" do
178       assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
179       assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 1
180       assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 1
181     end
182
183     session_for(user)
184
185     # Should see all traces when authenticated as the target user
186     get traces_path(:display_name => user.display_name)
187     check_trace_index [trace_c, trace_b]
188     assert_dom ".nav-tabs" do
189       assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
190       assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 1
191       assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 0
192     end
193
194     # Should only see traces with the correct tag when a tag is specified
195     get traces_path(:display_name => user.display_name, :tag => "London")
196     check_trace_index [trace_c]
197
198     # Should get an error if the user does not exist
199     get traces_path(:display_name => "UnknownUser")
200     assert_response :not_found
201     assert_template "users/no_such_user"
202   end
203
204   # Check a multi-page index
205   def test_index_paged
206     # Create several pages worth of traces
207     create_list(:trace, 50)
208
209     # Try and get the index
210     get traces_path
211     assert_response :success
212     assert_select "table#trace_list tbody", :count => 1 do
213       assert_select "tr", :count => 20
214     end
215     assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
216     assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
217
218     # Try and get the second page
219     get css_select("li.page-item a.page-link").last["href"]
220     assert_response :success
221     assert_select "table#trace_list tbody", :count => 1 do
222       assert_select "tr", :count => 20
223     end
224     assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
225     assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
226
227     # Try and get the third 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 => 10
232     end
233     assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
234     assert_select "li.page-item.disabled span.page-link", :text => "Older Traces", :count => 2
235
236     # Go back to the second page
237     get css_select("li.page-item a.page-link").first["href"]
238     assert_response :success
239     assert_select "table#trace_list tbody", :count => 1 do
240       assert_select "tr", :count => 20
241     end
242     assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
243     assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
244
245     # Go back to the first 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
250     end
251     assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
252     assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
253   end
254
255   # Check a multi-page index of tagged traces
256   def test_index_tagged_paged
257     # Create several pages worth of traces
258     create_list(:trace, 100) do |trace, index|
259       create(:tracetag, :trace => trace, :tag => "London") if index.even?
260     end
261
262     # Try and get the index
263     get traces_path(:tag => "London")
264     assert_response :success
265     assert_select "table#trace_list tbody", :count => 1 do
266       assert_select "tr", :count => 20
267     end
268     assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
269     assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
270
271     # Try and get the second page
272     get css_select("li.page-item a.page-link").last["href"]
273     assert_response :success
274     assert_select "table#trace_list tbody", :count => 1 do
275       assert_select "tr", :count => 20
276     end
277     assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
278     assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
279
280     # Try and get the third 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 => 10
285     end
286     assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
287     assert_select "li.page-item.disabled span.page-link", :text => "Older Traces", :count => 2
288
289     # Go back to the second page
290     get css_select("li.page-item a.page-link").first["href"]
291     assert_response :success
292     assert_select "table#trace_list tbody", :count => 1 do
293       assert_select "tr", :count => 20
294     end
295     assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
296     assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
297
298     # Go back to the first 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
303     end
304     assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
305     assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
306   end
307
308   def test_index_invalid_paged
309     # Try some invalid paged accesses
310     %w[-1 0 fred].each do |id|
311       get traces_path(:before => id)
312       assert_redirected_to :controller => :errors, :action => :bad_request
313
314       get traces_path(:after => id)
315       assert_redirected_to :controller => :errors, :action => :bad_request
316     end
317   end
318
319   # Test showing a trace
320   def test_show
321     public_trace_file = create(:trace, :visibility => "public")
322
323     # First with no auth, which should work since the trace is public
324     get show_trace_path(public_trace_file.user, public_trace_file)
325     check_trace_show public_trace_file
326
327     # Now with some other user, which should work since the trace is public
328     session_for(create(:user))
329     get show_trace_path(public_trace_file.user, public_trace_file)
330     check_trace_show public_trace_file
331
332     # And finally we should be able to do it with the owner of the trace
333     session_for(public_trace_file.user)
334     get show_trace_path(public_trace_file.user, public_trace_file)
335     check_trace_show public_trace_file
336   end
337
338   # Check an anonymous trace can't be viewed by another user
339   def test_show_anon
340     anon_trace_file = create(:trace, :visibility => "private")
341
342     # First with no auth
343     get show_trace_path(anon_trace_file.user, anon_trace_file)
344     assert_redirected_to :action => :index
345
346     # Now with some other user, which should not work since the trace is anon
347     session_for(create(:user))
348     get show_trace_path(anon_trace_file.user, anon_trace_file)
349     assert_redirected_to :action => :index
350
351     # And finally we should be able to do it with the owner of the trace
352     session_for(anon_trace_file.user)
353     get show_trace_path(anon_trace_file.user, anon_trace_file)
354     check_trace_show anon_trace_file
355   end
356
357   # Test showing a trace that doesn't exist
358   def test_show_not_found
359     deleted_trace_file = create(:trace, :deleted)
360
361     # First with a trace that has never existed
362     get show_trace_path(create(:user), 0)
363     assert_redirected_to :action => :index
364
365     # Now with a trace that has been deleted
366     session_for(deleted_trace_file.user)
367     get show_trace_path(deleted_trace_file.user, deleted_trace_file)
368     assert_redirected_to :action => :index
369   end
370
371   # Test downloading a trace
372   def test_data
373     public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
374
375     # First with no auth, which should work since the trace is public
376     get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
377     follow_redirect!
378     follow_redirect!
379     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
380
381     # Now with some other user, which should work since the trace is public
382     session_for(create(:user))
383     get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
384     follow_redirect!
385     follow_redirect!
386     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
387
388     # And finally we should be able to do it with the owner of the trace
389     session_for(public_trace_file.user)
390     get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
391     follow_redirect!
392     follow_redirect!
393     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
394   end
395
396   # Test downloading a compressed trace
397   def test_data_compressed
398     identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
399
400     # First get the data as is
401     get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file)
402     follow_redirect!
403     follow_redirect!
404     check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
405
406     # Now ask explicitly for XML format
407     get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml")
408     check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
409
410     # Now ask explicitly for GPX format
411     get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx")
412     check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
413   end
414
415   # Check an anonymous trace can't be downloaded by another user
416   def test_data_anon
417     anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
418
419     # First with no auth
420     get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
421     assert_response :not_found
422
423     # Now with some other user, which shouldn't work since the trace is anon
424     session_for(create(:user))
425     get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
426     assert_response :not_found
427
428     # And finally we should be able to do it with the owner of the trace
429     session_for(anon_trace_file.user)
430     get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
431     follow_redirect!
432     follow_redirect!
433     check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
434   end
435
436   # Test downloading a trace that doesn't exist
437   def test_data_not_found
438     deleted_trace_file = create(:trace, :deleted)
439
440     # First with a trace that has never existed
441     get trace_data_path(:display_name => create(:user).display_name, :id => 0)
442     assert_response :not_found
443
444     # Now with a trace that has been deleted
445     session_for(deleted_trace_file.user)
446     get trace_data_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
447     assert_response :not_found
448   end
449
450   # Test fetching the new trace page
451   def test_new_get
452     # First with no auth
453     get new_trace_path
454     assert_redirected_to login_path(:referer => new_trace_path)
455
456     # Now authenticated as a user with gps.trace.visibility set
457     user = create(:user)
458     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
459     session_for(user)
460     get new_trace_path
461     assert_response :success
462     assert_template :new
463     assert_select "select#trace_visibility option[value=identifiable][selected]", 1
464
465     # Now authenticated as a user with gps.trace.public set
466     second_user = create(:user)
467     create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
468     session_for(second_user)
469     get new_trace_path
470     assert_response :success
471     assert_template :new
472     assert_select "select#trace_visibility option[value=public][selected]", 1
473
474     # Now authenticated as a user with no preferences
475     third_user = create(:user)
476     session_for(third_user)
477     get new_trace_path
478     assert_response :success
479     assert_template :new
480     assert_select "select#trace_visibility option[value=private][selected]", 1
481   end
482
483   # Test creating a trace
484   def test_create_post
485     # Get file to use
486     fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
487     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
488     user = create(:user)
489
490     # First with no auth
491     post traces_path(:trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" })
492     assert_response :forbidden
493
494     # Rewind the file
495     file.rewind
496
497     # Now authenticated
498     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
499     assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
500     session_for(user)
501     post traces_path, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
502     assert_redirected_to :action => :index, :display_name => user.display_name
503     assert_match(/file has been uploaded/, flash[:notice])
504     trace = Trace.order(:id => :desc).first
505     assert_equal "a.gpx", trace.name
506     assert_equal "New Trace", trace.description
507     assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
508     assert_equal "trackable", trace.visibility
509     assert_not trace.inserted
510     assert_equal File.new(fixture).read, trace.file.blob.download
511     trace.destroy
512     assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
513   end
514
515   # Test creating a trace with validation errors
516   def test_create_post_with_validation_errors
517     # Get file to use
518     fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
519     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
520     user = create(:user)
521
522     # Now authenticated
523     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
524     assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
525     session_for(user)
526     post traces_path, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }
527     assert_template :new
528     assert_match "is too short (minimum is 1 character)", response.body
529   end
530
531   # Test fetching the edit page for a trace using GET
532   def test_edit_get
533     public_trace_file = create(:trace, :visibility => "public")
534     deleted_trace_file = create(:trace, :deleted)
535
536     # First with no auth
537     get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
538     assert_redirected_to login_path(:referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id))
539
540     # Now with some other user, which should fail
541     session_for(create(:user))
542     get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
543     assert_response :forbidden
544
545     # Now with a trace which doesn't exist
546     session_for(create(:user))
547     get edit_trace_path(:display_name => create(:user).display_name, :id => 0)
548     assert_response :not_found
549
550     # Now with a trace which has been deleted
551     session_for(deleted_trace_file.user)
552     get edit_trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
553     assert_response :not_found
554
555     # Finally with a trace that we are allowed to edit
556     session_for(public_trace_file.user)
557     get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
558     assert_response :success
559   end
560
561   # Test saving edits to a trace
562   def test_update
563     public_trace_file = create(:trace, :visibility => "public")
564     deleted_trace_file = create(:trace, :deleted)
565
566     # New details
567     new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
568
569     # First with no auth
570     put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
571     assert_response :forbidden
572
573     # Now with some other user, which should fail
574     session_for(create(:user))
575     put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
576     assert_response :forbidden
577
578     # Now with a trace which doesn't exist
579     session_for(create(:user))
580     put trace_path(:display_name => create(:user).display_name, :id => 0, :trace => new_details)
581     assert_response :not_found
582
583     # Now with a trace which has been deleted
584     session_for(deleted_trace_file.user)
585     put trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file, :trace => new_details)
586     assert_response :not_found
587
588     # Finally with a trace that we are allowed to edit
589     session_for(public_trace_file.user)
590     put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
591     assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
592     trace = Trace.find(public_trace_file.id)
593     assert_equal new_details[:description], trace.description
594     assert_equal new_details[:tagstring], trace.tagstring
595     assert_equal new_details[:visibility], trace.visibility
596   end
597
598   # Test invalid updates
599   def test_update_invalid
600     trace = create(:trace)
601
602     # Invalid visibility
603     session_for(trace.user)
604     put trace_path(trace, :trace => { :description => "Changed description", :tagstring => "new_tag", :visibility => "wrong" })
605     assert_response :success
606     assert_select "title", :text => /^Editing Trace/
607   end
608
609   # Test destroying a trace
610   def test_destroy
611     public_trace_file = create(:trace, :visibility => "public")
612     deleted_trace_file = create(:trace, :deleted)
613
614     # First with no auth
615     delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
616     assert_response :forbidden
617
618     # Now with some other user, which should fail
619     session_for(create(:user))
620     delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
621     assert_response :forbidden
622
623     # Now with a trace which doesn't exist
624     session_for(create(:user))
625     delete trace_path(:display_name => create(:user).display_name, :id => 0)
626     assert_response :not_found
627
628     # Now with a trace has already been deleted
629     session_for(deleted_trace_file.user)
630     delete trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
631     assert_response :not_found
632
633     # Now with a trace that we are allowed to delete
634     session_for(public_trace_file.user)
635     delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
636     assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
637     trace = Trace.find(public_trace_file.id)
638     assert_not trace.visible
639
640     # Finally with a trace that is destroyed by an admin
641     public_trace_file = create(:trace, :visibility => "public")
642     admin = create(:administrator_user)
643     session_for(admin)
644     delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
645     assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
646     trace = Trace.find(public_trace_file.id)
647     assert_not trace.visible
648   end
649
650   private
651
652   def check_trace_index(traces)
653     assert_response :success
654     assert_template "index"
655
656     if traces.empty?
657       assert_select "h2", /Nothing here yet/
658     else
659       assert_select "table#trace_list tbody", :count => 1 do
660         assert_select "tr", :count => traces.length do |rows|
661           traces.zip(rows).each do |trace, row|
662             assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
663             assert_select row, "li", Regexp.new(Regexp.escape("#{trace.size} points")) if trace.inserted?
664             assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
665             assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
666             assert_select row, "a[href='#{user_path trace.user}']", :text => trace.user.display_name
667           end
668         end
669       end
670     end
671   end
672
673   def check_trace_show(trace)
674     assert_response :success
675     assert_template "show"
676
677     assert_select "table", :count => 1 do
678       assert_select "td", /^#{Regexp.quote(trace.name)} /
679       assert_select "td a[href='#{user_path trace.user}']", :text => trace.user.display_name
680       assert_select "td", trace.description
681     end
682   end
683
684   def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
685     assert_equal digest, Digest::MD5.hexdigest(response.body)
686     assert_equal content_type, response.media_type
687     assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
688   end
689 end