3 class TracesControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/traces", :method => :get },
9 { :controller => "traces", :action => "index" }
12 { :path => "/traces/tag/tagname", :method => :get },
13 { :controller => "traces", :action => "index", :tag => "tagname" }
16 { :path => "/user/username/traces", :method => :get },
17 { :controller => "traces", :action => "index", :display_name => "username" }
20 { :path => "/user/username/traces/tag/tagname", :method => :get },
21 { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
25 { :path => "/traces/mine", :method => :get },
26 { :controller => "traces", :action => "mine" }
29 { :path => "/traces/mine/tag/tagname", :method => :get },
30 { :controller => "traces", :action => "mine", :tag => "tagname" }
34 { :path => "/user/username/traces/1", :method => :get },
35 { :controller => "traces", :action => "show", :display_name => "username", :id => "1" }
39 { :path => "/traces/new", :method => :get },
40 { :controller => "traces", :action => "new" }
43 { :path => "/traces", :method => :post },
44 { :controller => "traces", :action => "create" }
47 { :path => "/trace/1/data", :method => :get },
48 { :controller => "traces", :action => "data", :id => "1" }
51 { :path => "/trace/1/data.xml", :method => :get },
52 { :controller => "traces", :action => "data", :id => "1", :format => "xml" }
55 { :path => "/traces/1/edit", :method => :get },
56 { :controller => "traces", :action => "edit", :id => "1" }
59 { :path => "/traces/1", :method => :put },
60 { :controller => "traces", :action => "update", :id => "1" }
63 { :path => "/traces/1", :method => :delete },
64 { :controller => "traces", :action => "destroy", :id => "1" }
68 assert_redirected_to "/traces"
70 get "/traces/tag/tagname/page/1"
71 assert_redirected_to "/traces/tag/tagname"
73 get "/user/username/traces/page/1"
74 assert_redirected_to "/user/username/traces"
76 get "/user/username/traces/tag/tagname/page/1"
77 assert_redirected_to "/user/username/traces/tag/tagname"
79 get "/traces/mine/page/1"
80 assert_redirected_to "/traces/mine"
82 get "/traces/mine/tag/tagname/page/1"
83 assert_redirected_to "/traces/mine/tag/tagname"
86 # Check that the index of traces is displayed
89 # The fourth test below is surprisingly sensitive to timestamp ordering when the timestamps are equal.
90 trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
91 create(:tracetag, :trace => trace, :tag => "London")
93 trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
94 create(:tracetag, :trace => trace, :tag => "Birmingham")
96 trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
97 create(:tracetag, :trace => trace, :tag => "London")
99 trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
100 create(:tracetag, :trace => trace, :tag => "Birmingham")
103 # First with the public index
105 check_trace_index [trace_b, trace_a]
107 # Restrict traces to those with a given tag
108 get traces_path(:tag => "London")
109 check_trace_index [trace_a]
113 # Should see more when we are logged in
115 check_trace_index [trace_d, trace_c, trace_b, trace_a]
117 # Again, we should see more when we are logged in
118 get traces_path(:tag => "London")
119 check_trace_index [trace_c, trace_a]
122 # Check that I can get mine
125 create(:trace, :visibility => "public") do |trace|
126 create(:tracetag, :trace => trace, :tag => "Birmingham")
128 trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
129 create(:tracetag, :trace => trace, :tag => "London")
132 # First try to get it when not logged in
134 assert_redirected_to login_path(:referer => "/traces/mine")
138 # Now try when logged in
140 assert_redirected_to :action => "index", :display_name => user.display_name
142 # Fetch the actual index
143 get traces_path(:display_name => user.display_name)
144 check_trace_index [trace_b]
147 # Check the index of traces for a specific user
150 checked_user_traces_path = url_for :only_path => true, :controller => "traces", :action => "index", :display_name => user.display_name
151 second_user = create(:user)
152 third_user = create(:user)
154 trace_b = create(:trace, :visibility => "public", :user => user)
155 trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
156 create(:tracetag, :trace => trace, :tag => "London")
159 # Test a user with no traces
160 get traces_path(:display_name => second_user.display_name)
163 # Test the user with the traces - should see only public ones
164 get traces_path(:display_name => user.display_name)
165 check_trace_index [trace_b]
166 assert_dom ".nav-tabs" do
167 assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
168 assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 0
169 assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 1
172 session_for(third_user)
174 # Should still see only public ones when authenticated as another user
175 get traces_path(:display_name => user.display_name)
176 check_trace_index [trace_b]
177 assert_dom ".nav-tabs" do
178 assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
179 assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 1
180 assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 1
185 # Should see all traces when authenticated as the target user
186 get traces_path(:display_name => user.display_name)
187 check_trace_index [trace_c, trace_b]
188 assert_dom ".nav-tabs" do
189 assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
190 assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 1
191 assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 0
194 # Should only see traces with the correct tag when a tag is specified
195 get traces_path(:display_name => user.display_name, :tag => "London")
196 check_trace_index [trace_c]
198 # Should get an error if the user does not exist
199 get traces_path(:display_name => "UnknownUser")
200 assert_response :not_found
201 assert_template "users/no_such_user"
204 # Check a multi-page index
206 # Create several pages worth of traces
207 create_list(:trace, 50)
209 # Try and get the index
211 assert_response :success
212 assert_select "table#trace_list tbody", :count => 1 do
213 assert_select "tr", :count => 20
215 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
216 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
218 # Try and get the second page
219 get css_select("li.page-item a.page-link").last["href"]
220 assert_response :success
221 assert_select "table#trace_list tbody", :count => 1 do
222 assert_select "tr", :count => 20
224 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
225 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
227 # Try and get the third page
228 get css_select("li.page-item a.page-link").last["href"]
229 assert_response :success
230 assert_select "table#trace_list tbody", :count => 1 do
231 assert_select "tr", :count => 10
233 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
234 assert_select "li.page-item.disabled span.page-link", :text => "Older Traces", :count => 2
236 # Go back to the second page
237 get css_select("li.page-item a.page-link").first["href"]
238 assert_response :success
239 assert_select "table#trace_list tbody", :count => 1 do
240 assert_select "tr", :count => 20
242 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
243 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
245 # Go back to the first page
246 get css_select("li.page-item a.page-link").first["href"]
247 assert_response :success
248 assert_select "table#trace_list tbody", :count => 1 do
249 assert_select "tr", :count => 20
251 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
252 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
255 # Check a multi-page index of tagged traces
256 def test_index_tagged_paged
257 # Create several pages worth of traces
258 create_list(:trace, 100) do |trace, index|
259 create(:tracetag, :trace => trace, :tag => "London") if index.even?
262 # Try and get the index
263 get traces_path(:tag => "London")
264 assert_response :success
265 assert_select "table#trace_list tbody", :count => 1 do
266 assert_select "tr", :count => 20
268 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
269 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
271 # Try and get the second page
272 get css_select("li.page-item a.page-link").last["href"]
273 assert_response :success
274 assert_select "table#trace_list tbody", :count => 1 do
275 assert_select "tr", :count => 20
277 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
278 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
280 # Try and get the third page
281 get css_select("li.page-item a.page-link").last["href"]
282 assert_response :success
283 assert_select "table#trace_list tbody", :count => 1 do
284 assert_select "tr", :count => 10
286 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
287 assert_select "li.page-item.disabled span.page-link", :text => "Older Traces", :count => 2
289 # Go back to the second page
290 get css_select("li.page-item a.page-link").first["href"]
291 assert_response :success
292 assert_select "table#trace_list tbody", :count => 1 do
293 assert_select "tr", :count => 20
295 assert_select "li.page-item a.page-link", :text => "Newer Traces", :count => 2
296 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
298 # Go back to the first page
299 get css_select("li.page-item a.page-link").first["href"]
300 assert_response :success
301 assert_select "table#trace_list tbody", :count => 1 do
302 assert_select "tr", :count => 20
304 assert_select "li.page-item.disabled span.page-link", :text => "Newer Traces", :count => 2
305 assert_select "li.page-item a.page-link", :text => "Older Traces", :count => 2
308 def test_index_invalid_paged
309 # Try some invalid paged accesses
310 %w[-1 0 fred].each do |id|
311 get traces_path(:before => id)
312 assert_redirected_to :controller => :errors, :action => :bad_request
314 get traces_path(:after => id)
315 assert_redirected_to :controller => :errors, :action => :bad_request
319 # Test showing a trace
321 public_trace_file = create(:trace, :visibility => "public")
323 # First with no auth, which should work since the trace is public
324 get show_trace_path(public_trace_file.user, public_trace_file)
325 check_trace_show public_trace_file
327 # Now with some other user, which should work since the trace is public
328 session_for(create(:user))
329 get show_trace_path(public_trace_file.user, public_trace_file)
330 check_trace_show public_trace_file
332 # And finally we should be able to do it with the owner of the trace
333 session_for(public_trace_file.user)
334 get show_trace_path(public_trace_file.user, public_trace_file)
335 check_trace_show public_trace_file
338 # Check an anonymous trace can't be viewed by another user
340 anon_trace_file = create(:trace, :visibility => "private")
343 get show_trace_path(anon_trace_file.user, anon_trace_file)
344 assert_redirected_to :action => :index
346 # Now with some other user, which should not work since the trace is anon
347 session_for(create(:user))
348 get show_trace_path(anon_trace_file.user, anon_trace_file)
349 assert_redirected_to :action => :index
351 # And finally we should be able to do it with the owner of the trace
352 session_for(anon_trace_file.user)
353 get show_trace_path(anon_trace_file.user, anon_trace_file)
354 check_trace_show anon_trace_file
357 # Test showing a trace that doesn't exist
358 def test_show_not_found
359 deleted_trace_file = create(:trace, :deleted)
361 # First with a trace that has never existed
362 get show_trace_path(create(:user), 0)
363 assert_redirected_to :action => :index
365 # Now with a trace that has been deleted
366 session_for(deleted_trace_file.user)
367 get show_trace_path(deleted_trace_file.user, deleted_trace_file)
368 assert_redirected_to :action => :index
371 # Test downloading a trace
373 public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
375 # First with no auth, which should work since the trace is public
376 get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
379 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
381 # Now with some other user, which should work since the trace is public
382 session_for(create(:user))
383 get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
386 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
388 # And finally we should be able to do it with the owner of the trace
389 session_for(public_trace_file.user)
390 get trace_data_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
393 check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
396 # Test downloading a compressed trace
397 def test_data_compressed
398 identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
400 # First get the data as is
401 get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file)
404 check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/gzip", "gpx.gz"
406 # Now ask explicitly for XML format
407 get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml")
408 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
410 # Now ask explicitly for GPX format
411 get trace_data_path(:display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx")
412 check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
415 # Check an anonymous trace can't be downloaded by another user
417 anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
420 get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
421 assert_response :not_found
423 # Now with some other user, which shouldn't work since the trace is anon
424 session_for(create(:user))
425 get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
426 assert_response :not_found
428 # And finally we should be able to do it with the owner of the trace
429 session_for(anon_trace_file.user)
430 get trace_data_path(:display_name => anon_trace_file.user.display_name, :id => anon_trace_file)
433 check_trace_data anon_trace_file, "db4cb5ed2d7d2b627b3b504296c4f701"
436 # Test downloading a trace that doesn't exist
437 def test_data_not_found
438 deleted_trace_file = create(:trace, :deleted)
440 # First with a trace that has never existed
441 get trace_data_path(:display_name => create(:user).display_name, :id => 0)
442 assert_response :not_found
444 # Now with a trace that has been deleted
445 session_for(deleted_trace_file.user)
446 get trace_data_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
447 assert_response :not_found
450 # Test fetching the new trace page
454 assert_redirected_to login_path(:referer => new_trace_path)
456 # Now authenticated as a user with gps.trace.visibility set
458 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
461 assert_response :success
463 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
465 # Now authenticated as a user with gps.trace.public set
466 second_user = create(:user)
467 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
468 session_for(second_user)
470 assert_response :success
472 assert_select "select#trace_visibility option[value=public][selected]", 1
474 # Now authenticated as a user with no preferences
475 third_user = create(:user)
476 session_for(third_user)
478 assert_response :success
480 assert_select "select#trace_visibility option[value=private][selected]", 1
483 # Test creating a trace
486 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
487 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
491 post traces_path(:trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" })
492 assert_response :forbidden
498 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
499 assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
501 post traces_path, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
502 assert_redirected_to :action => :index, :display_name => user.display_name
503 assert_match(/file has been uploaded/, flash[:notice])
504 trace = Trace.order(:id => :desc).first
505 assert_equal "a.gpx", trace.name
506 assert_equal "New Trace", trace.description
507 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
508 assert_equal "trackable", trace.visibility
509 assert_not trace.inserted
510 assert_equal File.new(fixture).read, trace.file.blob.download
512 assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
515 # Test creating a trace with validation errors
516 def test_create_post_with_validation_errors
518 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
519 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
523 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
524 assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
526 post traces_path, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }
528 assert_match "is too short (minimum is 1 character)", response.body
531 # Test fetching the edit page for a trace using GET
533 public_trace_file = create(:trace, :visibility => "public")
534 deleted_trace_file = create(:trace, :deleted)
537 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
538 assert_redirected_to login_path(:referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id))
540 # Now with some other user, which should fail
541 session_for(create(:user))
542 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
543 assert_response :forbidden
545 # Now with a trace which doesn't exist
546 session_for(create(:user))
547 get edit_trace_path(:display_name => create(:user).display_name, :id => 0)
548 assert_response :not_found
550 # Now with a trace which has been deleted
551 session_for(deleted_trace_file.user)
552 get edit_trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
553 assert_response :not_found
555 # Finally with a trace that we are allowed to edit
556 session_for(public_trace_file.user)
557 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
558 assert_response :success
561 # Test saving edits to a trace
563 public_trace_file = create(:trace, :visibility => "public")
564 deleted_trace_file = create(:trace, :deleted)
567 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
570 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
571 assert_response :forbidden
573 # Now with some other user, which should fail
574 session_for(create(:user))
575 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
576 assert_response :forbidden
578 # Now with a trace which doesn't exist
579 session_for(create(:user))
580 put trace_path(:display_name => create(:user).display_name, :id => 0, :trace => new_details)
581 assert_response :not_found
583 # Now with a trace which has been deleted
584 session_for(deleted_trace_file.user)
585 put trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file, :trace => new_details)
586 assert_response :not_found
588 # Finally with a trace that we are allowed to edit
589 session_for(public_trace_file.user)
590 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
591 assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
592 trace = Trace.find(public_trace_file.id)
593 assert_equal new_details[:description], trace.description
594 assert_equal new_details[:tagstring], trace.tagstring
595 assert_equal new_details[:visibility], trace.visibility
598 # Test invalid updates
599 def test_update_invalid
600 trace = create(:trace)
603 session_for(trace.user)
604 put trace_path(trace, :trace => { :description => "Changed description", :tagstring => "new_tag", :visibility => "wrong" })
605 assert_response :success
606 assert_select "title", :text => /^Editing Trace/
609 # Test destroying a trace
611 public_trace_file = create(:trace, :visibility => "public")
612 deleted_trace_file = create(:trace, :deleted)
615 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
616 assert_response :forbidden
618 # Now with some other user, which should fail
619 session_for(create(:user))
620 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
621 assert_response :forbidden
623 # Now with a trace which doesn't exist
624 session_for(create(:user))
625 delete trace_path(:display_name => create(:user).display_name, :id => 0)
626 assert_response :not_found
628 # Now with a trace has already been deleted
629 session_for(deleted_trace_file.user)
630 delete trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
631 assert_response :not_found
633 # Now with a trace that we are allowed to delete
634 session_for(public_trace_file.user)
635 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
636 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
637 trace = Trace.find(public_trace_file.id)
638 assert_not trace.visible
640 # Finally with a trace that is destroyed by an admin
641 public_trace_file = create(:trace, :visibility => "public")
642 admin = create(:administrator_user)
644 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
645 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
646 trace = Trace.find(public_trace_file.id)
647 assert_not trace.visible
652 def check_trace_index(traces)
653 assert_response :success
654 assert_template "index"
657 assert_select "h2", /Nothing here yet/
659 assert_select "table#trace_list tbody", :count => 1 do
660 assert_select "tr", :count => traces.length do |rows|
661 traces.zip(rows).each do |trace, row|
662 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
663 assert_select row, "li", Regexp.new(Regexp.escape("#{trace.size} points")) if trace.inserted?
664 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
665 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
666 assert_select row, "a[href='#{user_path trace.user}']", :text => trace.user.display_name
673 def check_trace_show(trace)
674 assert_response :success
675 assert_template "show"
677 assert_select "table", :count => 1 do
678 assert_select "td", /^#{Regexp.quote(trace.name)} /
679 assert_select "td a[href='#{user_path trace.user}']", :text => trace.user.display_name
680 assert_select "td", trace.description
684 def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
685 assert_equal digest, Digest::MD5.hexdigest(response.body)
686 assert_equal content_type, response.media_type
687 assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"; filename*=UTF-8''#{trace.id}.#{extension}", @response.header["Content-Disposition"]