2 require "minitest/mock"
4 class TracesControllerTest < ActionController::TestCase
6 @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
7 Object.const_set("GPX_TRACE_DIR", Rails.root.join("test", "gpx", "traces"))
9 @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
10 Object.const_set("GPX_IMAGE_DIR", Rails.root.join("test", "gpx", "images"))
14 File.unlink(*Dir.glob(File.join(GPX_TRACE_DIR, "*.gpx")))
15 File.unlink(*Dir.glob(File.join(GPX_IMAGE_DIR, "*.gif")))
17 Object.send("remove_const", "GPX_TRACE_DIR")
18 Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
20 Object.send("remove_const", "GPX_IMAGE_DIR")
21 Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
25 # test all routes which lead to this controller
28 { :path => "/traces", :method => :get },
29 { :controller => "traces", :action => "index" }
32 { :path => "/traces/page/1", :method => :get },
33 { :controller => "traces", :action => "index", :page => "1" }
36 { :path => "/traces/tag/tagname", :method => :get },
37 { :controller => "traces", :action => "index", :tag => "tagname" }
40 { :path => "/traces/tag/tagname/page/1", :method => :get },
41 { :controller => "traces", :action => "index", :tag => "tagname", :page => "1" }
44 { :path => "/user/username/traces", :method => :get },
45 { :controller => "traces", :action => "index", :display_name => "username" }
48 { :path => "/user/username/traces/page/1", :method => :get },
49 { :controller => "traces", :action => "index", :display_name => "username", :page => "1" }
52 { :path => "/user/username/traces/tag/tagname", :method => :get },
53 { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
56 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
57 { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname", :page => "1" }
61 { :path => "/traces/mine", :method => :get },
62 { :controller => "traces", :action => "mine" }
65 { :path => "/traces/mine/page/1", :method => :get },
66 { :controller => "traces", :action => "mine", :page => "1" }
69 { :path => "/traces/mine/tag/tagname", :method => :get },
70 { :controller => "traces", :action => "mine", :tag => "tagname" }
73 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
74 { :controller => "traces", :action => "mine", :tag => "tagname", :page => "1" }
78 { :path => "/traces/rss", :method => :get },
79 { :controller => "traces", :action => "georss", :format => :rss }
82 { :path => "/traces/tag/tagname/rss", :method => :get },
83 { :controller => "traces", :action => "georss", :tag => "tagname", :format => :rss }
86 { :path => "/user/username/traces/rss", :method => :get },
87 { :controller => "traces", :action => "georss", :display_name => "username", :format => :rss }
90 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
91 { :controller => "traces", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
95 { :path => "/user/username/traces/1", :method => :get },
96 { :controller => "traces", :action => "show", :display_name => "username", :id => "1" }
99 { :path => "/user/username/traces/1/picture", :method => :get },
100 { :controller => "traces", :action => "picture", :display_name => "username", :id => "1" }
103 { :path => "/user/username/traces/1/icon", :method => :get },
104 { :controller => "traces", :action => "icon", :display_name => "username", :id => "1" }
108 { :path => "/traces/new", :method => :get },
109 { :controller => "traces", :action => "new" }
112 { :path => "/traces", :method => :post },
113 { :controller => "traces", :action => "create" }
116 { :path => "/trace/1/data", :method => :get },
117 { :controller => "traces", :action => "data", :id => "1" }
120 { :path => "/trace/1/data.xml", :method => :get },
121 { :controller => "traces", :action => "data", :id => "1", :format => "xml" }
124 { :path => "/traces/1/edit", :method => :get },
125 { :controller => "traces", :action => "edit", :id => "1" }
128 { :path => "/traces/1", :method => :put },
129 { :controller => "traces", :action => "update", :id => "1" }
132 { :path => "/trace/1/delete", :method => :post },
133 { :controller => "traces", :action => "delete", :id => "1" }
137 # Check that the index of traces is displayed
140 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
141 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
142 create(:tracetag, :trace => trace, :tag => "London")
144 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
145 create(:tracetag, :trace => trace, :tag => "Birmingham")
147 trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
148 create(:tracetag, :trace => trace, :tag => "London")
150 trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
151 create(:tracetag, :trace => trace, :tag => "Birmingham")
154 # First with the public index
156 check_trace_index [trace_b, trace_a]
158 # Restrict traces to those with a given tag
159 get :index, :params => { :tag => "London" }
160 check_trace_index [trace_a]
162 # Should see more when we are logged in
163 get :index, :session => { :user => user }
164 check_trace_index [trace_d, trace_c, trace_b, trace_a]
166 # Again, we should see more when we are logged in
167 get :index, :params => { :tag => "London" }, :session => { :user => user }
168 check_trace_index [trace_c, trace_a]
171 # Check that I can get mine
174 create(:trace, :visibility => "public") do |trace|
175 create(:tracetag, :trace => trace, :tag => "Birmingham")
177 trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
178 create(:tracetag, :trace => trace, :tag => "London")
181 # First try to get it when not logged in
183 assert_redirected_to :controller => "users", :action => "login", :referer => "/traces/mine"
185 # Now try when logged in
186 get :mine, :session => { :user => user }
187 assert_redirected_to :action => "index", :display_name => user.display_name
189 # Fetch the actual index
190 get :index, :params => { :display_name => user.display_name }, :session => { :user => user }
191 check_trace_index [trace_b]
194 # Check the index of traces for a specific user
197 second_user = create(:user)
198 third_user = create(:user)
200 trace_b = create(:trace, :visibility => "public", :user => user)
201 trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
202 create(:tracetag, :trace => trace, :tag => "London")
205 # Test a user with no traces
206 get :index, :params => { :display_name => second_user.display_name }
209 # Test the user with the traces - should see only public ones
210 get :index, :params => { :display_name => user.display_name }
211 check_trace_index [trace_b]
213 # Should still see only public ones when authenticated as another user
214 get :index, :params => { :display_name => user.display_name }, :session => { :user => third_user }
215 check_trace_index [trace_b]
217 # Should see all traces when authenticated as the target user
218 get :index, :params => { :display_name => user.display_name }, :session => { :user => user }
219 check_trace_index [trace_c, trace_b]
221 # Should only see traces with the correct tag when a tag is specified
222 get :index, :params => { :display_name => user.display_name, :tag => "London" }, :session => { :user => user }
223 check_trace_index [trace_c]
225 # Should get an error if the user does not exist
226 get :index, :params => { :display_name => "UnknownUser" }
227 assert_response :not_found
228 assert_template "users/no_such_user"
231 # Check a multi-page index
233 # Create several pages worth of traces
234 create_list(:trace, 50)
236 # Try and get the index
238 assert_response :success
239 assert_select "table#trace_list tbody", :count => 1 do
240 assert_select "tr", :count => 20
243 # Try and get the second page
244 get :index, :params => { :page => 2 }
245 assert_response :success
246 assert_select "table#trace_list tbody", :count => 1 do
247 assert_select "tr", :count => 20
254 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
255 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
256 create(:tracetag, :trace => trace, :tag => "London")
258 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
259 create(:tracetag, :trace => trace, :tag => "Birmingham")
261 create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
262 create(:tracetag, :trace => trace, :tag => "London")
264 create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
265 create(:tracetag, :trace => trace, :tag => "Birmingham")
268 # First with the public feed
269 get :georss, :params => { :format => :rss }
270 check_trace_feed [trace_b, trace_a]
272 # Restrict traces to those with a given tag
273 get :georss, :params => { :tag => "London", :format => :rss }
274 check_trace_feed [trace_a]
277 # Check the RSS feed for a specific user
280 second_user = create(:user)
283 trace_b = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago, :user => user)
284 trace_c = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago, :user => user) do |trace|
285 create(:tracetag, :trace => trace, :tag => "London")
287 create(:trace, :visibility => "private")
289 # Test a user with no traces
290 get :georss, :params => { :display_name => second_user.display_name, :format => :rss }
293 # Test the user with the traces - should see only public ones
294 get :georss, :params => { :display_name => user.display_name, :format => :rss }
295 check_trace_feed [trace_c, trace_b]
297 # Should only see traces with the correct tag when a tag is specified
298 get :georss, :params => { :display_name => user.display_name, :tag => "London", :format => :rss }
299 check_trace_feed [trace_c]
301 # Should no traces if the user does not exist
302 get :georss, :params => { :display_name => "UnknownUser", :format => :rss }
306 # Test showing a trace
308 public_trace_file = create(:trace, :visibility => "public")
310 # First with no auth, which should work since the trace is public
311 get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
312 check_trace_show public_trace_file
314 # Now with some other user, which should work since the trace is public
315 get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
316 check_trace_show public_trace_file
318 # And finally we should be able to do it with the owner of the trace
319 get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
320 check_trace_show public_trace_file
323 # Check an anonymous trace can't be viewed by another user
325 anon_trace_file = create(:trace, :visibility => "private")
328 get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
329 assert_response :redirect
330 assert_redirected_to :action => :index
332 # Now with some other user, which should not work since the trace is anon
333 get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
334 assert_response :redirect
335 assert_redirected_to :action => :index
337 # And finally we should be able to do it with the owner of the trace
338 get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
339 check_trace_show anon_trace_file
342 # Test showing a trace that doesn't exist
343 def test_show_not_found
344 deleted_trace_file = create(:trace, :deleted)
346 # First with a trace that has never existed
347 get :show, :params => { :display_name => create(:user).display_name, :id => 0 }
348 assert_response :redirect
349 assert_redirected_to :action => :index
351 # Now with a trace that has been deleted
352 get :show, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
353 assert_response :redirect
354 assert_redirected_to :action => :index
357 # Test downloading a trace
359 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
361 # First with no auth, which should work since the trace is public
362 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
363 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
365 # Now with some other user, which should work since the trace is public
366 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
367 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
369 # And finally we should be able to do it with the owner of the trace
370 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
371 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
374 # Test downloading a compressed trace
375 def test_data_compressed
376 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
378 # First get the data as is
379 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id }
380 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
382 # Now ask explicitly for XML format
383 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" }
384 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
386 # Now ask explicitly for GPX format
387 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" }
388 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
391 # Check an anonymous trace can't be downloaded by another user
393 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
396 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
397 assert_response :not_found
399 # Now with some other user, which shouldn't work since the trace is anon
400 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
401 assert_response :not_found
403 # And finally we should be able to do it with the owner of the trace
404 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
405 check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
408 # Test downloading a trace that doesn't exist
409 def test_data_not_found
410 deleted_trace_file = create(:trace, :deleted)
412 # First with a trace that has never existed
413 get :data, :params => { :display_name => create(:user).display_name, :id => 0 }
414 assert_response :not_found
416 # Now with a trace that has been deleted
417 get :data, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
418 assert_response :not_found
421 # Test downloading the picture for a trace
423 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
425 # First with no auth, which should work since the trace is public
426 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
427 check_trace_picture public_trace_file
429 # Now with some other user, which should work since the trace is public
430 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
431 check_trace_picture public_trace_file
433 # And finally we should be able to do it with the owner of the trace
434 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
435 check_trace_picture public_trace_file
438 # Check the picture for an anonymous trace can't be downloaded by another user
439 def test_picture_anon
440 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
443 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
444 assert_response :forbidden
446 # Now with some other user, which shouldn't work since the trace is anon
447 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
448 assert_response :forbidden
450 # And finally we should be able to do it with the owner of the trace
451 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
452 check_trace_picture anon_trace_file
455 # Test downloading the picture for a trace that doesn't exist
456 def test_picture_not_found
457 deleted_trace_file = create(:trace, :deleted)
459 # First with a trace that has never existed
460 get :picture, :params => { :display_name => create(:user).display_name, :id => 0 }
461 assert_response :not_found
463 # Now with a trace that has been deleted
464 get :picture, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
465 assert_response :not_found
468 # Test downloading the icon for a trace
470 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
472 # First with no auth, which should work since the trace is public
473 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
474 check_trace_icon public_trace_file
476 # Now with some other user, which should work since the trace is public
477 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
478 check_trace_icon public_trace_file
480 # And finally we should be able to do it with the owner of the trace
481 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
482 check_trace_icon public_trace_file
485 # Check the icon for an anonymous trace can't be downloaded by another user
487 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
490 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
491 assert_response :forbidden
493 # Now with some other user, which shouldn't work since the trace is anon
494 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
495 assert_response :forbidden
497 # And finally we should be able to do it with the owner of the trace
498 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
499 check_trace_icon anon_trace_file
502 # Test downloading the icon for a trace that doesn't exist
503 def test_icon_not_found
504 deleted_trace_file = create(:trace, :deleted)
506 # First with a trace that has never existed
507 get :icon, :params => { :display_name => create(:user).display_name, :id => 0 }
508 assert_response :not_found
510 # Now with a trace that has been deleted
511 get :icon, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
512 assert_response :not_found
515 # Test fetching the new trace page
519 assert_response :redirect
520 assert_redirected_to :controller => :users, :action => :login, :referer => new_trace_path
522 # Now authenticated as a user with gps.trace.visibility set
524 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
525 get :new, :session => { :user => user }
526 assert_response :success
528 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
530 # Now authenticated as a user with gps.trace.public set
531 second_user = create(:user)
532 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
533 get :new, :session => { :user => second_user }
534 assert_response :success
536 assert_select "select#trace_visibility option[value=public][selected]", 1
538 # Now authenticated as a user with no preferences
539 third_user = create(:user)
540 get :new, :session => { :user => third_user }
541 assert_response :success
543 assert_select "select#trace_visibility option[value=private][selected]", 1
546 # Test creating a trace
549 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
550 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
554 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
555 assert_response :forbidden
561 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
562 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
563 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
564 assert_response :redirect
565 assert_redirected_to :action => :index, :display_name => user.display_name
566 assert_match(/file has been uploaded/, flash[:notice])
567 trace = Trace.order(:id => :desc).first
568 assert_equal "a.gpx", trace.name
569 assert_equal "New Trace", trace.description
570 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
571 assert_equal "trackable", trace.visibility
572 assert_equal false, trace.inserted
573 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
575 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
578 # Test creating a trace with validation errors
579 def test_create_post_with_validation_errors
581 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
582 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
586 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
587 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
588 post :create, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
590 assert_match "Description is too short (minimum is 1 character)", response.body
593 # Test fetching the edit page for a trace using GET
595 public_trace_file = create(:trace, :visibility => "public")
596 deleted_trace_file = create(:trace, :deleted)
599 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
600 assert_response :redirect
601 assert_redirected_to :controller => :users, :action => :login, :referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)
603 # Now with some other user, which should fail
604 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
605 assert_response :forbidden
607 # Now with a trace which doesn't exist
608 get :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
609 assert_response :not_found
611 # Now with a trace which has been deleted
612 get :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
613 assert_response :not_found
615 # Finally with a trace that we are allowed to edit
616 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
617 assert_response :success
620 # Test saving edits to a trace
622 public_trace_file = create(:trace, :visibility => "public")
623 deleted_trace_file = create(:trace, :deleted)
626 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
629 put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }
630 assert_response :forbidden
632 # Now with some other user, which should fail
633 put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => create(:user) }
634 assert_response :forbidden
636 # Now with a trace which doesn't exist
637 put :update, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user), :trace => new_details }
638 assert_response :not_found
640 # Now with a trace which has been deleted
641 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 }
642 assert_response :not_found
644 # Finally with a trace that we are allowed to edit
645 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 }
646 assert_response :redirect
647 assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
648 trace = Trace.find(public_trace_file.id)
649 assert_equal new_details[:description], trace.description
650 assert_equal new_details[:tagstring], trace.tagstring
651 assert_equal new_details[:visibility], trace.visibility
654 # Test deleting a trace
656 public_trace_file = create(:trace, :visibility => "public")
657 deleted_trace_file = create(:trace, :deleted)
660 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
661 assert_response :forbidden
663 # Now with some other user, which should fail
664 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
665 assert_response :forbidden
667 # Now with a trace which doesn't exist
668 post :delete, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
669 assert_response :not_found
671 # Now with a trace has already been deleted
672 post :delete, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
673 assert_response :not_found
675 # Now with a trace that we are allowed to delete
676 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
677 assert_response :redirect
678 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
679 trace = Trace.find(public_trace_file.id)
680 assert_equal false, trace.visible
682 # Finally with a trace that is deleted by an admin
683 public_trace_file = create(:trace, :visibility => "public")
684 admin = create(:administrator_user)
686 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => admin }
687 assert_response :redirect
688 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
689 trace = Trace.find(public_trace_file.id)
690 assert_equal false, trace.visible
695 def check_trace_feed(traces)
696 assert_response :success
697 assert_template "georss"
698 assert_equal "application/rss+xml", @response.content_type
699 assert_select "rss", :count => 1 do
700 assert_select "channel", :count => 1 do
701 assert_select "title"
702 assert_select "description"
704 assert_select "image"
705 assert_select "item", :count => traces.length do |items|
706 traces.zip(items).each do |trace, item|
707 assert_select item, "title", trace.name
708 assert_select item, "link", "http://test.host/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
709 assert_select item, "guid", "http://test.host/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
710 assert_select item, "description"
711 # assert_select item, "dc:creator", trace.user.display_name
712 assert_select item, "pubDate", trace.timestamp.rfc822
719 def check_trace_index(traces)
720 assert_response :success
721 assert_template "index"
724 assert_select "table#trace_list tbody", :count => 1 do
725 assert_select "tr", :count => traces.length do |rows|
726 traces.zip(rows).each do |trace, row|
727 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
728 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
729 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
730 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
735 assert_select "h4", /Nothing here yet/
739 def check_trace_show(trace)
740 assert_response :success
741 assert_template "show"
743 assert_select "table", :count => 1 do
744 assert_select "td", /^#{Regexp.quote(trace.name)} /
745 assert_select "td", trace.user.display_name
746 assert_select "td", trace.description
750 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
751 assert_response :success
752 assert_equal digest, Digest::MD5.hexdigest(response.body)
753 assert_equal content_type, response.content_type
754 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
757 def check_trace_picture(trace)
758 assert_response :success
759 assert_equal "image/gif", response.content_type
760 assert_equal trace.large_picture, response.body
763 def check_trace_icon(trace)
764 assert_response :success
765 assert_equal "image/gif", response.content_type
766 assert_equal trace.icon_picture, response.body