2 require "minitest/mock"
4 class TracesControllerTest < ActionController::TestCase
6 File.unlink(*Dir.glob(File.join(Settings.gpx_trace_dir, "*.gpx")))
7 File.unlink(*Dir.glob(File.join(Settings.gpx_image_dir, "*.gif")))
11 # test all routes which lead to this controller
14 { :path => "/traces", :method => :get },
15 { :controller => "traces", :action => "index" }
18 { :path => "/traces/page/1", :method => :get },
19 { :controller => "traces", :action => "index", :page => "1" }
22 { :path => "/traces/tag/tagname", :method => :get },
23 { :controller => "traces", :action => "index", :tag => "tagname" }
26 { :path => "/traces/tag/tagname/page/1", :method => :get },
27 { :controller => "traces", :action => "index", :tag => "tagname", :page => "1" }
30 { :path => "/user/username/traces", :method => :get },
31 { :controller => "traces", :action => "index", :display_name => "username" }
34 { :path => "/user/username/traces/page/1", :method => :get },
35 { :controller => "traces", :action => "index", :display_name => "username", :page => "1" }
38 { :path => "/user/username/traces/tag/tagname", :method => :get },
39 { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
42 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
43 { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname", :page => "1" }
47 { :path => "/traces/mine", :method => :get },
48 { :controller => "traces", :action => "mine" }
51 { :path => "/traces/mine/page/1", :method => :get },
52 { :controller => "traces", :action => "mine", :page => "1" }
55 { :path => "/traces/mine/tag/tagname", :method => :get },
56 { :controller => "traces", :action => "mine", :tag => "tagname" }
59 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
60 { :controller => "traces", :action => "mine", :tag => "tagname", :page => "1" }
64 { :path => "/traces/rss", :method => :get },
65 { :controller => "traces", :action => "georss", :format => :rss }
68 { :path => "/traces/tag/tagname/rss", :method => :get },
69 { :controller => "traces", :action => "georss", :tag => "tagname", :format => :rss }
72 { :path => "/user/username/traces/rss", :method => :get },
73 { :controller => "traces", :action => "georss", :display_name => "username", :format => :rss }
76 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
77 { :controller => "traces", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
81 { :path => "/user/username/traces/1", :method => :get },
82 { :controller => "traces", :action => "show", :display_name => "username", :id => "1" }
85 { :path => "/user/username/traces/1/picture", :method => :get },
86 { :controller => "traces", :action => "picture", :display_name => "username", :id => "1" }
89 { :path => "/user/username/traces/1/icon", :method => :get },
90 { :controller => "traces", :action => "icon", :display_name => "username", :id => "1" }
94 { :path => "/traces/new", :method => :get },
95 { :controller => "traces", :action => "new" }
98 { :path => "/traces", :method => :post },
99 { :controller => "traces", :action => "create" }
102 { :path => "/trace/1/data", :method => :get },
103 { :controller => "traces", :action => "data", :id => "1" }
106 { :path => "/trace/1/data.xml", :method => :get },
107 { :controller => "traces", :action => "data", :id => "1", :format => "xml" }
110 { :path => "/traces/1/edit", :method => :get },
111 { :controller => "traces", :action => "edit", :id => "1" }
114 { :path => "/traces/1", :method => :put },
115 { :controller => "traces", :action => "update", :id => "1" }
118 { :path => "/traces/1", :method => :delete },
119 { :controller => "traces", :action => "destroy", :id => "1" }
123 # Check that the index of traces is displayed
126 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
127 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
128 create(:tracetag, :trace => trace, :tag => "London")
130 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
131 create(:tracetag, :trace => trace, :tag => "Birmingham")
133 trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
134 create(:tracetag, :trace => trace, :tag => "London")
136 trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
137 create(:tracetag, :trace => trace, :tag => "Birmingham")
140 # First with the public index
142 check_trace_index [trace_b, trace_a]
144 # Restrict traces to those with a given tag
145 get :index, :params => { :tag => "London" }
146 check_trace_index [trace_a]
148 # Should see more when we are logged in
149 get :index, :session => { :user => user }
150 check_trace_index [trace_d, trace_c, trace_b, trace_a]
152 # Again, we should see more when we are logged in
153 get :index, :params => { :tag => "London" }, :session => { :user => user }
154 check_trace_index [trace_c, trace_a]
157 # Check that I can get mine
160 create(:trace, :visibility => "public") do |trace|
161 create(:tracetag, :trace => trace, :tag => "Birmingham")
163 trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
164 create(:tracetag, :trace => trace, :tag => "London")
167 # First try to get it when not logged in
169 assert_redirected_to :controller => "users", :action => "login", :referer => "/traces/mine"
171 # Now try when logged in
172 get :mine, :session => { :user => user }
173 assert_redirected_to :action => "index", :display_name => user.display_name
175 # Fetch the actual index
176 get :index, :params => { :display_name => user.display_name }, :session => { :user => user }
177 check_trace_index [trace_b]
180 # Check the index of traces for a specific user
183 second_user = create(:user)
184 third_user = create(:user)
186 trace_b = create(:trace, :visibility => "public", :user => user)
187 trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
188 create(:tracetag, :trace => trace, :tag => "London")
191 # Test a user with no traces
192 get :index, :params => { :display_name => second_user.display_name }
195 # Test the user with the traces - should see only public ones
196 get :index, :params => { :display_name => user.display_name }
197 check_trace_index [trace_b]
199 # Should still see only public ones when authenticated as another user
200 get :index, :params => { :display_name => user.display_name }, :session => { :user => third_user }
201 check_trace_index [trace_b]
203 # Should see all traces when authenticated as the target user
204 get :index, :params => { :display_name => user.display_name }, :session => { :user => user }
205 check_trace_index [trace_c, trace_b]
207 # Should only see traces with the correct tag when a tag is specified
208 get :index, :params => { :display_name => user.display_name, :tag => "London" }, :session => { :user => user }
209 check_trace_index [trace_c]
211 # Should get an error if the user does not exist
212 get :index, :params => { :display_name => "UnknownUser" }
213 assert_response :not_found
214 assert_template "users/no_such_user"
217 # Check a multi-page index
219 # Create several pages worth of traces
220 create_list(:trace, 50)
222 # Try and get the index
224 assert_response :success
225 assert_select "table#trace_list tbody", :count => 1 do
226 assert_select "tr", :count => 20
229 # Try and get the second page
230 get :index, :params => { :page => 2 }
231 assert_response :success
232 assert_select "table#trace_list tbody", :count => 1 do
233 assert_select "tr", :count => 20
240 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
241 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
242 create(:tracetag, :trace => trace, :tag => "London")
244 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
245 create(:tracetag, :trace => trace, :tag => "Birmingham")
247 create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
248 create(:tracetag, :trace => trace, :tag => "London")
250 create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
251 create(:tracetag, :trace => trace, :tag => "Birmingham")
254 # First with the public feed
255 get :georss, :params => { :format => :rss }
256 check_trace_feed [trace_b, trace_a]
258 # Restrict traces to those with a given tag
259 get :georss, :params => { :tag => "London", :format => :rss }
260 check_trace_feed [trace_a]
263 # Check the RSS feed for a specific user
266 second_user = create(:user)
269 trace_b = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago, :user => user)
270 trace_c = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago, :user => user) do |trace|
271 create(:tracetag, :trace => trace, :tag => "London")
273 create(:trace, :visibility => "private")
275 # Test a user with no traces
276 get :georss, :params => { :display_name => second_user.display_name, :format => :rss }
279 # Test the user with the traces - should see only public ones
280 get :georss, :params => { :display_name => user.display_name, :format => :rss }
281 check_trace_feed [trace_c, trace_b]
283 # Should only see traces with the correct tag when a tag is specified
284 get :georss, :params => { :display_name => user.display_name, :tag => "London", :format => :rss }
285 check_trace_feed [trace_c]
287 # Should no traces if the user does not exist
288 get :georss, :params => { :display_name => "UnknownUser", :format => :rss }
292 # Test showing a trace
294 public_trace_file = create(:trace, :visibility => "public")
296 # First with no auth, which should work since the trace is public
297 get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
298 check_trace_show public_trace_file
300 # Now with some other user, which should work since the trace is public
301 get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
302 check_trace_show public_trace_file
304 # And finally we should be able to do it with the owner of the trace
305 get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
306 check_trace_show public_trace_file
309 # Check an anonymous trace can't be viewed by another user
311 anon_trace_file = create(:trace, :visibility => "private")
314 get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
315 assert_response :redirect
316 assert_redirected_to :action => :index
318 # Now with some other user, which should not work since the trace is anon
319 get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
320 assert_response :redirect
321 assert_redirected_to :action => :index
323 # And finally we should be able to do it with the owner of the trace
324 get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
325 check_trace_show anon_trace_file
328 # Test showing a trace that doesn't exist
329 def test_show_not_found
330 deleted_trace_file = create(:trace, :deleted)
332 # First with a trace that has never existed
333 get :show, :params => { :display_name => create(:user).display_name, :id => 0 }
334 assert_response :redirect
335 assert_redirected_to :action => :index
337 # Now with a trace that has been deleted
338 get :show, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
339 assert_response :redirect
340 assert_redirected_to :action => :index
343 # Test downloading a trace
345 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
347 # First with no auth, which should work since the trace is public
348 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
349 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
351 # Now with some other user, which should work since the trace is public
352 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
353 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
355 # And finally we should be able to do it with the owner of the trace
356 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
357 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
360 # Test downloading a compressed trace
361 def test_data_compressed
362 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
364 # First get the data as is
365 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id }
366 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
368 # Now ask explicitly for XML format
369 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" }
370 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
372 # Now ask explicitly for GPX format
373 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" }
374 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
377 # Check an anonymous trace can't be downloaded by another user
379 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
382 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
383 assert_response :not_found
385 # Now with some other user, which shouldn't work since the trace is anon
386 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
387 assert_response :not_found
389 # And finally we should be able to do it with the owner of the trace
390 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
391 check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
394 # Test downloading a trace that doesn't exist
395 def test_data_not_found
396 deleted_trace_file = create(:trace, :deleted)
398 # First with a trace that has never existed
399 get :data, :params => { :display_name => create(:user).display_name, :id => 0 }
400 assert_response :not_found
402 # Now with a trace that has been deleted
403 get :data, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
404 assert_response :not_found
407 # Test downloading the picture for a trace
409 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
411 # First with no auth, which should work since the trace is public
412 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
413 check_trace_picture public_trace_file
415 # Now with some other user, which should work since the trace is public
416 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
417 check_trace_picture public_trace_file
419 # And finally we should be able to do it with the owner of the trace
420 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
421 check_trace_picture public_trace_file
424 # Check the picture for an anonymous trace can't be downloaded by another user
425 def test_picture_anon
426 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
429 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
430 assert_response :forbidden
432 # Now with some other user, which shouldn't work since the trace is anon
433 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
434 assert_response :forbidden
436 # And finally we should be able to do it with the owner of the trace
437 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
438 check_trace_picture anon_trace_file
441 # Test downloading the picture for a trace that doesn't exist
442 def test_picture_not_found
443 deleted_trace_file = create(:trace, :deleted)
445 # First with a trace that has never existed
446 get :picture, :params => { :display_name => create(:user).display_name, :id => 0 }
447 assert_response :not_found
449 # Now with a trace that has been deleted
450 get :picture, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
451 assert_response :not_found
454 # Test downloading the icon for a trace
456 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
458 # First with no auth, which should work since the trace is public
459 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
460 check_trace_icon public_trace_file
462 # Now with some other user, which should work since the trace is public
463 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
464 check_trace_icon public_trace_file
466 # And finally we should be able to do it with the owner of the trace
467 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
468 check_trace_icon public_trace_file
471 # Check the icon for an anonymous trace can't be downloaded by another user
473 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
476 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
477 assert_response :forbidden
479 # Now with some other user, which shouldn't work since the trace is anon
480 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
481 assert_response :forbidden
483 # And finally we should be able to do it with the owner of the trace
484 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
485 check_trace_icon anon_trace_file
488 # Test downloading the icon for a trace that doesn't exist
489 def test_icon_not_found
490 deleted_trace_file = create(:trace, :deleted)
492 # First with a trace that has never existed
493 get :icon, :params => { :display_name => create(:user).display_name, :id => 0 }
494 assert_response :not_found
496 # Now with a trace that has been deleted
497 get :icon, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
498 assert_response :not_found
501 # Test fetching the new trace page
505 assert_response :redirect
506 assert_redirected_to :controller => :users, :action => :login, :referer => new_trace_path
508 # Now authenticated as a user with gps.trace.visibility set
510 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
511 get :new, :session => { :user => user }
512 assert_response :success
514 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
516 # Now authenticated as a user with gps.trace.public set
517 second_user = create(:user)
518 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
519 get :new, :session => { :user => second_user }
520 assert_response :success
522 assert_select "select#trace_visibility option[value=public][selected]", 1
524 # Now authenticated as a user with no preferences
525 third_user = create(:user)
526 get :new, :session => { :user => third_user }
527 assert_response :success
529 assert_select "select#trace_visibility option[value=private][selected]", 1
532 # Test creating a trace
535 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
536 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
540 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
541 assert_response :forbidden
547 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
548 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
549 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
550 assert_response :redirect
551 assert_redirected_to :action => :index, :display_name => user.display_name
552 assert_match(/file has been uploaded/, flash[:notice])
553 trace = Trace.order(:id => :desc).first
554 assert_equal "a.gpx", trace.name
555 assert_equal "New Trace", trace.description
556 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
557 assert_equal "trackable", trace.visibility
558 assert_equal false, trace.inserted
559 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
561 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
564 # Test creating a trace with validation errors
565 def test_create_post_with_validation_errors
567 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
568 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
572 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
573 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
574 post :create, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
576 assert_match "Description is too short (minimum is 1 character)", response.body
579 # Test fetching the edit page for a trace using GET
581 public_trace_file = create(:trace, :visibility => "public")
582 deleted_trace_file = create(:trace, :deleted)
585 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
586 assert_response :redirect
587 assert_redirected_to :controller => :users, :action => :login, :referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)
589 # Now with some other user, which should fail
590 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
591 assert_response :forbidden
593 # Now with a trace which doesn't exist
594 get :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
595 assert_response :not_found
597 # Now with a trace which has been deleted
598 get :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
599 assert_response :not_found
601 # Finally with a trace that we are allowed to edit
602 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
603 assert_response :success
606 # Test saving edits to a trace
608 public_trace_file = create(:trace, :visibility => "public")
609 deleted_trace_file = create(:trace, :deleted)
612 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
615 put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }
616 assert_response :forbidden
618 # Now with some other user, which should fail
619 put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => create(:user) }
620 assert_response :forbidden
622 # Now with a trace which doesn't exist
623 put :update, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user), :trace => new_details }
624 assert_response :not_found
626 # Now with a trace which has been deleted
627 put :update, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id, :trace => new_details }, :session => { :user => deleted_trace_file.user }
628 assert_response :not_found
630 # Finally with a trace that we are allowed to edit
631 put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => public_trace_file.user }
632 assert_response :redirect
633 assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
634 trace = Trace.find(public_trace_file.id)
635 assert_equal new_details[:description], trace.description
636 assert_equal new_details[:tagstring], trace.tagstring
637 assert_equal new_details[:visibility], trace.visibility
640 # Test destroying a trace
642 public_trace_file = create(:trace, :visibility => "public")
643 deleted_trace_file = create(:trace, :deleted)
646 delete :destroy, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
647 assert_response :forbidden
649 # Now with some other user, which should fail
650 delete :destroy, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
651 assert_response :forbidden
653 # Now with a trace which doesn't exist
654 delete :destroy, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
655 assert_response :not_found
657 # Now with a trace has already been deleted
658 delete :destroy, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
659 assert_response :not_found
661 # Now with a trace that we are allowed to delete
662 delete :destroy, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
663 assert_response :redirect
664 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
665 trace = Trace.find(public_trace_file.id)
666 assert_equal false, trace.visible
668 # Finally with a trace that is destroyed by an admin
669 public_trace_file = create(:trace, :visibility => "public")
670 admin = create(:administrator_user)
672 delete :destroy, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => admin }
673 assert_response :redirect
674 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
675 trace = Trace.find(public_trace_file.id)
676 assert_equal false, trace.visible
681 def check_trace_feed(traces)
682 assert_response :success
683 assert_template "georss"
684 assert_equal "application/rss+xml", @response.media_type
685 assert_select "rss", :count => 1 do
686 assert_select "channel", :count => 1 do
687 assert_select "title"
688 assert_select "description"
690 assert_select "image"
691 assert_select "item", :count => traces.length do |items|
692 traces.zip(items).each do |trace, item|
693 assert_select item, "title", trace.name
694 assert_select item, "link", "http://test.host/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
695 assert_select item, "guid", "http://test.host/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
696 assert_select item, "description"
697 # assert_select item, "dc:creator", trace.user.display_name
698 assert_select item, "pubDate", trace.timestamp.rfc822
705 def check_trace_index(traces)
706 assert_response :success
707 assert_template "index"
710 assert_select "table#trace_list tbody", :count => 1 do
711 assert_select "tr", :count => traces.length do |rows|
712 traces.zip(rows).each do |trace, row|
713 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
714 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
715 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
716 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
721 assert_select "h4", /Nothing here yet/
725 def check_trace_show(trace)
726 assert_response :success
727 assert_template "show"
729 assert_select "table", :count => 1 do
730 assert_select "td", /^#{Regexp.quote(trace.name)} /
731 assert_select "td", trace.user.display_name
732 assert_select "td", trace.description
736 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
737 assert_response :success
738 assert_equal digest, Digest::MD5.hexdigest(response.body)
739 assert_equal content_type, response.media_type
740 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]
743 def check_trace_picture(trace)
744 assert_response :success
745 assert_equal "image/gif", response.media_type
746 assert_equal trace.large_picture, response.body
749 def check_trace_icon(trace)
750 assert_response :success
751 assert_equal "image/gif", response.media_type
752 assert_equal trace.icon_picture, response.body