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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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
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).id }
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).id }
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).id }
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).id }
584 assert_response :success
587 # Test saving edits to a trace
589 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
590 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
592 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
595 post :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details
596 assert_response :forbidden
598 # Now with some other user, which should fail
599 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:public_user).id }
600 assert_response :forbidden
602 # Now with a trace which doesn't exist
603 post :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id, :trace => new_details }
604 assert_response :not_found
606 # Now with a trace which has been deleted
607 post :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id, :trace => new_details }, { :user => users(:public_user).id }
608 assert_response :not_found
610 # Finally with a trace that we are allowed to edit
611 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:normal_user).id }
612 assert_response :redirect
613 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
614 trace = Trace.find(public_trace_file.id)
615 assert_equal new_details[:description], trace.description
616 assert_equal new_details[:tagstring], trace.tagstring
617 assert_equal new_details[:visibility], trace.visibility
620 # Test deleting a trace
622 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
623 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
626 post :delete, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
627 assert_response :forbidden
629 # Now with some other user, which should fail
630 post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
631 assert_response :forbidden
633 # Now with a trace which doesn't exist
634 post :delete, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
635 assert_response :not_found
637 # Now with a trace has already been deleted
638 post :delete, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
639 assert_response :not_found
641 # Finally with a trace that we are allowed to delete
642 post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
643 assert_response :redirect
644 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
645 trace = Trace.find(public_trace_file.id)
646 assert_equal false, trace.visible
649 # Check getting a specific trace through the api
651 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
654 get :api_read, :id => public_trace_file.id
655 assert_response :unauthorized
657 # Now with some other user, which should work since the trace is public
658 basic_authorization(users(:public_user).display_name, "test")
659 get :api_read, :id => public_trace_file.id
660 assert_response :success
662 # And finally we should be able to do it with the owner of the trace
663 basic_authorization(users(:normal_user).display_name, "test")
664 get :api_read, :id => public_trace_file.id
665 assert_response :success
668 # Check an anoymous trace can't be specifically fetched by another user
669 def test_api_read_anon
670 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
673 get :api_read, :id => anon_trace_file.id
674 assert_response :unauthorized
676 # Now try with another user, which shouldn't work since the trace is anon
677 basic_authorization(users(:normal_user).display_name, "test")
678 get :api_read, :id => anon_trace_file.id
679 assert_response :forbidden
681 # And finally we should be able to get the trace details with the trace owner
682 basic_authorization(users(:public_user).display_name, "test")
683 get :api_read, :id => anon_trace_file.id
684 assert_response :success
687 # Check the api details for a trace that doesn't exist
688 def test_api_read_not_found
689 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
691 # Try first with no auth, as it should require it
692 get :api_read, :id => 0
693 assert_response :unauthorized
695 # Login, and try again
696 basic_authorization(users(:public_user).display_name, "test")
697 get :api_read, :id => 0
698 assert_response :not_found
700 # Now try a trace which did exist but has been deleted
701 basic_authorization(users(:public_user).display_name, "test")
702 get :api_read, :id => deleted_trace_file.id
703 assert_response :not_found
706 # Test downloading a trace through the api
708 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user), :fixture => "a")
711 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
712 assert_response :unauthorized
714 # Now with some other user, which should work since the trace is public
715 basic_authorization(users(:public_user).display_name, "test")
716 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
717 check_trace_data public_trace_file
719 # And finally we should be able to do it with the owner of the trace
720 basic_authorization(users(:normal_user).display_name, "test")
721 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
722 check_trace_data public_trace_file
725 # Test downloading a compressed trace through the api
726 def test_api_data_compressed
727 identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => users(:public_user), :fixture => "d")
729 # Authenticate as the owner of the trace we will be using
730 basic_authorization(users(:public_user).display_name, "test")
732 # First get the data as is
733 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id
734 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
736 # Now ask explicitly for XML format
737 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "xml"
738 check_trace_data identifiable_trace_file, "application/xml", "xml"
740 # Now ask explicitly for GPX format
741 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "gpx"
742 check_trace_data identifiable_trace_file
745 # Check an anonymous trace can't be downloaded by another user through the api
746 def test_api_data_anon
747 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user), :fixture => "b")
750 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
751 assert_response :unauthorized
753 # Now with some other user, which shouldn't work since the trace is anon
754 basic_authorization(users(:normal_user).display_name, "test")
755 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
756 assert_response :forbidden
758 # And finally we should be able to do it with the owner of the trace
759 basic_authorization(users(:public_user).display_name, "test")
760 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
761 check_trace_data anon_trace_file
764 # Test downloading a trace that doesn't exist through the api
765 def test_api_data_not_found
767 get :api_data, :display_name => users(:public_user).display_name, :id => 0
768 assert_response :unauthorized
770 # Now with a trace that has never existed
771 basic_authorization(users(:public_user).display_name, "test")
772 get :api_data, :display_name => users(:public_user).display_name, :id => 0
773 assert_response :not_found
775 # Now with a trace that has been deleted
776 deleted_trace_file = create(:trace, :deleted)
777 basic_authorization(users(:public_user).display_name, "test")
778 get :api_data, :display_name => users(:public_user).display_name, :id => deleted_trace_file.id
779 assert_response :not_found
782 # Test creating a trace through the api
785 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
786 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
789 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
790 assert_response :unauthorized
793 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
794 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
795 basic_authorization(users(:public_user).display_name, "test")
796 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
797 assert_response :success
798 trace = Trace.find(response.body.to_i)
799 assert_equal "a.gpx", trace.name
800 assert_equal "New Trace", trace.description
801 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
802 assert_equal "trackable", trace.visibility
803 assert_equal false, trace.inserted
804 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
806 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
811 # Now authenticated, with the legacy public flag
812 assert_not_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
813 basic_authorization(users(:public_user).display_name, "test")
814 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 1
815 assert_response :success
816 trace = Trace.find(response.body.to_i)
817 assert_equal "a.gpx", trace.name
818 assert_equal "New Trace", trace.description
819 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
820 assert_equal "public", trace.visibility
821 assert_equal false, trace.inserted
822 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
824 assert_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
829 # Now authenticated, with the legacy private flag
830 assert_nil users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first
831 basic_authorization(users(:second_public_user).display_name, "test")
832 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 0
833 assert_response :success
834 trace = Trace.find(response.body.to_i)
835 assert_equal "a.gpx", trace.name
836 assert_equal "New Trace", trace.description
837 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
838 assert_equal "private", trace.visibility
839 assert_equal false, trace.inserted
840 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
842 assert_equal "private", users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first.v
845 # Check updating a trace through the api
847 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user), :fixture => "a")
848 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
849 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
852 content public_trace_file.to_xml
853 put :api_update, :id => public_trace_file.id
854 assert_response :unauthorized
856 # Now with some other user, which should fail
857 basic_authorization(users(:public_user).display_name, "test")
858 content public_trace_file.to_xml
859 put :api_update, :id => public_trace_file.id
860 assert_response :forbidden
862 # Now with a trace which doesn't exist
863 basic_authorization(users(:public_user).display_name, "test")
864 content public_trace_file.to_xml
865 put :api_update, :id => 0
866 assert_response :not_found
868 # Now with a trace which did exist but has been deleted
869 basic_authorization(users(:public_user).display_name, "test")
870 content deleted_trace_file.to_xml
871 put :api_update, :id => deleted_trace_file.id
872 assert_response :not_found
874 # Now try an update with the wrong ID
875 basic_authorization(users(:normal_user).display_name, "test")
876 content anon_trace_file.to_xml
877 put :api_update, :id => public_trace_file.id
878 assert_response :bad_request,
879 "should not be able to update a trace with a different ID from the XML"
881 # And finally try an update that should work
882 basic_authorization(users(:normal_user).display_name, "test")
883 t = public_trace_file
884 t.description = "Changed description"
885 t.visibility = "private"
887 put :api_update, :id => t.id
888 assert_response :success
889 nt = Trace.find(t.id)
890 assert_equal nt.description, t.description
891 assert_equal nt.visibility, t.visibility
894 # Check deleting a trace through the api
896 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
899 delete :api_delete, :id => public_trace_file.id
900 assert_response :unauthorized
902 # Now with some other user, which should fail
903 basic_authorization(users(:public_user).display_name, "test")
904 delete :api_delete, :id => public_trace_file.id
905 assert_response :forbidden
907 # Now with a trace which doesn't exist
908 basic_authorization(users(:public_user).display_name, "test")
909 delete :api_delete, :id => 0
910 assert_response :not_found
912 # And finally we should be able to do it with the owner of the trace
913 basic_authorization(users(:normal_user).display_name, "test")
914 delete :api_delete, :id => public_trace_file.id
915 assert_response :success
917 # Try it a second time, which should fail
918 basic_authorization(users(:normal_user).display_name, "test")
919 delete :api_delete, :id => public_trace_file.id
920 assert_response :not_found
925 def check_trace_feed(traces)
926 assert_response :success
927 assert_template "georss"
928 assert_equal "application/rss+xml", @response.content_type
929 assert_select "rss", :count => 1 do
930 assert_select "channel", :count => 1 do
931 assert_select "title"
932 assert_select "description"
934 assert_select "image"
935 assert_select "item", :count => traces.visible.count do |items|
936 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
937 assert_select item, "title", trace.name
938 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
939 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
940 assert_select item, "description"
941 # assert_select item, "dc:creator", trace.user.display_name
942 assert_select item, "pubDate", trace.timestamp.rfc822
949 def check_trace_list(traces)
950 assert_response :success
951 assert_template "list"
954 assert_select "table#trace_list tbody", :count => 1 do
955 assert_select "tr", :count => traces.length do |rows|
956 traces.zip(rows).each do |trace, row|
957 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
958 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
959 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
960 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
965 assert_select "h4", /Nothing here yet/
969 def check_trace_view(trace)
970 assert_response :success
971 assert_template "view"
973 assert_select "table", :count => 1 do
974 assert_select "td", /^#{Regexp.quote(trace.name)} /
975 assert_select "td", trace.user.display_name
976 assert_select "td", trace.description
980 def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
981 assert_response :success
982 assert_equal content_type, response.content_type
983 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
986 def check_trace_picture(trace)
987 assert_response :success
988 assert_equal "image/gif", response.content_type
989 assert_equal trace.large_picture, response.body
992 def check_trace_icon(trace)
993 assert_response :success
994 assert_equal "image/gif", response.content_type
995 assert_equal trace.icon_picture, response.body