3 class TraceControllerTest < ActionController::TestCase
4 fixtures :users, :user_preferences, :gpx_files
5 set_fixture_class :gpx_files => Trace
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 # First with no auth, which should work since the trace is public
253 get :view, :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id
254 check_trace_view gpx_files(:public_trace_file)
256 # Now with some other user, which should work since the trace is public
257 get :view, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:public_user).id }
258 check_trace_view gpx_files(:public_trace_file)
260 # And finally we should be able to do it with the owner of the trace
261 get :view, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:normal_user).id }
262 check_trace_view gpx_files(:public_trace_file)
265 # Check an anonymous trace can't be viewed by another user
268 get :view, :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id
269 assert_response :redirect
270 assert_redirected_to :action => :list
272 # Now with some other user, which should work since the trace is anon
273 get :view, { :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id }, { :user => users(:normal_user).id }
274 assert_response :redirect
275 assert_redirected_to :action => :list
277 # And finally we should be able to do it with the owner of the trace
278 get :view, { :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id }, { :user => users(:public_user).id }
279 check_trace_view gpx_files(:anon_trace_file)
282 # Test viewing a trace that doesn't exist
283 def test_view_not_found
284 # First with no auth, which should work since the trace is public
285 get :view, :display_name => users(:public_user).display_name, :id => 0
286 assert_response :redirect
287 assert_redirected_to :action => :list
289 # Now with some other user, which should work since the trace is public
290 get :view, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
291 assert_response :redirect
292 assert_redirected_to :action => :list
294 # And finally we should be able to do it with the owner of the trace
295 get :view, { :display_name => users(:public_user).display_name, :id => 5 }, { :user => users(:public_user).id }
296 assert_response :redirect
297 assert_redirected_to :action => :list
300 # Test downloading a trace
302 # First with no auth, which should work since the trace is public
303 get :data, :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id
304 check_trace_data gpx_files(:public_trace_file)
306 # Now with some other user, which should work since the trace is public
307 get :data, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:public_user).id }
308 check_trace_data gpx_files(:public_trace_file)
310 # And finally we should be able to do it with the owner of the trace
311 get :data, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:normal_user).id }
312 check_trace_data gpx_files(:public_trace_file)
315 # Test downloading a compressed trace
316 def test_data_compressed
317 # First get the data as is
318 get :data, :display_name => users(:public_user).display_name, :id => gpx_files(:identifiable_trace_file).id
319 check_trace_data gpx_files(:identifiable_trace_file), "application/x-gzip", "gpx.gz"
321 # Now ask explicitly for XML format
322 get :data, :display_name => users(:public_user).display_name, :id => gpx_files(:identifiable_trace_file).id, :format => "xml"
323 check_trace_data gpx_files(:identifiable_trace_file), "application/xml", "xml"
325 # Now ask explicitly for GPX format
326 get :data, :display_name => users(:public_user).display_name, :id => gpx_files(:identifiable_trace_file).id, :format => "gpx"
327 check_trace_data gpx_files(:identifiable_trace_file)
330 # Check an anonymous trace can't be downloaded by another user
333 get :data, :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id
334 assert_response :not_found
336 # Now with some other user, which shouldn't work since the trace is anon
337 get :data, { :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id }, { :user => users(:normal_user).id }
338 assert_response :not_found
340 # And finally we should be able to do it with the owner of the trace
341 get :data, { :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id }, { :user => users(:public_user).id }
342 check_trace_data gpx_files(:anon_trace_file)
345 # Test downloading a trace that doesn't exist
346 def test_data_not_found
347 # First with no auth and a trace that has never existed
348 get :data, :display_name => users(:public_user).display_name, :id => 0
349 assert_response :not_found
351 # Now with a trace that has never existed
352 get :data, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
353 assert_response :not_found
355 # Now with a trace that has been deleted
356 get :data, { :display_name => users(:public_user).display_name, :id => 5 }, { :user => users(:public_user).id }
357 assert_response :not_found
360 # Test downloading the picture for a trace
362 # First with no auth, which should work since the trace is public
363 get :picture, :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id
364 check_trace_picture gpx_files(:public_trace_file)
366 # Now with some other user, which should work since the trace is public
367 get :picture, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:public_user).id }
368 check_trace_picture gpx_files(:public_trace_file)
370 # And finally we should be able to do it with the owner of the trace
371 get :picture, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:normal_user).id }
372 check_trace_picture gpx_files(:public_trace_file)
375 # Check the picture for an anonymous trace can't be downloaded by another user
376 def test_picture_anon
378 get :picture, :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id
379 assert_response :forbidden
381 # Now with some other user, which shouldn't work since the trace is anon
382 get :picture, { :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id }, { :user => users(:normal_user).id }
383 assert_response :forbidden
385 # And finally we should be able to do it with the owner of the trace
386 get :picture, { :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id }, { :user => users(:public_user).id }
387 check_trace_picture gpx_files(:anon_trace_file)
390 # Test downloading the picture for a trace that doesn't exist
391 def test_picture_not_found
392 # First with no auth, which should work since the trace is public
393 get :picture, :display_name => users(:public_user).display_name, :id => 0
394 assert_response :not_found
396 # Now with some other user, which should work since the trace is public
397 get :picture, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
398 assert_response :not_found
400 # And finally we should be able to do it with the owner of the trace
401 get :picture, { :display_name => users(:public_user).display_name, :id => 5 }, { :user => users(:public_user).id }
402 assert_response :not_found
405 # Test downloading the icon for a trace
407 # First with no auth, which should work since the trace is public
408 get :icon, :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id
409 check_trace_icon gpx_files(:public_trace_file)
411 # Now with some other user, which should work since the trace is public
412 get :icon, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:public_user).id }
413 check_trace_icon gpx_files(:public_trace_file)
415 # And finally we should be able to do it with the owner of the trace
416 get :icon, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:normal_user).id }
417 check_trace_icon gpx_files(:public_trace_file)
420 # Check the icon for an anonymous trace can't be downloaded by another user
423 get :icon, :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id
424 assert_response :forbidden
426 # Now with some other user, which shouldn't work since the trace is anon
427 get :icon, { :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id }, { :user => users(:normal_user).id }
428 assert_response :forbidden
430 # And finally we should be able to do it with the owner of the trace
431 get :icon, { :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id }, { :user => users(:public_user).id }
432 check_trace_icon gpx_files(:anon_trace_file)
435 # Test downloading the icon for a trace that doesn't exist
436 def test_icon_not_found
437 # First with no auth, which should work since the trace is public
438 get :icon, :display_name => users(:public_user).display_name, :id => 0
439 assert_response :not_found
441 # Now with some other user, which should work since the trace is public
442 get :icon, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
443 assert_response :not_found
445 # And finally we should be able to do it with the owner of the trace
446 get :icon, { :display_name => users(:public_user).display_name, :id => 5 }, { :user => users(:public_user).id }
447 assert_response :not_found
450 # Test fetching the create page
454 assert_response :redirect
455 assert_redirected_to :controller => :user, :action => :login, :referer => trace_create_path
457 # Now authenticated as a user with gps.trace.visibility set
458 get :create, {}, { :user => users(:public_user).id }
459 assert_response :success
460 assert_template :create
461 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
463 # Now authenticated as a user with gps.trace.public set
464 get :create, {}, { :user => users(:second_public_user).id }
465 assert_response :success
466 assert_template :create
467 assert_select "select#trace_visibility option[value=public][selected]", 1
469 # Now authenticated as a user with no preferences
470 get :create, {}, { :user => users(:normal_user).id }
471 assert_response :success
472 assert_template :create
473 assert_select "select#trace_visibility option[value=private][selected]", 1
476 # Test creating a trace
479 file = Rack::Test::UploadedFile.new(gpx_files(:public_trace_file).trace_name, "application/gpx+xml")
482 post :create, :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" }
483 assert_response :forbidden
486 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
487 post :create, { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, { :user => users(:public_user).id }
488 assert_response :redirect
489 assert_redirected_to :action => :list, :display_name => users(:public_user).display_name
490 assert_match /file has been uploaded/, flash[:notice]
491 trace = Trace.order(:id => :desc).first
492 assert_equal "1.gpx", trace.name
493 assert_equal "New Trace", trace.description
494 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
495 assert_equal "trackable", trace.visibility
496 assert_equal false, trace.inserted
497 assert_equal File.new(gpx_files(:public_trace_file).trace_name).read, File.new(trace.trace_name).read
499 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
502 # Test fetching the edit page for a trace
505 get :edit, :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id
506 assert_response :redirect
507 assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id)
509 # Now with some other user, which should fail
510 get :edit, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:public_user).id }
511 assert_response :forbidden
513 # Now with a trace which doesn't exist
514 get :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
515 assert_response :not_found
517 # Now with a trace which has been deleted
518 get :edit, { :display_name => users(:public_user).display_name, :id => gpx_files(:deleted_trace_file).id }, { :user => users(:public_user).id }
519 assert_response :not_found
521 # Finally with a trace that we are allowed to edit
522 get :edit, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:normal_user).id }
523 assert_response :success
526 # Test saving edits to a trace
529 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
532 post :edit, :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id, :trace => new_details
533 assert_response :forbidden
535 # Now with some other user, which should fail
536 post :edit, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id, :trace => new_details }, { :user => users(:public_user).id }
537 assert_response :forbidden
539 # Now with a trace which doesn't exist
540 post :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id, :trace => new_details }
541 assert_response :not_found
543 # Now with a trace which has been deleted
544 post :edit, { :display_name => users(:public_user).display_name, :id => gpx_files(:deleted_trace_file).id, :trace => new_details }, { :user => users(:public_user).id }
545 assert_response :not_found
547 # Finally with a trace that we are allowed to edit
548 post :edit, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id, :trace => new_details }, { :user => users(:normal_user).id }
549 assert_response :redirect
550 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
551 trace = Trace.find(gpx_files(:public_trace_file).id)
552 assert_equal new_details[:description], trace.description
553 assert_equal new_details[:tagstring], trace.tagstring
554 assert_equal new_details[:visibility], trace.visibility
557 # Test deleting a trace
560 post :delete, :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id
561 assert_response :forbidden
563 # Now with some other user, which should fail
564 post :delete, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:public_user).id }
565 assert_response :forbidden
567 # Now with a trace which doesn't exist
568 post :delete, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
569 assert_response :not_found
571 # Now with a trace has already been deleted
572 post :delete, { :display_name => users(:public_user).display_name, :id => gpx_files(:deleted_trace_file).id }, { :user => users(:public_user).id }
573 assert_response :not_found
575 # Finally with a trace that we are allowed to delete
576 post :delete, { :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id }, { :user => users(:normal_user).id }
577 assert_response :redirect
578 assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
579 trace = Trace.find(gpx_files(:public_trace_file).id)
580 assert_equal false, trace.visible
583 # Check getting a specific trace through the api
586 get :api_read, :id => gpx_files(:public_trace_file).id
587 assert_response :unauthorized
589 # Now with some other user, which should work since the trace is public
590 basic_authorization(users(:public_user).display_name, "test")
591 get :api_read, :id => gpx_files(:public_trace_file).id
592 assert_response :success
594 # And finally we should be able to do it with the owner of the trace
595 basic_authorization(users(:normal_user).display_name, "test")
596 get :api_read, :id => gpx_files(:public_trace_file).id
597 assert_response :success
600 # Check an anoymous trace can't be specifically fetched by another user
601 def test_api_read_anon
603 get :api_read, :id => gpx_files(:anon_trace_file).id
604 assert_response :unauthorized
606 # Now try with another user, which shouldn't work since the trace is anon
607 basic_authorization(users(:normal_user).display_name, "test")
608 get :api_read, :id => gpx_files(:anon_trace_file).id
609 assert_response :forbidden
611 # And finally we should be able to get the trace details with the trace owner
612 basic_authorization(users(:public_user).display_name, "test")
613 get :api_read, :id => gpx_files(:anon_trace_file).id
614 assert_response :success
617 # Check the api details for a trace that doesn't exist
618 def test_api_read_not_found
619 # Try first with no auth, as it should requure it
620 get :api_read, :id => 0
621 assert_response :unauthorized
623 # Login, and try again
624 basic_authorization(users(:public_user).display_name, "test")
625 get :api_read, :id => 0
626 assert_response :not_found
628 # Now try a trace which did exist but has been deleted
629 basic_authorization(users(:public_user).display_name, "test")
630 get :api_read, :id => 5
631 assert_response :not_found
634 # Test downloading a trace through the api
637 get :api_data, :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id
638 assert_response :unauthorized
640 # Now with some other user, which should work since the trace is public
641 basic_authorization(users(:public_user).display_name, "test")
642 get :api_data, :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id
643 check_trace_data gpx_files(:public_trace_file)
645 # And finally we should be able to do it with the owner of the trace
646 basic_authorization(users(:normal_user).display_name, "test")
647 get :api_data, :display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id
648 check_trace_data gpx_files(:public_trace_file)
651 # Test downloading a compressed trace through the api
652 def test_api_data_compressed
653 # Authenticate as the owner of the trace we will be using
654 basic_authorization(users(:public_user).display_name, "test")
656 # First get the data as is
657 get :api_data, :display_name => users(:public_user).display_name, :id => gpx_files(:identifiable_trace_file).id
658 check_trace_data gpx_files(:identifiable_trace_file), "application/x-gzip", "gpx.gz"
660 # Now ask explicitly for XML format
661 get :api_data, :display_name => users(:public_user).display_name, :id => gpx_files(:identifiable_trace_file).id, :format => "xml"
662 check_trace_data gpx_files(:identifiable_trace_file), "application/xml", "xml"
664 # Now ask explicitly for GPX format
665 get :api_data, :display_name => users(:public_user).display_name, :id => gpx_files(:identifiable_trace_file).id, :format => "gpx"
666 check_trace_data gpx_files(:identifiable_trace_file)
669 # Check an anonymous trace can't be downloaded by another user through the api
670 def test_api_data_anon
672 get :api_data, :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id
673 assert_response :unauthorized
675 # Now with some other user, which shouldn't work since the trace is anon
676 basic_authorization(users(:normal_user).display_name, "test")
677 get :api_data, :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id
678 assert_response :forbidden
680 # And finally we should be able to do it with the owner of the trace
681 basic_authorization(users(:public_user).display_name, "test")
682 get :api_data, :display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id
683 check_trace_data gpx_files(:anon_trace_file)
686 # Test downloading a trace that doesn't exist through the api
687 def test_api_data_not_found
689 get :api_data, :display_name => users(:public_user).display_name, :id => 0
690 assert_response :unauthorized
692 # Now with a trace that has never existed
693 basic_authorization(users(:public_user).display_name, "test")
694 get :api_data, :display_name => users(:public_user).display_name, :id => 0
695 assert_response :not_found
697 # Now with a trace that has been deleted
698 basic_authorization(users(:public_user).display_name, "test")
699 get :api_data, :display_name => users(:public_user).display_name, :id => 5
700 assert_response :not_found
703 # Test creating a trace through the api
706 file = Rack::Test::UploadedFile.new(gpx_files(:public_trace_file).trace_name, "application/gpx+xml")
709 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
710 assert_response :unauthorized
713 assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
714 basic_authorization(users(:public_user).display_name, "test")
715 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
716 assert_response :success
717 trace = Trace.find(response.body.to_i)
718 assert_equal "1.gpx", trace.name
719 assert_equal "New Trace", trace.description
720 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
721 assert_equal "trackable", trace.visibility
722 assert_equal false, trace.inserted
723 assert_equal File.new(gpx_files(:public_trace_file).trace_name).read, File.new(trace.trace_name).read
725 assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
730 # Now authenticated, with the legacy public flag
731 assert_not_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
732 basic_authorization(users(:public_user).display_name, "test")
733 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 1
734 assert_response :success
735 trace = Trace.find(response.body.to_i)
736 assert_equal "1.gpx", trace.name
737 assert_equal "New Trace", trace.description
738 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
739 assert_equal "public", trace.visibility
740 assert_equal false, trace.inserted
741 assert_equal File.new(gpx_files(:public_trace_file).trace_name).read, File.new(trace.trace_name).read
743 assert_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
748 # Now authenticated, with the legacy private flag
749 assert_nil users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first
750 basic_authorization(users(:second_public_user).display_name, "test")
751 post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 0
752 assert_response :success
753 trace = Trace.find(response.body.to_i)
754 assert_equal "1.gpx", trace.name
755 assert_equal "New Trace", trace.description
756 assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
757 assert_equal "private", trace.visibility
758 assert_equal false, trace.inserted
759 assert_equal File.new(gpx_files(:public_trace_file).trace_name).read, File.new(trace.trace_name).read
761 assert_equal "private", users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first.v
764 # Check updating a trace through the api
767 content gpx_files(:public_trace_file).to_xml
768 put :api_update, :id => gpx_files(:public_trace_file).id
769 assert_response :unauthorized
771 # Now with some other user, which should fail
772 basic_authorization(users(:public_user).display_name, "test")
773 content gpx_files(:public_trace_file).to_xml
774 put :api_update, :id => gpx_files(:public_trace_file).id
775 assert_response :forbidden
777 # Now with a trace which doesn't exist
778 basic_authorization(users(:public_user).display_name, "test")
779 content gpx_files(:public_trace_file).to_xml
780 put :api_update, :id => 0
781 assert_response :not_found
783 # Now with a trace which did exist but has been deleted
784 basic_authorization(users(:public_user).display_name, "test")
785 content gpx_files(:deleted_trace_file).to_xml
786 put :api_update, :id => gpx_files(:deleted_trace_file).id
787 assert_response :not_found
789 # Now try an update with the wrong ID
790 basic_authorization(users(:normal_user).display_name, "test")
791 content gpx_files(:anon_trace_file).to_xml
792 put :api_update, :id => gpx_files(:public_trace_file).id
793 assert_response :bad_request,
794 "should not be able to update a trace with a different ID from the XML"
796 # And finally try an update that should work
797 basic_authorization(users(:normal_user).display_name, "test")
798 t = gpx_files(:public_trace_file)
799 t.description = "Changed description"
800 t.visibility = "private"
802 put :api_update, :id => t.id
803 assert_response :success
804 nt = Trace.find(t.id)
805 assert_equal nt.description, t.description
806 assert_equal nt.visibility, t.visibility
809 # Check deleting a trace through the api
812 delete :api_delete, :id => gpx_files(:public_trace_file).id
813 assert_response :unauthorized
815 # Now with some other user, which should fail
816 basic_authorization(users(:public_user).display_name, "test")
817 delete :api_delete, :id => gpx_files(:public_trace_file).id
818 assert_response :forbidden
820 # Now with a trace which doesn't exist
821 basic_authorization(users(:public_user).display_name, "test")
822 delete :api_delete, :id => 0
823 assert_response :not_found
825 # And finally we should be able to do it with the owner of the trace
826 basic_authorization(users(:normal_user).display_name, "test")
827 delete :api_delete, :id => gpx_files(:public_trace_file).id
828 assert_response :success
830 # Try it a second time, which should fail
831 basic_authorization(users(:normal_user).display_name, "test")
832 delete :api_delete, :id => gpx_files(:public_trace_file).id
833 assert_response :not_found
838 def check_trace_feed(traces)
839 assert_response :success
840 assert_template "georss"
841 assert_equal "application/rss+xml", @response.content_type
842 assert_select "rss", :count => 1 do
843 assert_select "channel", :count => 1 do
844 assert_select "title"
845 assert_select "description"
847 assert_select "image"
848 assert_select "item", :count => traces.visible.count do |items|
849 traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
850 assert_select item, "title", trace.name
851 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
852 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
853 assert_select item, "description"
854 # assert_select item, "dc:creator", trace.user.display_name
855 assert_select item, "pubDate", trace.timestamp.rfc822
862 def check_trace_list(traces)
863 assert_response :success
864 assert_template "list"
867 assert_select "table#trace_list tbody", :count => 1 do
868 assert_select "tr", :count => traces.visible.count do |rows|
869 traces.visible.order("timestamp DESC").zip(rows).each do |trace, row|
870 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
871 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
872 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
873 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
878 assert_select "h4", /Nothing here yet/
882 def check_trace_view(trace)
883 assert_response :success
884 assert_template "view"
886 assert_select "table", :count => 1 do
887 assert_select "td", /^#{Regexp.quote(trace.name)} /
888 assert_select "td", trace.user.display_name
889 assert_select "td", trace.description
893 def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
894 assert_response :success
895 assert_equal content_type, response.content_type
896 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
899 def check_trace_picture(trace)
900 assert_response :success
901 assert_equal "image/gif", response.content_type
902 assert_equal trace.large_picture, response.body
905 def check_trace_icon(trace)
906 assert_response :success
907 assert_equal "image/gif", response.content_type
908 assert_equal trace.icon_picture, response.body