1 require File.dirname(__FILE__) + '/../test_helper'
3 class TraceControllerTest < ActionController::TestCase
4 fixtures :users, :gpx_files
5 set_fixture_class :gpx_files => 'Trace'
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/gpx/create", :method => :post },
12 { :controller => "trace", :action => "api_create" }
15 { :path => "/api/0.6/gpx/1", :method => :get },
16 { :controller => "trace", :action => "api_read", :id => "1" }
19 { :path => "/api/0.6/gpx/1", :method => :put },
20 { :controller => "trace", :action => "api_update", :id => "1" }
23 { :path => "/api/0.6/gpx/1", :method => :delete },
24 { :controller => "trace", :action => "api_delete", :id => "1" }
27 { :controller => "trace", :action => "api_read", :id => "1" },
28 { :path => "/api/0.6/gpx/1/details", :method => :get }
31 { :path => "/api/0.6/gpx/1/data", :method => :get },
32 { :controller => "trace", :action => "api_data", :id => "1" }
35 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
36 { :controller => "trace", :action => "api_data", :id => "1", :format => "xml" }
40 { :path => "/traces", :method => :get },
41 { :controller => "trace", :action => "list" }
44 { :path => "/traces/page/1", :method => :get },
45 { :controller => "trace", :action => "list", :page => "1" }
48 { :path => "/traces/tag/tagname", :method => :get },
49 { :controller => "trace", :action => "list", :tag => "tagname" }
52 { :path => "/traces/tag/tagname/page/1", :method => :get },
53 { :controller => "trace", :action => "list", :tag => "tagname", :page => "1" }
56 { :path => "/user/username/traces", :method => :get },
57 { :controller => "trace", :action => "list", :display_name => "username" }
60 { :path => "/user/username/traces/page/1", :method => :get },
61 { :controller => "trace", :action => "list", :display_name => "username", :page => "1" }
64 { :path => "/user/username/traces/tag/tagname", :method => :get },
65 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname" }
68 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
69 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname", :page => "1" }
73 { :path => "/traces/mine", :method => :get },
74 { :controller => "trace", :action => "mine" }
77 { :path => "/traces/mine/page/1", :method => :get },
78 { :controller => "trace", :action => "mine", :page => "1" }
81 { :path => "/traces/mine/tag/tagname", :method => :get },
82 { :controller => "trace", :action => "mine", :tag => "tagname" }
85 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
86 { :controller => "trace", :action => "mine", :tag => "tagname", :page => "1" }
90 { :path => "/traces/rss", :method => :get },
91 { :controller => "trace", :action => "georss", :format => :rss }
94 { :path => "/traces/tag/tagname/rss", :method => :get },
95 { :controller => "trace", :action => "georss", :tag => "tagname", :format => :rss }
98 { :path => "/user/username/traces/rss", :method => :get },
99 { :controller => "trace", :action => "georss", :display_name => "username", :format => :rss }
102 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
103 { :controller => "trace", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
107 { :path => "/user/username/traces/1", :method => :get },
108 { :controller => "trace", :action => "view", :display_name => "username", :id => "1" }
111 { :path => "/user/username/traces/1/picture", :method => :get },
112 { :controller => "trace", :action => "picture", :display_name => "username", :id => "1" }
115 { :path => "/user/username/traces/1/icon", :method => :get },
116 { :controller => "trace", :action => "icon", :display_name => "username", :id => "1" }
120 { :path => "/trace/create", :method => :get },
121 { :controller => "trace", :action => "create" }
124 { :path => "/trace/create", :method => :post },
125 { :controller => "trace", :action => "create" }
128 { :path => "/trace/1/data", :method => :get },
129 { :controller => "trace", :action => "data", :id => "1" }
132 { :path => "/trace/1/data.xml", :method => :get },
133 { :controller => "trace", :action => "data", :id => "1", :format => "xml" }
136 { :path => "/trace/1/edit", :method => :get },
137 { :controller => "trace", :action => "edit", :id => "1" }
140 { :path => "/trace/1/edit", :method => :post },
141 { :controller => "trace", :action => "edit", :id => "1" }
144 { :path => "/trace/1/edit", :method => :patch },
145 { :controller => "trace", :action => "edit", :id => "1" }
148 { :path => "/trace/1/delete", :method => :post },
149 { :controller => "trace", :action => "delete", :id => "1" }
153 # Check that the list of changesets is displayed
156 check_trace_list Trace.public
158 get :list, :tag => "London"
159 check_trace_list Trace.tagged("London").public
162 # Check that I can get mine
164 @request.cookies["_osm_username"] = users(:public_user).display_name
166 # First try to get it when not logged in
168 assert_redirected_to :controller => 'user', :action => 'login', :referer => '/traces/mine'
170 # Now try when logged in
171 get :mine, {}, {:user => users(:public_user).id}
172 assert_redirected_to :controller => 'trace', :action => 'list', :display_name => users(:public_user).display_name
174 # Fetch the actual list
175 get :list, {:display_name => users(:public_user).display_name}, {:user => users(:public_user).id}
176 check_trace_list users(:public_user).traces
179 # Check the list of changesets for a specific user
181 # Test a user with no traces
182 get :list, :display_name => users(:second_public_user).display_name
183 check_trace_list users(:second_public_user).traces.public
185 # Test a user with some traces - should see only public ones
186 get :list, :display_name => users(:public_user).display_name
187 check_trace_list users(:public_user).traces.public
189 @request.cookies["_osm_username"] = users(:normal_user).display_name
191 # Should still see only public ones when authenticated as another user
192 get :list, {:display_name => users(:public_user).display_name}, {:user => users(:normal_user).id}
193 check_trace_list users(:public_user).traces.public
195 @request.cookies["_osm_username"] = users(:public_user).display_name
197 # Should see all traces when authenticated as the target user
198 get :list, {:display_name => users(:public_user).display_name}, {:user => users(:public_user).id}
199 check_trace_list users(:public_user).traces
201 # Should only see traces with the correct tag when a tag is specified
202 get :list, {:display_name => users(:public_user).display_name, :tag => "London"}, {:user => users(:public_user).id}
203 check_trace_list users(:public_user).traces.tagged("London")
206 # Check that the rss loads
208 get :georss, :format => :rss
209 check_trace_feed Trace.public
211 get :georss, :tag => "London", :format => :rss
212 check_trace_feed Trace.tagged("London").public
214 get :georss, :display_name => users(:public_user).display_name, :format => :rss
215 check_trace_feed users(:public_user).traces.public
217 get :georss, :display_name => users(:public_user).display_name, :tag => "Birmingham", :format => :rss
218 check_trace_feed users(:public_user).traces.tagged("Birmingham").public
221 # Test viewing a trace
223 # First with no auth, which should work since the trace is public
224 get :view, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}
225 check_trace_view gpx_files(:public_trace_file)
227 @request.cookies["_osm_username"] = users(:public_user).display_name
229 # Now with some other user, which should work since the trace is public
230 get :view, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}, {:user => users(:public_user).id}
231 check_trace_view gpx_files(:public_trace_file)
233 @request.cookies["_osm_username"] = users(:normal_user).display_name
235 # And finally we should be able to do it with the owner of the trace
236 get :view, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}, {:user => users(:normal_user).id}
237 check_trace_view gpx_files(:public_trace_file)
240 # Check an anonymous trace can't be viewed by another user
243 get :view, {:display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id}
244 assert_response :redirect
245 assert_redirected_to :action => :list
247 @request.cookies["_osm_username"] = users(:normal_user).display_name
249 # Now with some other user, which should work since the trace is anon
250 get :view, {:display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id}, {:user => users(:normal_user).id}
251 assert_response :redirect
252 assert_redirected_to :action => :list
254 @request.cookies["_osm_username"] = users(:public_user).display_name
256 # And finally we should be able to do it with the owner of the trace
257 get :view, {:display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id}, {:user => users(:public_user).id}
258 check_trace_view gpx_files(:anon_trace_file)
261 # Test viewing a trace that doesn't exist
262 def test_view_not_found
263 # First with no auth, which should work since the trace is public
264 get :view, {:display_name => users(:public_user).display_name, :id => 0}
265 assert_response :redirect
266 assert_redirected_to :action => :list
268 @request.cookies["_osm_username"] = users(:public_user).display_name
270 # Now with some other user, which should work since the trace is public
271 get :view, {:display_name => users(:public_user).display_name, :id => 0}, {:user => users(:public_user).id}
272 assert_response :redirect
273 assert_redirected_to :action => :list
275 # And finally we should be able to do it with the owner of the trace
276 get :view, {:display_name => users(:public_user).display_name, :id => 5}, {:user => users(:public_user).id}
277 assert_response :redirect
278 assert_redirected_to :action => :list
281 # Test fetching the edit page for a trace
284 get :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}
285 assert_response :redirect
286 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)
288 @request.cookies["_osm_username"] = users(:public_user).display_name
290 # Now with some other user, which should fail
291 get :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}, {:user => users(:public_user).id}
292 assert_response :forbidden
294 # Now with a trace which doesn't exist
295 get :edit, {:display_name => users(:public_user).display_name, :id => 0}, {:user => users(:public_user).id}
296 assert_response :not_found
298 # Now with a trace which has been deleted
299 get :edit, {:display_name => users(:public_user).display_name, :id => gpx_files(:deleted_trace_file).id}, {:user => users(:public_user).id}
300 assert_response :not_found
302 @request.cookies["_osm_username"] = users(:normal_user).display_name
304 # Finally with a trace that we are allowed to edit
305 get :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}, {:user => users(:normal_user).id}
306 assert_response :success
309 # Test saving edits to a trace
312 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
315 post :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id, :trace => new_details}
316 assert_response :forbidden
318 @request.cookies["_osm_username"] = users(:public_user).display_name
320 # Now with some other user, which should fail
321 post :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id, :trace => new_details}, {:user => users(:public_user).id}
322 assert_response :forbidden
324 # Now with a trace which doesn't exist
325 post :edit, {:display_name => users(:public_user).display_name, :id => 0}, {:user => users(:public_user).id, :trace => new_details}
326 assert_response :not_found
328 # Now with a trace which has been deleted
329 post :edit, {:display_name => users(:public_user).display_name, :id => gpx_files(:deleted_trace_file).id, :trace => new_details}, {:user => users(:public_user).id}
330 assert_response :not_found
332 @request.cookies["_osm_username"] = users(:normal_user).display_name
334 # Finally with a trace that we are allowed to edit
335 post :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id, :trace => new_details}, {:user => users(:normal_user).id}
336 assert_response :redirect
337 assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
338 trace = Trace.find(gpx_files(:public_trace_file).id)
339 assert_equal new_details[:description], trace.description
340 assert_equal new_details[:tagstring], trace.tagstring
341 assert_equal new_details[:visibility], trace.visibility
344 # Check getting a specific trace through the api
347 get :api_read, :id => gpx_files(:public_trace_file).id
348 assert_response :unauthorized
350 # Now with some other user, which should work since the trace is public
351 basic_authorization(users(:public_user).display_name, "test")
352 get :api_read, :id => gpx_files(:public_trace_file).id
353 assert_response :success
355 # And finally we should be able to do it with the owner of the trace
356 basic_authorization(users(:normal_user).display_name, "test")
357 get :api_read, :id => gpx_files(:public_trace_file).id
358 assert_response :success
361 # Check an anoymous trace can't be specifically fetched by another user
362 def test_api_read_anon
364 get :api_read, :id => gpx_files(:anon_trace_file).id
365 assert_response :unauthorized
367 # Now try with another user, which shouldn't work since the trace is anon
368 basic_authorization(users(:normal_user).display_name, "test")
369 get :api_read, :id => gpx_files(:anon_trace_file).id
370 assert_response :forbidden
372 # And finally we should be able to get the trace details with the trace owner
373 basic_authorization(users(:public_user).display_name, "test")
374 get :api_read, :id => gpx_files(:anon_trace_file).id
375 assert_response :success
378 # Check the api details for a trace that doesn't exist
379 def test_api_read_not_found
380 # Try first with no auth, as it should requure it
381 get :api_read, :id => 0
382 assert_response :unauthorized
384 # Login, and try again
385 basic_authorization(users(:public_user).display_name, "test")
386 get :api_read, :id => 0
387 assert_response :not_found
389 # Now try a trace which did exist but has been deleted
390 basic_authorization(users(:public_user).display_name, "test")
391 get :api_read, :id => 5
392 assert_response :not_found
395 # Check updating a trace through the api
398 content gpx_files(:public_trace_file).to_xml
399 put :api_update, :id => gpx_files(:public_trace_file).id
400 assert_response :unauthorized
402 # Now with some other user, which should fail
403 basic_authorization(users(:public_user).display_name, "test")
404 content gpx_files(:public_trace_file).to_xml
405 put :api_update, :id => gpx_files(:public_trace_file).id
406 assert_response :forbidden
408 # Now with a trace which doesn't exist
409 basic_authorization(users(:public_user).display_name, "test")
410 content gpx_files(:public_trace_file).to_xml
411 put :api_update, :id => 0
412 assert_response :not_found
414 # Now with a trace which did exist but has been deleted
415 basic_authorization(users(:public_user).display_name, "test")
416 content gpx_files(:deleted_trace_file).to_xml
417 put :api_update, :id => gpx_files(:deleted_trace_file).id
418 assert_response :not_found
420 # Now try an update with the wrong ID
421 basic_authorization(users(:normal_user).display_name, "test")
422 content gpx_files(:anon_trace_file).to_xml
423 put :api_update, :id => gpx_files(:public_trace_file).id
424 assert_response :bad_request,
425 "should not be able to update a trace with a different ID from the XML"
427 # And finally try an update that should work
428 basic_authorization(users(:normal_user).display_name, "test")
429 t = gpx_files(:public_trace_file)
430 t.description = "Changed description"
431 t.visibility = "private"
433 put :api_update, :id => t.id
434 assert_response :success
435 nt = Trace.find(t.id)
436 assert_equal nt.description, t.description
437 assert_equal nt.visibility, t.visibility
440 # Check deleting a trace through the api
443 delete :api_delete, :id => gpx_files(:public_trace_file).id
444 assert_response :unauthorized
446 # Now with some other user, which should fail
447 basic_authorization(users(:public_user).display_name, "test")
448 delete :api_delete, :id => gpx_files(:public_trace_file).id
449 assert_response :forbidden
451 # Now with a trace which doesn't exist
452 basic_authorization(users(:public_user).display_name, "test")
453 delete :api_delete, :id => 0
454 assert_response :not_found
456 # And finally we should be able to do it with the owner of the trace
457 basic_authorization(users(:normal_user).display_name, "test")
458 delete :api_delete, :id => gpx_files(:public_trace_file).id
459 assert_response :success
461 # Try it a second time, which should fail
462 basic_authorization(users(:normal_user).display_name, "test")
463 delete :api_delete, :id => gpx_files(:public_trace_file).id
464 assert_response :not_found
469 def check_trace_feed(traces)
470 assert_response :success
471 assert_template "georss"
472 assert_equal "application/rss+xml", @response.content_type
473 assert_select "rss", :count => 1 do
474 assert_select "channel", :count => 1 do
475 assert_select "title"
476 assert_select "description"
478 assert_select "image"
479 assert_select "item", :count => traces.visible.count do |items|
480 traces.visible.order("timestamp DESC").zip(items).each do |trace,item|
481 assert_select item, "title", trace.name
482 assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
483 assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
484 assert_select item, "description"
485 # assert_select item, "dc:creator", trace.user.display_name
486 assert_select item, "pubDate", trace.timestamp.rfc822
493 def check_trace_list(traces)
494 assert_response :success
495 assert_template "list"
498 assert_select "table#trace_list tbody", :count => 1 do
499 assert_select "tr", :count => traces.visible.count do |rows|
500 traces.visible.order("timestamp DESC").zip(rows).each do |trace,row|
501 assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)"))
502 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
503 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
508 assert_select "h4", /Nothing here yet/
512 def check_trace_view(trace)
513 assert_response :success
514 assert_template "view"
516 assert_select "table", :count => 1 do
517 assert_select "td", /^#{Regexp.quote(trace.name)} /
518 assert_select "td", trace.user.display_name
519 assert_select "td", trace.description