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