]> git.openstreetmap.org Git - rails.git/blob - test/controllers/traces_controller_test.rb
Merge pull request #7302 from tomhughes/terms-locale
[rails.git] / test / controllers / traces_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class TracesControllerTest < ActionDispatch::IntegrationTest
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/traces", :method => :get },
11       { :controller => "traces", :action => "index" }
12     )
13     assert_routing(
14       { :path => "/traces/tag/tagname", :method => :get },
15       { :controller => "traces", :action => "index", :tag => "tagname" }
16     )
17     assert_routing(
18       { :path => "/user/username/traces", :method => :get },
19       { :controller => "traces", :action => "index", :display_name => "username" }
20     )
21     assert_routing(
22       { :path => "/user/username/traces/tag/tagname", :method => :get },
23       { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
24     )
25
26     assert_routing(
27       { :path => "/traces/mine", :method => :get },
28       { :controller => "traces", :action => "mine" }
29     )
30     assert_routing(
31       { :path => "/traces/mine/tag/tagname", :method => :get },
32       { :controller => "traces", :action => "mine", :tag => "tagname" }
33     )
34
35     assert_routing(
36       { :path => "/user/username/traces/1", :method => :get },
37       { :controller => "traces", :action => "show", :display_name => "username", :id => "1" }
38     )
39
40     assert_routing(
41       { :path => "/traces/new", :method => :get },
42       { :controller => "traces", :action => "new" }
43     )
44     assert_routing(
45       { :path => "/traces", :method => :post },
46       { :controller => "traces", :action => "create" }
47     )
48     assert_routing(
49       { :path => "/traces/1/edit", :method => :get },
50       { :controller => "traces", :action => "edit", :id => "1" }
51     )
52     assert_routing(
53       { :path => "/traces/1", :method => :put },
54       { :controller => "traces", :action => "update", :id => "1" }
55     )
56     assert_routing(
57       { :path => "/traces/1", :method => :delete },
58       { :controller => "traces", :action => "destroy", :id => "1" }
59     )
60
61     get "/traces/page/1"
62     assert_redirected_to "/traces"
63
64     get "/traces/tag/tagname/page/1"
65     assert_redirected_to "/traces/tag/tagname"
66
67     get "/user/username/traces/page/1"
68     assert_redirected_to "/user/username/traces"
69
70     get "/user/username/traces/tag/tagname/page/1"
71     assert_redirected_to "/user/username/traces/tag/tagname"
72
73     get "/traces/mine/page/1"
74     assert_redirected_to "/traces/mine"
75
76     get "/traces/mine/tag/tagname/page/1"
77     assert_redirected_to "/traces/mine/tag/tagname"
78   end
79
80   # Check that the index of traces is displayed
81   def test_index
82     user = create(:user)
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")
86     end
87     trace_b = create(:trace, :visibility => "identifiable", :timestamp => 3.seconds.ago) do |trace|
88       create(:tracetag, :trace => trace, :tag => "Birmingham")
89     end
90     trace_c = create(:trace, :visibility => "trackable", :user => user, :timestamp => 2.seconds.ago) do |trace|
91       create(:tracetag, :trace => trace, :tag => "London")
92     end
93     trace_d = create(:trace, :visibility => "trackable", :user => user, :timestamp => 1.second.ago) do |trace|
94       create(:tracetag, :trace => trace, :tag => "Birmingham")
95     end
96
97     # First with the public index
98     get traces_path
99     check_trace_index [trace_b, trace_a]
100
101     # Restrict traces to those with a given tag
102     get traces_path(:tag => "London")
103     check_trace_index [trace_a]
104
105     session_for(user)
106
107     # Should see more when we are logged in
108     get traces_path
109     check_trace_index [trace_d, trace_c, trace_b, trace_a]
110
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]
114   end
115
116   # Check that I can get mine
117   def test_index_mine
118     user = create(:user)
119     create(:trace, :visibility => "identifiable") do |trace|
120       create(:tracetag, :trace => trace, :tag => "Birmingham")
121     end
122     trace_b = create(:trace, :visibility => "trackable", :user => user) do |trace|
123       create(:tracetag, :trace => trace, :tag => "London")
124     end
125
126     # First try to get it when not logged in
127     get traces_mine_path
128     assert_redirected_to login_path(:referer => "/traces/mine")
129
130     session_for(user)
131
132     # Now try when logged in
133     get traces_mine_path
134     assert_redirected_to :action => "index", :display_name => user.display_name
135
136     # Fetch the actual index
137     get traces_path(:display_name => user.display_name)
138     check_trace_index [trace_b]
139   end
140
141   # Check the index of traces for a specific user
142   def test_index_user
143     user = create(:user)
144     checked_user_traces_path = url_for :only_path => true, :controller => "traces", :action => "index", :display_name => user.display_name
145     second_user = create(:user)
146     third_user = create(:user)
147     create(:trace)
148     trace_b = create(:trace, :visibility => "identifiable", :user => user)
149     trace_c = create(:trace, :visibility => "trackable", :user => user) do |trace|
150       create(:tracetag, :trace => trace, :tag => "London")
151     end
152
153     # Test a user with no traces
154     get traces_path(:display_name => second_user.display_name)
155     check_trace_index []
156
157     # Test the user with the traces - should see only public ones
158     get traces_path(:display_name => user.display_name)
159     check_trace_index [trace_b]
160     assert_dom ".nav-tabs" do
161       assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
162       assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 0
163       assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 1
164     end
165
166     session_for(third_user)
167
168     # Should still see only public ones when authenticated as another user
169     get traces_path(:display_name => user.display_name)
170     check_trace_index [trace_b]
171     assert_dom ".nav-tabs" do
172       assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
173       assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 1
174       assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 1
175     end
176
177     session_for(user)
178
179     # Should see all traces when authenticated as the target user
180     get traces_path(:display_name => user.display_name)
181     check_trace_index [trace_c, trace_b]
182     assert_dom ".nav-tabs" do
183       assert_dom "a[href='#{traces_path}']", :text => "All Traces", :count => 1
184       assert_dom "a[href='#{traces_mine_path}']", :text => "My Traces", :count => 1
185       assert_dom "a[href='#{checked_user_traces_path}']", :text => Regexp.new(Regexp.escape(user.display_name)), :count => 0
186     end
187
188     # Should only see traces with the correct tag when a tag is specified
189     get traces_path(:display_name => user.display_name, :tag => "London")
190     check_trace_index [trace_c]
191
192     # Should get an error if the user does not exist
193     get traces_path(:display_name => "UnknownUser")
194     assert_response :not_found
195     assert_template "users/no_such_user"
196   end
197
198   # Check a multi-page index
199   def test_index_paged
200     # Create several pages worth of traces
201     create_list(:trace, 50, :visibility => "identifiable")
202     next_path = traces_path
203
204     # Try and get the index
205     get next_path
206     assert_response :success
207     assert_select "table#trace_list tbody", :count => 1 do
208       assert_select "tr", :count => 20
209     end
210     check_no_page_link "Newer Traces"
211     next_path = check_page_link "Older Traces"
212
213     # Try and get the second page
214     get next_path
215     assert_response :success
216     assert_select "table#trace_list tbody", :count => 1 do
217       assert_select "tr", :count => 20
218     end
219     check_page_link "Newer Traces"
220     next_path = check_page_link "Older Traces"
221
222     # Try and get the third page
223     get next_path
224     assert_response :success
225     assert_select "table#trace_list tbody", :count => 1 do
226       assert_select "tr", :count => 10
227     end
228     next_path = check_page_link "Newer Traces"
229     check_no_page_link "Older Traces"
230
231     # Go back to the second page
232     get next_path
233     assert_response :success
234     assert_select "table#trace_list tbody", :count => 1 do
235       assert_select "tr", :count => 20
236     end
237     next_path = check_page_link "Newer Traces"
238     check_page_link "Older Traces"
239
240     # Go back to the first page
241     get next_path
242     assert_response :success
243     assert_select "table#trace_list tbody", :count => 1 do
244       assert_select "tr", :count => 20
245     end
246     check_no_page_link "Newer Traces"
247     check_page_link "Older Traces"
248   end
249
250   # Check a multi-page index of tagged traces
251   def test_index_tagged_paged
252     # Create several pages worth of traces
253     create_list(:trace, 100, :visibility => "identifiable") do |trace, index|
254       create(:tracetag, :trace => trace, :tag => "London") if index.even?
255     end
256     next_path = traces_path :tag => "London"
257
258     # Try and get the index
259     get next_path
260     assert_response :success
261     assert_select "table#trace_list tbody", :count => 1 do
262       assert_select "tr", :count => 20
263     end
264     check_no_page_link "Newer Traces"
265     next_path = check_page_link "Older Traces"
266
267     # Try and get the second page
268     get next_path
269     assert_response :success
270     assert_select "table#trace_list tbody", :count => 1 do
271       assert_select "tr", :count => 20
272     end
273     check_page_link "Newer Traces"
274     next_path = check_page_link "Older Traces"
275
276     # Try and get the third page
277     get next_path
278     assert_response :success
279     assert_select "table#trace_list tbody", :count => 1 do
280       assert_select "tr", :count => 10
281     end
282     next_path = check_page_link "Newer Traces"
283     check_no_page_link "Older Traces"
284
285     # Go back to the second page
286     get next_path
287     assert_response :success
288     assert_select "table#trace_list tbody", :count => 1 do
289       assert_select "tr", :count => 20
290     end
291     next_path = check_page_link "Newer Traces"
292     check_page_link "Older Traces"
293
294     # Go back to the first page
295     get next_path
296     assert_response :success
297     assert_select "table#trace_list tbody", :count => 1 do
298       assert_select "tr", :count => 20
299     end
300     check_no_page_link "Newer Traces"
301     check_page_link "Older Traces"
302   end
303
304   def test_index_invalid_paged
305     # Try some invalid paged accesses
306     %w[-1 fred].each do |id|
307       get traces_path(:before => id)
308       assert_redirected_to :controller => :errors, :action => :bad_request
309
310       get traces_path(:after => id)
311       assert_redirected_to :controller => :errors, :action => :bad_request
312     end
313   end
314
315   # Check that the traces index is not displayed when the traces feature is disabled
316   def test_index_disabled
317     with_settings(:traces_disabled => true) do
318       get traces_path
319       assert_response :not_found
320     end
321   end
322
323   # Test showing a trace
324   def test_show
325     public_trace_file = create(:trace, :visibility => "identifiable")
326
327     # First with no auth, which should work since the trace is public
328     get show_trace_path(public_trace_file.user, public_trace_file)
329     check_trace_show public_trace_file
330
331     # Now with some other user, which should work since the trace is public
332     session_for(create(:user))
333     get show_trace_path(public_trace_file.user, public_trace_file)
334     check_trace_show public_trace_file
335
336     # And finally we should be able to do it with the owner of the trace
337     session_for(public_trace_file.user)
338     get show_trace_path(public_trace_file.user, public_trace_file)
339     check_trace_show public_trace_file
340   end
341
342   # Check an anonymous trace can't be viewed by another user
343   def test_show_anon
344     anon_trace_file = create(:trace, :visibility => "trackable")
345
346     # First with no auth
347     get show_trace_path(anon_trace_file.user, anon_trace_file)
348     assert_redirected_to :action => :index
349
350     # Now with some other user, which should not work since the trace is anon
351     session_for(create(:user))
352     get show_trace_path(anon_trace_file.user, anon_trace_file)
353     assert_redirected_to :action => :index
354
355     # And finally we should be able to do it with the owner of the trace
356     session_for(anon_trace_file.user)
357     get show_trace_path(anon_trace_file.user, anon_trace_file)
358     check_trace_show anon_trace_file
359   end
360
361   # Test showing a trace that doesn't exist
362   def test_show_not_found
363     deleted_trace_file = create(:trace, :deleted)
364
365     # First with a trace that has never existed
366     get show_trace_path(create(:user), 0)
367     assert_redirected_to :action => :index
368
369     # Now with a trace that has been deleted
370     session_for(deleted_trace_file.user)
371     get show_trace_path(deleted_trace_file.user, deleted_trace_file)
372     assert_redirected_to :action => :index
373   end
374
375   # Test fetching the new trace page
376   def test_new_get
377     # First with no auth
378     get new_trace_path
379     assert_redirected_to login_path(:referer => new_trace_path)
380
381     # Now authenticated as a user with gps.trace.visibility set
382     user = create(:user)
383     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
384     session_for(user)
385     get new_trace_path
386     assert_response :success
387     assert_template :new
388     assert_select "select#trace_visibility option[value=identifiable][selected]", 1
389
390     # Now authenticated as a user with the legacy gps.trace.public preference,
391     # which is no longer supported and falls back to trackable
392     second_user = create(:user)
393     create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
394     session_for(second_user)
395     get new_trace_path
396     assert_response :success
397     assert_template :new
398     assert_select "select#trace_visibility option[value=trackable][selected]", 1
399
400     # Now authenticated as a user with no preferences, defaults to trackable
401     third_user = create(:user)
402     session_for(third_user)
403     get new_trace_path
404     assert_response :success
405     assert_template :new
406     assert_select "select#trace_visibility option[value=trackable][selected]", 1
407     # New uploads only offer the two supported visibilities
408     assert_select "select#trace_visibility option", 2
409   end
410
411   # Test creating a trace
412   def test_create_post
413     # Get file to use
414     fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
415     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
416     user = create(:user)
417
418     # First with no auth
419     post traces_path(:trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" })
420     assert_response :forbidden
421
422     # Rewind the file
423     file.rewind
424
425     # Now authenticated
426     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
427     assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
428     session_for(user)
429     post traces_path, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
430     assert_redirected_to :action => :index, :display_name => user.display_name
431     assert_match(/file has been uploaded/, flash[:notice])
432     trace = Trace.order(:id => :desc).first
433     assert_equal "a.gpx", trace.name
434     assert_equal "New Trace", trace.description
435     assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
436     assert_equal "trackable", trace.visibility
437     assert_not trace.inserted
438     assert_equal File.new(fixture).read, trace.file.blob.download
439     trace.destroy
440     assert_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
441   end
442
443   # A legacy visibility (public or private) is no longer accepted on upload
444   def test_create_post_with_legacy_visibility
445     fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
446     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
447     user = create(:user)
448     session_for(user)
449
450     assert_no_difference "Trace.count" do
451       post traces_path, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "public" } }
452     end
453     assert_template :new
454   end
455
456   # Test creating a trace with validation errors
457   def test_create_post_with_validation_errors
458     # Get file to use
459     fixture = Rails.root.join("test/gpx/fixtures/a.gpx")
460     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
461     user = create(:user)
462
463     # Now authenticated
464     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
465     assert_not_equal "trackable", user.preferences.find_by(:k => "gps.trace.visibility").v
466     session_for(user)
467     post traces_path, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }
468     assert_template :new
469     assert_match "is too short (minimum is 1 character)", response.body
470   end
471
472   # Test fetching the edit page for a trace using GET
473   def test_edit_get
474     public_trace_file = create(:trace, :visibility => "identifiable")
475     deleted_trace_file = create(:trace, :deleted)
476
477     # First with no auth
478     get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
479     assert_redirected_to login_path(:referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id))
480
481     # Now with some other user, which should fail
482     session_for(create(:user))
483     get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
484     assert_response :forbidden
485
486     # Now with a trace which doesn't exist
487     session_for(create(:user))
488     get edit_trace_path(:display_name => create(:user).display_name, :id => 0)
489     assert_response :not_found
490
491     # Now with a trace which has been deleted
492     session_for(deleted_trace_file.user)
493     get edit_trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
494     assert_response :not_found
495
496     # Finally with a trace that we are allowed to edit
497     session_for(public_trace_file.user)
498     get edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
499     assert_response :success
500   end
501
502   # An old visibility stays selected in the edit form, so changing other fields
503   # does not change it by mistake.
504   def test_edit_get_keeps_legacy_visibility
505     trace = create(:trace, :without_validations, :visibility => "private")
506
507     session_for(trace.user)
508     get edit_trace_path(:display_name => trace.user.display_name, :id => trace)
509     assert_response :success
510     assert_select "select#trace_visibility option", 3
511     assert_select "select#trace_visibility option[value=private][selected]", 1
512   end
513
514   # Test saving edits to a trace
515   def test_update
516     public_trace_file = create(:trace, :visibility => "identifiable")
517     deleted_trace_file = create(:trace, :deleted)
518
519     # New details
520     new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "trackable" }
521
522     # First with no auth
523     put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
524     assert_response :forbidden
525
526     # Now with some other user, which should fail
527     session_for(create(:user))
528     put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
529     assert_response :forbidden
530
531     # Now with a trace which doesn't exist
532     session_for(create(:user))
533     put trace_path(:display_name => create(:user).display_name, :id => 0, :trace => new_details)
534     assert_response :not_found
535
536     # Now with a trace which has been deleted
537     session_for(deleted_trace_file.user)
538     put trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file, :trace => new_details)
539     assert_response :not_found
540
541     # Finally with a trace that we are allowed to edit
542     session_for(public_trace_file.user)
543     put trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file, :trace => new_details)
544     assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
545     trace = Trace.find(public_trace_file.id)
546     assert_equal new_details[:description], trace.description
547     assert_equal new_details[:tagstring], trace.tagstring
548     assert_equal new_details[:visibility], trace.visibility
549   end
550
551   # Test invalid updates
552   def test_update_invalid
553     trace = create(:trace)
554
555     # Invalid visibility
556     session_for(trace.user)
557     put trace_path(trace, :trace => { :description => "Changed description", :tagstring => "new_tag", :visibility => "wrong" })
558     assert_response :success
559     assert_select "title", :text => /^Editing Trace/
560   end
561
562   # Test destroying a trace
563   def test_destroy
564     public_trace_file = create(:trace, :visibility => "identifiable")
565     deleted_trace_file = create(:trace, :deleted)
566
567     # First with no auth
568     delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
569     assert_response :forbidden
570
571     # Now with some other user, which should fail
572     session_for(create(:user))
573     delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
574     assert_response :forbidden
575
576     # Now with a trace which doesn't exist
577     session_for(create(:user))
578     delete trace_path(:display_name => create(:user).display_name, :id => 0)
579     assert_response :not_found
580
581     # Now with a trace has already been deleted
582     session_for(deleted_trace_file.user)
583     delete trace_path(:display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file)
584     assert_response :not_found
585
586     # Now with a trace that we are allowed to delete
587     session_for(public_trace_file.user)
588     delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
589     assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
590     trace = Trace.find(public_trace_file.id)
591     assert_not trace.visible
592
593     # Finally with a trace that is destroyed by an admin
594     public_trace_file = create(:trace, :visibility => "identifiable")
595     admin = create(:administrator_user)
596     session_for(admin)
597     delete trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file)
598     assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
599     trace = Trace.find(public_trace_file.id)
600     assert_not trace.visible
601   end
602
603   private
604
605   def check_trace_index(traces)
606     assert_response :success
607     assert_template "index"
608
609     if traces.empty?
610       assert_select "h2", /Nothing here yet/
611     else
612       assert_select "table#trace_list tbody", :count => 1 do
613         assert_select "tr", :count => traces.length do |rows|
614           traces.zip(rows).each do |trace, row|
615             assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
616             assert_select row, "li", Regexp.new(Regexp.escape("#{trace.size} points")) if trace.inserted?
617             assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
618             assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
619             assert_select row, "a[href='#{user_path trace.user}']", :text => trace.user.display_name
620           end
621         end
622       end
623     end
624   end
625
626   def check_no_page_link(name)
627     assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/, :count => 0 }, "unexpected #{name} page link"
628   end
629
630   def check_page_link(name)
631     assert_select "a.page-link", { :text => /#{Regexp.quote(name)}/ }, "missing #{name} page link" do |buttons|
632       return buttons.first.attributes["href"].value
633     end
634   end
635
636   def check_trace_show(trace)
637     assert_response :success
638     assert_template "show"
639
640     assert_select "table", :count => 1 do
641       assert_select "td", /^#{Regexp.quote(trace.name)} /
642       assert_select "td a[href='#{user_path trace.user}']", :text => trace.user.display_name
643       assert_select "td", trace.description
644     end
645   end
646 end