2 require "minitest/mock"
4 class TraceControllerTest < ActionController::TestCase
6 @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
7 Object.const_set("GPX_TRACE_DIR", Rails.root.join("test", "gpx", "traces"))
9 @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
10 Object.const_set("GPX_IMAGE_DIR", Rails.root.join("test", "gpx", "images"))
14 File.unlink(*Dir.glob(File.join(GPX_TRACE_DIR, "*.gpx")))
15 File.unlink(*Dir.glob(File.join(GPX_IMAGE_DIR, "*.gif")))
17 Object.send("remove_const", "GPX_TRACE_DIR")
18 Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
20 Object.send("remove_const", "GPX_IMAGE_DIR")
21 Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
25 # test all routes which lead to this controller
28 { :path => "/api/0.6/gpx/create", :method => :post },
29 { :controller => "trace", :action => "api_create" }
32 { :path => "/api/0.6/gpx/1", :method => :get },
33 { :controller => "trace", :action => "api_read", :id => "1" }
36 { :path => "/api/0.6/gpx/1", :method => :put },
37 { :controller => "trace", :action => "api_update", :id => "1" }
40 { :path => "/api/0.6/gpx/1", :method => :delete },
41 { :controller => "trace", :action => "api_delete", :id => "1" }
44 { :controller => "trace", :action => "api_read", :id => "1" },
45 { :path => "/api/0.6/gpx/1/details", :method => :get }
48 { :path => "/api/0.6/gpx/1/data", :method => :get },
49 { :controller => "trace", :action => "api_data", :id => "1" }
52 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
53 { :controller => "trace", :action => "api_data", :id => "1", :format => "xml" }
57 { :path => "/traces", :method => :get },
58 { :controller => "trace", :action => "list" }
61 { :path => "/traces/page/1", :method => :get },
62 { :controller => "trace", :action => "list", :page => "1" }
65 { :path => "/traces/tag/tagname", :method => :get },
66 { :controller => "trace", :action => "list", :tag => "tagname" }
69 { :path => "/traces/tag/tagname/page/1", :method => :get },
70 { :controller => "trace", :action => "list", :tag => "tagname", :page => "1" }
73 { :path => "/user/username/traces", :method => :get },
74 { :controller => "trace", :action => "list", :display_name => "username" }
77 { :path => "/user/username/traces/page/1", :method => :get },
78 { :controller => "trace", :action => "list", :display_name => "username", :page => "1" }
81 { :path => "/user/username/traces/tag/tagname", :method => :get },
82 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname" }
85 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
86 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname", :page => "1" }
90 { :path => "/traces/mine", :method => :get },
91 { :controller => "trace", :action => "mine" }
94 { :path => "/traces/mine/page/1", :method => :get },
95 { :controller => "trace", :action => "mine", :page => "1" }
98 { :path => "/traces/mine/tag/tagname", :method => :get },
99 { :controller => "trace", :action => "mine", :tag => "tagname" }
102 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
103 { :controller => "trace", :action => "mine", :tag => "tagname", :page => "1" }
107 { :path => "/traces/rss", :method => :get },
108 { :controller => "trace", :action => "georss", :format => :rss }
111 { :path => "/traces/tag/tagname/rss", :method => :get },
112 { :controller => "trace", :action => "georss", :tag => "tagname", :format => :rss }
115 { :path => "/user/username/traces/rss", :method => :get },
116 { :controller => "trace", :action => "georss", :display_name => "username", :format => :rss }
119 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
120 { :controller => "trace", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
124 { :path => "/user/username/traces/1", :method => :get },
125 { :controller => "trace", :action => "view", :display_name => "username", :id => "1" }
128 { :path => "/user/username/traces/1/picture", :method => :get },
129 { :controller => "trace", :action => "picture", :display_name => "username", :id => "1" }
132 { :path => "/user/username/traces/1/icon", :method => :get },
133 { :controller => "trace", :action => "icon", :display_name => "username", :id => "1" }
137 { :path => "/trace/create", :method => :get },
138 { :controller => "trace", :action => "create" }
141 { :path => "/trace/create", :method => :post },
142 { :controller => "trace", :action => "create" }
145 { :path => "/trace/1/data", :method => :get },
146 { :controller => "trace", :action => "data", :id => "1" }
149 { :path => "/trace/1/data.xml", :method => :get },
150 { :controller => "trace", :action => "data", :id => "1", :format => "xml" }
153 { :path => "/trace/1/edit", :method => :get },
154 { :controller => "trace", :action => "edit", :id => "1" }
157 { :path => "/trace/1/edit", :method => :post },
158 { :controller => "trace", :action => "edit", :id => "1" }
161 { :path => "/trace/1/delete", :method => :post },
162 { :controller => "trace", :action => "delete", :id => "1" }
166 # Check that the list of traces is displayed
169 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
170 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
171 create(:tracetag, :trace => trace, :tag => "London")
173 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
174 create(:tracetag, :trace => trace, :tag => "Birmingham")
176 trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
177 create(:tracetag, :trace => trace, :tag => "London")
179 trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
180 create(:tracetag, :trace => trace, :tag => "Birmingham")
183 # First with the public list
185 check_trace_list [trace_b, trace_a]
187 # Restrict traces to those with a given tag
188 get :list, :params => { :tag => "London" }
189 check_trace_list [trace_a]
191 # Should see more when we are logged in
192 get :list, :session => { :user => user }
193 check_trace_list [trace_d, trace_c, trace_b, trace_a]
195 # Again, we should see more when we are logged in
196 get :list, :params => { :tag => "London" }, :session => { :user => user }
197 check_trace_list [trace_c, trace_a]
200 # Check that I can get mine
203 create(:trace, :visibility => "public") do |trace|
204 create(:tracetag, :trace => trace, :tag => "Birmingham")
206 trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
207 create(:tracetag, :trace => trace, :tag => "London")
210 # First try to get it when not logged in
212 assert_redirected_to :controller => "user", :action => "login", :referer => "/traces/mine"
214 # Now try when logged in
215 get :mine, :session => { :user => user }
216 assert_redirected_to :controller => "trace", :action => "list", :display_name => user.display_name
218 # Fetch the actual list
219 get :list, :params => { :display_name => user.display_name }, :session => { :user => user }
220 check_trace_list [trace_b]
223 # Check the list of traces for a specific user
226 second_user = create(:user)
227 third_user = create(:user)
229 trace_b = create(:trace, :visibility => "public", :user => user)
230 trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
231 create(:tracetag, :trace => trace, :tag => "London")
234 # Test a user with no traces
235 get :list, :params => { :display_name => second_user.display_name }
238 # Test the user with the traces - should see only public ones
239 get :list, :params => { :display_name => user.display_name }
240 check_trace_list [trace_b]
242 # Should still see only public ones when authenticated as another user
243 get :list, :params => { :display_name => user.display_name }, :session => { :user => third_user }
244 check_trace_list [trace_b]
246 # Should see all traces when authenticated as the target user
247 get :list, :params => { :display_name => user.display_name }, :session => { :user => user }
248 check_trace_list [trace_c, trace_b]
250 # Should only see traces with the correct tag when a tag is specified
251 get :list, :params => { :display_name => user.display_name, :tag => "London" }, :session => { :user => user }
252 check_trace_list [trace_c]
254 # Should get an error if the user does not exist
255 get :list, :params => { :display_name => "UnknownUser" }
256 assert_response :not_found
257 assert_template "user/no_such_user"
260 # Check a multi-page list
262 # Create several pages worth of traces
263 create_list(:trace, 50)
265 # Try and get the list
267 assert_response :success
268 assert_select "table#trace_list tbody", :count => 1 do
269 assert_select "tr", :count => 20
272 # Try and get the second page
273 get :list, :params => { :page => 2 }
274 assert_response :success
275 assert_select "table#trace_list tbody", :count => 1 do
276 assert_select "tr", :count => 20
280 # Check that the rss loads
284 # First with the public feed
285 get :georss, :params => { :format => :rss }
286 check_trace_feed Trace.visible_to_all
288 # Restrict traces to those with a given tag
289 get :georss, :params => { :tag => "London", :format => :rss }
290 check_trace_feed Trace.tagged("London").visible_to_all
292 # Restrict traces to those for a given user
293 get :georss, :params => { :display_name => user.display_name, :format => :rss }
294 check_trace_feed user.traces.visible_to_all
296 # Restrict traces to those for a given user with a tiven tag
297 get :georss, :params => { :display_name => user.display_name, :tag => "Birmingham", :format => :rss }
298 check_trace_feed user.traces.tagged("Birmingham").visible_to_all
301 # Test viewing a trace
303 public_trace_file = create(:trace, :visibility => "public")
305 # First with no auth, which should work since the trace is public
306 get :view, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
307 check_trace_view public_trace_file
309 # Now with some other user, which should work since the trace is public
310 get :view, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
311 check_trace_view public_trace_file
313 # And finally we should be able to do it with the owner of the trace
314 get :view, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
315 check_trace_view public_trace_file
318 # Check an anonymous trace can't be viewed by another user
320 anon_trace_file = create(:trace, :visibility => "private")
323 get :view, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
324 assert_response :redirect
325 assert_redirected_to :action => :list
327 # Now with some other user, which should not work since the trace is anon
328 get :view, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
329 assert_response :redirect
330 assert_redirected_to :action => :list
332 # And finally we should be able to do it with the owner of the trace
333 get :view, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
334 check_trace_view anon_trace_file
337 # Test viewing a trace that doesn't exist
338 def test_view_not_found
339 deleted_trace_file = create(:trace, :deleted)
341 # First with a trace that has never existed
342 get :view, :params => { :display_name => create(:user).display_name, :id => 0 }
343 assert_response :redirect
344 assert_redirected_to :action => :list
346 # Now with a trace that has been deleted
347 get :view, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
348 assert_response :redirect
349 assert_redirected_to :action => :list
352 # Test downloading a trace
354 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
356 # First with no auth, which should work since the trace is public
357 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
358 check_trace_data public_trace_file
360 # Now with some other user, which should work since the trace is public
361 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
362 check_trace_data public_trace_file
364 # And finally we should be able to do it with the owner of the trace
365 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
366 check_trace_data public_trace_file
369 # Test downloading a compressed trace
370 def test_data_compressed
371 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
373 # First get the data as is
374 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id }
375 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
377 # Now ask explicitly for XML format
378 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" }
379 check_trace_data identifiable_trace_file, "application/xml", "xml"
381 # Now ask explicitly for GPX format
382 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" }
383 check_trace_data identifiable_trace_file
386 # Check an anonymous trace can't be downloaded by another user
388 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
391 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
392 assert_response :not_found
394 # Now with some other user, which shouldn't work since the trace is anon
395 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
396 assert_response :not_found
398 # And finally we should be able to do it with the owner of the trace
399 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
400 check_trace_data anon_trace_file
403 # Test downloading a trace that doesn't exist
404 def test_data_not_found
405 deleted_trace_file = create(:trace, :deleted)
407 # First with a trace that has never existed
408 get :data, :params => { :display_name => create(:user).display_name, :id => 0 }
409 assert_response :not_found
411 # Now with a trace that has been deleted
412 get :data, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
413 assert_response :not_found
416 # Test downloading the picture for a trace
418 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
420 # First with no auth, which should work since the trace is public
421 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
422 check_trace_picture public_trace_file
424 # Now with some other user, which should work since the trace is public
425 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
426 check_trace_picture public_trace_file
428 # And finally we should be able to do it with the owner of the trace
429 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
430 check_trace_picture public_trace_file
433 # Check the picture for an anonymous trace can't be downloaded by another user
434 def test_picture_anon
435 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
438 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
439 assert_response :forbidden
441 # Now with some other user, which shouldn't work since the trace is anon
442 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
443 assert_response :forbidden
445 # And finally we should be able to do it with the owner of the trace
446 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
447 check_trace_picture anon_trace_file
450 # Test downloading the picture for a trace that doesn't exist
451 def test_picture_not_found
452 deleted_trace_file = create(:trace, :deleted)
454 # First with a trace that has never existed
455 get :picture, :params => { :display_name => create(:user).display_name, :id => 0 }
456 assert_response :not_found
458 # Now with a trace that has been deleted
459 get :picture, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
460 assert_response :not_found
463 # Test downloading the icon for a trace
465 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
467 # First with no auth, which should work since the trace is public
468 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
469 check_trace_icon public_trace_file
471 # Now with some other user, which should work since the trace is public
472 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
473 check_trace_icon public_trace_file
475 # And finally we should be able to do it with the owner of the trace
476 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
477 check_trace_icon public_trace_file
480 # Check the icon for an anonymous trace can't be downloaded by another user
482 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
485 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
486 assert_response :forbidden
488 # Now with some other user, which shouldn't work since the trace is anon
489 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
490 assert_response :forbidden
492 # And finally we should be able to do it with the owner of the trace
493 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
494 check_trace_icon anon_trace_file
497 # Test downloading the icon for a trace that doesn't exist
498 def test_icon_not_found
499 deleted_trace_file = create(:trace, :deleted)
501 # First with a trace that has never existed
502 get :icon, :params => { :display_name => create(:user).display_name, :id => 0 }
503 assert_response :not_found
505 # Now with a trace that has been deleted
506 get :icon, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
507 assert_response :not_found
510 # Test fetching the create page
514 assert_response :redirect
515 assert_redirected_to :controller => :user, :action => :login, :referer => trace_create_path
517 # Now authenticated as a user with gps.trace.visibility set
519 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
520 get :create, :session => { :user => user }
521 assert_response :success
522 assert_template :create
523 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
525 # Now authenticated as a user with gps.trace.public set
526 second_user = create(:user)
527 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
528 get :create, :session => { :user => second_user }
529 assert_response :success
530 assert_template :create
531 assert_select "select#trace_visibility option[value=public][selected]", 1
533 # Now authenticated as a user with no preferences
534 third_user = create(:user)
535 get :create, :session => { :user => third_user }
536 assert_response :success
537 assert_template :create
538 assert_select "select#trace_visibility option[value=private][selected]", 1
541 # Test creating a trace
544 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
545 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
549 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
550 assert_response :forbidden
553 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
554 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
555 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
556 assert_response :redirect
557 assert_redirected_to :action => :list, :display_name => user.display_name
558 assert_match /file has been uploaded/, flash[:notice]
559 trace = Trace.order(:id => :desc).first
560 assert_equal "a.gpx", trace.name
561 assert_equal "New Trace", trace.description
562 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
563 assert_equal "trackable", trace.visibility
564 assert_equal false, trace.inserted
565 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
567 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
570 # Test fetching the edit page for a trace using GET
572 public_trace_file = create(:trace, :visibility => "public")
573 deleted_trace_file = create(:trace, :deleted)
576 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
577 assert_response :redirect
578 assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)
580 # Now with some other user, which should fail
581 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
582 assert_response :forbidden
584 # Now with a trace which doesn't exist
585 get :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
586 assert_response :not_found
588 # Now with a trace which has been deleted
589 get :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
590 assert_response :not_found
592 # Finally with a trace that we are allowed to edit
593 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
594 assert_response :success
597 # Test fetching the edit page for a trace using POST
598 def test_edit_post_no_details
599 public_trace_file = create(:trace, :visibility => "public")
600 deleted_trace_file = create(:trace, :deleted)
603 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
604 assert_response :forbidden
606 # Now with some other user, which should fail
607 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
608 assert_response :forbidden
610 # Now with a trace which doesn't exist
611 post :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
612 assert_response :not_found
614 # Now with a trace which has been deleted
615 post :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
616 assert_response :not_found
618 # Finally with a trace that we are allowed to edit
619 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
620 assert_response :success
623 # Test saving edits to a trace
624 def test_edit_post_with_details
625 public_trace_file = create(:trace, :visibility => "public")
626 deleted_trace_file = create(:trace, :deleted)
629 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
632 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }
633 assert_response :forbidden
635 # Now with some other user, which should fail
636 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => create(:user) }
637 assert_response :forbidden
639 # Now with a trace which doesn't exist
640 post :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user), :trace => new_details }
641 assert_response :not_found
643 # Now with a trace which has been deleted
644 post :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id, :trace => new_details }, :session => { :user => deleted_trace_file.user }
645 assert_response :not_found
647 # Finally with a trace that we are allowed to edit
648 post :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => public_trace_file.user }
649 assert_response :redirect
650 assert_redirected_to :action => :view, :display_name => public_trace_file.user.display_name
651 trace = Trace.find(public_trace_file.id)
652 assert_equal new_details[:description], trace.description
653 assert_equal new_details[:tagstring], trace.tagstring
654 assert_equal new_details[:visibility], trace.visibility
657 # Test deleting a trace
659 public_trace_file = create(:trace, :visibility => "public")
660 deleted_trace_file = create(:trace, :deleted)
663 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
664 assert_response :forbidden
666 # Now with some other user, which should fail
667 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
668 assert_response :forbidden
670 # Now with a trace which doesn't exist
671 post :delete, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
672 assert_response :not_found
674 # Now with a trace has already been deleted
675 post :delete, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
676 assert_response :not_found
678 # Finally with a trace that we are allowed to delete
679 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
680 assert_response :redirect
681 assert_redirected_to :action => :list, :display_name => public_trace_file.user.display_name
682 trace = Trace.find(public_trace_file.id)
683 assert_equal false, trace.visible
686 # Check getting a specific trace through the api
688 public_trace_file = create(:trace, :visibility => "public")
691 get :api_read, :params => { :id => public_trace_file.id }
692 assert_response :unauthorized
694 # Now with some other user, which should work since the trace is public
695 basic_authorization create(:user).display_name, "test"
696 get :api_read, :params => { :id => public_trace_file.id }
697 assert_response :success
699 # And finally we should be able to do it with the owner of the trace
700 basic_authorization public_trace_file.user.display_name, "test"
701 get :api_read, :params => { :id => public_trace_file.id }
702 assert_response :success
705 # Check an anoymous trace can't be specifically fetched by another user
706 def test_api_read_anon
707 anon_trace_file = create(:trace, :visibility => "private")
710 get :api_read, :params => { :id => anon_trace_file.id }
711 assert_response :unauthorized
713 # Now try with another user, which shouldn't work since the trace is anon
714 basic_authorization create(:user).display_name, "test"
715 get :api_read, :params => { :id => anon_trace_file.id }
716 assert_response :forbidden
718 # And finally we should be able to get the trace details with the trace owner
719 basic_authorization anon_trace_file.user.display_name, "test"
720 get :api_read, :params => { :id => anon_trace_file.id }
721 assert_response :success
724 # Check the api details for a trace that doesn't exist
725 def test_api_read_not_found
726 deleted_trace_file = create(:trace, :deleted)
728 # Try first with no auth, as it should require it
729 get :api_read, :params => { :id => 0 }
730 assert_response :unauthorized
732 # Login, and try again
733 basic_authorization deleted_trace_file.user.display_name, "test"
734 get :api_read, :params => { :id => 0 }
735 assert_response :not_found
737 # Now try a trace which did exist but has been deleted
738 basic_authorization deleted_trace_file.user.display_name, "test"
739 get :api_read, :params => { :id => deleted_trace_file.id }
740 assert_response :not_found
743 # Test downloading a trace through the api
745 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
748 get :api_data, :params => { :id => public_trace_file.id }
749 assert_response :unauthorized
751 # Now with some other user, which should work since the trace is public
752 basic_authorization create(:user).display_name, "test"
753 get :api_data, :params => { :id => public_trace_file.id }
754 check_trace_data public_trace_file
756 # And finally we should be able to do it with the owner of the trace
757 basic_authorization public_trace_file.user.display_name, "test"
758 get :api_data, :params => { :id => public_trace_file.id }
759 check_trace_data public_trace_file
762 # Test downloading a compressed trace through the api
763 def test_api_data_compressed
764 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
766 # Authenticate as the owner of the trace we will be using
767 basic_authorization identifiable_trace_file.user.display_name, "test"
769 # First get the data as is
770 get :api_data, :params => { :id => identifiable_trace_file.id }
771 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
773 # Now ask explicitly for XML format
774 get :api_data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
775 check_trace_data identifiable_trace_file, "application/xml", "xml"
777 # Now ask explicitly for GPX format
778 get :api_data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
779 check_trace_data identifiable_trace_file
782 # Check an anonymous trace can't be downloaded by another user through the api
783 def test_api_data_anon
784 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
787 get :api_data, :params => { :id => anon_trace_file.id }
788 assert_response :unauthorized
790 # Now with some other user, which shouldn't work since the trace is anon
791 basic_authorization create(:user).display_name, "test"
792 get :api_data, :params => { :id => anon_trace_file.id }
793 assert_response :forbidden
795 # And finally we should be able to do it with the owner of the trace
796 basic_authorization anon_trace_file.user.display_name, "test"
797 get :api_data, :params => { :id => anon_trace_file.id }
798 check_trace_data anon_trace_file
801 # Test downloading a trace that doesn't exist through the api
802 def test_api_data_not_found
803 deleted_trace_file = create(:trace, :deleted)
805 # Try first with no auth, as it should require it
806 get :api_data, :params => { :id => 0 }
807 assert_response :unauthorized
809 # Login, and try again
810 basic_authorization create(:user).display_name, "test"
811 get :api_data, :params => { :id => 0 }
812 assert_response :not_found
814 # Now try a trace which did exist but has been deleted
815 basic_authorization deleted_trace_file.user.display_name, "test"
816 get :api_data, :params => { :id => deleted_trace_file.id }
817 assert_response :not_found
820 # Test creating a trace through the api
823 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
824 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
828 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
829 assert_response :unauthorized
832 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
833 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
834 basic_authorization user.display_name, "test"
835 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
836 assert_response :success
837 trace = Trace.find(response.body.to_i)
838 assert_equal "a.gpx", trace.name
839 assert_equal "New Trace", trace.description
840 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
841 assert_equal "trackable", trace.visibility
842 assert_equal false, trace.inserted
843 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
845 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
850 # Now authenticated, with the legacy public flag
851 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
852 basic_authorization user.display_name, "test"
853 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }
854 assert_response :success
855 trace = Trace.find(response.body.to_i)
856 assert_equal "a.gpx", trace.name
857 assert_equal "New Trace", trace.description
858 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
859 assert_equal "public", trace.visibility
860 assert_equal false, trace.inserted
861 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
863 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
868 # Now authenticated, with the legacy private flag
869 second_user = create(:user)
870 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
871 basic_authorization second_user.display_name, "test"
872 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }
873 assert_response :success
874 trace = Trace.find(response.body.to_i)
875 assert_equal "a.gpx", trace.name
876 assert_equal "New Trace", trace.description
877 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
878 assert_equal "private", trace.visibility
879 assert_equal false, trace.inserted
880 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
882 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
885 # Check updating a trace through the api
887 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
888 deleted_trace_file = create(:trace, :deleted)
889 anon_trace_file = create(:trace, :visibility => "private")
892 content public_trace_file.to_xml
893 put :api_update, :params => { :id => public_trace_file.id }
894 assert_response :unauthorized
896 # Now with some other user, which should fail
897 basic_authorization create(:user).display_name, "test"
898 content public_trace_file.to_xml
899 put :api_update, :params => { :id => public_trace_file.id }
900 assert_response :forbidden
902 # Now with a trace which doesn't exist
903 basic_authorization create(:user).display_name, "test"
904 content public_trace_file.to_xml
905 put :api_update, :params => { :id => 0 }
906 assert_response :not_found
908 # Now with a trace which did exist but has been deleted
909 basic_authorization deleted_trace_file.user.display_name, "test"
910 content deleted_trace_file.to_xml
911 put :api_update, :params => { :id => deleted_trace_file.id }
912 assert_response :not_found
914 # Now try an update with the wrong ID
915 basic_authorization public_trace_file.user.display_name, "test"
916 content anon_trace_file.to_xml
917 put :api_update, :params => { :id => public_trace_file.id }
918 assert_response :bad_request,
919 "should not be able to update a trace with a different ID from the XML"
921 # And finally try an update that should work
922 basic_authorization public_trace_file.user.display_name, "test"
923 t = public_trace_file
924 t.description = "Changed description"
925 t.visibility = "private"
927 put :api_update, :params => { :id => t.id }
928 assert_response :success
929 nt = Trace.find(t.id)
930 assert_equal nt.description, t.description
931 assert_equal nt.visibility, t.visibility
934 # Test that updating a trace doesn't duplicate the tags
935 def test_api_update_tags
936 tracetag = create(:tracetag)
937 trace = tracetag.trace
938 basic_authorization trace.user.display_name, "test"
941 put :api_update, :params => { :id => trace.id }
942 assert_response :success
944 updated = Trace.find(trace.id)
945 assert_equal trace.tags, updated.tags
948 # Check deleting a trace through the api
950 public_trace_file = create(:trace, :visibility => "public")
953 delete :api_delete, :params => { :id => public_trace_file.id }
954 assert_response :unauthorized
956 # Now with some other user, which should fail
957 basic_authorization create(:user).display_name, "test"
958 delete :api_delete, :params => { :id => public_trace_file.id }
959 assert_response :forbidden
961 # Now with a trace which doesn't exist
962 basic_authorization create(:user).display_name, "test"
963 delete :api_delete, :params => { :id => 0 }
964 assert_response :not_found
966 # And finally we should be able to do it with the owner of the trace
967 basic_authorization public_trace_file.user.display_name, "test"
968 delete :api_delete, :params => { :id => public_trace_file.id }
969 assert_response :success
971 # Try it a second time, which should fail
972 basic_authorization public_trace_file.user.display_name, "test"
973 delete :api_delete, :params => { :id => public_trace_file.id }
974 assert_response :not_found
979 def check_trace_feed(traces)
980 assert_response :success
981 assert_template "georss"
982 assert_equal "application/rss+xml", @response.content_type
983 assert_select "rss", :count => 1 do
984 assert_select "channel", :count => 1 do
985 assert_select "title"
986 assert_select "description"
988 assert_select "image"
989 assert_select "item", :count => traces.visible.count do |items|
990 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
991 assert_select item, "title", trace.name
992 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
993 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
994 assert_select item, "description"
995 # assert_select item, "dc:creator", trace.user.display_name
996 assert_select item, "pubDate", trace.timestamp.rfc822
1003 def check_trace_list(traces)
1004 assert_response :success
1005 assert_template "list"
1008 assert_select "table#trace_list tbody", :count => 1 do
1009 assert_select "tr", :count => traces.length do |rows|
1010 traces.zip(rows).each do |trace, row|
1011 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
1012 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
1013 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
1014 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
1019 assert_select "h4", /Nothing here yet/
1023 def check_trace_view(trace)
1024 assert_response :success
1025 assert_template "view"
1027 assert_select "table", :count => 1 do
1028 assert_select "td", /^#{Regexp.quote(trace.name)} /
1029 assert_select "td", trace.user.display_name
1030 assert_select "td", trace.description
1034 def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
1035 assert_response :success
1036 assert_equal content_type, response.content_type
1037 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
1040 def check_trace_picture(trace)
1041 assert_response :success
1042 assert_equal "image/gif", response.content_type
1043 assert_equal trace.large_picture, response.body
1046 def check_trace_icon(trace)
1047 assert_response :success
1048 assert_equal "image/gif", response.content_type
1049 assert_equal trace.icon_picture, response.body