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
289 get :view, :display_name => users(:public_user).display_name, :id => 0
290 assert_response :redirect
291 assert_redirected_to :action => :list
293 # Now with some other user
294 get :view, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
295 assert_response :redirect
296 assert_redirected_to :action => :list
298 # And finally we should not be able to view a deleted trace
299 deleted_trace_file = create(:trace, :deleted)
300 get :view, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
301 assert_response :redirect
302 assert_redirected_to :action => :list
305 # Test downloading a trace
307 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
308 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/1.gpx" do
309 # First with no auth, which should work since the trace is public
310 get :data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
311 check_trace_data public_trace_file
313 # Now with some other user, which should work since the trace is public
314 get :data, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
315 check_trace_data public_trace_file
317 # And finally we should be able to do it with the owner of the trace
318 get :data, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
319 check_trace_data public_trace_file
323 # Test downloading a compressed trace
324 def test_data_compressed
325 identifiable_trace_file = create(:trace, :visibility => "identifiable")
326 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/4.gpx" do
327 # First get the data as is
328 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id
329 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
331 # Now ask explicitly for XML format
332 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "xml"
333 check_trace_data identifiable_trace_file, "application/xml", "xml"
335 # Now ask explicitly for GPX format
336 get :data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "gpx"
337 check_trace_data identifiable_trace_file
341 # Check an anonymous trace can't be downloaded by another user
343 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
344 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/2.gpx" do
346 get :data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
347 assert_response :not_found
349 # Now with some other user, which shouldn't work since the trace is anon
350 get :data, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
351 assert_response :not_found
353 # And finally we should be able to do it with the owner of the trace
354 get :data, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
355 check_trace_data anon_trace_file
359 # Test downloading a trace that doesn't exist
360 def test_data_not_found
361 # First with no auth and a trace that has never existed
362 get :data, :display_name => users(:public_user).display_name, :id => 0
363 assert_response :not_found
365 # Now with a trace that has never existed
366 get :data, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
367 assert_response :not_found
369 # Now with a trace that has been deleted
370 deleted_trace_file = create(:trace, :deleted)
371 get :data, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
372 assert_response :not_found
375 # Test downloading the picture for a trace
377 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
378 Trace.stub_any_instance :large_picture_name, "#{GPX_TRACE_DIR}/1.gif" do
379 # First with no auth, which should work since the trace is public
380 get :picture, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
381 check_trace_picture public_trace_file
383 # Now with some other user, which should work since the trace is public
384 get :picture, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
385 check_trace_picture public_trace_file
387 # And finally we should be able to do it with the owner of the trace
388 get :picture, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
389 check_trace_picture public_trace_file
393 # Check the picture for an anonymous trace can't be downloaded by another user
394 def test_picture_anon
395 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
396 Trace.stub_any_instance :large_picture_name, "#{GPX_TRACE_DIR}/2.gif" do
398 get :picture, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
399 assert_response :forbidden
401 # Now with some other user, which shouldn't work since the trace is anon
402 get :picture, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
403 assert_response :forbidden
405 # And finally we should be able to do it with the owner of the trace
406 get :picture, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
407 check_trace_picture anon_trace_file
411 # Test downloading the picture for a trace that doesn't exist
412 def test_picture_not_found
413 # First with no auth, which should work since the trace is public
414 get :picture, :display_name => users(:public_user).display_name, :id => 0
415 assert_response :not_found
417 # Now with some other user, which should work since the trace is public
418 get :picture, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
419 assert_response :not_found
421 # And finally we should not be able to do it with a deleted trace
422 deleted_trace_file = create(:trace, :deleted)
423 get :picture, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
424 assert_response :not_found
427 # Test downloading the icon for a trace
429 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
430 Trace.stub_any_instance :icon_picture_name, "#{GPX_TRACE_DIR}/1_icon.gif" do
431 # First with no auth, which should work since the trace is public
432 get :icon, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
433 check_trace_icon public_trace_file
435 # Now with some other user, which should work since the trace is public
436 get :icon, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
437 check_trace_icon public_trace_file
439 # And finally we should be able to do it with the owner of the trace
440 get :icon, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
441 check_trace_icon public_trace_file
445 # Check the icon for an anonymous trace can't be downloaded by another user
447 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
448 Trace.stub_any_instance :icon_picture_name, "#{GPX_TRACE_DIR}/2_icon.gif" do
450 get :icon, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
451 assert_response :forbidden
453 # Now with some other user, which shouldn't work since the trace is anon
454 get :icon, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:normal_user).id }
455 assert_response :forbidden
457 # And finally we should be able to do it with the owner of the trace
458 get :icon, { :display_name => users(:public_user).display_name, :id => anon_trace_file.id }, { :user => users(:public_user).id }
459 check_trace_icon anon_trace_file
463 # Test downloading the icon for a trace that doesn't exist
464 def test_icon_not_found
466 get :icon, :display_name => users(:public_user).display_name, :id => 0
467 assert_response :not_found
469 # Now with some other user
470 get :icon, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
471 assert_response :not_found
473 # And finally we should not be able to do it with a deleted trace
474 deleted_trace_file = create(:trace, :deleted)
475 get :icon, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
476 assert_response :not_found
479 # Test fetching the create page
483 assert_response :redirect
484 assert_redirected_to :controller => :user, :action => :login, :referer => trace_create_path
486 # Now authenticated as a user with gps.trace.visibility set
487 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
488 get :create, {}, { :user => users(:public_user).id }
489 assert_response :success
490 assert_template :create
491 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
493 # Now authenticated as a user with gps.trace.public set
494 create(:user_preference, :user => users(:second_public_user), :k => "gps.trace.public", :v => "default")
495 get :create, {}, { :user => users(:second_public_user).id }
496 assert_response :success
497 assert_template :create
498 assert_select "select#trace_visibility option[value=public][selected]", 1
500 # Now authenticated as a user with no preferences
501 get :create, {}, { :user => users(:normal_user).id }
502 assert_response :success
503 assert_template :create
504 assert_select "select#trace_visibility option[value=private][selected]", 1
507 # Test creating a trace
509 public_trace_file = create(:trace, :visibility => "public")
510 public_trace_file.stub :trace_name, "#{GPX_TRACE_DIR}/1.gpx" do
512 file = Rack::Test::UploadedFile.new(public_trace_file.trace_name, "application/gpx+xml")
515 post :create, :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" }
516 assert_response :forbidden
519 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
520 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
521 post :create, { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, { :user => users(:public_user).id }
522 assert_response :redirect
523 assert_redirected_to :action => :list, :display_name => users(:public_user).display_name
524 assert_match /file has been uploaded/, flash[:notice]
525 trace = Trace.order(:id => :desc).first
526 assert_equal "1.gpx", trace.name
527 assert_equal "New Trace", trace.description
528 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
529 assert_equal "trackable", trace.visibility
530 assert_equal false, trace.inserted
531 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
533 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
537 # Test fetching the edit page for a trace
539 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
540 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
543 get :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
544 assert_response :redirect
545 assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => users(:normal_user).display_name, :id => public_trace_file.id)
547 # Now with some other user, which should fail
548 get :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
549 assert_response :forbidden
551 # Now with a trace which doesn't exist
552 get :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
553 assert_response :not_found
555 # Now with a trace which has been deleted
556 get :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
557 assert_response :not_found
559 # Finally with a trace that we are allowed to edit
560 get :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
561 assert_response :success
564 # Test saving edits to a trace
566 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
567 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
569 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
572 post :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details
573 assert_response :forbidden
575 # Now with some other user, which should fail
576 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:public_user).id }
577 assert_response :forbidden
579 # Now with a trace which doesn't exist
580 post :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id, :trace => new_details }
581 assert_response :not_found
583 # Now with a trace which has been deleted
584 post :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id, :trace => new_details }, { :user => users(:public_user).id }
585 assert_response :not_found
587 # Finally with a trace that we are allowed to edit
588 post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:normal_user).id }
589 assert_response :redirect
590 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
591 trace = Trace.find(public_trace_file.id)
592 assert_equal new_details[:description], trace.description
593 assert_equal new_details[:tagstring], trace.tagstring
594 assert_equal new_details[:visibility], trace.visibility
597 # Test deleting a trace
599 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
600 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
603 post :delete, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
604 assert_response :forbidden
606 # Now with some other user, which should fail
607 post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
608 assert_response :forbidden
610 # Now with a trace which doesn't exist
611 post :delete, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
612 assert_response :not_found
614 # Now with a trace has already been deleted
615 post :delete, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
616 assert_response :not_found
618 # Finally with a trace that we are allowed to delete
619 post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
620 assert_response :redirect
621 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
622 trace = Trace.find(public_trace_file.id)
623 assert_equal false, trace.visible
626 # Check getting a specific trace through the api
628 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
631 get :api_read, :id => public_trace_file.id
632 assert_response :unauthorized
634 # Now with some other user, which should work since the trace is public
635 basic_authorization(users(:public_user).display_name, "test")
636 get :api_read, :id => public_trace_file.id
637 assert_response :success
639 # And finally we should be able to do it with the owner of the trace
640 basic_authorization(users(:normal_user).display_name, "test")
641 get :api_read, :id => public_trace_file.id
642 assert_response :success
645 # Check an anoymous trace can't be specifically fetched by another user
646 def test_api_read_anon
647 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
650 get :api_read, :id => anon_trace_file.id
651 assert_response :unauthorized
653 # Now try with another user, which shouldn't work since the trace is anon
654 basic_authorization(users(:normal_user).display_name, "test")
655 get :api_read, :id => anon_trace_file.id
656 assert_response :forbidden
658 # And finally we should be able to get the trace details with the trace owner
659 basic_authorization(users(:public_user).display_name, "test")
660 get :api_read, :id => anon_trace_file.id
661 assert_response :success
664 # Check the api details for a trace that doesn't exist
665 def test_api_read_not_found
666 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
668 # Try first with no auth, as it should require it
669 get :api_read, :id => 0
670 assert_response :unauthorized
672 # Login, and try again
673 basic_authorization(users(:public_user).display_name, "test")
674 get :api_read, :id => 0
675 assert_response :not_found
677 # Now try a trace which did exist but has been deleted
678 basic_authorization(users(:public_user).display_name, "test")
679 get :api_read, :id => deleted_trace_file.id
680 assert_response :not_found
683 # Test downloading a trace through the api
685 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
686 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/1.gpx" do
688 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
689 assert_response :unauthorized
691 # Now with some other user, which should work since the trace is public
692 basic_authorization(users(:public_user).display_name, "test")
693 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
694 check_trace_data public_trace_file
696 # # And finally we should be able to do it with the owner of the trace
697 basic_authorization(users(:normal_user).display_name, "test")
698 get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
699 check_trace_data public_trace_file
703 # Test downloading a compressed trace through the api
704 def test_api_data_compressed
705 identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => users(:public_user))
706 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/4.gpx" do
707 # Authenticate as the owner of the trace we will be using
708 basic_authorization(users(:public_user).display_name, "test")
710 # First get the data as is
711 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id
712 check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
714 # Now ask explicitly for XML format
715 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "xml"
716 check_trace_data identifiable_trace_file, "application/xml", "xml"
718 # # Now ask explicitly for GPX format
719 get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "gpx"
720 check_trace_data identifiable_trace_file
724 # Check an anonymous trace can't be downloaded by another user through the api
725 def test_api_data_anon
726 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
727 Trace.stub_any_instance :trace_name, "#{GPX_TRACE_DIR}/2.gpx" do
729 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
730 assert_response :unauthorized
732 # Now with some other user, which shouldn't work since the trace is anon
733 basic_authorization(users(:normal_user).display_name, "test")
734 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
735 assert_response :forbidden
737 # And finally we should be able to do it with the owner of the trace
738 basic_authorization(users(:public_user).display_name, "test")
739 get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
740 check_trace_data anon_trace_file
744 # Test downloading a trace that doesn't exist through the api
745 def test_api_data_not_found
747 get :api_data, :display_name => users(:public_user).display_name, :id => 0
748 assert_response :unauthorized
750 # Now with a trace that has never existed
751 basic_authorization(users(:public_user).display_name, "test")
752 get :api_data, :display_name => users(:public_user).display_name, :id => 0
753 assert_response :not_found
755 # Now with a trace that has been deleted
756 deleted_trace_file = create(:trace, :deleted)
757 basic_authorization(users(:public_user).display_name, "test")
758 get :api_data, :display_name => users(:public_user).display_name, :id => deleted_trace_file.id
759 assert_response :not_found
762 # Test creating a trace through the api
764 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
765 public_trace_file.stub :trace_name, "#{GPX_TRACE_DIR}/1.gpx" do
767 file = Rack::Test::UploadedFile.new(public_trace_file.trace_name, "application/gpx+xml")
770 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
771 assert_response :unauthorized
774 create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
775 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
776 basic_authorization(users(:public_user).display_name, "test")
777 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
778 assert_response :success
779 trace = Trace.find(response.body.to_i)
780 assert_equal "1.gpx", trace.name
781 assert_equal "New Trace", trace.description
782 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
783 assert_equal "trackable", trace.visibility
784 assert_equal false, trace.inserted
785 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
787 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
792 # Now authenticated, with the legacy public flag
793 assert_not_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
794 basic_authorization(users(:public_user).display_name, "test")
795 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 1
796 assert_response :success
797 trace = Trace.find(response.body.to_i)
798 assert_equal "1.gpx", trace.name
799 assert_equal "New Trace", trace.description
800 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
801 assert_equal "public", trace.visibility
802 assert_equal false, trace.inserted
803 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
805 assert_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
810 # Now authenticated, with the legacy private flag
811 assert_nil users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first
812 basic_authorization(users(:second_public_user).display_name, "test")
813 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 0
814 assert_response :success
815 trace = Trace.find(response.body.to_i)
816 assert_equal "1.gpx", trace.name
817 assert_equal "New Trace", trace.description
818 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
819 assert_equal "private", trace.visibility
820 assert_equal false, trace.inserted
821 assert_equal File.new(public_trace_file.trace_name).read, File.new(trace.trace_name).read
823 assert_equal "private", users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first.v
827 # Check updating a trace through the api
829 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
830 deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
831 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
833 public_trace_file.stub :trace_name, "#{GPX_TRACE_DIR}/1.gpx" do
835 content public_trace_file.to_xml
836 put :api_update, :id => public_trace_file.id
837 assert_response :unauthorized
839 # Now with some other user, which should fail
840 basic_authorization(users(:public_user).display_name, "test")
841 content public_trace_file.to_xml
842 put :api_update, :id => public_trace_file.id
843 assert_response :forbidden
845 # Now with a trace which doesn't exist
846 basic_authorization(users(:public_user).display_name, "test")
847 content public_trace_file.to_xml
848 put :api_update, :id => 0
849 assert_response :not_found
851 # Now with a trace which did exist but has been deleted
852 basic_authorization(users(:public_user).display_name, "test")
853 content deleted_trace_file.to_xml
854 put :api_update, :id => deleted_trace_file.id
855 assert_response :not_found
857 # Now try an update with the wrong ID
858 basic_authorization(users(:normal_user).display_name, "test")
859 content anon_trace_file.to_xml
860 put :api_update, :id => public_trace_file.id
861 assert_response :bad_request,
862 "should not be able to update a trace with a different ID from the XML"
864 # And finally try an update that should work
865 basic_authorization(users(:normal_user).display_name, "test")
866 t = public_trace_file
867 t.description = "Changed description"
868 t.visibility = "private"
870 put :api_update, :id => t.id
871 assert_response :success
872 nt = Trace.find(t.id)
873 assert_equal nt.description, t.description
874 assert_equal nt.visibility, t.visibility
878 # Check deleting a trace through the api
880 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
883 delete :api_delete, :id => public_trace_file.id
884 assert_response :unauthorized
886 # Now with some other user, which should fail
887 basic_authorization(users(:public_user).display_name, "test")
888 delete :api_delete, :id => public_trace_file.id
889 assert_response :forbidden
891 # Now with a trace which doesn't exist
892 basic_authorization(users(:public_user).display_name, "test")
893 delete :api_delete, :id => 0
894 assert_response :not_found
896 # And finally we should be able to do it with the owner of the trace
897 basic_authorization(users(:normal_user).display_name, "test")
898 delete :api_delete, :id => public_trace_file.id
899 assert_response :success
901 # Try it a second time, which should fail
902 basic_authorization(users(:normal_user).display_name, "test")
903 delete :api_delete, :id => public_trace_file.id
904 assert_response :not_found
909 def check_trace_feed(traces)
910 assert_response :success
911 assert_template "georss"
912 assert_equal "application/rss+xml", @response.content_type
913 assert_select "rss", :count => 1 do
914 assert_select "channel", :count => 1 do
915 assert_select "title"
916 assert_select "description"
918 assert_select "image"
919 assert_select "item", :count => traces.visible.count do |items|
920 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
921 assert_select item, "title", trace.name
922 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
923 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
924 assert_select item, "description"
925 # assert_select item, "dc:creator", trace.user.display_name
926 assert_select item, "pubDate", trace.timestamp.rfc822
933 def check_trace_list(traces)
934 assert_response :success
935 assert_template "list"
938 assert_select "table#trace_list tbody", :count => 1 do
939 assert_select "tr", :count => traces.visible.count do |rows|
940 traces.visible.order("timestamp DESC").zip(rows).each do |trace, row|
941 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
942 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
943 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
944 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
949 assert_select "h4", /Nothing here yet/
953 def check_trace_view(trace)
954 assert_response :success
955 assert_template "view"
957 assert_select "table", :count => 1 do
958 assert_select "td", /^#{Regexp.quote(trace.name)} /
959 assert_select "td", trace.user.display_name
960 assert_select "td", trace.description
964 def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
965 assert_response :success
966 assert_equal content_type, response.content_type
967 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
970 def check_trace_picture(trace)
971 assert_response :success
972 assert_equal "image/gif", response.content_type
973 assert_equal trace.large_picture, response.body
976 def check_trace_icon(trace)
977 assert_response :success
978 assert_equal "image/gif", response.content_type
979 assert_equal trace.icon_picture, response.body