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/edit", :method => :patch },
162 { :controller => "trace", :action => "edit", :id => "1" }
165 { :path => "/trace/1/delete", :method => :post },
166 { :controller => "trace", :action => "delete", :id => "1" }
170 # Check that the list of traces is displayed
173 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
174 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
175 create(:tracetag, :trace => trace, :tag => "London")
177 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
178 create(:tracetag, :trace => trace, :tag => "Birmingham")
180 trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
181 create(:tracetag, :trace => trace, :tag => "London")
183 trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
184 create(:tracetag, :trace => trace, :tag => "Birmingham")
187 # First with the public list
189 check_trace_list [trace_b, trace_a]
191 # Restrict traces to those with a given tag
192 get :list, :tag => "London"
193 check_trace_list [trace_a]
195 # Should see more when we are logged in
196 get :list, {}, { :user => user }
197 check_trace_list [trace_d, trace_c, trace_b, trace_a]
199 # Again, we should see more when we are logged in
200 get :list, { :tag => "London" }, { :user => user }
201 check_trace_list [trace_c, trace_a]
204 # 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 => 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 => user }
220 assert_redirected_to :controller => "trace", :action => "list", :display_name => user.display_name
222 # Fetch the actual list
223 get :list, { :display_name => user.display_name }, { :user => user }
224 check_trace_list [trace_b]
227 # Check the list of traces for a specific user
230 second_user = create(:user)
231 third_user = create(:user)
233 trace_b = create(:trace, :visibility => "public", :user => user)
234 trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
235 create(:tracetag, :trace => trace, :tag => "London")
238 # Test a user with no traces
239 get :list, :display_name => second_user.display_name
242 # Test the user with the traces - should see only public ones
243 get :list, :display_name => user.display_name
244 check_trace_list [trace_b]
246 # Should still see only public ones when authenticated as another user
247 get :list, { :display_name => user.display_name }, { :user => third_user }
248 check_trace_list [trace_b]
250 # Should see all traces when authenticated as the target user
251 get :list, { :display_name => user.display_name }, { :user => user }
252 check_trace_list [trace_c, trace_b]
254 # Should only see traces with the correct tag when a tag is specified
255 get :list, { :display_name => user.display_name, :tag => "London" }, { :user => user }
256 check_trace_list [trace_c]
258 # Should get an error if the user does not exist
259 get :list, :display_name => "UnknownUser"
260 assert_response :not_found
261 assert_template "user/no_such_user"
264 # Check that the rss loads
268 # First with the public feed
269 get :georss, :format => :rss
270 check_trace_feed Trace.visible_to_all
272 # Restrict traces to those with a given tag
273 get :georss, :tag => "London", :format => :rss
274 check_trace_feed Trace.tagged("London").visible_to_all
276 # Restrict traces to those for a given user
277 get :georss, :display_name => user.display_name, :format => :rss
278 check_trace_feed user.traces.visible_to_all
280 # Restrict traces to those for a given user with a tiven tag
281 get :georss, :display_name => user.display_name, :tag => "Birmingham", :format => :rss
282 check_trace_feed user.traces.tagged("Birmingham").visible_to_all
285 # Test viewing a trace
287 public_trace_file = create(:trace, :visibility => "public")
289 # First with no auth, which should work since the trace is public
290 get :view, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
291 check_trace_view public_trace_file
293 # Now with some other user, which should work since the trace is public
294 get :view, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
295 check_trace_view public_trace_file
297 # And finally we should be able to do it with the owner of the trace
298 get :view, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
299 check_trace_view public_trace_file
302 # Check an anonymous trace can't be viewed by another user
304 anon_trace_file = create(:trace, :visibility => "private")
307 get :view, :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id
308 assert_response :redirect
309 assert_redirected_to :action => :list
311 # Now with some other user, which should not work since the trace is anon
312 get :view, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => create(:user) }
313 assert_response :redirect
314 assert_redirected_to :action => :list
316 # And finally we should be able to do it with the owner of the trace
317 get :view, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => anon_trace_file.user }
318 check_trace_view anon_trace_file
321 # Test viewing a trace that doesn't exist
322 def test_view_not_found
323 deleted_trace_file = create(:trace, :deleted)
325 # First with a trace that has never existed
326 get :view, :display_name => create(:user).display_name, :id => 0
327 assert_response :redirect
328 assert_redirected_to :action => :list
330 # Now with a trace that has been deleted
331 get :view, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
332 assert_response :redirect
333 assert_redirected_to :action => :list
336 # Test downloading a trace
338 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
340 # First with no auth, which should work since the trace is public
341 get :data, :display_name => public_trace_file.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 => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
346 check_trace_data public_trace_file
348 # And finally we should be able to do it with the owner of the trace
349 get :data, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
350 check_trace_data public_trace_file
353 # Test downloading a compressed trace
354 def test_data_compressed
355 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
357 # First get the data as is
358 get :data, :display_name => identifiable_trace_file.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 => identifiable_trace_file.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 => identifiable_trace_file.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", :fixture => "b")
375 get :data, :display_name => anon_trace_file.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 => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => create(:user) }
380 assert_response :not_found
382 # And finally we should be able to do it with the owner of the trace
383 get :data, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => anon_trace_file.user }
384 check_trace_data anon_trace_file
387 # Test downloading a trace that doesn't exist
388 def test_data_not_found
389 deleted_trace_file = create(:trace, :deleted)
391 # First with a trace that has never existed
392 get :data, :display_name => create(:user).display_name, :id => 0
393 assert_response :not_found
395 # Now with a trace that has been deleted
396 get :data, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
397 assert_response :not_found
400 # Test downloading the picture for a trace
402 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
404 # First with no auth, which should work since the trace is public
405 get :picture, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
406 check_trace_picture public_trace_file
408 # Now with some other user, which should work since the trace is public
409 get :picture, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
410 check_trace_picture public_trace_file
412 # And finally we should be able to do it with the owner of the trace
413 get :picture, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
414 check_trace_picture public_trace_file
417 # Check the picture for an anonymous trace can't be downloaded by another user
418 def test_picture_anon
419 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
422 get :picture, :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id
423 assert_response :forbidden
425 # Now with some other user, which shouldn't work since the trace is anon
426 get :picture, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => create(:user) }
427 assert_response :forbidden
429 # And finally we should be able to do it with the owner of the trace
430 get :picture, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => anon_trace_file.user }
431 check_trace_picture anon_trace_file
434 # Test downloading the picture for a trace that doesn't exist
435 def test_picture_not_found
436 deleted_trace_file = create(:trace, :deleted)
438 # First with a trace that has never existed
439 get :picture, :display_name => create(:user).display_name, :id => 0
440 assert_response :not_found
442 # Now with a trace that has been deleted
443 get :picture, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
444 assert_response :not_found
447 # Test downloading the icon for a trace
449 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
451 # First with no auth, which should work since the trace is public
452 get :icon, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
453 check_trace_icon public_trace_file
455 # Now with some other user, which should work since the trace is public
456 get :icon, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
457 check_trace_icon public_trace_file
459 # And finally we should be able to do it with the owner of the trace
460 get :icon, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
461 check_trace_icon public_trace_file
464 # Check the icon for an anonymous trace can't be downloaded by another user
466 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
469 get :icon, :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id
470 assert_response :forbidden
472 # Now with some other user, which shouldn't work since the trace is anon
473 get :icon, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => create(:user) }
474 assert_response :forbidden
476 # And finally we should be able to do it with the owner of the trace
477 get :icon, { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, { :user => anon_trace_file.user }
478 check_trace_icon anon_trace_file
481 # Test downloading the icon for a trace that doesn't exist
482 def test_icon_not_found
483 deleted_trace_file = create(:trace, :deleted)
485 # First with a trace that has never existed
486 get :icon, :display_name => create(:user).display_name, :id => 0
487 assert_response :not_found
489 # Now with a trace that has been deleted
490 get :icon, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
491 assert_response :not_found
494 # Test fetching the create page
498 assert_response :redirect
499 assert_redirected_to :controller => :user, :action => :login, :referer => trace_create_path
501 # Now authenticated as a user with gps.trace.visibility set
503 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
504 get :create, {}, { :user => user }
505 assert_response :success
506 assert_template :create
507 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
509 # Now authenticated as a user with gps.trace.public set
510 second_user = create(:user)
511 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
512 get :create, {}, { :user => second_user }
513 assert_response :success
514 assert_template :create
515 assert_select "select#trace_visibility option[value=public][selected]", 1
517 # Now authenticated as a user with no preferences
518 third_user = create(:user)
519 get :create, {}, { :user => third_user }
520 assert_response :success
521 assert_template :create
522 assert_select "select#trace_visibility option[value=private][selected]", 1
525 # Test creating a trace
528 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
529 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
533 post :create, :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" }
534 assert_response :forbidden
537 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
538 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
539 post :create, { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, { :user => user }
540 assert_response :redirect
541 assert_redirected_to :action => :list, :display_name => user.display_name
542 assert_match /file has been uploaded/, flash[:notice]
543 trace = Trace.order(:id => :desc).first
544 assert_equal "a.gpx", trace.name
545 assert_equal "New Trace", trace.description
546 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
547 assert_equal "trackable", trace.visibility
548 assert_equal false, trace.inserted
549 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
551 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
554 # Test fetching the edit page for a trace using GET
556 public_trace_file = create(:trace, :visibility => "public")
557 deleted_trace_file = create(:trace, :deleted)
560 get :edit, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
561 assert_response :redirect
562 assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)
564 # Now with some other user, which should fail
565 get :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
566 assert_response :forbidden
568 # Now with a trace which doesn't exist
569 get :edit, { :display_name => create(:user).display_name, :id => 0 }, { :user => create(:user) }
570 assert_response :not_found
572 # Now with a trace which has been deleted
573 get :edit, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
574 assert_response :not_found
576 # Finally with a trace that we are allowed to edit
577 get :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
578 assert_response :success
581 # Test fetching the edit page for a trace using POST
582 def test_edit_post_no_details
583 public_trace_file = create(:trace, :visibility => "public")
584 deleted_trace_file = create(:trace, :deleted)
587 post :edit, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
588 assert_response :forbidden
590 # Now with some other user, which should fail
591 post :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
592 assert_response :forbidden
594 # Now with a trace which doesn't exist
595 post :edit, { :display_name => create(:user).display_name, :id => 0 }, { :user => create(:user) }
596 assert_response :not_found
598 # Now with a trace which has been deleted
599 post :edit, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
600 assert_response :not_found
602 # Finally with a trace that we are allowed to edit
603 post :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
604 assert_response :success
607 # Test saving edits to a trace
608 def test_edit_post_with_details
609 public_trace_file = create(:trace, :visibility => "public")
610 deleted_trace_file = create(:trace, :deleted)
613 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
616 post :edit, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details
617 assert_response :forbidden
619 # Now with some other user, which should fail
620 post :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, { :user => create(:user) }
621 assert_response :forbidden
623 # Now with a trace which doesn't exist
624 post :edit, { :display_name => create(:user).display_name, :id => 0 }, { :user => create(:user), :trace => new_details }
625 assert_response :not_found
627 # Now with a trace which has been deleted
628 post :edit, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id, :trace => new_details }, { :user => deleted_trace_file.user }
629 assert_response :not_found
631 # Finally with a trace that we are allowed to edit
632 post :edit, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, { :user => public_trace_file.user }
633 assert_response :redirect
634 assert_redirected_to :action => :view, :display_name => public_trace_file.user.display_name
635 trace = Trace.find(public_trace_file.id)
636 assert_equal new_details[:description], trace.description
637 assert_equal new_details[:tagstring], trace.tagstring
638 assert_equal new_details[:visibility], trace.visibility
641 # Test deleting a trace
643 public_trace_file = create(:trace, :visibility => "public")
644 deleted_trace_file = create(:trace, :deleted)
647 post :delete, :display_name => public_trace_file.user.display_name, :id => public_trace_file.id
648 assert_response :forbidden
650 # Now with some other user, which should fail
651 post :delete, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => create(:user) }
652 assert_response :forbidden
654 # Now with a trace which doesn't exist
655 post :delete, { :display_name => create(:user).display_name, :id => 0 }, { :user => create(:user) }
656 assert_response :not_found
658 # Now with a trace has already been deleted
659 post :delete, { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, { :user => deleted_trace_file.user }
660 assert_response :not_found
662 # Finally with a trace that we are allowed to delete
663 post :delete, { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, { :user => public_trace_file.user }
664 assert_response :redirect
665 assert_redirected_to :action => :list, :display_name => public_trace_file.user.display_name
666 trace = Trace.find(public_trace_file.id)
667 assert_equal false, trace.visible
670 # Check getting a specific trace through the api
672 public_trace_file = create(:trace, :visibility => "public")
675 get :api_read, :id => public_trace_file.id
676 assert_response :unauthorized
678 # Now with some other user, which should work since the trace is public
679 basic_authorization(create(:user).display_name, "test")
680 get :api_read, :id => public_trace_file.id
681 assert_response :success
683 # And finally we should be able to do it with the owner of the trace
684 basic_authorization(public_trace_file.user.display_name, "test")
685 get :api_read, :id => public_trace_file.id
686 assert_response :success
689 # Check an anoymous trace can't be specifically fetched by another user
690 def test_api_read_anon
691 anon_trace_file = create(:trace, :visibility => "private")
694 get :api_read, :id => anon_trace_file.id
695 assert_response :unauthorized
697 # Now try with another user, which shouldn't work since the trace is anon
698 basic_authorization(create(:user).display_name, "test")
699 get :api_read, :id => anon_trace_file.id
700 assert_response :forbidden
702 # And finally we should be able to get the trace details with the trace owner
703 basic_authorization(anon_trace_file.user.display_name, "test")
704 get :api_read, :id => anon_trace_file.id
705 assert_response :success
708 # Check the api details for a trace that doesn't exist
709 def test_api_read_not_found
710 deleted_trace_file = create(:trace, :deleted)
712 # Try first with no auth, as it should require it
713 get :api_read, :id => 0
714 assert_response :unauthorized
716 # Login, and try again
717 basic_authorization(deleted_trace_file.user.display_name, "test")
718 get :api_read, :id => 0
719 assert_response :not_found
721 # Now try a trace which did exist but has been deleted
722 basic_authorization(deleted_trace_file.user.display_name, "test")
723 get :api_read, :id => deleted_trace_file.id
724 assert_response :not_found
727 # Test downloading a trace through the api
729 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
732 get :api_data, :id => public_trace_file.id
733 assert_response :unauthorized
735 # Now with some other user, which should work since the trace is public
736 basic_authorization(create(:user).display_name, "test")
737 get :api_data, :id => public_trace_file.id
738 check_trace_data public_trace_file
740 # And finally we should be able to do it with the owner of the trace
741 basic_authorization(public_trace_file.user.display_name, "test")
742 get :api_data, :id => public_trace_file.id
743 check_trace_data public_trace_file
746 # Test downloading a compressed trace through the api
747 def test_api_data_compressed
748 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
750 # Authenticate as the owner of the trace we will be using
751 basic_authorization(identifiable_trace_file.user.display_name, "test")
753 # First get the data as is
754 get :api_data, :id => identifiable_trace_file.id
755 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
757 # Now ask explicitly for XML format
758 get :api_data, :id => identifiable_trace_file.id, :format => "xml"
759 check_trace_data identifiable_trace_file, "application/xml", "xml"
761 # Now ask explicitly for GPX format
762 get :api_data, :id => identifiable_trace_file.id, :format => "gpx"
763 check_trace_data identifiable_trace_file
766 # Check an anonymous trace can't be downloaded by another user through the api
767 def test_api_data_anon
768 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
771 get :api_data, :id => anon_trace_file.id
772 assert_response :unauthorized
774 # Now with some other user, which shouldn't work since the trace is anon
775 basic_authorization(create(:user).display_name, "test")
776 get :api_data, :id => anon_trace_file.id
777 assert_response :forbidden
779 # And finally we should be able to do it with the owner of the trace
780 basic_authorization(anon_trace_file.user.display_name, "test")
781 get :api_data, :id => anon_trace_file.id
782 check_trace_data anon_trace_file
785 # Test downloading a trace that doesn't exist through the api
786 def test_api_data_not_found
787 deleted_trace_file = create(:trace, :deleted)
789 # Try first with no auth, as it should require it
790 get :api_data, :id => 0
791 assert_response :unauthorized
793 # Login, and try again
794 basic_authorization(create(:user).display_name, "test")
795 get :api_data, :id => 0
796 assert_response :not_found
798 # Now try a trace which did exist but has been deleted
799 basic_authorization(deleted_trace_file.user.display_name, "test")
800 get :api_data, :id => deleted_trace_file.id
801 assert_response :not_found
804 # Test creating a trace through the api
807 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
808 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
812 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
813 assert_response :unauthorized
816 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
817 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
818 basic_authorization(user.display_name, "test")
819 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
820 assert_response :success
821 trace = Trace.find(response.body.to_i)
822 assert_equal "a.gpx", trace.name
823 assert_equal "New Trace", trace.description
824 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
825 assert_equal "trackable", trace.visibility
826 assert_equal false, trace.inserted
827 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
829 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
834 # Now authenticated, with the legacy public flag
835 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
836 basic_authorization(user.display_name, "test")
837 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 1
838 assert_response :success
839 trace = Trace.find(response.body.to_i)
840 assert_equal "a.gpx", trace.name
841 assert_equal "New Trace", trace.description
842 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
843 assert_equal "public", trace.visibility
844 assert_equal false, trace.inserted
845 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
847 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
852 # Now authenticated, with the legacy private flag
853 second_user = create(:user)
854 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
855 basic_authorization(second_user.display_name, "test")
856 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 0
857 assert_response :success
858 trace = Trace.find(response.body.to_i)
859 assert_equal "a.gpx", trace.name
860 assert_equal "New Trace", trace.description
861 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
862 assert_equal "private", trace.visibility
863 assert_equal false, trace.inserted
864 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
866 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
869 # Check updating a trace through the api
871 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
872 deleted_trace_file = create(:trace, :deleted)
873 anon_trace_file = create(:trace, :visibility => "private")
876 content public_trace_file.to_xml
877 put :api_update, :id => public_trace_file.id
878 assert_response :unauthorized
880 # Now with some other user, which should fail
881 basic_authorization(create(:user).display_name, "test")
882 content public_trace_file.to_xml
883 put :api_update, :id => public_trace_file.id
884 assert_response :forbidden
886 # Now with a trace which doesn't exist
887 basic_authorization(create(:user).display_name, "test")
888 content public_trace_file.to_xml
889 put :api_update, :id => 0
890 assert_response :not_found
892 # Now with a trace which did exist but has been deleted
893 basic_authorization(deleted_trace_file.user.display_name, "test")
894 content deleted_trace_file.to_xml
895 put :api_update, :id => deleted_trace_file.id
896 assert_response :not_found
898 # Now try an update with the wrong ID
899 basic_authorization(public_trace_file.user.display_name, "test")
900 content anon_trace_file.to_xml
901 put :api_update, :id => public_trace_file.id
902 assert_response :bad_request,
903 "should not be able to update a trace with a different ID from the XML"
905 # And finally try an update that should work
906 basic_authorization(public_trace_file.user.display_name, "test")
907 t = public_trace_file
908 t.description = "Changed description"
909 t.visibility = "private"
911 put :api_update, :id => t.id
912 assert_response :success
913 nt = Trace.find(t.id)
914 assert_equal nt.description, t.description
915 assert_equal nt.visibility, t.visibility
918 # Check deleting a trace through the api
920 public_trace_file = create(:trace, :visibility => "public")
923 delete :api_delete, :id => public_trace_file.id
924 assert_response :unauthorized
926 # Now with some other user, which should fail
927 basic_authorization(create(:user).display_name, "test")
928 delete :api_delete, :id => public_trace_file.id
929 assert_response :forbidden
931 # Now with a trace which doesn't exist
932 basic_authorization(create(:user).display_name, "test")
933 delete :api_delete, :id => 0
934 assert_response :not_found
936 # And finally we should be able to do it with the owner of the trace
937 basic_authorization(public_trace_file.user.display_name, "test")
938 delete :api_delete, :id => public_trace_file.id
939 assert_response :success
941 # Try it a second time, which should fail
942 basic_authorization(public_trace_file.user.display_name, "test")
943 delete :api_delete, :id => public_trace_file.id
944 assert_response :not_found
949 def check_trace_feed(traces)
950 assert_response :success
951 assert_template "georss"
952 assert_equal "application/rss+xml", @response.content_type
953 assert_select "rss", :count => 1 do
954 assert_select "channel", :count => 1 do
955 assert_select "title"
956 assert_select "description"
958 assert_select "image"
959 assert_select "item", :count => traces.visible.count do |items|
960 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
961 assert_select item, "title", trace.name
962 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
963 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
964 assert_select item, "description"
965 # assert_select item, "dc:creator", trace.user.display_name
966 assert_select item, "pubDate", trace.timestamp.rfc822
973 def check_trace_list(traces)
974 assert_response :success
975 assert_template "list"
978 assert_select "table#trace_list tbody", :count => 1 do
979 assert_select "tr", :count => traces.length do |rows|
980 traces.zip(rows).each do |trace, row|
981 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
982 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
983 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
984 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
989 assert_select "h4", /Nothing here yet/
993 def check_trace_view(trace)
994 assert_response :success
995 assert_template "view"
997 assert_select "table", :count => 1 do
998 assert_select "td", /^#{Regexp.quote(trace.name)} /
999 assert_select "td", trace.user.display_name
1000 assert_select "td", trace.description
1004 def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
1005 assert_response :success
1006 assert_equal content_type, response.content_type
1007 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
1010 def check_trace_picture(trace)
1011 assert_response :success
1012 assert_equal "image/gif", response.content_type
1013 assert_equal trace.large_picture, response.body
1016 def check_trace_icon(trace)
1017 assert_response :success
1018 assert_equal "image/gif", response.content_type
1019 assert_equal trace.icon_picture, response.body