3 require "minitest/mock"
5 class TracesControllerTest < ActionController::TestCase
7 @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
8 Object.const_set("GPX_TRACE_DIR", Rails.root.join("test", "gpx", "traces"))
10 @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
11 Object.const_set("GPX_IMAGE_DIR", Rails.root.join("test", "gpx", "images"))
15 File.unlink(*Dir.glob(File.join(GPX_TRACE_DIR, "*.gpx")))
16 File.unlink(*Dir.glob(File.join(GPX_IMAGE_DIR, "*.gif")))
18 Object.send("remove_const", "GPX_TRACE_DIR")
19 Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
21 Object.send("remove_const", "GPX_IMAGE_DIR")
22 Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
26 # test all routes which lead to this controller
29 { :path => "/api/0.6/gpx/create", :method => :post },
30 { :controller => "traces", :action => "api_create" }
33 { :path => "/api/0.6/gpx/1", :method => :get },
34 { :controller => "traces", :action => "api_read", :id => "1" }
37 { :path => "/api/0.6/gpx/1", :method => :put },
38 { :controller => "traces", :action => "api_update", :id => "1" }
41 { :path => "/api/0.6/gpx/1", :method => :delete },
42 { :controller => "traces", :action => "api_delete", :id => "1" }
45 { :controller => "traces", :action => "api_read", :id => "1" },
46 { :path => "/api/0.6/gpx/1/details", :method => :get }
49 { :path => "/api/0.6/gpx/1/data", :method => :get },
50 { :controller => "traces", :action => "api_data", :id => "1" }
53 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
54 { :controller => "traces", :action => "api_data", :id => "1", :format => "xml" }
58 { :path => "/traces", :method => :get },
59 { :controller => "traces", :action => "list" }
62 { :path => "/traces/page/1", :method => :get },
63 { :controller => "traces", :action => "list", :page => "1" }
66 { :path => "/traces/tag/tagname", :method => :get },
67 { :controller => "traces", :action => "list", :tag => "tagname" }
70 { :path => "/traces/tag/tagname/page/1", :method => :get },
71 { :controller => "traces", :action => "list", :tag => "tagname", :page => "1" }
74 { :path => "/user/username/traces", :method => :get },
75 { :controller => "traces", :action => "list", :display_name => "username" }
78 { :path => "/user/username/traces/page/1", :method => :get },
79 { :controller => "traces", :action => "list", :display_name => "username", :page => "1" }
82 { :path => "/user/username/traces/tag/tagname", :method => :get },
83 { :controller => "traces", :action => "list", :display_name => "username", :tag => "tagname" }
86 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
87 { :controller => "traces", :action => "list", :display_name => "username", :tag => "tagname", :page => "1" }
91 { :path => "/traces/mine", :method => :get },
92 { :controller => "traces", :action => "mine" }
95 { :path => "/traces/mine/page/1", :method => :get },
96 { :controller => "traces", :action => "mine", :page => "1" }
99 { :path => "/traces/mine/tag/tagname", :method => :get },
100 { :controller => "traces", :action => "mine", :tag => "tagname" }
103 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
104 { :controller => "traces", :action => "mine", :tag => "tagname", :page => "1" }
108 { :path => "/traces/rss", :method => :get },
109 { :controller => "traces", :action => "georss", :format => :rss }
112 { :path => "/traces/tag/tagname/rss", :method => :get },
113 { :controller => "traces", :action => "georss", :tag => "tagname", :format => :rss }
116 { :path => "/user/username/traces/rss", :method => :get },
117 { :controller => "traces", :action => "georss", :display_name => "username", :format => :rss }
120 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
121 { :controller => "traces", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
125 { :path => "/user/username/traces/1", :method => :get },
126 { :controller => "traces", :action => "view", :display_name => "username", :id => "1" }
129 { :path => "/user/username/traces/1/picture", :method => :get },
130 { :controller => "traces", :action => "picture", :display_name => "username", :id => "1" }
133 { :path => "/user/username/traces/1/icon", :method => :get },
134 { :controller => "traces", :action => "icon", :display_name => "username", :id => "1" }
138 { :path => "/traces/new", :method => :get },
139 { :controller => "traces", :action => "new" }
142 { :path => "/traces", :method => :post },
143 { :controller => "traces", :action => "create" }
146 { :path => "/trace/1/data", :method => :get },
147 { :controller => "traces", :action => "data", :id => "1" }
150 { :path => "/trace/1/data.xml", :method => :get },
151 { :controller => "traces", :action => "data", :id => "1", :format => "xml" }
154 { :path => "/trace/1/edit", :method => :get },
155 { :controller => "traces", :action => "edit", :id => "1" }
158 { :path => "/trace/1/edit", :method => :post },
159 { :controller => "traces", :action => "edit", :id => "1" }
162 { :path => "/trace/1/delete", :method => :post },
163 { :controller => "traces", :action => "delete", :id => "1" }
167 # Check that the list of traces is displayed
170 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
171 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
172 create(:tracetag, :trace => trace, :tag => "London")
174 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
175 create(:tracetag, :trace => trace, :tag => "Birmingham")
177 trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
178 create(:tracetag, :trace => trace, :tag => "London")
180 trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
181 create(:tracetag, :trace => trace, :tag => "Birmingham")
184 # First with the public list
186 check_trace_list [trace_b, trace_a]
188 # Restrict traces to those with a given tag
189 get :list, :params => { :tag => "London" }
190 check_trace_list [trace_a]
192 # Should see more when we are logged in
193 get :list, :session => { :user => user }
194 check_trace_list [trace_d, trace_c, trace_b, trace_a]
196 # Again, we should see more when we are logged in
197 get :list, :params => { :tag => "London" }, :session => { :user => user }
198 check_trace_list [trace_c, trace_a]
201 # Check that I can get mine
204 create(:trace, :visibility => "public") do |trace|
205 create(:tracetag, :trace => trace, :tag => "Birmingham")
207 trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
208 create(:tracetag, :trace => trace, :tag => "London")
211 # First try to get it when not logged in
213 assert_redirected_to :controller => "user", :action => "login", :referer => "/traces/mine"
215 # Now try when logged in
216 get :mine, :session => { :user => user }
217 assert_redirected_to :action => "list", :display_name => user.display_name
219 # Fetch the actual list
220 get :list, :params => { :display_name => user.display_name }, :session => { :user => user }
221 check_trace_list [trace_b]
224 # Check the list of traces for a specific user
227 second_user = create(:user)
228 third_user = create(:user)
230 trace_b = create(:trace, :visibility => "public", :user => user)
231 trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
232 create(:tracetag, :trace => trace, :tag => "London")
235 # Test a user with no traces
236 get :list, :params => { :display_name => second_user.display_name }
239 # Test the user with the traces - should see only public ones
240 get :list, :params => { :display_name => user.display_name }
241 check_trace_list [trace_b]
243 # Should still see only public ones when authenticated as another user
244 get :list, :params => { :display_name => user.display_name }, :session => { :user => third_user }
245 check_trace_list [trace_b]
247 # Should see all traces when authenticated as the target user
248 get :list, :params => { :display_name => user.display_name }, :session => { :user => user }
249 check_trace_list [trace_c, trace_b]
251 # Should only see traces with the correct tag when a tag is specified
252 get :list, :params => { :display_name => user.display_name, :tag => "London" }, :session => { :user => user }
253 check_trace_list [trace_c]
255 # Should get an error if the user does not exist
256 get :list, :params => { :display_name => "UnknownUser" }
257 assert_response :not_found
258 assert_template "user/no_such_user"
261 # Check a multi-page list
263 # Create several pages worth of traces
264 create_list(:trace, 50)
266 # Try and get the list
268 assert_response :success
269 assert_select "table#trace_list tbody", :count => 1 do
270 assert_select "tr", :count => 20
273 # Try and get the second page
274 get :list, :params => { :page => 2 }
275 assert_response :success
276 assert_select "table#trace_list tbody", :count => 1 do
277 assert_select "tr", :count => 20
281 # Check that the rss loads
285 # First with the public feed
286 get :georss, :params => { :format => :rss }
287 check_trace_feed Trace.visible_to_all
289 # Restrict traces to those with a given tag
290 get :georss, :params => { :tag => "London", :format => :rss }
291 check_trace_feed Trace.tagged("London").visible_to_all
293 # Restrict traces to those for a given user
294 get :georss, :params => { :display_name => user.display_name, :format => :rss }
295 check_trace_feed user.traces.visible_to_all
297 # Restrict traces to those for a given user with a tiven tag
298 get :georss, :params => { :display_name => user.display_name, :tag => "Birmingham", :format => :rss }
299 check_trace_feed user.traces.tagged("Birmingham").visible_to_all
302 # Test viewing a trace
304 public_trace_file = create(:trace, :visibility => "public")
306 # First with no auth, which should work since the trace is public
307 get :view, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
308 check_trace_view public_trace_file
310 # Now with some other user, which should work since the trace is public
311 get :view, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
312 check_trace_view public_trace_file
314 # And finally we should be able to do it with the owner of the trace
315 get :view, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
316 check_trace_view public_trace_file
319 # Check an anonymous trace can't be viewed by another user
321 anon_trace_file = create(:trace, :visibility => "private")
324 get :view, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
325 assert_response :redirect
326 assert_redirected_to :action => :list
328 # Now with some other user, which should not work since the trace is anon
329 get :view, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
330 assert_response :redirect
331 assert_redirected_to :action => :list
333 # And finally we should be able to do it with the owner of the trace
334 get :view, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
335 check_trace_view anon_trace_file
338 # Test viewing a trace that doesn't exist
339 def test_view_not_found
340 deleted_trace_file = create(:trace, :deleted)
342 # First with a trace that has never existed
343 get :view, :params => { :display_name => create(:user).display_name, :id => 0 }
344 assert_response :redirect
345 assert_redirected_to :action => :list
347 # Now with a trace that has been deleted
348 get :view, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
349 assert_response :redirect
350 assert_redirected_to :action => :list
353 # Test downloading a trace
355 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
357 # First with no auth, which should work since the trace is public
358 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
359 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
361 # Now with some other user, 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 }, :session => { :user => create(:user) }
363 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
365 # And finally we should be able to do it with the owner of the trace
366 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
367 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
370 # Test downloading a compressed trace
371 def test_data_compressed
372 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
374 # First get the data as is
375 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id }
376 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
378 # Now ask explicitly for XML format
379 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" }
380 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
382 # Now ask explicitly for GPX format
383 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" }
384 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
387 # Check an anonymous trace can't be downloaded by another user
389 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
392 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
393 assert_response :not_found
395 # Now with some other user, which shouldn't work since the trace is anon
396 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
397 assert_response :not_found
399 # And finally we should be able to do it with the owner of the trace
400 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
401 check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
404 # Test downloading a trace that doesn't exist
405 def test_data_not_found
406 deleted_trace_file = create(:trace, :deleted)
408 # First with a trace that has never existed
409 get :data, :params => { :display_name => create(:user).display_name, :id => 0 }
410 assert_response :not_found
412 # Now with a trace that has been deleted
413 get :data, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
414 assert_response :not_found
417 # Test downloading the picture for a trace
419 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
421 # First with no auth, which should work since the trace is public
422 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
423 check_trace_picture public_trace_file
425 # Now with some other user, 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 }, :session => { :user => create(:user) }
427 check_trace_picture public_trace_file
429 # And finally we should be able to do it with the owner of the trace
430 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
431 check_trace_picture public_trace_file
434 # Check the picture for an anonymous trace can't be downloaded by another user
435 def test_picture_anon
436 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
439 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
440 assert_response :forbidden
442 # Now with some other user, which shouldn't work since the trace is anon
443 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
444 assert_response :forbidden
446 # And finally we should be able to do it with the owner of the trace
447 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
448 check_trace_picture anon_trace_file
451 # Test downloading the picture for a trace that doesn't exist
452 def test_picture_not_found
453 deleted_trace_file = create(:trace, :deleted)
455 # First with a trace that has never existed
456 get :picture, :params => { :display_name => create(:user).display_name, :id => 0 }
457 assert_response :not_found
459 # Now with a trace that has been deleted
460 get :picture, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
461 assert_response :not_found
464 # Test downloading the icon for a trace
466 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
468 # First with no auth, which should work since the trace is public
469 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
470 check_trace_icon public_trace_file
472 # Now with some other user, 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 }, :session => { :user => create(:user) }
474 check_trace_icon public_trace_file
476 # And finally we should be able to do it with the owner of the trace
477 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
478 check_trace_icon public_trace_file
481 # Check the icon for an anonymous trace can't be downloaded by another user
483 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
486 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
487 assert_response :forbidden
489 # Now with some other user, which shouldn't work since the trace is anon
490 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
491 assert_response :forbidden
493 # And finally we should be able to do it with the owner of the trace
494 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
495 check_trace_icon anon_trace_file
498 # Test downloading the icon for a trace that doesn't exist
499 def test_icon_not_found
500 deleted_trace_file = create(:trace, :deleted)
502 # First with a trace that has never existed
503 get :icon, :params => { :display_name => create(:user).display_name, :id => 0 }
504 assert_response :not_found
506 # Now with a trace that has been deleted
507 get :icon, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
508 assert_response :not_found
511 # Test fetching the new trace page
515 assert_response :redirect
516 assert_redirected_to :controller => :user, :action => :login, :referer => new_trace_path
518 # Now authenticated as a user with gps.trace.visibility set
520 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
521 get :new, :session => { :user => user }
522 assert_response :success
524 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
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 get :new, :session => { :user => second_user }
530 assert_response :success
532 assert_select "select#trace_visibility option[value=public][selected]", 1
534 # Now authenticated as a user with no preferences
535 third_user = create(:user)
536 get :new, :session => { :user => third_user }
537 assert_response :success
539 assert_select "select#trace_visibility option[value=private][selected]", 1
542 # Test creating a trace
545 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
546 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
550 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
551 assert_response :forbidden
557 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
558 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
559 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
560 assert_response :redirect
561 assert_redirected_to :action => :list, :display_name => user.display_name
562 assert_match /file has been uploaded/, flash[:notice]
563 trace = Trace.order(:id => :desc).first
564 assert_equal "a.gpx", trace.name
565 assert_equal "New Trace", trace.description
566 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
567 assert_equal "trackable", trace.visibility
568 assert_equal false, trace.inserted
569 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
571 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
574 # Test creating a trace with validation errors
575 def test_create_post_with_validation_errors
577 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
578 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
582 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
583 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
584 post :create, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
586 assert_match "Description is too short (minimum is 1 character)", response.body
589 # Test fetching the edit page for a trace using GET
591 public_trace_file = create(:trace, :visibility => "public")
592 deleted_trace_file = create(:trace, :deleted)
595 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
596 assert_response :redirect
597 assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)
599 # Now with some other user, which should fail
600 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
601 assert_response :forbidden
603 # Now with a trace which doesn't exist
604 get :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
605 assert_response :not_found
607 # Now with a trace which has been deleted
608 get :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
609 assert_response :not_found
611 # Finally with a trace that we are allowed to edit
612 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
613 assert_response :success
616 # Test fetching the edit page for a trace using POST
617 def test_edit_post_no_details
618 public_trace_file = create(:trace, :visibility => "public")
619 deleted_trace_file = create(:trace, :deleted)
622 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
623 assert_response :forbidden
625 # Now with some other user, which should fail
626 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
627 assert_response :forbidden
629 # Now with a trace which doesn't exist
630 post :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
631 assert_response :not_found
633 # Now with a trace which has been deleted
634 post :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
635 assert_response :not_found
637 # Finally with a trace that we are allowed to edit
638 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
639 assert_response :success
642 # Test saving edits to a trace
643 def test_edit_post_with_details
644 public_trace_file = create(:trace, :visibility => "public")
645 deleted_trace_file = create(:trace, :deleted)
648 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
651 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }
652 assert_response :forbidden
654 # Now with some other user, which should fail
655 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => create(:user) }
656 assert_response :forbidden
658 # Now with a trace which doesn't exist
659 post :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user), :trace => new_details }
660 assert_response :not_found
662 # Now with a trace which has been deleted
663 post :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id, :trace => new_details }, :session => { :user => deleted_trace_file.user }
664 assert_response :not_found
666 # Finally with a trace that we are allowed to edit
667 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => public_trace_file.user }
668 assert_response :redirect
669 assert_redirected_to :action => :view, :display_name => public_trace_file.user.display_name
670 trace = Trace.find(public_trace_file.id)
671 assert_equal new_details[:description], trace.description
672 assert_equal new_details[:tagstring], trace.tagstring
673 assert_equal new_details[:visibility], trace.visibility
676 # Test deleting a trace
678 public_trace_file = create(:trace, :visibility => "public")
679 deleted_trace_file = create(:trace, :deleted)
682 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
683 assert_response :forbidden
685 # Now with some other user, which should fail
686 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
687 assert_response :forbidden
689 # Now with a trace which doesn't exist
690 post :delete, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
691 assert_response :not_found
693 # Now with a trace has already been deleted
694 post :delete, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
695 assert_response :not_found
697 # Now with a trace that we are allowed to delete
698 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
699 assert_response :redirect
700 assert_redirected_to :action => :list, :display_name => public_trace_file.user.display_name
701 trace = Trace.find(public_trace_file.id)
702 assert_equal false, trace.visible
704 # Finally with a trace that is deleted by an admin
705 public_trace_file = create(:trace, :visibility => "public")
706 admin = create(:administrator_user)
708 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => admin }
709 assert_response :redirect
710 assert_redirected_to :action => :list, :display_name => public_trace_file.user.display_name
711 trace = Trace.find(public_trace_file.id)
712 assert_equal false, trace.visible
715 # Check getting a specific trace through the api
717 public_trace_file = create(:trace, :visibility => "public")
720 get :api_read, :params => { :id => public_trace_file.id }
721 assert_response :unauthorized
723 # Now with some other user, which should work since the trace is public
724 basic_authorization create(:user).display_name, "test"
725 get :api_read, :params => { :id => public_trace_file.id }
726 assert_response :success
728 # And finally we should be able to do it with the owner of the trace
729 basic_authorization public_trace_file.user.display_name, "test"
730 get :api_read, :params => { :id => public_trace_file.id }
731 assert_response :success
734 # Check an anoymous trace can't be specifically fetched by another user
735 def test_api_read_anon
736 anon_trace_file = create(:trace, :visibility => "private")
739 get :api_read, :params => { :id => anon_trace_file.id }
740 assert_response :unauthorized
742 # Now try with another user, which shouldn't work since the trace is anon
743 basic_authorization create(:user).display_name, "test"
744 get :api_read, :params => { :id => anon_trace_file.id }
745 assert_response :forbidden
747 # And finally we should be able to get the trace details with the trace owner
748 basic_authorization anon_trace_file.user.display_name, "test"
749 get :api_read, :params => { :id => anon_trace_file.id }
750 assert_response :success
753 # Check the api details for a trace that doesn't exist
754 def test_api_read_not_found
755 deleted_trace_file = create(:trace, :deleted)
757 # Try first with no auth, as it should require it
758 get :api_read, :params => { :id => 0 }
759 assert_response :unauthorized
761 # Login, and try again
762 basic_authorization deleted_trace_file.user.display_name, "test"
763 get :api_read, :params => { :id => 0 }
764 assert_response :not_found
766 # Now try a trace which did exist but has been deleted
767 basic_authorization deleted_trace_file.user.display_name, "test"
768 get :api_read, :params => { :id => deleted_trace_file.id }
769 assert_response :not_found
772 # Test downloading a trace through the api
774 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
777 get :api_data, :params => { :id => public_trace_file.id }
778 assert_response :unauthorized
780 # Now with some other user, which should work since the trace is public
781 basic_authorization create(:user).display_name, "test"
782 get :api_data, :params => { :id => public_trace_file.id }
783 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
785 # And finally we should be able to do it with the owner of the trace
786 basic_authorization public_trace_file.user.display_name, "test"
787 get :api_data, :params => { :id => public_trace_file.id }
788 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
791 # Test downloading a compressed trace through the api
792 def test_api_data_compressed
793 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
795 # Authenticate as the owner of the trace we will be using
796 basic_authorization identifiable_trace_file.user.display_name, "test"
798 # First get the data as is
799 get :api_data, :params => { :id => identifiable_trace_file.id }
800 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
802 # Now ask explicitly for XML format
803 get :api_data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
804 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
806 # Now ask explicitly for GPX format
807 get :api_data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
808 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
811 # Check an anonymous trace can't be downloaded by another user through the api
812 def test_api_data_anon
813 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
816 get :api_data, :params => { :id => anon_trace_file.id }
817 assert_response :unauthorized
819 # Now with some other user, which shouldn't work since the trace is anon
820 basic_authorization create(:user).display_name, "test"
821 get :api_data, :params => { :id => anon_trace_file.id }
822 assert_response :forbidden
824 # And finally we should be able to do it with the owner of the trace
825 basic_authorization anon_trace_file.user.display_name, "test"
826 get :api_data, :params => { :id => anon_trace_file.id }
827 check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
830 # Test downloading a trace that doesn't exist through the api
831 def test_api_data_not_found
832 deleted_trace_file = create(:trace, :deleted)
834 # Try first with no auth, as it should require it
835 get :api_data, :params => { :id => 0 }
836 assert_response :unauthorized
838 # Login, and try again
839 basic_authorization create(:user).display_name, "test"
840 get :api_data, :params => { :id => 0 }
841 assert_response :not_found
843 # Now try a trace which did exist but has been deleted
844 basic_authorization deleted_trace_file.user.display_name, "test"
845 get :api_data, :params => { :id => deleted_trace_file.id }
846 assert_response :not_found
849 # Test creating a trace through the api
852 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
853 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
857 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
858 assert_response :unauthorized
864 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
865 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
866 basic_authorization user.display_name, "test"
867 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
868 assert_response :success
869 trace = Trace.find(response.body.to_i)
870 assert_equal "a.gpx", trace.name
871 assert_equal "New Trace", trace.description
872 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
873 assert_equal "trackable", trace.visibility
874 assert_equal false, trace.inserted
875 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
877 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
882 # Now authenticated, with the legacy public flag
883 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
884 basic_authorization user.display_name, "test"
885 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }
886 assert_response :success
887 trace = Trace.find(response.body.to_i)
888 assert_equal "a.gpx", trace.name
889 assert_equal "New Trace", trace.description
890 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
891 assert_equal "public", trace.visibility
892 assert_equal false, trace.inserted
893 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
895 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
900 # Now authenticated, with the legacy private flag
901 second_user = create(:user)
902 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
903 basic_authorization second_user.display_name, "test"
904 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }
905 assert_response :success
906 trace = Trace.find(response.body.to_i)
907 assert_equal "a.gpx", trace.name
908 assert_equal "New Trace", trace.description
909 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
910 assert_equal "private", trace.visibility
911 assert_equal false, trace.inserted
912 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
914 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
917 # Check updating a trace through the api
919 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
920 deleted_trace_file = create(:trace, :deleted)
921 anon_trace_file = create(:trace, :visibility => "private")
924 content public_trace_file.to_xml
925 put :api_update, :params => { :id => public_trace_file.id }
926 assert_response :unauthorized
928 # Now with some other user, which should fail
929 basic_authorization create(:user).display_name, "test"
930 content public_trace_file.to_xml
931 put :api_update, :params => { :id => public_trace_file.id }
932 assert_response :forbidden
934 # Now with a trace which doesn't exist
935 basic_authorization create(:user).display_name, "test"
936 content public_trace_file.to_xml
937 put :api_update, :params => { :id => 0 }
938 assert_response :not_found
940 # Now with a trace which did exist but has been deleted
941 basic_authorization deleted_trace_file.user.display_name, "test"
942 content deleted_trace_file.to_xml
943 put :api_update, :params => { :id => deleted_trace_file.id }
944 assert_response :not_found
946 # Now try an update with the wrong ID
947 basic_authorization public_trace_file.user.display_name, "test"
948 content anon_trace_file.to_xml
949 put :api_update, :params => { :id => public_trace_file.id }
950 assert_response :bad_request,
951 "should not be able to update a trace with a different ID from the XML"
953 # And finally try an update that should work
954 basic_authorization public_trace_file.user.display_name, "test"
955 t = public_trace_file
956 t.description = "Changed description"
957 t.visibility = "private"
959 put :api_update, :params => { :id => t.id }
960 assert_response :success
961 nt = Trace.find(t.id)
962 assert_equal nt.description, t.description
963 assert_equal nt.visibility, t.visibility
966 # Test that updating a trace doesn't duplicate the tags
967 def test_api_update_tags
968 tracetag = create(:tracetag)
969 trace = tracetag.trace
970 basic_authorization trace.user.display_name, "test"
973 put :api_update, :params => { :id => trace.id }
974 assert_response :success
976 updated = Trace.find(trace.id)
977 # Ensure there's only one tag in the database after updating
978 assert_equal Tracetag.count, 1
979 # The new tag object might have a different id, so check the string representation
980 assert_equal trace.tagstring, updated.tagstring
983 # Check deleting a trace through the api
985 public_trace_file = create(:trace, :visibility => "public")
988 delete :api_delete, :params => { :id => public_trace_file.id }
989 assert_response :unauthorized
991 # Now with some other user, which should fail
992 basic_authorization create(:user).display_name, "test"
993 delete :api_delete, :params => { :id => public_trace_file.id }
994 assert_response :forbidden
996 # Now with a trace which doesn't exist
997 basic_authorization create(:user).display_name, "test"
998 delete :api_delete, :params => { :id => 0 }
999 assert_response :not_found
1001 # And finally we should be able to do it with the owner of the trace
1002 basic_authorization public_trace_file.user.display_name, "test"
1003 delete :api_delete, :params => { :id => public_trace_file.id }
1004 assert_response :success
1006 # Try it a second time, which should fail
1007 basic_authorization public_trace_file.user.display_name, "test"
1008 delete :api_delete, :params => { :id => public_trace_file.id }
1009 assert_response :not_found
1014 def check_trace_feed(traces)
1015 assert_response :success
1016 assert_template "georss"
1017 assert_equal "application/rss+xml", @response.content_type
1018 assert_select "rss", :count => 1 do
1019 assert_select "channel", :count => 1 do
1020 assert_select "title"
1021 assert_select "description"
1022 assert_select "link"
1023 assert_select "image"
1024 assert_select "item", :count => traces.visible.count do |items|
1025 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
1026 assert_select item, "title", trace.name
1027 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
1028 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
1029 assert_select item, "description"
1030 # assert_select item, "dc:creator", trace.user.display_name
1031 assert_select item, "pubDate", trace.timestamp.rfc822
1038 def check_trace_list(traces)
1039 assert_response :success
1040 assert_template "list"
1043 assert_select "table#trace_list tbody", :count => 1 do
1044 assert_select "tr", :count => traces.length do |rows|
1045 traces.zip(rows).each do |trace, row|
1046 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
1047 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
1048 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
1049 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
1054 assert_select "h4", /Nothing here yet/
1058 def check_trace_view(trace)
1059 assert_response :success
1060 assert_template "view"
1062 assert_select "table", :count => 1 do
1063 assert_select "td", /^#{Regexp.quote(trace.name)} /
1064 assert_select "td", trace.user.display_name
1065 assert_select "td", trace.description
1069 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
1070 assert_response :success
1071 assert_equal digest, Digest::MD5.hexdigest(response.body)
1072 assert_equal content_type, response.content_type
1073 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
1076 def check_trace_picture(trace)
1077 assert_response :success
1078 assert_equal "image/gif", response.content_type
1079 assert_equal trace.large_picture, response.body
1082 def check_trace_icon(trace)
1083 assert_response :success
1084 assert_equal "image/gif", response.content_type
1085 assert_equal trace.icon_picture, response.body