2 require "minitest/mock"
4 class TracesControllerTest < 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 => "traces", :action => "api_create" }
32 { :path => "/api/0.6/gpx/1", :method => :get },
33 { :controller => "traces", :action => "api_read", :id => "1" }
36 { :path => "/api/0.6/gpx/1", :method => :put },
37 { :controller => "traces", :action => "api_update", :id => "1" }
40 { :path => "/api/0.6/gpx/1", :method => :delete },
41 { :controller => "traces", :action => "api_delete", :id => "1" }
44 { :controller => "traces", :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 => "traces", :action => "api_data", :id => "1" }
52 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
53 { :controller => "traces", :action => "api_data", :id => "1", :format => "xml" }
57 { :path => "/traces", :method => :get },
58 { :controller => "traces", :action => "index" }
61 { :path => "/traces/page/1", :method => :get },
62 { :controller => "traces", :action => "index", :page => "1" }
65 { :path => "/traces/tag/tagname", :method => :get },
66 { :controller => "traces", :action => "index", :tag => "tagname" }
69 { :path => "/traces/tag/tagname/page/1", :method => :get },
70 { :controller => "traces", :action => "index", :tag => "tagname", :page => "1" }
73 { :path => "/user/username/traces", :method => :get },
74 { :controller => "traces", :action => "index", :display_name => "username" }
77 { :path => "/user/username/traces/page/1", :method => :get },
78 { :controller => "traces", :action => "index", :display_name => "username", :page => "1" }
81 { :path => "/user/username/traces/tag/tagname", :method => :get },
82 { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
85 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
86 { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname", :page => "1" }
90 { :path => "/traces/mine", :method => :get },
91 { :controller => "traces", :action => "mine" }
94 { :path => "/traces/mine/page/1", :method => :get },
95 { :controller => "traces", :action => "mine", :page => "1" }
98 { :path => "/traces/mine/tag/tagname", :method => :get },
99 { :controller => "traces", :action => "mine", :tag => "tagname" }
102 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
103 { :controller => "traces", :action => "mine", :tag => "tagname", :page => "1" }
107 { :path => "/traces/rss", :method => :get },
108 { :controller => "traces", :action => "georss", :format => :rss }
111 { :path => "/traces/tag/tagname/rss", :method => :get },
112 { :controller => "traces", :action => "georss", :tag => "tagname", :format => :rss }
115 { :path => "/user/username/traces/rss", :method => :get },
116 { :controller => "traces", :action => "georss", :display_name => "username", :format => :rss }
119 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
120 { :controller => "traces", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
124 { :path => "/user/username/traces/1", :method => :get },
125 { :controller => "traces", :action => "show", :display_name => "username", :id => "1" }
128 { :path => "/user/username/traces/1/picture", :method => :get },
129 { :controller => "traces", :action => "picture", :display_name => "username", :id => "1" }
132 { :path => "/user/username/traces/1/icon", :method => :get },
133 { :controller => "traces", :action => "icon", :display_name => "username", :id => "1" }
137 { :path => "/traces/new", :method => :get },
138 { :controller => "traces", :action => "new" }
141 { :path => "/traces", :method => :post },
142 { :controller => "traces", :action => "create" }
145 { :path => "/trace/1/data", :method => :get },
146 { :controller => "traces", :action => "data", :id => "1" }
149 { :path => "/trace/1/data.xml", :method => :get },
150 { :controller => "traces", :action => "data", :id => "1", :format => "xml" }
153 { :path => "/traces/1/edit", :method => :get },
154 { :controller => "traces", :action => "edit", :id => "1" }
157 { :path => "/traces/1", :method => :put },
158 { :controller => "traces", :action => "update", :id => "1" }
161 { :path => "/trace/1/delete", :method => :post },
162 { :controller => "traces", :action => "delete", :id => "1" }
166 # Check that the index 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 index
185 check_trace_index [trace_b, trace_a]
187 # Restrict traces to those with a given tag
188 get :index, :params => { :tag => "London" }
189 check_trace_index [trace_a]
191 # Should see more when we are logged in
192 get :index, :session => { :user => user }
193 check_trace_index [trace_d, trace_c, trace_b, trace_a]
195 # Again, we should see more when we are logged in
196 get :index, :params => { :tag => "London" }, :session => { :user => user }
197 check_trace_index [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 => "users", :action => "login", :referer => "/traces/mine"
214 # Now try when logged in
215 get :mine, :session => { :user => user }
216 assert_redirected_to :action => "index", :display_name => user.display_name
218 # Fetch the actual index
219 get :index, :params => { :display_name => user.display_name }, :session => { :user => user }
220 check_trace_index [trace_b]
223 # Check the index 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 :index, :params => { :display_name => second_user.display_name }
238 # Test the user with the traces - should see only public ones
239 get :index, :params => { :display_name => user.display_name }
240 check_trace_index [trace_b]
242 # Should still see only public ones when authenticated as another user
243 get :index, :params => { :display_name => user.display_name }, :session => { :user => third_user }
244 check_trace_index [trace_b]
246 # Should see all traces when authenticated as the target user
247 get :index, :params => { :display_name => user.display_name }, :session => { :user => user }
248 check_trace_index [trace_c, trace_b]
250 # Should only see traces with the correct tag when a tag is specified
251 get :index, :params => { :display_name => user.display_name, :tag => "London" }, :session => { :user => user }
252 check_trace_index [trace_c]
254 # Should get an error if the user does not exist
255 get :index, :params => { :display_name => "UnknownUser" }
256 assert_response :not_found
257 assert_template "users/no_such_user"
260 # Check a multi-page index
262 # Create several pages worth of traces
263 create_list(:trace, 50)
265 # Try and get the index
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 :index, :params => { :page => 2 }
274 assert_response :success
275 assert_select "table#trace_list tbody", :count => 1 do
276 assert_select "tr", :count => 20
283 # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
284 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
285 create(:tracetag, :trace => trace, :tag => "London")
287 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
288 create(:tracetag, :trace => trace, :tag => "Birmingham")
290 create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
291 create(:tracetag, :trace => trace, :tag => "London")
293 create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
294 create(:tracetag, :trace => trace, :tag => "Birmingham")
297 # First with the public feed
298 get :georss, :params => { :format => :rss }
299 check_trace_feed [trace_b, trace_a]
301 # Restrict traces to those with a given tag
302 get :georss, :params => { :tag => "London", :format => :rss }
303 check_trace_feed [trace_a]
306 # Check the RSS feed for a specific user
309 second_user = create(:user)
312 trace_b = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago, :user => user)
313 trace_c = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago, :user => user) do |trace|
314 create(:tracetag, :trace => trace, :tag => "London")
316 create(:trace, :visibility => "private")
318 # Test a user with no traces
319 get :georss, :params => { :display_name => second_user.display_name, :format => :rss }
322 # Test the user with the traces - should see only public ones
323 get :georss, :params => { :display_name => user.display_name, :format => :rss }
324 check_trace_feed [trace_c, trace_b]
326 # Should only see traces with the correct tag when a tag is specified
327 get :georss, :params => { :display_name => user.display_name, :tag => "London", :format => :rss }
328 check_trace_feed [trace_c]
330 # Should no traces if the user does not exist
331 get :georss, :params => { :display_name => "UnknownUser", :format => :rss }
335 # Test showing a trace
337 public_trace_file = create(:trace, :visibility => "public")
339 # First with no auth, which should work since the trace is public
340 get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
341 check_trace_show public_trace_file
343 # Now with some other user, which should work since the trace is public
344 get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
345 check_trace_show public_trace_file
347 # And finally we should be able to do it with the owner of the trace
348 get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
349 check_trace_show public_trace_file
352 # Check an anonymous trace can't be viewed by another user
354 anon_trace_file = create(:trace, :visibility => "private")
357 get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
358 assert_response :redirect
359 assert_redirected_to :action => :index
361 # Now with some other user, which should not work since the trace is anon
362 get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
363 assert_response :redirect
364 assert_redirected_to :action => :index
366 # And finally we should be able to do it with the owner of the trace
367 get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
368 check_trace_show anon_trace_file
371 # Test showing a trace that doesn't exist
372 def test_show_not_found
373 deleted_trace_file = create(:trace, :deleted)
375 # First with a trace that has never existed
376 get :show, :params => { :display_name => create(:user).display_name, :id => 0 }
377 assert_response :redirect
378 assert_redirected_to :action => :index
380 # Now with a trace that has been deleted
381 get :show, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
382 assert_response :redirect
383 assert_redirected_to :action => :index
386 # Test downloading a trace
388 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
390 # First with no auth, which should work since the trace is public
391 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
392 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
394 # Now with some other user, which should work since the trace is public
395 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
396 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
398 # And finally we should be able to do it with the owner of the trace
399 get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
400 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
403 # Test downloading a compressed trace
404 def test_data_compressed
405 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
407 # First get the data as is
408 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id }
409 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
411 # Now ask explicitly for XML format
412 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" }
413 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
415 # Now ask explicitly for GPX format
416 get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" }
417 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
420 # Check an anonymous trace can't be downloaded by another user
422 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
425 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
426 assert_response :not_found
428 # Now with some other user, which shouldn't work since the trace is anon
429 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
430 assert_response :not_found
432 # And finally we should be able to do it with the owner of the trace
433 get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
434 check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
437 # Test downloading a trace that doesn't exist
438 def test_data_not_found
439 deleted_trace_file = create(:trace, :deleted)
441 # First with a trace that has never existed
442 get :data, :params => { :display_name => create(:user).display_name, :id => 0 }
443 assert_response :not_found
445 # Now with a trace that has been deleted
446 get :data, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
447 assert_response :not_found
450 # Test downloading the picture for a trace
452 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
454 # First with no auth, which should work since the trace is public
455 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
456 check_trace_picture public_trace_file
458 # Now with some other user, which should work since the trace is public
459 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
460 check_trace_picture public_trace_file
462 # And finally we should be able to do it with the owner of the trace
463 get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
464 check_trace_picture public_trace_file
467 # Check the picture for an anonymous trace can't be downloaded by another user
468 def test_picture_anon
469 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
472 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
473 assert_response :forbidden
475 # Now with some other user, which shouldn't work since the trace is anon
476 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
477 assert_response :forbidden
479 # And finally we should be able to do it with the owner of the trace
480 get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
481 check_trace_picture anon_trace_file
484 # Test downloading the picture for a trace that doesn't exist
485 def test_picture_not_found
486 deleted_trace_file = create(:trace, :deleted)
488 # First with a trace that has never existed
489 get :picture, :params => { :display_name => create(:user).display_name, :id => 0 }
490 assert_response :not_found
492 # Now with a trace that has been deleted
493 get :picture, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
494 assert_response :not_found
497 # Test downloading the icon for a trace
499 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
501 # First with no auth, which should work since the trace is public
502 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
503 check_trace_icon public_trace_file
505 # Now with some other user, which should work since the trace is public
506 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
507 check_trace_icon public_trace_file
509 # And finally we should be able to do it with the owner of the trace
510 get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
511 check_trace_icon public_trace_file
514 # Check the icon for an anonymous trace can't be downloaded by another user
516 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
519 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
520 assert_response :forbidden
522 # Now with some other user, which shouldn't work since the trace is anon
523 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
524 assert_response :forbidden
526 # And finally we should be able to do it with the owner of the trace
527 get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
528 check_trace_icon anon_trace_file
531 # Test downloading the icon for a trace that doesn't exist
532 def test_icon_not_found
533 deleted_trace_file = create(:trace, :deleted)
535 # First with a trace that has never existed
536 get :icon, :params => { :display_name => create(:user).display_name, :id => 0 }
537 assert_response :not_found
539 # Now with a trace that has been deleted
540 get :icon, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
541 assert_response :not_found
544 # Test fetching the new trace page
548 assert_response :redirect
549 assert_redirected_to :controller => :users, :action => :login, :referer => new_trace_path
551 # Now authenticated as a user with gps.trace.visibility set
553 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
554 get :new, :session => { :user => user }
555 assert_response :success
557 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
559 # Now authenticated as a user with gps.trace.public set
560 second_user = create(:user)
561 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
562 get :new, :session => { :user => second_user }
563 assert_response :success
565 assert_select "select#trace_visibility option[value=public][selected]", 1
567 # Now authenticated as a user with no preferences
568 third_user = create(:user)
569 get :new, :session => { :user => third_user }
570 assert_response :success
572 assert_select "select#trace_visibility option[value=private][selected]", 1
575 # Test creating a trace
578 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
579 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
583 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
584 assert_response :forbidden
590 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
591 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
592 post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
593 assert_response :redirect
594 assert_redirected_to :action => :index, :display_name => user.display_name
595 assert_match(/file has been uploaded/, flash[:notice])
596 trace = Trace.order(:id => :desc).first
597 assert_equal "a.gpx", trace.name
598 assert_equal "New Trace", trace.description
599 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
600 assert_equal "trackable", trace.visibility
601 assert_equal false, trace.inserted
602 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
604 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
607 # Test creating a trace with validation errors
608 def test_create_post_with_validation_errors
610 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
611 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
615 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
616 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
617 post :create, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
619 assert_match "Description is too short (minimum is 1 character)", response.body
622 # Test fetching the edit page for a trace using GET
624 public_trace_file = create(:trace, :visibility => "public")
625 deleted_trace_file = create(:trace, :deleted)
628 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
629 assert_response :redirect
630 assert_redirected_to :controller => :users, :action => :login, :referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)
632 # Now with some other user, which should fail
633 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
634 assert_response :forbidden
636 # Now with a trace which doesn't exist
637 get :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
638 assert_response :not_found
640 # Now with a trace which has been deleted
641 get :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
642 assert_response :not_found
644 # Finally with a trace that we are allowed to edit
645 get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
646 assert_response :success
649 # Test saving edits to a trace
651 public_trace_file = create(:trace, :visibility => "public")
652 deleted_trace_file = create(:trace, :deleted)
655 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
658 put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }
659 assert_response :forbidden
661 # Now with some other user, which should fail
662 put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => create(:user) }
663 assert_response :forbidden
665 # Now with a trace which doesn't exist
666 put :update, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user), :trace => new_details }
667 assert_response :not_found
669 # Now with a trace which has been deleted
670 put :update, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id, :trace => new_details }, :session => { :user => deleted_trace_file.user }
671 assert_response :not_found
673 # Finally with a trace that we are allowed to edit
674 put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => public_trace_file.user }
675 assert_response :redirect
676 assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
677 trace = Trace.find(public_trace_file.id)
678 assert_equal new_details[:description], trace.description
679 assert_equal new_details[:tagstring], trace.tagstring
680 assert_equal new_details[:visibility], trace.visibility
683 # Test deleting a trace
685 public_trace_file = create(:trace, :visibility => "public")
686 deleted_trace_file = create(:trace, :deleted)
689 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
690 assert_response :forbidden
692 # Now with some other user, which should fail
693 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
694 assert_response :forbidden
696 # Now with a trace which doesn't exist
697 post :delete, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
698 assert_response :not_found
700 # Now with a trace has already been deleted
701 post :delete, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
702 assert_response :not_found
704 # Now with a trace that we are allowed to delete
705 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
706 assert_response :redirect
707 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
708 trace = Trace.find(public_trace_file.id)
709 assert_equal false, trace.visible
711 # Finally with a trace that is deleted by an admin
712 public_trace_file = create(:trace, :visibility => "public")
713 admin = create(:administrator_user)
715 post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => admin }
716 assert_response :redirect
717 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
718 trace = Trace.find(public_trace_file.id)
719 assert_equal false, trace.visible
722 # Check getting a specific trace through the api
724 public_trace_file = create(:trace, :visibility => "public")
727 get :api_read, :params => { :id => public_trace_file.id }
728 assert_response :unauthorized
730 # Now with some other user, which should work since the trace is public
731 basic_authorization create(:user).display_name, "test"
732 get :api_read, :params => { :id => public_trace_file.id }
733 assert_response :success
735 # And finally we should be able to do it with the owner of the trace
736 basic_authorization public_trace_file.user.display_name, "test"
737 get :api_read, :params => { :id => public_trace_file.id }
738 assert_response :success
741 # Check an anoymous trace can't be specifically fetched by another user
742 def test_api_read_anon
743 anon_trace_file = create(:trace, :visibility => "private")
746 get :api_read, :params => { :id => anon_trace_file.id }
747 assert_response :unauthorized
749 # Now try with another user, which shouldn't work since the trace is anon
750 basic_authorization create(:user).display_name, "test"
751 get :api_read, :params => { :id => anon_trace_file.id }
752 assert_response :forbidden
754 # And finally we should be able to get the trace details with the trace owner
755 basic_authorization anon_trace_file.user.display_name, "test"
756 get :api_read, :params => { :id => anon_trace_file.id }
757 assert_response :success
760 # Check the api details for a trace that doesn't exist
761 def test_api_read_not_found
762 deleted_trace_file = create(:trace, :deleted)
764 # Try first with no auth, as it should require it
765 get :api_read, :params => { :id => 0 }
766 assert_response :unauthorized
768 # Login, and try again
769 basic_authorization deleted_trace_file.user.display_name, "test"
770 get :api_read, :params => { :id => 0 }
771 assert_response :not_found
773 # Now try a trace which did exist but has been deleted
774 basic_authorization deleted_trace_file.user.display_name, "test"
775 get :api_read, :params => { :id => deleted_trace_file.id }
776 assert_response :not_found
779 # Test downloading a trace through the api
781 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
784 get :api_data, :params => { :id => public_trace_file.id }
785 assert_response :unauthorized
787 # Now with some other user, which should work since the trace is public
788 basic_authorization create(:user).display_name, "test"
789 get :api_data, :params => { :id => public_trace_file.id }
790 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
792 # And finally we should be able to do it with the owner of the trace
793 basic_authorization public_trace_file.user.display_name, "test"
794 get :api_data, :params => { :id => public_trace_file.id }
795 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
798 # Test downloading a compressed trace through the api
799 def test_api_data_compressed
800 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
802 # Authenticate as the owner of the trace we will be using
803 basic_authorization identifiable_trace_file.user.display_name, "test"
805 # First get the data as is
806 get :api_data, :params => { :id => identifiable_trace_file.id }
807 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
809 # Now ask explicitly for XML format
810 get :api_data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
811 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
813 # Now ask explicitly for GPX format
814 get :api_data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
815 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
818 # Check an anonymous trace can't be downloaded by another user through the api
819 def test_api_data_anon
820 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
823 get :api_data, :params => { :id => anon_trace_file.id }
824 assert_response :unauthorized
826 # Now with some other user, which shouldn't work since the trace is anon
827 basic_authorization create(:user).display_name, "test"
828 get :api_data, :params => { :id => anon_trace_file.id }
829 assert_response :forbidden
831 # And finally we should be able to do it with the owner of the trace
832 basic_authorization anon_trace_file.user.display_name, "test"
833 get :api_data, :params => { :id => anon_trace_file.id }
834 check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
837 # Test downloading a trace that doesn't exist through the api
838 def test_api_data_not_found
839 deleted_trace_file = create(:trace, :deleted)
841 # Try first with no auth, as it should require it
842 get :api_data, :params => { :id => 0 }
843 assert_response :unauthorized
845 # Login, and try again
846 basic_authorization create(:user).display_name, "test"
847 get :api_data, :params => { :id => 0 }
848 assert_response :not_found
850 # Now try a trace which did exist but has been deleted
851 basic_authorization deleted_trace_file.user.display_name, "test"
852 get :api_data, :params => { :id => deleted_trace_file.id }
853 assert_response :not_found
856 # Test creating a trace through the api
859 fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
860 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
864 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
865 assert_response :unauthorized
871 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
872 assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
873 basic_authorization user.display_name, "test"
874 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
875 assert_response :success
876 trace = Trace.find(response.body.to_i)
877 assert_equal "a.gpx", trace.name
878 assert_equal "New Trace", trace.description
879 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
880 assert_equal "trackable", trace.visibility
881 assert_equal false, trace.inserted
882 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
884 assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
889 # Now authenticated, with the legacy public flag
890 assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
891 basic_authorization user.display_name, "test"
892 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }
893 assert_response :success
894 trace = Trace.find(response.body.to_i)
895 assert_equal "a.gpx", trace.name
896 assert_equal "New Trace", trace.description
897 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
898 assert_equal "public", trace.visibility
899 assert_equal false, trace.inserted
900 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
902 assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
907 # Now authenticated, with the legacy private flag
908 second_user = create(:user)
909 assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
910 basic_authorization second_user.display_name, "test"
911 post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }
912 assert_response :success
913 trace = Trace.find(response.body.to_i)
914 assert_equal "a.gpx", trace.name
915 assert_equal "New Trace", trace.description
916 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
917 assert_equal "private", trace.visibility
918 assert_equal false, trace.inserted
919 assert_equal File.new(fixture).read, File.new(trace.trace_name).read
921 assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
924 # Check updating a trace through the api
926 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
927 deleted_trace_file = create(:trace, :deleted)
928 anon_trace_file = create(:trace, :visibility => "private")
931 put :api_update, :params => { :id => public_trace_file.id }, :body => public_trace_file.to_xml.to_s
932 assert_response :unauthorized
934 # Now with some other user, which should fail
935 basic_authorization create(:user).display_name, "test"
936 put :api_update, :params => { :id => public_trace_file.id }, :body => public_trace_file.to_xml.to_s
937 assert_response :forbidden
939 # Now with a trace which doesn't exist
940 basic_authorization create(:user).display_name, "test"
941 put :api_update, :params => { :id => 0 }, :body => public_trace_file.to_xml.to_s
942 assert_response :not_found
944 # Now with a trace which did exist but has been deleted
945 basic_authorization deleted_trace_file.user.display_name, "test"
946 put :api_update, :params => { :id => deleted_trace_file.id }, :body => deleted_trace_file.to_xml.to_s
947 assert_response :not_found
949 # Now try an update with the wrong ID
950 basic_authorization public_trace_file.user.display_name, "test"
951 put :api_update, :params => { :id => public_trace_file.id }, :body => anon_trace_file.to_xml.to_s
952 assert_response :bad_request,
953 "should not be able to update a trace with a different ID from the XML"
955 # And finally try an update that should work
956 basic_authorization public_trace_file.user.display_name, "test"
957 t = public_trace_file
958 t.description = "Changed description"
959 t.visibility = "private"
960 put :api_update, :params => { :id => t.id }, :body => t.to_xml.to_s
961 assert_response :success
962 nt = Trace.find(t.id)
963 assert_equal nt.description, t.description
964 assert_equal nt.visibility, t.visibility
967 # Test that updating a trace doesn't duplicate the tags
968 def test_api_update_tags
969 tracetag = create(:tracetag)
970 trace = tracetag.trace
971 basic_authorization trace.user.display_name, "test"
973 put :api_update, :params => { :id => trace.id }, :body => trace.to_xml.to_s
974 assert_response :success
976 updated = Trace.find(trace.id)
977 # Ensure there's only one tag in the database after updating
978 assert_equal Tracetag.count, 1
979 # The new tag object might have a different id, so check the string representation
980 assert_equal trace.tagstring, updated.tagstring
983 # Check deleting a trace through the api
985 public_trace_file = create(:trace, :visibility => "public")
988 delete :api_delete, :params => { :id => public_trace_file.id }
989 assert_response :unauthorized
991 # Now with some other user, which should fail
992 basic_authorization create(:user).display_name, "test"
993 delete :api_delete, :params => { :id => public_trace_file.id }
994 assert_response :forbidden
996 # Now with a trace which doesn't exist
997 basic_authorization create(:user).display_name, "test"
998 delete :api_delete, :params => { :id => 0 }
999 assert_response :not_found
1001 # And finally we should be able to do it with the owner of the trace
1002 basic_authorization public_trace_file.user.display_name, "test"
1003 delete :api_delete, :params => { :id => public_trace_file.id }
1004 assert_response :success
1006 # Try it a second time, which should fail
1007 basic_authorization public_trace_file.user.display_name, "test"
1008 delete :api_delete, :params => { :id => public_trace_file.id }
1009 assert_response :not_found
1014 def check_trace_feed(traces)
1015 assert_response :success
1016 assert_template "georss"
1017 assert_equal "application/rss+xml", @response.content_type
1018 assert_select "rss", :count => 1 do
1019 assert_select "channel", :count => 1 do
1020 assert_select "title"
1021 assert_select "description"
1022 assert_select "link"
1023 assert_select "image"
1024 assert_select "item", :count => traces.length do |items|
1025 traces.zip(items).each do |trace, item|
1026 assert_select item, "title", trace.name
1027 assert_select item, "link", "http://test.host/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
1028 assert_select item, "guid", "http://test.host/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
1029 assert_select item, "description"
1030 # assert_select item, "dc:creator", trace.user.display_name
1031 assert_select item, "pubDate", trace.timestamp.rfc822
1038 def check_trace_index(traces)
1039 assert_response :success
1040 assert_template "index"
1043 assert_select "table#trace_list tbody", :count => 1 do
1044 assert_select "tr", :count => traces.length do |rows|
1045 traces.zip(rows).each do |trace, row|
1046 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
1047 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
1048 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
1049 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
1054 assert_select "h4", /Nothing here yet/
1058 def check_trace_show(trace)
1059 assert_response :success
1060 assert_template "show"
1062 assert_select "table", :count => 1 do
1063 assert_select "td", /^#{Regexp.quote(trace.name)} /
1064 assert_select "td", trace.user.display_name
1065 assert_select "td", trace.description
1069 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
1070 assert_response :success
1071 assert_equal digest, Digest::MD5.hexdigest(response.body)
1072 assert_equal content_type, response.content_type
1073 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
1076 def check_trace_picture(trace)
1077 assert_response :success
1078 assert_equal "image/gif", response.content_type
1079 assert_equal trace.large_picture, response.body
1082 def check_trace_icon(trace)
1083 assert_response :success
1084 assert_equal "image/gif", response.content_type
1085 assert_equal trace.icon_picture, response.body