2 require "minitest/mock"
4 class TraceControllerTest < ActionController::TestCase
8 @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
9 Object.const_set("GPX_TRACE_DIR", File.dirname(__FILE__) + "/../traces")
11 @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
12 Object.const_set("GPX_IMAGE_DIR", File.dirname(__FILE__) + "/../traces")
16 Object.send("remove_const", "GPX_TRACE_DIR")
17 Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
19 Object.send("remove_const", "GPX_IMAGE_DIR")
20 Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
24 # test all routes which lead to this controller
27 { :path => "/api/0.6/gpx/create", :method => :post },
28 { :controller => "trace", :action => "api_create" }
31 { :path => "/api/0.6/gpx/1", :method => :get },
32 { :controller => "trace", :action => "api_read", :id => "1" }
35 { :path => "/api/0.6/gpx/1", :method => :put },
36 { :controller => "trace", :action => "api_update", :id => "1" }
39 { :path => "/api/0.6/gpx/1", :method => :delete },
40 { :controller => "trace", :action => "api_delete", :id => "1" }
43 { :controller => "trace", :action => "api_read", :id => "1" },
44 { :path => "/api/0.6/gpx/1/details", :method => :get }
47 { :path => "/api/0.6/gpx/1/data", :method => :get },
48 { :controller => "trace", :action => "api_data", :id => "1" }
51 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
52 { :controller => "trace", :action => "api_data", :id => "1", :format => "xml" }
56 { :path => "/traces", :method => :get },
57 { :controller => "trace", :action => "list" }
60 { :path => "/traces/page/1", :method => :get },
61 { :controller => "trace", :action => "list", :page => "1" }
64 { :path => "/traces/tag/tagname", :method => :get },
65 { :controller => "trace", :action => "list", :tag => "tagname" }
68 { :path => "/traces/tag/tagname/page/1", :method => :get },
69 { :controller => "trace", :action => "list", :tag => "tagname", :page => "1" }
72 { :path => "/user/username/traces", :method => :get },
73 { :controller => "trace", :action => "list", :display_name => "username" }
76 { :path => "/user/username/traces/page/1", :method => :get },
77 { :controller => "trace", :action => "list", :display_name => "username", :page => "1" }
80 { :path => "/user/username/traces/tag/tagname", :method => :get },
81 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname" }
84 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
85 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname", :page => "1" }
89 { :path => "/traces/mine", :method => :get },
90 { :controller => "trace", :action => "mine" }
93 { :path => "/traces/mine/page/1", :method => :get },
94 { :controller => "trace", :action => "mine", :page => "1" }
97 { :path => "/traces/mine/tag/tagname", :method => :get },
98 { :controller => "trace", :action => "mine", :tag => "tagname" }
101 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
102 { :controller => "trace", :action => "mine", :tag => "tagname", :page => "1" }
106 { :path => "/traces/rss", :method => :get },
107 { :controller => "trace", :action => "georss", :format => :rss }
110 { :path => "/traces/tag/tagname/rss", :method => :get },
111 { :controller => "trace", :action => "georss", :tag => "tagname", :format => :rss }
114 { :path => "/user/username/traces/rss", :method => :get },
115 { :controller => "trace", :action => "georss", :display_name => "username", :format => :rss }
118 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
119 { :controller => "trace", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
123 { :path => "/user/username/traces/1", :method => :get },
124 { :controller => "trace", :action => "view", :display_name => "username", :id => "1" }
127 { :path => "/user/username/traces/1/picture", :method => :get },
128 { :controller => "trace", :action => "picture", :display_name => "username", :id => "1" }
131 { :path => "/user/username/traces/1/icon", :method => :get },
132 { :controller => "trace", :action => "icon", :display_name => "username", :id => "1" }
136 { :path => "/trace/create", :method => :get },
137 { :controller => "trace", :action => "create" }
140 { :path => "/trace/create", :method => :post },
141 { :controller => "trace", :action => "create" }
144 { :path => "/trace/1/data", :method => :get },
145 { :controller => "trace", :action => "data", :id => "1" }
148 { :path => "/trace/1/data.xml", :method => :get },
149 { :controller => "trace", :action => "data", :id => "1", :format => "xml" }
152 { :path => "/trace/1/edit", :method => :get },
153 { :controller => "trace", :action => "edit", :id => "1" }
156 { :path => "/trace/1/edit", :method => :post },
157 { :controller => "trace", :action => "edit", :id => "1" }
160 { :path => "/trace/1/edit", :method => :patch },
161 { :controller => "trace", :action => "edit", :id => "1" }
164 { :path => "/trace/1/delete", :method => :post },
165 { :controller => "trace", :action => "delete", :id => "1" }
169 # Check that the list of changesets is displayed
171 # First with the public list
173 check_trace_list Trace.visible_to_all
175 # Restrict traces to those with a given tag
176 get :list, :tag => "London"
177 check_trace_list Trace.tagged("London").visible_to_all
179 # Should see more when we are logged in
180 get :list, {}, { :user => users(:public_user).id }
181 check_trace_list Trace.visible_to(users(:public_user).id)
183 # Again, we should see more when we are logged in
184 get :list, { :tag => "London" }, { :user => users(:public_user).id }
185 check_trace_list Trace.tagged("London").visible_to(users(:public_user).id)
188 # Check that I can get mine
190 # First try to get it when not logged in
192 assert_redirected_to :controller => "user", :action => "login", :referer => "/traces/mine"
194 # Now try when logged in
195 get :mine, {}, { :user => users(:public_user).id }
196 assert_redirected_to :controller => "trace", :action => "list", :display_name => users(:public_user).display_name
198 # Fetch the actual list
199 get :list, { :display_name => users(:public_user).display_name }, { :user => users(:public_user).id }
200 check_trace_list users(:public_user).traces
203 # Check the list of changesets for a specific user
205 # Test a user with no traces
206 get :list, :display_name => users(:second_public_user).display_name
207 check_trace_list users(:second_public_user).traces.visible_to_all
209 # Test a user with some traces - should see only public ones
210 get :list, :display_name => users(:public_user).display_name
211 check_trace_list users(:public_user).traces.visible_to_all
213 # Should still see only public ones when authenticated as another user
214 get :list, { :display_name => users(:public_user).display_name }, { :user => users(:normal_user).id }
215 check_trace_list users(:public_user).traces.visible_to_all
217 # Should see all traces when authenticated as the target user
218 get :list, { :display_name => users(:public_user).display_name }, { :user => users(:public_user).id }
219 check_trace_list users(:public_user).traces
221 # Should only see traces with the correct tag when a tag is specified
222 get :list, { :display_name => users(:public_user).display_name, :tag => "London" }, { :user => users(:public_user).id }
223 check_trace_list users(:public_user).traces.tagged("London")
225 # Should get an error if the user does not exist
226 get :list, :display_name => "UnknownUser"
227 assert_response :not_found
228 assert_template "user/no_such_user"
231 # Check that the rss loads
233 # First with the public feed
234 get :georss, :format => :rss
235 check_trace_feed Trace.visible_to_all
237 # Restrict traces to those with a given tag
238 get :georss, :tag => "London", :format => :rss
239 check_trace_feed Trace.tagged("London").visible_to_all
241 # Restrict traces to those for a given user
242 get :georss, :display_name => users(:public_user).display_name, :format => :rss
243 check_trace_feed users(:public_user).traces.visible_to_all
245 # Restrict traces to those for a given user with a tiven tag
246 get :georss, :display_name => users(:public_user).display_name, :tag => "Birmingham", :format => :rss
247 check_trace_feed users(:public_user).traces.tagged("Birmingham").visible_to_all
250 # Test viewing a trace
252 public_trace_file = create(:trace, :visibility => "public")
254 # First with no auth, which should work since the trace is public
255 get :view, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
256 check_trace_view public_trace_file
258 # Now with some other user, which should work since the trace is public
259 get :view, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
260 check_trace_view public_trace_file
262 # And finally we should be able to do it with the owner of the trace
263 get :view, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
264 check_trace_view public_trace_file
267 # Check an anonymous trace can't be viewed by another user
269 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
272 get :view, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
273 assert_response :redirect
274 assert_redirected_to :action => :list
276 # Now with some other user, which should not work since the trace is anon
277 get :view, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
278 assert_response :redirect
279 assert_redirected_to :action => :list
281 # And finally we should be able to do it with the owner of the trace
282 get :view, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
283 check_trace_view anon_trace_file
286 # Test viewing a trace that doesn't exist
287 def test_view_not_found
288 deleted_trace_file = create(:trace, :deleted)
291 get :view, :display_name => users(:public_user).display_name, :id => 0
292 assert_response :redirect
293 assert_redirected_to :action => :list
295 # Now with some other user
296 get :view, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
297 assert_response :redirect
298 assert_redirected_to :action => :list
300 # And finally we should not be able to view a deleted trace
301 get :view, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
302 assert_response :redirect
303 assert_redirected_to :action => :list
306 # Test downloading a trace
308 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
309 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/a.gpx" do
310 # First with no auth, which should work since the trace is public
311 get :data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
312 check_trace_data public_trace_file
314 # Now with some other user, which should work since the trace is public
315 get :data, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
316 check_trace_data public_trace_file
318 # And finally we should be able to do it with the owner of the trace
319 get :data, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
320 check_trace_data public_trace_file
324 # Test downloading a compressed trace
325 def test_data_compressed
326 identifiable_trace_file = create(:trace, :visibility => "identifiable")
327 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/d.gpx" do
328 # First get the data as is
329 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id
330 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
332 # Now ask explicitly for XML format
333 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "xml"
334 check_trace_data identifiable_trace_file, "application/xml", "xml"
336 # Now ask explicitly for GPX format
337 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "gpx"
338 check_trace_data identifiable_trace_file
342 # Check an anonymous trace can't be downloaded by another user
344 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
345 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/b.gpx" do
347 get :data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
348 assert_response :not_found
350 # Now with some other user, which shouldn't work since the trace is anon
351 get :data, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
352 assert_response :not_found
354 # And finally we should be able to do it with the owner of the trace
355 get :data, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
356 check_trace_data anon_trace_file
360 # Test downloading a trace that doesn't exist
361 def test_data_not_found
362 deleted_trace_file = create(:trace, :deleted)
364 # First with no auth and a trace that has never existed
365 get :data, :display_name => users(:public_user).display_name, :id => 0
366 assert_response :not_found
368 # Now with a trace that has never existed
369 get :data, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
370 assert_response :not_found
372 # Now with a trace that has been deleted
373 get :data, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
374 assert_response :not_found
377 # Test downloading the picture for a trace
379 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
380 Trace.stub_any_instance :large_picture_name, "#{GPX_TRACE_DIR}/a.gif" do
381 # First with no auth, which should work since the trace is public
382 get :picture, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
383 check_trace_picture public_trace_file
385 # Now with some other user, which should work since the trace is public
386 get :picture, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
387 check_trace_picture public_trace_file
389 # And finally we should be able to do it with the owner of the trace
390 get :picture, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
391 check_trace_picture public_trace_file
395 # Check the picture for an anonymous trace can't be downloaded by another user
396 def test_picture_anon
397 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
398 Trace.stub_any_instance :large_picture_name, "#{GPX_TRACE_DIR}/b.gif" do
400 get :picture, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
401 assert_response :forbidden
403 # Now with some other user, which shouldn't work since the trace is anon
404 get :picture, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
405 assert_response :forbidden
407 # And finally we should be able to do it with the owner of the trace
408 get :picture, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
409 check_trace_picture anon_trace_file
413 # Test downloading the picture for a trace that doesn't exist
414 def test_picture_not_found
415 # First with no auth, which should work since the trace is public
416 get :picture, :display_name => users(:public_user).display_name, :id => 0
417 assert_response :not_found
419 # Now with some other user, which should work since the trace is public
420 get :picture, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
421 assert_response :not_found
423 # And finally we should not be able to do it with a deleted trace
424 deleted_trace_file = create(:trace, :deleted)
425 get :picture, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
426 assert_response :not_found
429 # Test downloading the icon for a trace
431 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
432 Trace.stub_any_instance :icon_picture_name, "#{GPX_TRACE_DIR}/a_icon.gif" do
433 # First with no auth, which should work since the trace is public
434 get :icon, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
435 check_trace_icon public_trace_file
437 # Now with some other user, which should work since the trace is public
438 get :icon, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
439 check_trace_icon public_trace_file
441 # And finally we should be able to do it with the owner of the trace
442 get :icon, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
443 check_trace_icon public_trace_file
447 # Check the icon for an anonymous trace can't be downloaded by another user
449 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
450 Trace.stub_any_instance :icon_picture_name, "#{GPX_TRACE_DIR}/b_icon.gif" do
452 get :icon, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
453 assert_response :forbidden
455 # Now with some other user, which shouldn't work since the trace is anon
456 get :icon, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
457 assert_response :forbidden
459 # And finally we should be able to do it with the owner of the trace
460 get :icon, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
461 check_trace_icon anon_trace_file
465 # Test downloading the icon for a trace that doesn't exist
466 def test_icon_not_found
468 get :icon, :display_name => users(:public_user).display_name, :id => 0
469 assert_response :not_found
471 # Now with some other user
472 get :icon, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
473 assert_response :not_found
475 # And finally we should not be able to do it with a deleted trace
476 deleted_trace_file = create(:trace, :deleted)
477 get :icon, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
478 assert_response :not_found
481 # Test fetching the create page
485 assert_response :redirect
486 assert_redirected_to :controller => :user, :action => :login, :referer => trace_create_path
488 # Now authenticated as a user with gps.trace.visibility set
489 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
490 get :create, {}, { :user => users(:public_user).id }
491 assert_response :success
492 assert_template :create
493 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
495 # Now authenticated as a user with gps.trace.public set
496 create(:user_preference, :user => users(:second_public_user), :k => "gps.trace.public", :v => "default")
497 get :create, {}, { :user => users(:second_public_user).id }
498 assert_response :success
499 assert_template :create
500 assert_select "select#trace_visibility option[value=public][selected]", 1
502 # Now authenticated as a user with no preferences
503 get :create, {}, { :user => users(:normal_user).id }
504 assert_response :success
505 assert_template :create
506 assert_select "select#trace_visibility option[value=private][selected]", 1
509 # Test creating a trace
511 public_trace_file = create(:trace, :visibility => "public")
512 public_trace_file.stub :trace_name, "#{GPX_TRACE_DIR}/a.gpx" do
514 file = Rack::Test::UploadedFile.new(public_trace_file.trace_name, "application/gpx+xml")
517 post :create, :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" }
518 assert_response :forbidden
521 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
522 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
523 post :create, { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, { :user => users(:public_user).id }
524 assert_response :redirect
525 assert_redirected_to :action => :list, :display_name => users(:public_user).display_name
526 assert_match /file has been uploaded/, flash[:notice]
527 trace = Trace.order(:id => :desc).first
528 assert_equal "a.gpx", trace.name
529 assert_equal "New Trace", trace.description
530 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
531 assert_equal "trackable", trace.visibility
532 assert_equal false, trace.inserted
533 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
535 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
539 # Test fetching the edit page for a trace
541 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
542 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
545 get :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
546 assert_response :redirect
547 assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => users(:normal_user).display_name, :id => public_trace_file.id)
549 # Now with some other user, which should fail
550 get :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
551 assert_response :forbidden
553 # Now with a trace which doesn't exist
554 get :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
555 assert_response :not_found
557 # Now with a trace which has been deleted
558 get :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
559 assert_response :not_found
561 # Finally with a trace that we are allowed to edit
562 get :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
563 assert_response :success
566 # Test saving edits to a trace
568 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
569 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
571 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
574 post :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details
575 assert_response :forbidden
577 # Now with some other user, which should fail
578 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:public_user).id }
579 assert_response :forbidden
581 # Now with a trace which doesn't exist
582 post :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id, :trace => new_details }
583 assert_response :not_found
585 # Now with a trace which has been deleted
586 post :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id, :trace => new_details }, { :user => users(:public_user).id }
587 assert_response :not_found
589 # Finally with a trace that we are allowed to edit
590 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:normal_user).id }
591 assert_response :redirect
592 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
593 trace = Trace.find(public_trace_file.id)
594 assert_equal new_details[:description], trace.description
595 assert_equal new_details[:tagstring], trace.tagstring
596 assert_equal new_details[:visibility], trace.visibility
599 # Test deleting a trace
601 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
602 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
605 post :delete, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
606 assert_response :forbidden
608 # Now with some other user, which should fail
609 post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
610 assert_response :forbidden
612 # Now with a trace which doesn't exist
613 post :delete, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
614 assert_response :not_found
616 # Now with a trace has already been deleted
617 post :delete, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
618 assert_response :not_found
620 # Finally with a trace that we are allowed to delete
621 post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
622 assert_response :redirect
623 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
624 trace = Trace.find(public_trace_file.id)
625 assert_equal false, trace.visible
628 # Check getting a specific trace through the api
630 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
633 get :api_read, :id => public_trace_file.id
634 assert_response :unauthorized
636 # Now with some other user, which should work since the trace is public
637 basic_authorization(users(:public_user).display_name, "test")
638 get :api_read, :id => public_trace_file.id
639 assert_response :success
641 # And finally we should be able to do it with the owner of the trace
642 basic_authorization(users(:normal_user).display_name, "test")
643 get :api_read, :id => public_trace_file.id
644 assert_response :success
647 # Check an anoymous trace can't be specifically fetched by another user
648 def test_api_read_anon
649 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
652 get :api_read, :id => anon_trace_file.id
653 assert_response :unauthorized
655 # Now try with another user, which shouldn't work since the trace is anon
656 basic_authorization(users(:normal_user).display_name, "test")
657 get :api_read, :id => anon_trace_file.id
658 assert_response :forbidden
660 # And finally we should be able to get the trace details with the trace owner
661 basic_authorization(users(:public_user).display_name, "test")
662 get :api_read, :id => anon_trace_file.id
663 assert_response :success
666 # Check the api details for a trace that doesn't exist
667 def test_api_read_not_found
668 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
670 # Try first with no auth, as it should require it
671 get :api_read, :id => 0
672 assert_response :unauthorized
674 # Login, and try again
675 basic_authorization(users(:public_user).display_name, "test")
676 get :api_read, :id => 0
677 assert_response :not_found
679 # Now try a trace which did exist but has been deleted
680 basic_authorization(users(:public_user).display_name, "test")
681 get :api_read, :id => deleted_trace_file.id
682 assert_response :not_found
685 # Test downloading a trace through the api
687 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
688 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/a.gpx" do
690 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
691 assert_response :unauthorized
693 # Now with some other user, which should work since the trace is public
694 basic_authorization(users(:public_user).display_name, "test")
695 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
696 check_trace_data public_trace_file
698 # # And finally we should be able to do it with the owner of the trace
699 basic_authorization(users(:normal_user).display_name, "test")
700 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
701 check_trace_data public_trace_file
705 # Test downloading a compressed trace through the api
706 def test_api_data_compressed
707 identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => users(:public_user))
708 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/d.gpx" do
709 # Authenticate as the owner of the trace we will be using
710 basic_authorization(users(:public_user).display_name, "test")
712 # First get the data as is
713 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id
714 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
716 # Now ask explicitly for XML format
717 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "xml"
718 check_trace_data identifiable_trace_file, "application/xml", "xml"
720 # # Now ask explicitly for GPX format
721 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "gpx"
722 check_trace_data identifiable_trace_file
726 # Check an anonymous trace can't be downloaded by another user through the api
727 def test_api_data_anon
728 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
729 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/b.gpx" do
731 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
732 assert_response :unauthorized
734 # Now with some other user, which shouldn't work since the trace is anon
735 basic_authorization(users(:normal_user).display_name, "test")
736 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
737 assert_response :forbidden
739 # And finally we should be able to do it with the owner of the trace
740 basic_authorization(users(:public_user).display_name, "test")
741 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
742 check_trace_data anon_trace_file
746 # Test downloading a trace that doesn't exist through the api
747 def test_api_data_not_found
749 get :api_data, :display_name => users(:public_user).display_name, :id => 0
750 assert_response :unauthorized
752 # Now with a trace that has never existed
753 basic_authorization(users(:public_user).display_name, "test")
754 get :api_data, :display_name => users(:public_user).display_name, :id => 0
755 assert_response :not_found
757 # Now with a trace that has been deleted
758 deleted_trace_file = create(:trace, :deleted)
759 basic_authorization(users(:public_user).display_name, "test")
760 get :api_data, :display_name => users(:public_user).display_name, :id => deleted_trace_file.id
761 assert_response :not_found
764 # Test creating a trace through the api
766 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
767 public_trace_file.stub :trace_name, "#{GPX_TRACE_DIR}/a.gpx" do
769 file = Rack::Test::UploadedFile.new(public_trace_file.trace_name, "application/gpx+xml")
772 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
773 assert_response :unauthorized
776 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
777 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
778 basic_authorization(users(:public_user).display_name, "test")
779 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
780 assert_response :success
781 trace = Trace.find(response.body.to_i)
782 assert_equal "a.gpx", trace.name
783 assert_equal "New Trace", trace.description
784 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
785 assert_equal "trackable", trace.visibility
786 assert_equal false, trace.inserted
787 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
789 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
794 # Now authenticated, with the legacy public flag
795 assert_not_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
796 basic_authorization(users(:public_user).display_name, "test")
797 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 1
798 assert_response :success
799 trace = Trace.find(response.body.to_i)
800 assert_equal "a.gpx", trace.name
801 assert_equal "New Trace", trace.description
802 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
803 assert_equal "public", trace.visibility
804 assert_equal false, trace.inserted
805 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
807 assert_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
812 # Now authenticated, with the legacy private flag
813 assert_nil users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first
814 basic_authorization(users(:second_public_user).display_name, "test")
815 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 0
816 assert_response :success
817 trace = Trace.find(response.body.to_i)
818 assert_equal "a.gpx", trace.name
819 assert_equal "New Trace", trace.description
820 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
821 assert_equal "private", trace.visibility
822 assert_equal false, trace.inserted
823 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
825 assert_equal "private", users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first.v
829 # Check updating a trace through the api
831 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
832 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
833 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
835 public_trace_file.stub :trace_name, "#{GPX_TRACE_DIR}/a.gpx" do
837 content public_trace_file.to_xml
838 put :api_update, :id => public_trace_file.id
839 assert_response :unauthorized
841 # Now with some other user, which should fail
842 basic_authorization(users(:public_user).display_name, "test")
843 content public_trace_file.to_xml
844 put :api_update, :id => public_trace_file.id
845 assert_response :forbidden
847 # Now with a trace which doesn't exist
848 basic_authorization(users(:public_user).display_name, "test")
849 content public_trace_file.to_xml
850 put :api_update, :id => 0
851 assert_response :not_found
853 # Now with a trace which did exist but has been deleted
854 basic_authorization(users(:public_user).display_name, "test")
855 content deleted_trace_file.to_xml
856 put :api_update, :id => deleted_trace_file.id
857 assert_response :not_found
859 # Now try an update with the wrong ID
860 basic_authorization(users(:normal_user).display_name, "test")
861 content anon_trace_file.to_xml
862 put :api_update, :id => public_trace_file.id
863 assert_response :bad_request,
864 "should not be able to update a trace with a different ID from the XML"
866 # And finally try an update that should work
867 basic_authorization(users(:normal_user).display_name, "test")
868 t = public_trace_file
869 t.description = "Changed description"
870 t.visibility = "private"
872 put :api_update, :id => t.id
873 assert_response :success
874 nt = Trace.find(t.id)
875 assert_equal nt.description, t.description
876 assert_equal nt.visibility, t.visibility
880 # Check deleting a trace through the api
882 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
885 delete :api_delete, :id => public_trace_file.id
886 assert_response :unauthorized
888 # Now with some other user, which should fail
889 basic_authorization(users(:public_user).display_name, "test")
890 delete :api_delete, :id => public_trace_file.id
891 assert_response :forbidden
893 # Now with a trace which doesn't exist
894 basic_authorization(users(:public_user).display_name, "test")
895 delete :api_delete, :id => 0
896 assert_response :not_found
898 # And finally we should be able to do it with the owner of the trace
899 basic_authorization(users(:normal_user).display_name, "test")
900 delete :api_delete, :id => public_trace_file.id
901 assert_response :success
903 # Try it a second time, which should fail
904 basic_authorization(users(:normal_user).display_name, "test")
905 delete :api_delete, :id => public_trace_file.id
906 assert_response :not_found
911 def check_trace_feed(traces)
912 assert_response :success
913 assert_template "georss"
914 assert_equal "application/rss+xml", @response.content_type
915 assert_select "rss", :count => 1 do
916 assert_select "channel", :count => 1 do
917 assert_select "title"
918 assert_select "description"
920 assert_select "image"
921 assert_select "item", :count => traces.visible.count do |items|
922 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
923 assert_select item, "title", trace.name
924 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
925 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
926 assert_select item, "description"
927 # assert_select item, "dc:creator", trace.user.display_name
928 assert_select item, "pubDate", trace.timestamp.rfc822
935 def check_trace_list(traces)
936 assert_response :success
937 assert_template "list"
940 assert_select "table#trace_list tbody", :count => 1 do
941 assert_select "tr", :count => traces.visible.count do |rows|
942 traces.visible.order("timestamp DESC").zip(rows).each do |trace, row|
943 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
944 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
945 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
946 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
951 assert_select "h4", /Nothing here yet/
955 def check_trace_view(trace)
956 assert_response :success
957 assert_template "view"
959 assert_select "table", :count => 1 do
960 assert_select "td", /^#{Regexp.quote(trace.name)} /
961 assert_select "td", trace.user.display_name
962 assert_select "td", trace.description
966 def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
967 assert_response :success
968 assert_equal content_type, response.content_type
969 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
972 def check_trace_picture(trace)
973 assert_response :success
974 assert_equal "image/gif", response.content_type
975 assert_equal trace.large_picture, response.body
978 def check_trace_icon(trace)
979 assert_response :success
980 assert_equal "image/gif", response.content_type
981 assert_equal trace.icon_picture, response.body