2 require "minitest/mock"
4 class TraceControllerTest < ActionController::TestCase
8 @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
9 Object.const_set("GPX_TRACE_DIR", Rails.root.join("test", "gpx", "traces"))
11 @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
12 Object.const_set("GPX_IMAGE_DIR", Rails.root.join("test", "gpx", "images"))
16 File.unlink(*Dir.glob(File.join(GPX_TRACE_DIR, "*.gpx")))
17 File.unlink(*Dir.glob(File.join(GPX_IMAGE_DIR, "*.gif")))
19 Object.send("remove_const", "GPX_TRACE_DIR")
20 Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
22 Object.send("remove_const", "GPX_IMAGE_DIR")
23 Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
27 # test all routes which lead to this controller
30 { :path => "/api/0.6/gpx/create", :method => :post },
31 { :controller => "trace", :action => "api_create" }
34 { :path => "/api/0.6/gpx/1", :method => :get },
35 { :controller => "trace", :action => "api_read", :id => "1" }
38 { :path => "/api/0.6/gpx/1", :method => :put },
39 { :controller => "trace", :action => "api_update", :id => "1" }
42 { :path => "/api/0.6/gpx/1", :method => :delete },
43 { :controller => "trace", :action => "api_delete", :id => "1" }
46 { :controller => "trace", :action => "api_read", :id => "1" },
47 { :path => "/api/0.6/gpx/1/details", :method => :get }
50 { :path => "/api/0.6/gpx/1/data", :method => :get },
51 { :controller => "trace", :action => "api_data", :id => "1" }
54 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
55 { :controller => "trace", :action => "api_data", :id => "1", :format => "xml" }
59 { :path => "/traces", :method => :get },
60 { :controller => "trace", :action => "list" }
63 { :path => "/traces/page/1", :method => :get },
64 { :controller => "trace", :action => "list", :page => "1" }
67 { :path => "/traces/tag/tagname", :method => :get },
68 { :controller => "trace", :action => "list", :tag => "tagname" }
71 { :path => "/traces/tag/tagname/page/1", :method => :get },
72 { :controller => "trace", :action => "list", :tag => "tagname", :page => "1" }
75 { :path => "/user/username/traces", :method => :get },
76 { :controller => "trace", :action => "list", :display_name => "username" }
79 { :path => "/user/username/traces/page/1", :method => :get },
80 { :controller => "trace", :action => "list", :display_name => "username", :page => "1" }
83 { :path => "/user/username/traces/tag/tagname", :method => :get },
84 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname" }
87 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
88 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname", :page => "1" }
92 { :path => "/traces/mine", :method => :get },
93 { :controller => "trace", :action => "mine" }
96 { :path => "/traces/mine/page/1", :method => :get },
97 { :controller => "trace", :action => "mine", :page => "1" }
100 { :path => "/traces/mine/tag/tagname", :method => :get },
101 { :controller => "trace", :action => "mine", :tag => "tagname" }
104 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
105 { :controller => "trace", :action => "mine", :tag => "tagname", :page => "1" }
109 { :path => "/traces/rss", :method => :get },
110 { :controller => "trace", :action => "georss", :format => :rss }
113 { :path => "/traces/tag/tagname/rss", :method => :get },
114 { :controller => "trace", :action => "georss", :tag => "tagname", :format => :rss }
117 { :path => "/user/username/traces/rss", :method => :get },
118 { :controller => "trace", :action => "georss", :display_name => "username", :format => :rss }
121 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
122 { :controller => "trace", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
126 { :path => "/user/username/traces/1", :method => :get },
127 { :controller => "trace", :action => "view", :display_name => "username", :id => "1" }
130 { :path => "/user/username/traces/1/picture", :method => :get },
131 { :controller => "trace", :action => "picture", :display_name => "username", :id => "1" }
134 { :path => "/user/username/traces/1/icon", :method => :get },
135 { :controller => "trace", :action => "icon", :display_name => "username", :id => "1" }
139 { :path => "/trace/create", :method => :get },
140 { :controller => "trace", :action => "create" }
143 { :path => "/trace/create", :method => :post },
144 { :controller => "trace", :action => "create" }
147 { :path => "/trace/1/data", :method => :get },
148 { :controller => "trace", :action => "data", :id => "1" }
151 { :path => "/trace/1/data.xml", :method => :get },
152 { :controller => "trace", :action => "data", :id => "1", :format => "xml" }
155 { :path => "/trace/1/edit", :method => :get },
156 { :controller => "trace", :action => "edit", :id => "1" }
159 { :path => "/trace/1/edit", :method => :post },
160 { :controller => "trace", :action => "edit", :id => "1" }
163 { :path => "/trace/1/edit", :method => :patch },
164 { :controller => "trace", :action => "edit", :id => "1" }
167 { :path => "/trace/1/delete", :method => :post },
168 { :controller => "trace", :action => "delete", :id => "1" }
172 # Check that the list of traces is displayed
174 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
175 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
176 create(:tracetag, :trace => trace, :tag => "London")
178 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
179 create(:tracetag, :trace => trace, :tag => "Birmingham")
181 trace_c = create(:trace, :visibility => "private", :user => users(:public_user), :timestamp => 2.seconds.ago) do |trace|
182 create(:tracetag, :trace => trace, :tag => "London")
184 trace_d = create(:trace, :visibility => "private", :user => users(:public_user), :timestamp => 1.second.ago) do |trace|
185 create(:tracetag, :trace => trace, :tag => "Birmingham")
188 # First with the public list
190 check_trace_list [trace_b, trace_a]
192 # Restrict traces to those with a given tag
193 get :list, :tag => "London"
194 check_trace_list [trace_a]
196 # Should see more when we are logged in
197 get :list, {}, { :user => users(:public_user) }
198 check_trace_list [trace_d, trace_c, trace_b, trace_a]
200 # Again, we should see more when we are logged in
201 get :list, { :tag => "London" }, { :user => users(:public_user) }
202 check_trace_list [trace_c, trace_a]
205 # Check that I can get mine
207 create(:trace, :visibility => "public") do |trace|
208 create(:tracetag, :trace => trace, :tag => "Birmingham")
210 trace_b = create(:trace, :visibility => "private", :user => users(:public_user)) do |trace|
211 create(:tracetag, :trace => trace, :tag => "London")
214 # First try to get it when not logged in
216 assert_redirected_to :controller => "user", :action => "login", :referer => "/traces/mine"
218 # Now try when logged in
219 get :mine, {}, { :user => users(:public_user) }
220 assert_redirected_to :controller => "trace", :action => "list", :display_name => users(:public_user).display_name
222 # Fetch the actual list
223 get :list, { :display_name => users(:public_user).display_name }, { :user => users(:public_user) }
224 check_trace_list [trace_b]
227 # Check the list of traces for a specific user
230 trace_b = create(:trace, :visibility => "public", :user => users(:public_user))
231 trace_c = create(:trace, :visibility => "private", :user => users(:public_user)) do |trace|
232 create(:tracetag, :trace => trace, :tag => "London")
235 # Test a user with no traces
236 get :list, :display_name => users(:second_public_user).display_name
239 # Test a user with some traces - should see only public ones
240 get :list, :display_name => users(:public_user).display_name
241 check_trace_list [trace_b]
243 # Should still see only public ones when authenticated as another user
244 get :list, { :display_name => users(:public_user).display_name }, { :user => users(:normal_user) }
245 check_trace_list [trace_b]
247 # Should see all traces when authenticated as the target user
248 get :list, { :display_name => users(:public_user).display_name }, { :user => users(:public_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, { :display_name => users(:public_user).display_name, :tag => "London" }, { :user => users(:public_user) }
253 check_trace_list [trace_c]
255 # Should get an error if the user does not exist
256 get :list, :display_name => "UnknownUser"
257 assert_response :not_found
258 assert_template "user/no_such_user"
261 # Check that the rss loads
263 # First with the public feed
264 get :georss, :format => :rss
265 check_trace_feed Trace.visible_to_all
267 # Restrict traces to those with a given tag
268 get :georss, :tag => "London", :format => :rss
269 check_trace_feed Trace.tagged("London").visible_to_all
271 # Restrict traces to those for a given user
272 get :georss, :display_name => users(:public_user).display_name, :format => :rss
273 check_trace_feed users(:public_user).traces.visible_to_all
275 # Restrict traces to those for a given user with a tiven tag
276 get :georss, :display_name => users(:public_user).display_name, :tag => "Birmingham", :format => :rss
277 check_trace_feed users(:public_user).traces.tagged("Birmingham").visible_to_all
280 # Test viewing a trace
282 public_trace_file = create(:trace, :visibility => "public")
284 # First with no auth, which should work since the trace is public
285 get :view, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
286 check_trace_view public_trace_file
288 # Now with some other user, which should work since the trace is public
289 get :view, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user) }
290 check_trace_view public_trace_file
292 # And finally we should be able to do it with the owner of the trace
293 get :view, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user) }
294 check_trace_view public_trace_file
297 # Check an anonymous trace can't be viewed by another user
299 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
302 get :view, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
303 assert_response :redirect
304 assert_redirected_to :action => :list
306 # Now with some other user, which should not work since the trace is anon
307 get :view, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user) }
308 assert_response :redirect
309 assert_redirected_to :action => :list
311 # And finally we should be able to do it with the owner of the trace
312 get :view, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user) }
313 check_trace_view anon_trace_file
316 # Test viewing a trace that doesn't exist
317 def test_view_not_found
318 deleted_trace_file = create(:trace, :deleted)
321 get :view, :display_name => users(:public_user).display_name, :id => 0
322 assert_response :redirect
323 assert_redirected_to :action => :list
325 # Now with some other user
326 get :view, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user) }
327 assert_response :redirect
328 assert_redirected_to :action => :list
330 # And finally we should not be able to view a deleted trace
331 get :view, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user) }
332 assert_response :redirect
333 assert_redirected_to :action => :list
336 # Test downloading a trace
338 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user), :fixture => "a")
340 # First with no auth, which should work since the trace is public
341 get :data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
342 check_trace_data public_trace_file
344 # Now with some other user, which should work since the trace is public
345 get :data, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user) }
346 check_trace_data public_trace_file
348 # And finally we should be able to do it with the owner of the trace
349 get :data, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user) }
350 check_trace_data public_trace_file
353 # Test downloading a compressed trace
354 def test_data_compressed
355 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
357 # First get the data as is
358 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id
359 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
361 # Now ask explicitly for XML format
362 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "xml"
363 check_trace_data identifiable_trace_file, "application/xml", "xml"
365 # Now ask explicitly for GPX format
366 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "gpx"
367 check_trace_data identifiable_trace_file
370 # Check an anonymous trace can't be downloaded by another user
372 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user), :fixture => "b")
375 get :data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
376 assert_response :not_found
378 # Now with some other user, which shouldn't work since the trace is anon
379 get :data, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user) }
380 assert_response :not_found
382 # And finally we should be able to do it with the owner of the trace
383 get :data, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user) }
384 check_trace_data anon_trace_file
387 # Test downloading a trace that doesn't exist
388 def test_data_not_found
389 deleted_trace_file = create(:trace, :deleted)
391 # First with no auth and a trace that has never existed
392 get :data, :display_name => users(:public_user).display_name, :id => 0
393 assert_response :not_found
395 # Now with a trace that has never existed
396 get :data, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user) }
397 assert_response :not_found
399 # Now with a trace that has been deleted
400 get :data, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user) }
401 assert_response :not_found
404 # Test downloading the picture for a trace
406 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user), :fixture => "a")
408 # First with no auth, which should work since the trace is public
409 get :picture, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
410 check_trace_picture public_trace_file
412 # Now with some other user, which should work since the trace is public
413 get :picture, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user) }
414 check_trace_picture public_trace_file
416 # And finally we should be able to do it with the owner of the trace
417 get :picture, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user) }
418 check_trace_picture public_trace_file
421 # Check the picture for an anonymous trace can't be downloaded by another user
422 def test_picture_anon
423 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user), :fixture => "b")
426 get :picture, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
427 assert_response :forbidden
429 # Now with some other user, which shouldn't work since the trace is anon
430 get :picture, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user) }
431 assert_response :forbidden
433 # And finally we should be able to do it with the owner of the trace
434 get :picture, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user) }
435 check_trace_picture anon_trace_file
438 # Test downloading the picture for a trace that doesn't exist
439 def test_picture_not_found
440 # First with no auth, which should work since the trace is public
441 get :picture, :display_name => users(:public_user).display_name, :id => 0
442 assert_response :not_found
444 # Now with some other user, which should work since the trace is public
445 get :picture, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user) }
446 assert_response :not_found
448 # And finally we should not be able to do it with a deleted trace
449 deleted_trace_file = create(:trace, :deleted)
450 get :picture, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user) }
451 assert_response :not_found
454 # Test downloading the icon for a trace
456 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user), :fixture => "a")
458 # First with no auth, which should work since the trace is public
459 get :icon, :display_name => users(:normal_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, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_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, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_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", :user => users(:public_user), :fixture => "b")
476 get :icon, :display_name => users(:public_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, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user) }
481 assert_response :forbidden
483 # And finally we should be able to do it with the owner of the trace
484 get :icon, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_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
491 get :icon, :display_name => users(:public_user).display_name, :id => 0
492 assert_response :not_found
494 # Now with some other user
495 get :icon, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user) }
496 assert_response :not_found
498 # And finally we should not be able to do it with a deleted trace
499 deleted_trace_file = create(:trace, :deleted)
500 get :icon, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user) }
501 assert_response :not_found
504 # Test fetching the create page
508 assert_response :redirect
509 assert_redirected_to :controller => :user, :action => :login, :referer => trace_create_path
511 # Now authenticated as a user with gps.trace.visibility set
512 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
513 get :create, {}, { :user => users(:public_user) }
514 assert_response :success
515 assert_template :create
516 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
518 # Now authenticated as a user with gps.trace.public set
519 create(:user_preference, :user => users(:second_public_user), :k => "gps.trace.public", :v => "default")
520 get :create, {}, { :user => users(:second_public_user) }
521 assert_response :success
522 assert_template :create
523 assert_select "select#trace_visibility option[value=public][selected]", 1
525 # Now authenticated as a user with no preferences
526 get :create, {}, { :user => users(:normal_user) }
527 assert_response :success
528 assert_template :create
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")
539 post :create, :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" }
540 assert_response :forbidden
543 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
544 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
545 post :create, { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, { :user => users(:public_user) }
546 assert_response :redirect
547 assert_redirected_to :action => :list, :display_name => users(:public_user).display_name
548 assert_match /file has been uploaded/, flash[:notice]
549 trace = Trace.order(:id => :desc).first
550 assert_equal "a.gpx", trace.name
551 assert_equal "New Trace", trace.description
552 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
553 assert_equal "trackable", trace.visibility
554 assert_equal false, trace.inserted
555 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
557 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
560 # Test fetching the edit page for a trace using GET
562 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
563 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
566 get :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
567 assert_response :redirect
568 assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => users(:normal_user).display_name, :id => public_trace_file.id)
570 # Now with some other user, which should fail
571 get :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user) }
572 assert_response :forbidden
574 # Now with a trace which doesn't exist
575 get :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user) }
576 assert_response :not_found
578 # Now with a trace which has been deleted
579 get :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user) }
580 assert_response :not_found
582 # Finally with a trace that we are allowed to edit
583 get :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user) }
584 assert_response :success
587 # Test fetching the edit page for a trace using POST
588 def test_edit_post_no_details
589 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
590 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
593 post :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
594 assert_response :forbidden
596 # Now with some other user, which should fail
597 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user) }
598 assert_response :forbidden
600 # Now with a trace which doesn't exist
601 post :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user) }
602 assert_response :not_found
604 # Now with a trace which has been deleted
605 post :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user) }
606 assert_response :not_found
608 # Finally with a trace that we are allowed to edit
609 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user) }
610 assert_response :success
613 # Test saving edits to a trace
614 def test_edit_post_with_details
615 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
616 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
619 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
622 post :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details
623 assert_response :forbidden
625 # Now with some other user, which should fail
626 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:public_user) }
627 assert_response :forbidden
629 # Now with a trace which doesn't exist
630 post :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user), :trace => new_details }
631 assert_response :not_found
633 # Now with a trace which has been deleted
634 post :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id, :trace => new_details }, { :user => users(:public_user) }
635 assert_response :not_found
637 # Finally with a trace that we are allowed to edit
638 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:normal_user) }
639 assert_response :redirect
640 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
641 trace = Trace.find(public_trace_file.id)
642 assert_equal new_details[:description], trace.description
643 assert_equal new_details[:tagstring], trace.tagstring
644 assert_equal new_details[:visibility], trace.visibility
647 # Test deleting a trace
649 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
650 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
653 post :delete, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
654 assert_response :forbidden
656 # Now with some other user, which should fail
657 post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user) }
658 assert_response :forbidden
660 # Now with a trace which doesn't exist
661 post :delete, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user) }
662 assert_response :not_found
664 # Now with a trace has already been deleted
665 post :delete, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user) }
666 assert_response :not_found
668 # Finally with a trace that we are allowed to delete
669 post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user) }
670 assert_response :redirect
671 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
672 trace = Trace.find(public_trace_file.id)
673 assert_equal false, trace.visible
676 # Check getting a specific trace through the api
678 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
681 get :api_read, :id => public_trace_file.id
682 assert_response :unauthorized
684 # Now with some other user, which should work since the trace is public
685 basic_authorization(users(:public_user).display_name, "test")
686 get :api_read, :id => public_trace_file.id
687 assert_response :success
689 # And finally we should be able to do it with the owner of the trace
690 basic_authorization(users(:normal_user).display_name, "test")
691 get :api_read, :id => public_trace_file.id
692 assert_response :success
695 # Check an anoymous trace can't be specifically fetched by another user
696 def test_api_read_anon
697 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
700 get :api_read, :id => anon_trace_file.id
701 assert_response :unauthorized
703 # Now try with another user, which shouldn't work since the trace is anon
704 basic_authorization(users(:normal_user).display_name, "test")
705 get :api_read, :id => anon_trace_file.id
706 assert_response :forbidden
708 # And finally we should be able to get the trace details with the trace owner
709 basic_authorization(users(:public_user).display_name, "test")
710 get :api_read, :id => anon_trace_file.id
711 assert_response :success
714 # Check the api details for a trace that doesn't exist
715 def test_api_read_not_found
716 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
718 # Try first with no auth, as it should require it
719 get :api_read, :id => 0
720 assert_response :unauthorized
722 # Login, and try again
723 basic_authorization(users(:public_user).display_name, "test")
724 get :api_read, :id => 0
725 assert_response :not_found
727 # Now try a trace which did exist but has been deleted
728 basic_authorization(users(:public_user).display_name, "test")
729 get :api_read, :id => deleted_trace_file.id
730 assert_response :not_found
733 # Test downloading a trace through the api
735 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user), :fixture => "a")
738 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
739 assert_response :unauthorized
741 # Now with some other user, which should work since the trace is public
742 basic_authorization(users(:public_user).display_name, "test")
743 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
744 check_trace_data public_trace_file
746 # And finally we should be able to do it with the owner of the trace
747 basic_authorization(users(:normal_user).display_name, "test")
748 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
749 check_trace_data public_trace_file
752 # Test downloading a compressed trace through the api
753 def test_api_data_compressed
754 identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => users(:public_user), :fixture => "d")
756 # Authenticate as the owner of the trace we will be using
757 basic_authorization(users(:public_user).display_name, "test")
759 # First get the data as is
760 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id
761 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
763 # Now ask explicitly for XML format
764 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "xml"
765 check_trace_data identifiable_trace_file, "application/xml", "xml"
767 # Now ask explicitly for GPX format
768 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "gpx"
769 check_trace_data identifiable_trace_file
772 # Check an anonymous trace can't be downloaded by another user through the api
773 def test_api_data_anon
774 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user), :fixture => "b")
777 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
778 assert_response :unauthorized
780 # Now with some other user, which shouldn't work since the trace is anon
781 basic_authorization(users(:normal_user).display_name, "test")
782 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
783 assert_response :forbidden
785 # And finally we should be able to do it with the owner of the trace
786 basic_authorization(users(:public_user).display_name, "test")
787 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
788 check_trace_data anon_trace_file
791 # Test downloading a trace that doesn't exist through the api
792 def test_api_data_not_found
794 get :api_data, :display_name => users(:public_user).display_name, :id => 0
795 assert_response :unauthorized
797 # Now with a trace that has never existed
798 basic_authorization(users(:public_user).display_name, "test")
799 get :api_data, :display_name => users(:public_user).display_name, :id => 0
800 assert_response :not_found
802 # Now with a trace that has been deleted
803 deleted_trace_file = create(:trace, :deleted)
804 basic_authorization(users(:public_user).display_name, "test")
805 get :api_data, :display_name => users(:public_user).display_name, :id => deleted_trace_file.id
806 assert_response :not_found
809 # Test creating a trace through the api
812 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
813 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
816 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
817 assert_response :unauthorized
820 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
821 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
822 basic_authorization(users(:public_user).display_name, "test")
823 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
824 assert_response :success
825 trace = Trace.find(response.body.to_i)
826 assert_equal "a.gpx", trace.name
827 assert_equal "New Trace", trace.description
828 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
829 assert_equal "trackable", trace.visibility
830 assert_equal false, trace.inserted
831 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
833 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
838 # Now authenticated, with the legacy public flag
839 assert_not_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
840 basic_authorization(users(:public_user).display_name, "test")
841 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 1
842 assert_response :success
843 trace = Trace.find(response.body.to_i)
844 assert_equal "a.gpx", trace.name
845 assert_equal "New Trace", trace.description
846 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
847 assert_equal "public", trace.visibility
848 assert_equal false, trace.inserted
849 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
851 assert_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
856 # Now authenticated, with the legacy private flag
857 assert_nil users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first
858 basic_authorization(users(:second_public_user).display_name, "test")
859 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 0
860 assert_response :success
861 trace = Trace.find(response.body.to_i)
862 assert_equal "a.gpx", trace.name
863 assert_equal "New Trace", trace.description
864 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
865 assert_equal "private", trace.visibility
866 assert_equal false, trace.inserted
867 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
869 assert_equal "private", users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first.v
872 # Check updating a trace through the api
874 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user), :fixture => "a")
875 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
876 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
879 content public_trace_file.to_xml
880 put :api_update, :id => public_trace_file.id
881 assert_response :unauthorized
883 # Now with some other user, which should fail
884 basic_authorization(users(:public_user).display_name, "test")
885 content public_trace_file.to_xml
886 put :api_update, :id => public_trace_file.id
887 assert_response :forbidden
889 # Now with a trace which doesn't exist
890 basic_authorization(users(:public_user).display_name, "test")
891 content public_trace_file.to_xml
892 put :api_update, :id => 0
893 assert_response :not_found
895 # Now with a trace which did exist but has been deleted
896 basic_authorization(users(:public_user).display_name, "test")
897 content deleted_trace_file.to_xml
898 put :api_update, :id => deleted_trace_file.id
899 assert_response :not_found
901 # Now try an update with the wrong ID
902 basic_authorization(users(:normal_user).display_name, "test")
903 content anon_trace_file.to_xml
904 put :api_update, :id => public_trace_file.id
905 assert_response :bad_request,
906 "should not be able to update a trace with a different ID from the XML"
908 # And finally try an update that should work
909 basic_authorization(users(:normal_user).display_name, "test")
910 t = public_trace_file
911 t.description = "Changed description"
912 t.visibility = "private"
914 put :api_update, :id => t.id
915 assert_response :success
916 nt = Trace.find(t.id)
917 assert_equal nt.description, t.description
918 assert_equal nt.visibility, t.visibility
921 # Check deleting a trace through the api
923 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
926 delete :api_delete, :id => public_trace_file.id
927 assert_response :unauthorized
929 # Now with some other user, which should fail
930 basic_authorization(users(:public_user).display_name, "test")
931 delete :api_delete, :id => public_trace_file.id
932 assert_response :forbidden
934 # Now with a trace which doesn't exist
935 basic_authorization(users(:public_user).display_name, "test")
936 delete :api_delete, :id => 0
937 assert_response :not_found
939 # And finally we should be able to do it with the owner of the trace
940 basic_authorization(users(:normal_user).display_name, "test")
941 delete :api_delete, :id => public_trace_file.id
942 assert_response :success
944 # Try it a second time, which should fail
945 basic_authorization(users(:normal_user).display_name, "test")
946 delete :api_delete, :id => public_trace_file.id
947 assert_response :not_found
952 def check_trace_feed(traces)
953 assert_response :success
954 assert_template "georss"
955 assert_equal "application/rss+xml", @response.content_type
956 assert_select "rss", :count => 1 do
957 assert_select "channel", :count => 1 do
958 assert_select "title"
959 assert_select "description"
961 assert_select "image"
962 assert_select "item", :count => traces.visible.count do |items|
963 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
964 assert_select item, "title", trace.name
965 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
966 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
967 assert_select item, "description"
968 # assert_select item, "dc:creator", trace.user.display_name
969 assert_select item, "pubDate", trace.timestamp.rfc822
976 def check_trace_list(traces)
977 assert_response :success
978 assert_template "list"
981 assert_select "table#trace_list tbody", :count => 1 do
982 assert_select "tr", :count => traces.length do |rows|
983 traces.zip(rows).each do |trace, row|
984 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
985 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
986 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
987 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
992 assert_select "h4", /Nothing here yet/
996 def check_trace_view(trace)
997 assert_response :success
998 assert_template "view"
1000 assert_select "table", :count => 1 do
1001 assert_select "td", /^#{Regexp.quote(trace.name)} /
1002 assert_select "td", trace.user.display_name
1003 assert_select "td", trace.description
1007 def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
1008 assert_response :success
1009 assert_equal content_type, response.content_type
1010 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
1013 def check_trace_picture(trace)
1014 assert_response :success
1015 assert_equal "image/gif", response.content_type
1016 assert_equal trace.large_picture, response.body
1019 def check_trace_icon(trace)
1020 assert_response :success
1021 assert_equal "image/gif", response.content_type
1022 assert_equal trace.icon_picture, response.body