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", File.dirname(__FILE__) + "/../traces")
11 @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
12 Object.const_set("GPX_IMAGE_DIR", File.dirname(__FILE__) + "/../traces")
16 Object.send("remove_const", "GPX_TRACE_DIR")
17 Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
19 Object.send("remove_const", "GPX_IMAGE_DIR")
20 Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
24 # test all routes which lead to this controller
27 { :path => "/api/0.6/gpx/create", :method => :post },
28 { :controller => "trace", :action => "api_create" }
31 { :path => "/api/0.6/gpx/1", :method => :get },
32 { :controller => "trace", :action => "api_read", :id => "1" }
35 { :path => "/api/0.6/gpx/1", :method => :put },
36 { :controller => "trace", :action => "api_update", :id => "1" }
39 { :path => "/api/0.6/gpx/1", :method => :delete },
40 { :controller => "trace", :action => "api_delete", :id => "1" }
43 { :controller => "trace", :action => "api_read", :id => "1" },
44 { :path => "/api/0.6/gpx/1/details", :method => :get }
47 { :path => "/api/0.6/gpx/1/data", :method => :get },
48 { :controller => "trace", :action => "api_data", :id => "1" }
51 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
52 { :controller => "trace", :action => "api_data", :id => "1", :format => "xml" }
56 { :path => "/traces", :method => :get },
57 { :controller => "trace", :action => "list" }
60 { :path => "/traces/page/1", :method => :get },
61 { :controller => "trace", :action => "list", :page => "1" }
64 { :path => "/traces/tag/tagname", :method => :get },
65 { :controller => "trace", :action => "list", :tag => "tagname" }
68 { :path => "/traces/tag/tagname/page/1", :method => :get },
69 { :controller => "trace", :action => "list", :tag => "tagname", :page => "1" }
72 { :path => "/user/username/traces", :method => :get },
73 { :controller => "trace", :action => "list", :display_name => "username" }
76 { :path => "/user/username/traces/page/1", :method => :get },
77 { :controller => "trace", :action => "list", :display_name => "username", :page => "1" }
80 { :path => "/user/username/traces/tag/tagname", :method => :get },
81 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname" }
84 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
85 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname", :page => "1" }
89 { :path => "/traces/mine", :method => :get },
90 { :controller => "trace", :action => "mine" }
93 { :path => "/traces/mine/page/1", :method => :get },
94 { :controller => "trace", :action => "mine", :page => "1" }
97 { :path => "/traces/mine/tag/tagname", :method => :get },
98 { :controller => "trace", :action => "mine", :tag => "tagname" }
101 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
102 { :controller => "trace", :action => "mine", :tag => "tagname", :page => "1" }
106 { :path => "/traces/rss", :method => :get },
107 { :controller => "trace", :action => "georss", :format => :rss }
110 { :path => "/traces/tag/tagname/rss", :method => :get },
111 { :controller => "trace", :action => "georss", :tag => "tagname", :format => :rss }
114 { :path => "/user/username/traces/rss", :method => :get },
115 { :controller => "trace", :action => "georss", :display_name => "username", :format => :rss }
118 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
119 { :controller => "trace", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
123 { :path => "/user/username/traces/1", :method => :get },
124 { :controller => "trace", :action => "view", :display_name => "username", :id => "1" }
127 { :path => "/user/username/traces/1/picture", :method => :get },
128 { :controller => "trace", :action => "picture", :display_name => "username", :id => "1" }
131 { :path => "/user/username/traces/1/icon", :method => :get },
132 { :controller => "trace", :action => "icon", :display_name => "username", :id => "1" }
136 { :path => "/trace/create", :method => :get },
137 { :controller => "trace", :action => "create" }
140 { :path => "/trace/create", :method => :post },
141 { :controller => "trace", :action => "create" }
144 { :path => "/trace/1/data", :method => :get },
145 { :controller => "trace", :action => "data", :id => "1" }
148 { :path => "/trace/1/data.xml", :method => :get },
149 { :controller => "trace", :action => "data", :id => "1", :format => "xml" }
152 { :path => "/trace/1/edit", :method => :get },
153 { :controller => "trace", :action => "edit", :id => "1" }
156 { :path => "/trace/1/edit", :method => :post },
157 { :controller => "trace", :action => "edit", :id => "1" }
160 { :path => "/trace/1/edit", :method => :patch },
161 { :controller => "trace", :action => "edit", :id => "1" }
164 { :path => "/trace/1/delete", :method => :post },
165 { :controller => "trace", :action => "delete", :id => "1" }
169 # Check that the list of traces is displayed
171 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
172 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
173 create(:tracetag, :trace => trace, :tag => "London")
175 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
176 create(:tracetag, :trace => trace, :tag => "Birmingham")
178 trace_c = create(:trace, :visibility => "private", :user => users(:public_user), :timestamp => 2.seconds.ago) do |trace|
179 create(:tracetag, :trace => trace, :tag => "London")
181 trace_d = create(:trace, :visibility => "private", :user => users(:public_user), :timestamp => 1.second.ago) do |trace|
182 create(:tracetag, :trace => trace, :tag => "Birmingham")
185 # First with the public list
187 check_trace_list [trace_b, trace_a]
189 # Restrict traces to those with a given tag
190 get :list, :tag => "London"
191 check_trace_list [trace_a]
193 # Should see more when we are logged in
194 get :list, {}, { :user => users(:public_user).id }
195 check_trace_list [trace_d, trace_c, trace_b, trace_a]
197 # Again, we should see more when we are logged in
198 get :list, { :tag => "London" }, { :user => users(:public_user).id }
199 check_trace_list [trace_c, trace_a]
202 # 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 => users(:public_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, {}, { :user => users(:public_user).id }
217 assert_redirected_to :controller => "trace", :action => "list", :display_name => users(:public_user).display_name
219 # Fetch the actual list
220 get :list, { :display_name => users(:public_user).display_name }, { :user => users(:public_user).id }
221 check_trace_list [trace_b]
224 # Check the list of traces for a specific user
227 trace_b = create(:trace, :visibility => "public", :user => users(:public_user))
228 trace_c = create(:trace, :visibility => "private", :user => users(:public_user)) do |trace|
229 create(:tracetag, :trace => trace, :tag => "London")
232 # Test a user with no traces
233 get :list, :display_name => users(:second_public_user).display_name
236 # Test a user with some traces - should see only public ones
237 get :list, :display_name => users(:public_user).display_name
238 check_trace_list [trace_b]
240 # Should still see only public ones when authenticated as another user
241 get :list, { :display_name => users(:public_user).display_name }, { :user => users(:normal_user).id }
242 check_trace_list [trace_b]
244 # Should see all traces when authenticated as the target user
245 get :list, { :display_name => users(:public_user).display_name }, { :user => users(:public_user).id }
246 check_trace_list [trace_c, trace_b]
248 # Should only see traces with the correct tag when a tag is specified
249 get :list, { :display_name => users(:public_user).display_name, :tag => "London" }, { :user => users(:public_user).id }
250 check_trace_list [trace_c]
252 # Should get an error if the user does not exist
253 get :list, :display_name => "UnknownUser"
254 assert_response :not_found
255 assert_template "user/no_such_user"
258 # Check that the rss loads
260 # First with the public feed
261 get :georss, :format => :rss
262 check_trace_feed Trace.visible_to_all
264 # Restrict traces to those with a given tag
265 get :georss, :tag => "London", :format => :rss
266 check_trace_feed Trace.tagged("London").visible_to_all
268 # Restrict traces to those for a given user
269 get :georss, :display_name => users(:public_user).display_name, :format => :rss
270 check_trace_feed users(:public_user).traces.visible_to_all
272 # Restrict traces to those for a given user with a tiven tag
273 get :georss, :display_name => users(:public_user).display_name, :tag => "Birmingham", :format => :rss
274 check_trace_feed users(:public_user).traces.tagged("Birmingham").visible_to_all
277 # Test viewing a trace
279 public_trace_file = create(:trace, :visibility => "public")
281 # First with no auth, which should work since the trace is public
282 get :view, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
283 check_trace_view public_trace_file
285 # Now with some other user, which should work since the trace is public
286 get :view, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
287 check_trace_view public_trace_file
289 # And finally we should be able to do it with the owner of the trace
290 get :view, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
291 check_trace_view public_trace_file
294 # Check an anonymous trace can't be viewed by another user
296 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
299 get :view, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
300 assert_response :redirect
301 assert_redirected_to :action => :list
303 # Now with some other user, which should not work since the trace is anon
304 get :view, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
305 assert_response :redirect
306 assert_redirected_to :action => :list
308 # And finally we should be able to do it with the owner of the trace
309 get :view, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
310 check_trace_view anon_trace_file
313 # Test viewing a trace that doesn't exist
314 def test_view_not_found
315 deleted_trace_file = create(:trace, :deleted)
318 get :view, :display_name => users(:public_user).display_name, :id => 0
319 assert_response :redirect
320 assert_redirected_to :action => :list
322 # Now with some other user
323 get :view, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
324 assert_response :redirect
325 assert_redirected_to :action => :list
327 # And finally we should not be able to view a deleted trace
328 get :view, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
329 assert_response :redirect
330 assert_redirected_to :action => :list
333 # Test downloading a trace
335 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
336 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/a.gpx" do
337 # First with no auth, which should work since the trace is public
338 get :data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
339 check_trace_data public_trace_file
341 # Now with some other user, which should work since the trace is public
342 get :data, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
343 check_trace_data public_trace_file
345 # And finally we should be able to do it with the owner of the trace
346 get :data, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
347 check_trace_data public_trace_file
351 # Test downloading a compressed trace
352 def test_data_compressed
353 identifiable_trace_file = create(:trace, :visibility => "identifiable")
354 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/d.gpx" do
355 # First get the data as is
356 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id
357 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
359 # Now ask explicitly for XML format
360 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "xml"
361 check_trace_data identifiable_trace_file, "application/xml", "xml"
363 # Now ask explicitly for GPX format
364 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "gpx"
365 check_trace_data identifiable_trace_file
369 # Check an anonymous trace can't be downloaded by another user
371 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
372 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/b.gpx" do
374 get :data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
375 assert_response :not_found
377 # Now with some other user, which shouldn't work since the trace is anon
378 get :data, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
379 assert_response :not_found
381 # And finally we should be able to do it with the owner of the trace
382 get :data, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
383 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))
407 Trace.stub_any_instance :large_picture_name, "#{GPX_TRACE_DIR}/a.gif" do
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
422 # Check the picture for an anonymous trace can't be downloaded by another user
423 def test_picture_anon
424 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
425 Trace.stub_any_instance :large_picture_name, "#{GPX_TRACE_DIR}/b.gif" do
427 get :picture, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
428 assert_response :forbidden
430 # Now with some other user, which shouldn't work since the trace is anon
431 get :picture, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
432 assert_response :forbidden
434 # And finally we should be able to do it with the owner of the trace
435 get :picture, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
436 check_trace_picture anon_trace_file
440 # Test downloading the picture for a trace that doesn't exist
441 def test_picture_not_found
442 # First with no auth, which should work since the trace is public
443 get :picture, :display_name => users(:public_user).display_name, :id => 0
444 assert_response :not_found
446 # Now with some other user, which should work since the trace is public
447 get :picture, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
448 assert_response :not_found
450 # And finally we should not be able to do it with a deleted trace
451 deleted_trace_file = create(:trace, :deleted)
452 get :picture, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
453 assert_response :not_found
456 # Test downloading the icon for a trace
458 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
459 Trace.stub_any_instance :icon_picture_name, "#{GPX_TRACE_DIR}/a_icon.gif" do
460 # First with no auth, which should work since the trace is public
461 get :icon, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
462 check_trace_icon public_trace_file
464 # Now with some other user, which should work since the trace is public
465 get :icon, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
466 check_trace_icon public_trace_file
468 # And finally we should be able to do it with the owner of the trace
469 get :icon, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
470 check_trace_icon public_trace_file
474 # Check the icon for an anonymous trace can't be downloaded by another user
476 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
477 Trace.stub_any_instance :icon_picture_name, "#{GPX_TRACE_DIR}/b_icon.gif" do
479 get :icon, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
480 assert_response :forbidden
482 # Now with some other user, which shouldn't work since the trace is anon
483 get :icon, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
484 assert_response :forbidden
486 # And finally we should be able to do it with the owner of the trace
487 get :icon, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
488 check_trace_icon anon_trace_file
492 # Test downloading the icon for a trace that doesn't exist
493 def test_icon_not_found
495 get :icon, :display_name => users(:public_user).display_name, :id => 0
496 assert_response :not_found
498 # Now with some other user
499 get :icon, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
500 assert_response :not_found
502 # And finally we should not be able to do it with a deleted trace
503 deleted_trace_file = create(:trace, :deleted)
504 get :icon, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
505 assert_response :not_found
508 # Test fetching the create page
512 assert_response :redirect
513 assert_redirected_to :controller => :user, :action => :login, :referer => trace_create_path
515 # Now authenticated as a user with gps.trace.visibility set
516 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
517 get :create, {}, { :user => users(:public_user).id }
518 assert_response :success
519 assert_template :create
520 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
522 # Now authenticated as a user with gps.trace.public set
523 create(:user_preference, :user => users(:second_public_user), :k => "gps.trace.public", :v => "default")
524 get :create, {}, { :user => users(:second_public_user).id }
525 assert_response :success
526 assert_template :create
527 assert_select "select#trace_visibility option[value=public][selected]", 1
529 # Now authenticated as a user with no preferences
530 get :create, {}, { :user => users(:normal_user).id }
531 assert_response :success
532 assert_template :create
533 assert_select "select#trace_visibility option[value=private][selected]", 1
536 # Test creating a trace
538 public_trace_file = create(:trace, :visibility => "public")
539 public_trace_file.stub :trace_name, "#{GPX_TRACE_DIR}/a.gpx" do
541 file = Rack::Test::UploadedFile.new(public_trace_file.trace_name, "application/gpx+xml")
544 post :create, :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" }
545 assert_response :forbidden
548 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
549 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
550 post :create, { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, { :user => users(:public_user).id }
551 assert_response :redirect
552 assert_redirected_to :action => :list, :display_name => users(:public_user).display_name
553 assert_match /file has been uploaded/, flash[:notice]
554 trace = Trace.order(:id => :desc).first
555 assert_equal "a.gpx", trace.name
556 assert_equal "New Trace", trace.description
557 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
558 assert_equal "trackable", trace.visibility
559 assert_equal false, trace.inserted
560 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
562 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
566 # Test fetching the edit page for a trace
568 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
569 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
572 get :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
573 assert_response :redirect
574 assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => users(:normal_user).display_name, :id => public_trace_file.id)
576 # Now with some other user, which should fail
577 get :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
578 assert_response :forbidden
580 # Now with a trace which doesn't exist
581 get :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
582 assert_response :not_found
584 # Now with a trace which has been deleted
585 get :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
586 assert_response :not_found
588 # Finally with a trace that we are allowed to edit
589 get :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
590 assert_response :success
593 # Test saving edits to a trace
595 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
596 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
598 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
601 post :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details
602 assert_response :forbidden
604 # Now with some other user, which should fail
605 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:public_user).id }
606 assert_response :forbidden
608 # Now with a trace which doesn't exist
609 post :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id, :trace => new_details }
610 assert_response :not_found
612 # Now with a trace which has been deleted
613 post :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id, :trace => new_details }, { :user => users(:public_user).id }
614 assert_response :not_found
616 # Finally with a trace that we are allowed to edit
617 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:normal_user).id }
618 assert_response :redirect
619 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
620 trace = Trace.find(public_trace_file.id)
621 assert_equal new_details[:description], trace.description
622 assert_equal new_details[:tagstring], trace.tagstring
623 assert_equal new_details[:visibility], trace.visibility
626 # Test deleting a trace
628 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
629 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
632 post :delete, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
633 assert_response :forbidden
635 # Now with some other user, which should fail
636 post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
637 assert_response :forbidden
639 # Now with a trace which doesn't exist
640 post :delete, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
641 assert_response :not_found
643 # Now with a trace has already been deleted
644 post :delete, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
645 assert_response :not_found
647 # Finally with a trace that we are allowed to delete
648 post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
649 assert_response :redirect
650 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
651 trace = Trace.find(public_trace_file.id)
652 assert_equal false, trace.visible
655 # Check getting a specific trace through the api
657 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
660 get :api_read, :id => public_trace_file.id
661 assert_response :unauthorized
663 # Now with some other user, which should work since the trace is public
664 basic_authorization(users(:public_user).display_name, "test")
665 get :api_read, :id => public_trace_file.id
666 assert_response :success
668 # And finally we should be able to do it with the owner of the trace
669 basic_authorization(users(:normal_user).display_name, "test")
670 get :api_read, :id => public_trace_file.id
671 assert_response :success
674 # Check an anoymous trace can't be specifically fetched by another user
675 def test_api_read_anon
676 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
679 get :api_read, :id => anon_trace_file.id
680 assert_response :unauthorized
682 # Now try with another user, which shouldn't work since the trace is anon
683 basic_authorization(users(:normal_user).display_name, "test")
684 get :api_read, :id => anon_trace_file.id
685 assert_response :forbidden
687 # And finally we should be able to get the trace details with the trace owner
688 basic_authorization(users(:public_user).display_name, "test")
689 get :api_read, :id => anon_trace_file.id
690 assert_response :success
693 # Check the api details for a trace that doesn't exist
694 def test_api_read_not_found
695 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
697 # Try first with no auth, as it should require it
698 get :api_read, :id => 0
699 assert_response :unauthorized
701 # Login, and try again
702 basic_authorization(users(:public_user).display_name, "test")
703 get :api_read, :id => 0
704 assert_response :not_found
706 # Now try a trace which did exist but has been deleted
707 basic_authorization(users(:public_user).display_name, "test")
708 get :api_read, :id => deleted_trace_file.id
709 assert_response :not_found
712 # Test downloading a trace through the api
714 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
715 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/a.gpx" do
717 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
718 assert_response :unauthorized
720 # Now with some other user, which should work since the trace is public
721 basic_authorization(users(:public_user).display_name, "test")
722 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
723 check_trace_data public_trace_file
725 # And finally we should be able to do it with the owner of the trace
726 basic_authorization(users(:normal_user).display_name, "test")
727 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
728 check_trace_data public_trace_file
732 # Test downloading a compressed trace through the api
733 def test_api_data_compressed
734 identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => users(:public_user))
735 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/d.gpx" do
736 # Authenticate as the owner of the trace we will be using
737 basic_authorization(users(:public_user).display_name, "test")
739 # First get the data as is
740 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id
741 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
743 # Now ask explicitly for XML format
744 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "xml"
745 check_trace_data identifiable_trace_file, "application/xml", "xml"
747 # Now ask explicitly for GPX format
748 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "gpx"
749 check_trace_data identifiable_trace_file
753 # Check an anonymous trace can't be downloaded by another user through the api
754 def test_api_data_anon
755 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
756 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/b.gpx" do
758 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
759 assert_response :unauthorized
761 # Now with some other user, which shouldn't work since the trace is anon
762 basic_authorization(users(:normal_user).display_name, "test")
763 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
764 assert_response :forbidden
766 # And finally we should be able to do it with the owner of the trace
767 basic_authorization(users(:public_user).display_name, "test")
768 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
769 check_trace_data anon_trace_file
773 # Test downloading a trace that doesn't exist through the api
774 def test_api_data_not_found
776 get :api_data, :display_name => users(:public_user).display_name, :id => 0
777 assert_response :unauthorized
779 # Now with a trace that has never existed
780 basic_authorization(users(:public_user).display_name, "test")
781 get :api_data, :display_name => users(:public_user).display_name, :id => 0
782 assert_response :not_found
784 # Now with a trace that has been deleted
785 deleted_trace_file = create(:trace, :deleted)
786 basic_authorization(users(:public_user).display_name, "test")
787 get :api_data, :display_name => users(:public_user).display_name, :id => deleted_trace_file.id
788 assert_response :not_found
791 # Test creating a trace through the api
793 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
794 public_trace_file.stub :trace_name, "#{GPX_TRACE_DIR}/a.gpx" do
796 file = Rack::Test::UploadedFile.new(public_trace_file.trace_name, "application/gpx+xml")
799 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
800 assert_response :unauthorized
803 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
804 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
805 basic_authorization(users(:public_user).display_name, "test")
806 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
807 assert_response :success
808 trace = Trace.find(response.body.to_i)
809 assert_equal "a.gpx", trace.name
810 assert_equal "New Trace", trace.description
811 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
812 assert_equal "trackable", trace.visibility
813 assert_equal false, trace.inserted
814 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
816 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
821 # Now authenticated, with the legacy public flag
822 assert_not_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
823 basic_authorization(users(:public_user).display_name, "test")
824 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 1
825 assert_response :success
826 trace = Trace.find(response.body.to_i)
827 assert_equal "a.gpx", trace.name
828 assert_equal "New Trace", trace.description
829 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
830 assert_equal "public", trace.visibility
831 assert_equal false, trace.inserted
832 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
834 assert_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
839 # Now authenticated, with the legacy private flag
840 assert_nil users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first
841 basic_authorization(users(:second_public_user).display_name, "test")
842 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 0
843 assert_response :success
844 trace = Trace.find(response.body.to_i)
845 assert_equal "a.gpx", trace.name
846 assert_equal "New Trace", trace.description
847 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
848 assert_equal "private", trace.visibility
849 assert_equal false, trace.inserted
850 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
852 assert_equal "private", users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first.v
856 # Check updating a trace through the api
858 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
859 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
860 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
862 public_trace_file.stub :trace_name, "#{GPX_TRACE_DIR}/a.gpx" do
864 content public_trace_file.to_xml
865 put :api_update, :id => public_trace_file.id
866 assert_response :unauthorized
868 # Now with some other user, which should fail
869 basic_authorization(users(:public_user).display_name, "test")
870 content public_trace_file.to_xml
871 put :api_update, :id => public_trace_file.id
872 assert_response :forbidden
874 # Now with a trace which doesn't exist
875 basic_authorization(users(:public_user).display_name, "test")
876 content public_trace_file.to_xml
877 put :api_update, :id => 0
878 assert_response :not_found
880 # Now with a trace which did exist but has been deleted
881 basic_authorization(users(:public_user).display_name, "test")
882 content deleted_trace_file.to_xml
883 put :api_update, :id => deleted_trace_file.id
884 assert_response :not_found
886 # Now try an update with the wrong ID
887 basic_authorization(users(:normal_user).display_name, "test")
888 content anon_trace_file.to_xml
889 put :api_update, :id => public_trace_file.id
890 assert_response :bad_request,
891 "should not be able to update a trace with a different ID from the XML"
893 # And finally try an update that should work
894 basic_authorization(users(:normal_user).display_name, "test")
895 t = public_trace_file
896 t.description = "Changed description"
897 t.visibility = "private"
899 put :api_update, :id => t.id
900 assert_response :success
901 nt = Trace.find(t.id)
902 assert_equal nt.description, t.description
903 assert_equal nt.visibility, t.visibility
907 # Check deleting a trace through the api
909 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
912 delete :api_delete, :id => public_trace_file.id
913 assert_response :unauthorized
915 # Now with some other user, which should fail
916 basic_authorization(users(:public_user).display_name, "test")
917 delete :api_delete, :id => public_trace_file.id
918 assert_response :forbidden
920 # Now with a trace which doesn't exist
921 basic_authorization(users(:public_user).display_name, "test")
922 delete :api_delete, :id => 0
923 assert_response :not_found
925 # And finally we should be able to do it with the owner of the trace
926 basic_authorization(users(:normal_user).display_name, "test")
927 delete :api_delete, :id => public_trace_file.id
928 assert_response :success
930 # Try it a second time, which should fail
931 basic_authorization(users(:normal_user).display_name, "test")
932 delete :api_delete, :id => public_trace_file.id
933 assert_response :not_found
938 def check_trace_feed(traces)
939 assert_response :success
940 assert_template "georss"
941 assert_equal "application/rss+xml", @response.content_type
942 assert_select "rss", :count => 1 do
943 assert_select "channel", :count => 1 do
944 assert_select "title"
945 assert_select "description"
947 assert_select "image"
948 assert_select "item", :count => traces.visible.count do |items|
949 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
950 assert_select item, "title", trace.name
951 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
952 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
953 assert_select item, "description"
954 # assert_select item, "dc:creator", trace.user.display_name
955 assert_select item, "pubDate", trace.timestamp.rfc822
962 def check_trace_list(traces)
963 assert_response :success
964 assert_template "list"
967 assert_select "table#trace_list tbody", :count => 1 do
968 assert_select "tr", :count => traces.length do |rows|
969 traces.zip(rows).each do |trace, row|
970 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
971 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
972 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
973 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
978 assert_select "h4", /Nothing here yet/
982 def check_trace_view(trace)
983 assert_response :success
984 assert_template "view"
986 assert_select "table", :count => 1 do
987 assert_select "td", /^#{Regexp.quote(trace.name)} /
988 assert_select "td", trace.user.display_name
989 assert_select "td", trace.description
993 def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
994 assert_response :success
995 assert_equal content_type, response.content_type
996 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
999 def check_trace_picture(trace)
1000 assert_response :success
1001 assert_equal "image/gif", response.content_type
1002 assert_equal trace.large_picture, response.body
1005 def check_trace_icon(trace)
1006 assert_response :success
1007 assert_equal "image/gif", response.content_type
1008 assert_equal trace.icon_picture, response.body