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