1 # frozen_string_literal: true
5 class TracesControllerTest < ActionDispatch::IntegrationTest
7 # test all routes which lead to this controller
10 { :path => "/traces", :method => :get },
11 { :controller => "traces", :action => "index" }
14 { :path => "/traces/tag/tagname", :method => :get },
15 { :controller => "traces", :action => "index", :tag => "tagname" }
18 { :path => "/user/username/traces", :method => :get },
19 { :controller => "traces", :action => "index", :display_name => "username" }
22 { :path => "/user/username/traces/tag/tagname", :method => :get },
23 { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
27 { :path => "/traces/mine", :method => :get },
28 { :controller => "traces", :action => "mine" }
31 { :path => "/traces/mine/tag/tagname", :method => :get },
32 { :controller => "traces", :action => "mine", :tag => "tagname" }
36 { :path => "/user/username/traces/1", :method => :get },
37 { :controller => "traces", :action => "show", :display_name => "username", :id => "1" }
41 { :path => "/traces/new", :method => :get },
42 { :controller => "traces", :action => "new" }
45 { :path => "/traces", :method => :post },
46 { :controller => "traces", :action => "create" }
49 { :path => "/traces/1/edit", :method => :get },
50 { :controller => "traces", :action => "edit", :id => "1" }
53 { :path => "/traces/1", :method => :put },
54 { :controller => "traces", :action => "update", :id => "1" }
57 { :path => "/traces/1", :method => :delete },
58 { :controller => "traces", :action => "destroy", :id => "1" }
62 assert_redirected_to "/traces"
64 get "/traces/tag/tagname/page/1"
65 assert_redirected_to "/traces/tag/tagname"
67 get "/user/username/traces/page/1"
68 assert_redirected_to "/user/username/traces"
70 get "/user/username/traces/tag/tagname/page/1"
71 assert_redirected_to "/user/username/traces/tag/tagname"
73 get "/traces/mine/page/1"
74 assert_redirected_to "/traces/mine"
76 get "/traces/mine/tag/tagname/page/1"
77 assert_redirected_to "/traces/mine/tag/tagname"
80 # Check that the index of traces is displayed
83 # The fourth test below is surprisingly sensitive to timestamp ordering when the timestamps are equal.
84 trace_a = create(:trace, :visibility => "identifiable", :timestamp => 4.seconds.ago) do |trace|
85 create(:tracetag, :trace => trace, :tag => "London")
87 trace_b = create(:trace, :visibility => "identifiable", :timestamp => 3.seconds.ago) do |trace|
88 create(:tracetag, :trace => trace, :tag => "Birmingham")
90 trace_c = create(:trace, :visibility => "trackable", :user => user, :timestamp => 2.seconds.ago) do |trace|
91 create(:tracetag, :trace => trace, :tag => "London")
93 trace_d = create(:trace, :visibility => "trackable", :user => user, :timestamp => 1.second.ago) do |trace|
94 create(:tracetag, :trace => trace, :tag => "Birmingham")
97 # First with the public index
99 check_trace_index [trace_b, trace_a]
101 # Restrict traces to those with a given tag
102 get traces_path(:tag => "London")
103 check_trace_index [trace_a]
107 # Should see more when we are logged in
109 check_trace_index [trace_d, trace_c, trace_b, trace_a]
111 # Again, we should see more when we are logged in
112 get traces_path(:tag => "London")
113 check_trace_index [trace_c, trace_a]
116 # Check that I can get mine
119 create(:trace, :visibility => "identifiable") do |trace|
120 create(:tracetag, :trace => trace, :tag => "Birmingham")
122 trace_b = create(:trace, :visibility => "trackable", :user => user) do |trace|
123 create(:tracetag, :trace => trace, :tag => "London")
126 # First try to get it when not logged in
128 assert_redirected_to login_path(:referer => "/traces/mine")
132 # Now try when logged in
134 assert_redirected_to :action => "index", :display_name => user.display_name
136 # Fetch the actual index
137 get traces_path(:display_name => user.display_name)
138 check_trace_index [trace_b]
141 # The link to the legacy visibility page only shows up while the user
142 # still has traces using a legacy visibility
143 def test_index_legacy_traces_link
145 create(:trace, :visibility => "trackable", :user => user)
149 assert_response :success
150 assert_select "a[href=?]", edit_traces_legacy_visibility_path, :count => 0
152 create(:trace, :without_validations, :visibility => "public", :user => user)
155 assert_response :success
156 assert_select "a[href=?]", edit_traces_legacy_visibility_path, :count => 1
159 # Check the index of traces for a specific user
162 checked_user_traces_path = url_for :only_path => true, :controller => "traces", :action => "index", :display_name => user.display_name
163 second_user = create(:user)
164 third_user = create(:user)
166 trace_b = create(:trace, :visibility => "identifiable", :user => user)
167 trace_c = create(:trace, :visibility => "trackable", :user => user) do |trace|
168 create(:tracetag, :trace => trace, :tag => "London")
171 # Test a user with no traces
172 get traces_path(:display_name => second_user.display_name)
175 # Test the user with the traces - should see only public ones
176 get traces_path(:display_name => user.display_name)
177 check_trace_index [trace_b]
178 assert_dom ".nav-tabs" do
179 assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
180 assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 0
181 assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 1
184 session_for(third_user)
186 # Should still see only public ones when authenticated as another user
187 get traces_path(:display_name => user.display_name)
188 check_trace_index [trace_b]
189 assert_dom ".nav-tabs" do
190 assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
191 assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 1
192 assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 1
197 # Should see all traces when authenticated as the target user
198 get traces_path(:display_name => user.display_name)
199 check_trace_index [trace_c, trace_b]
200 assert_dom ".nav-tabs" do
201 assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
202 assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 1
203 assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 0
206 # Should only see traces with the correct tag when a tag is specified
207 get traces_path(:display_name => user.display_name, :tag => "London")
208 check_trace_index [trace_c]
210 # Should get an error if the user does not exist
211 get traces_path(:display_name => "UnknownUser")
212 assert_response :not_found
213 assert_template "users/no_such_user"
216 # Check a multi-page index
218 # Create several pages worth of traces
219 create_list(:trace, 50, :visibility => "identifiable")
220 next_path = traces_path
222 # Try and get the index
224 assert_response :success
225 assert_select "table#trace_list tbody", :count => 1 do
226 assert_select "tr", :count => 20
228 check_no_page_link "Newer Traces"
229 next_path = check_page_link "Older Traces"
231 # Try and get the second page
233 assert_response :success
234 assert_select "table#trace_list tbody", :count => 1 do
235 assert_select "tr", :count => 20
237 check_page_link "Newer Traces"
238 next_path = check_page_link "Older Traces"
240 # Try and get the third page
242 assert_response :success
243 assert_select "table#trace_list tbody", :count => 1 do
244 assert_select "tr", :count => 10
246 next_path = check_page_link "Newer Traces"
247 check_no_page_link "Older Traces"
249 # Go back to the second page
251 assert_response :success
252 assert_select "table#trace_list tbody", :count => 1 do
253 assert_select "tr", :count => 20
255 next_path = check_page_link "Newer Traces"
256 check_page_link "Older Traces"
258 # Go back to the first page
260 assert_response :success
261 assert_select "table#trace_list tbody", :count => 1 do
262 assert_select "tr", :count => 20
264 check_no_page_link "Newer Traces"
265 check_page_link "Older Traces"
268 # Check a multi-page index of tagged traces
269 def test_index_tagged_paged
270 # Create several pages worth of traces
271 create_list(:trace, 100, :visibility => "identifiable") do |trace, index|
272 create(:tracetag, :trace => trace, :tag => "London") if index.even?
274 next_path = traces_path :tag => "London"
276 # Try and get the index
278 assert_response :success
279 assert_select "table#trace_list tbody", :count => 1 do
280 assert_select "tr", :count => 20
282 check_no_page_link "Newer Traces"
283 next_path = check_page_link "Older Traces"
285 # Try and get the second page
287 assert_response :success
288 assert_select "table#trace_list tbody", :count => 1 do
289 assert_select "tr", :count => 20
291 check_page_link "Newer Traces"
292 next_path = check_page_link "Older Traces"
294 # Try and get the third page
296 assert_response :success
297 assert_select "table#trace_list tbody", :count => 1 do
298 assert_select "tr", :count => 10
300 next_path = check_page_link "Newer Traces"
301 check_no_page_link "Older Traces"
303 # Go back to the second page
305 assert_response :success
306 assert_select "table#trace_list tbody", :count => 1 do
307 assert_select "tr", :count => 20
309 next_path = check_page_link "Newer Traces"
310 check_page_link "Older Traces"
312 # Go back to the first page
314 assert_response :success
315 assert_select "table#trace_list tbody", :count => 1 do
316 assert_select "tr", :count => 20
318 check_no_page_link "Newer Traces"
319 check_page_link "Older Traces"
322 def test_index_invalid_paged
323 # Try some invalid paged accesses
324 %w[-1 fred].each do |id|
325 get traces_path(:before => id)
326 assert_redirected_to :controller => :errors, :action => :bad_request
328 get traces_path(:after => id)
329 assert_redirected_to :controller => :errors, :action => :bad_request
333 # Check that the traces index is not displayed when the traces feature is disabled
334 def test_index_disabled
335 with_settings(:traces_disabled => true) do
337 assert_response :not_found
341 # Test showing a trace
343 public_trace_file = create(:trace, :visibility => "identifiable")
345 # First with no auth, which should work since the trace is public
346 get show_trace_path(public_trace_file.user, public_trace_file)
347 check_trace_show public_trace_file
349 # Now with some other user, which should work since the trace is public
350 session_for(create(:user))
351 get show_trace_path(public_trace_file.user, public_trace_file)
352 check_trace_show public_trace_file
354 # And finally we should be able to do it with the owner of the trace
355 session_for(public_trace_file.user)
356 get show_trace_path(public_trace_file.user, public_trace_file)
357 check_trace_show public_trace_file
360 # Check an anonymous trace can't be viewed by another user
362 anon_trace_file = create(:trace, :visibility => "trackable")
365 get show_trace_path(anon_trace_file.user, anon_trace_file)
366 assert_redirected_to :action => :index
368 # Now with some other user, which should not work since the trace is anon
369 session_for(create(:user))
370 get show_trace_path(anon_trace_file.user, anon_trace_file)
371 assert_redirected_to :action => :index
373 # And finally we should be able to do it with the owner of the trace
374 session_for(anon_trace_file.user)
375 get show_trace_path(anon_trace_file.user, anon_trace_file)
376 check_trace_show anon_trace_file
379 # Test showing a trace that doesn't exist
380 def test_show_not_found
381 deleted_trace_file = create(:trace, :deleted)
383 # First with a trace that has never existed
384 get show_trace_path(create(:user), 0)
385 assert_redirected_to :action => :index
387 # Now with a trace that has been deleted
388 session_for(deleted_trace_file.user)
389 get show_trace_path(deleted_trace_file.user, deleted_trace_file)
390 assert_redirected_to :action => :index
393 # Test fetching the new trace page
397 assert_redirected_to login_path(:referer => new_trace_path)
399 # Now authenticated as a user with gps.trace.visibility set
401 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
404 assert_response :success
406 assert_select "select#trace_visibility option[value=identifiable][selected]", 1
408 # Now authenticated as a user with the legacy gps.trace.public preference,
409 # which is no longer supported and falls back to trackable
410 second_user = create(:user)
411 create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
412 session_for(second_user)
414 assert_response :success
416 assert_select "select#trace_visibility option[value=trackable][selected]", 1
418 # Now authenticated as a user with no preferences, defaults to trackable
419 third_user = create(:user)
420 session_for(third_user)
422 assert_response :success
424 assert_select "select#trace_visibility option[value=trackable][selected]", 1
425 # New uploads only offer the two supported visibilities
426 assert_select "select#trace_visibility option", 2
429 # Test creating a trace
432 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
433 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
437 post traces_path(:trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" })
438 assert_response :forbidden
444 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
445 assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
447 post traces_path, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
448 assert_redirected_to :action => :index, :display_name => user.display_name
449 assert_match(/file has been uploaded/, flash[:notice])
450 trace = Trace.order(:id => :desc).first
451 assert_equal "a.gpx", trace.name
452 assert_equal "New Trace", trace.description
453 assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
454 assert_equal "trackable", trace.visibility
455 assert_not trace.inserted
456 assert_equal File.new(fixture).read, trace.file.blob.download
458 assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
461 # A legacy visibility (public or private) is no longer accepted on upload
462 def test_create_post_with_legacy_visibility
463 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
464 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
468 assert_no_difference "Trace.count" do
469 post traces_path, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "public" } }
474 # Test creating a trace with validation errors
475 def test_create_post_with_validation_errors
477 fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
478 file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
482 create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
483 assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
485 post traces_path, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }
487 assert_match "is too short (minimum is 1 character)", response.body
490 # Test fetching the edit page for a trace using GET
492 public_trace_file = create(:trace, :visibility => "identifiable")
493 deleted_trace_file = create(:trace, :deleted)
496 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
497 assert_redirected_to login_path(:referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id))
499 # Now with some other user, which should fail
500 session_for(create(:user))
501 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
502 assert_response :forbidden
504 # Now with a trace which doesn't exist
505 session_for(create(:user))
506 get edit_trace_path(:display_name => create(:user).display_name, :id => 0)
507 assert_response :not_found
509 # Now with a trace which has been deleted
510 session_for(deleted_trace_file.user)
511 get edit_trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
512 assert_response :not_found
514 # Finally with a trace that we are allowed to edit
515 session_for(public_trace_file.user)
516 get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
517 assert_response :success
520 # An old visibility stays selected in the edit form, so changing other fields
521 # does not change it by mistake.
522 def test_edit_get_keeps_legacy_visibility
523 trace = create(:trace, :without_validations, :visibility => "private")
525 session_for(trace.user)
526 get edit_trace_path(:display_name => trace.user.display_name, :id => trace)
527 assert_response :success
528 assert_select "select#trace_visibility option", 3
529 assert_select "select#trace_visibility option[value=private][selected]", 1
532 # Test saving edits to a trace
534 public_trace_file = create(:trace, :visibility => "identifiable")
535 deleted_trace_file = create(:trace, :deleted)
538 new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "trackable" }
541 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
542 assert_response :forbidden
544 # Now with some other user, which should fail
545 session_for(create(:user))
546 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
547 assert_response :forbidden
549 # Now with a trace which doesn't exist
550 session_for(create(:user))
551 put trace_path(:display_name => create(:user).display_name, :id => 0, :trace => new_details)
552 assert_response :not_found
554 # Now with a trace which has been deleted
555 session_for(deleted_trace_file.user)
556 put trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file, :trace => new_details)
557 assert_response :not_found
559 # Finally with a trace that we are allowed to edit
560 session_for(public_trace_file.user)
561 put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
562 assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
563 trace = Trace.find(public_trace_file.id)
564 assert_equal new_details[:description], trace.description
565 assert_equal new_details[:tagstring], trace.tagstring
566 assert_equal new_details[:visibility], trace.visibility
569 # Test invalid updates
570 def test_update_invalid
571 trace = create(:trace)
574 session_for(trace.user)
575 put trace_path(trace, :trace => { :description => "Changed description", :tagstring => "new_tag", :visibility => "wrong" })
576 assert_response :success
577 assert_select "title", :text => /^Editing Trace/
580 # Test destroying a trace
582 public_trace_file = create(:trace, :visibility => "identifiable")
583 deleted_trace_file = create(:trace, :deleted)
586 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
587 assert_response :forbidden
589 # Now with some other user, which should fail
590 session_for(create(:user))
591 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
592 assert_response :forbidden
594 # Now with a trace which doesn't exist
595 session_for(create(:user))
596 delete trace_path(:display_name => create(:user).display_name, :id => 0)
597 assert_response :not_found
599 # Now with a trace has already been deleted
600 session_for(deleted_trace_file.user)
601 delete trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
602 assert_response :not_found
604 # Now with a trace that we are allowed to delete
605 session_for(public_trace_file.user)
606 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
607 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
608 trace = Trace.find(public_trace_file.id)
609 assert_not trace.visible
611 # Finally with a trace that is destroyed by an admin
612 public_trace_file = create(:trace, :visibility => "identifiable")
613 admin = create(:administrator_user)
615 delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
616 assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
617 trace = Trace.find(public_trace_file.id)
618 assert_not trace.visible
623 def check_trace_index(traces)
624 assert_response :success
625 assert_template "index"
628 assert_select "h2", /Nothing here yet/
630 assert_select "table#trace_list tbody", :count => 1 do
631 assert_select "tr", :count => traces.length do |rows|
632 traces.zip(rows).each do |trace, row|
633 assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
634 assert_select row, "li", Regexp.new(Regexp.escape("#{trace.size} points")) if trace.inserted?
635 assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
636 assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
637 assert_select row, "a[href='#{user_path trace.user}']", :text => trace.user.display_name
644 def check_no_page_link(name)
645 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
648 def check_page_link(name)
649 assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
650 return buttons.first.attributes["href"].value
654 def check_trace_show(trace)
655 assert_response :success
656 assert_template "show"
658 assert_select "table", :count => 1 do
659 assert_select "td", /^#{Regexp.quote(trace.name)} /
660 assert_select "td a[href='#{user_path trace.user}']", :text => trace.user.display_name
661 assert_select "td", trace.description