]> git.openstreetmap.org Git - rails.git/blob - test/controllers/traces_controller_test.rb
Rubocop fixes
[rails.git] / test / controllers / traces_controller_test.rb
1 require "test_helper"
2 require "minitest/mock"
3
4 class TracesControllerTest < ActionController::TestCase
5   def setup
6     @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
7     Object.const_set("GPX_TRACE_DIR", Rails.root.join("test", "gpx", "traces"))
8
9     @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
10     Object.const_set("GPX_IMAGE_DIR", Rails.root.join("test", "gpx", "images"))
11   end
12
13   def teardown
14     File.unlink(*Dir.glob(File.join(GPX_TRACE_DIR, "*.gpx")))
15     File.unlink(*Dir.glob(File.join(GPX_IMAGE_DIR, "*.gif")))
16
17     Object.send("remove_const", "GPX_TRACE_DIR")
18     Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
19
20     Object.send("remove_const", "GPX_IMAGE_DIR")
21     Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
22   end
23
24   ##
25   # test all routes which lead to this controller
26   def test_routes
27     assert_routing(
28       { :path => "/traces", :method => :get },
29       { :controller => "traces", :action => "index" }
30     )
31     assert_routing(
32       { :path => "/traces/page/1", :method => :get },
33       { :controller => "traces", :action => "index", :page => "1" }
34     )
35     assert_routing(
36       { :path => "/traces/tag/tagname", :method => :get },
37       { :controller => "traces", :action => "index", :tag => "tagname" }
38     )
39     assert_routing(
40       { :path => "/traces/tag/tagname/page/1", :method => :get },
41       { :controller => "traces", :action => "index", :tag => "tagname", :page => "1" }
42     )
43     assert_routing(
44       { :path => "/user/username/traces", :method => :get },
45       { :controller => "traces", :action => "index", :display_name => "username" }
46     )
47     assert_routing(
48       { :path => "/user/username/traces/page/1", :method => :get },
49       { :controller => "traces", :action => "index", :display_name => "username", :page => "1" }
50     )
51     assert_routing(
52       { :path => "/user/username/traces/tag/tagname", :method => :get },
53       { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
54     )
55     assert_routing(
56       { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
57       { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname", :page => "1" }
58     )
59
60     assert_routing(
61       { :path => "/traces/mine", :method => :get },
62       { :controller => "traces", :action => "mine" }
63     )
64     assert_routing(
65       { :path => "/traces/mine/page/1", :method => :get },
66       { :controller => "traces", :action => "mine", :page => "1" }
67     )
68     assert_routing(
69       { :path => "/traces/mine/tag/tagname", :method => :get },
70       { :controller => "traces", :action => "mine", :tag => "tagname" }
71     )
72     assert_routing(
73       { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
74       { :controller => "traces", :action => "mine", :tag => "tagname", :page => "1" }
75     )
76
77     assert_routing(
78       { :path => "/traces/rss", :method => :get },
79       { :controller => "traces", :action => "georss", :format => :rss }
80     )
81     assert_routing(
82       { :path => "/traces/tag/tagname/rss", :method => :get },
83       { :controller => "traces", :action => "georss", :tag => "tagname", :format => :rss }
84     )
85     assert_routing(
86       { :path => "/user/username/traces/rss", :method => :get },
87       { :controller => "traces", :action => "georss", :display_name => "username", :format => :rss }
88     )
89     assert_routing(
90       { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
91       { :controller => "traces", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
92     )
93
94     assert_routing(
95       { :path => "/user/username/traces/1", :method => :get },
96       { :controller => "traces", :action => "show", :display_name => "username", :id => "1" }
97     )
98     assert_routing(
99       { :path => "/user/username/traces/1/picture", :method => :get },
100       { :controller => "traces", :action => "picture", :display_name => "username", :id => "1" }
101     )
102     assert_routing(
103       { :path => "/user/username/traces/1/icon", :method => :get },
104       { :controller => "traces", :action => "icon", :display_name => "username", :id => "1" }
105     )
106
107     assert_routing(
108       { :path => "/traces/new", :method => :get },
109       { :controller => "traces", :action => "new" }
110     )
111     assert_routing(
112       { :path => "/traces", :method => :post },
113       { :controller => "traces", :action => "create" }
114     )
115     assert_routing(
116       { :path => "/trace/1/data", :method => :get },
117       { :controller => "traces", :action => "data", :id => "1" }
118     )
119     assert_routing(
120       { :path => "/trace/1/data.xml", :method => :get },
121       { :controller => "traces", :action => "data", :id => "1", :format => "xml" }
122     )
123     assert_routing(
124       { :path => "/traces/1/edit", :method => :get },
125       { :controller => "traces", :action => "edit", :id => "1" }
126     )
127     assert_routing(
128       { :path => "/traces/1", :method => :put },
129       { :controller => "traces", :action => "update", :id => "1" }
130     )
131     assert_routing(
132       { :path => "/trace/1/delete", :method => :post },
133       { :controller => "traces", :action => "delete", :id => "1" }
134     )
135   end
136
137   # Check that the index of traces is displayed
138   def test_index
139     user = create(:user)
140     # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
141     trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
142       create(:tracetag, :trace => trace, :tag => "London")
143     end
144     trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
145       create(:tracetag, :trace => trace, :tag => "Birmingham")
146     end
147     trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
148       create(:tracetag, :trace => trace, :tag => "London")
149     end
150     trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
151       create(:tracetag, :trace => trace, :tag => "Birmingham")
152     end
153
154     # First with the public index
155     get :index
156     check_trace_index [trace_b, trace_a]
157
158     # Restrict traces to those with a given tag
159     get :index, :params => { :tag => "London" }
160     check_trace_index [trace_a]
161
162     # Should see more when we are logged in
163     get :index, :session => { :user => user }
164     check_trace_index [trace_d, trace_c, trace_b, trace_a]
165
166     # Again, we should see more when we are logged in
167     get :index, :params => { :tag => "London" }, :session => { :user => user }
168     check_trace_index [trace_c, trace_a]
169   end
170
171   # Check that I can get mine
172   def test_index_mine
173     user = create(:user)
174     create(:trace, :visibility => "public") do |trace|
175       create(:tracetag, :trace => trace, :tag => "Birmingham")
176     end
177     trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
178       create(:tracetag, :trace => trace, :tag => "London")
179     end
180
181     # First try to get it when not logged in
182     get :mine
183     assert_redirected_to :controller => "users", :action => "login", :referer => "/traces/mine"
184
185     # Now try when logged in
186     get :mine, :session => { :user => user }
187     assert_redirected_to :action => "index", :display_name => user.display_name
188
189     # Fetch the actual index
190     get :index, :params => { :display_name => user.display_name }, :session => { :user => user }
191     check_trace_index [trace_b]
192   end
193
194   # Check the index of traces for a specific user
195   def test_index_user
196     user = create(:user)
197     second_user = create(:user)
198     third_user = create(:user)
199     create(:trace)
200     trace_b = create(:trace, :visibility => "public", :user => user)
201     trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
202       create(:tracetag, :trace => trace, :tag => "London")
203     end
204
205     # Test a user with no traces
206     get :index, :params => { :display_name => second_user.display_name }
207     check_trace_index []
208
209     # Test the user with the traces - should see only public ones
210     get :index, :params => { :display_name => user.display_name }
211     check_trace_index [trace_b]
212
213     # Should still see only public ones when authenticated as another user
214     get :index, :params => { :display_name => user.display_name }, :session => { :user => third_user }
215     check_trace_index [trace_b]
216
217     # Should see all traces when authenticated as the target user
218     get :index, :params => { :display_name => user.display_name }, :session => { :user => user }
219     check_trace_index [trace_c, trace_b]
220
221     # Should only see traces with the correct tag when a tag is specified
222     get :index, :params => { :display_name => user.display_name, :tag => "London" }, :session => { :user => user }
223     check_trace_index [trace_c]
224
225     # Should get an error if the user does not exist
226     get :index, :params => { :display_name => "UnknownUser" }
227     assert_response :not_found
228     assert_template "users/no_such_user"
229   end
230
231   # Check a multi-page index
232   def test_index_paged
233     # Create several pages worth of traces
234     create_list(:trace, 50)
235
236     # Try and get the index
237     get :index
238     assert_response :success
239     assert_select "table#trace_list tbody", :count => 1 do
240       assert_select "tr", :count => 20
241     end
242
243     # Try and get the second page
244     get :index, :params => { :page => 2 }
245     assert_response :success
246     assert_select "table#trace_list tbody", :count => 1 do
247       assert_select "tr", :count => 20
248     end
249   end
250
251   # Check the RSS feed
252   def test_rss
253     user = create(:user)
254     # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
255     trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
256       create(:tracetag, :trace => trace, :tag => "London")
257     end
258     trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
259       create(:tracetag, :trace => trace, :tag => "Birmingham")
260     end
261     create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
262       create(:tracetag, :trace => trace, :tag => "London")
263     end
264     create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
265       create(:tracetag, :trace => trace, :tag => "Birmingham")
266     end
267
268     # First with the public feed
269     get :georss, :params => { :format => :rss }
270     check_trace_feed [trace_b, trace_a]
271
272     # Restrict traces to those with a given tag
273     get :georss, :params => { :tag => "London", :format => :rss }
274     check_trace_feed [trace_a]
275   end
276
277   # Check the RSS feed for a specific user
278   def test_rss_user
279     user = create(:user)
280     second_user = create(:user)
281     create(:user)
282     create(:trace)
283     trace_b = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago, :user => user)
284     trace_c = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago, :user => user) do |trace|
285       create(:tracetag, :trace => trace, :tag => "London")
286     end
287     create(:trace, :visibility => "private")
288
289     # Test a user with no traces
290     get :georss, :params => { :display_name => second_user.display_name, :format => :rss }
291     check_trace_feed []
292
293     # Test the user with the traces - should see only public ones
294     get :georss, :params => { :display_name => user.display_name, :format => :rss }
295     check_trace_feed [trace_c, trace_b]
296
297     # Should only see traces with the correct tag when a tag is specified
298     get :georss, :params => { :display_name => user.display_name, :tag => "London", :format => :rss }
299     check_trace_feed [trace_c]
300
301     # Should no traces if the user does not exist
302     get :georss, :params => { :display_name => "UnknownUser", :format => :rss }
303     check_trace_feed []
304   end
305
306   # Test showing a trace
307   def test_show
308     public_trace_file = create(:trace, :visibility => "public")
309
310     # First with no auth, which should work since the trace is public
311     get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
312     check_trace_show public_trace_file
313
314     # Now with some other user, which should work since the trace is public
315     get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
316     check_trace_show public_trace_file
317
318     # And finally we should be able to do it with the owner of the trace
319     get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
320     check_trace_show public_trace_file
321   end
322
323   # Check an anonymous trace can't be viewed by another user
324   def test_show_anon
325     anon_trace_file = create(:trace, :visibility => "private")
326
327     # First with no auth
328     get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
329     assert_response :redirect
330     assert_redirected_to :action => :index
331
332     # Now with some other user, which should not work since the trace is anon
333     get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
334     assert_response :redirect
335     assert_redirected_to :action => :index
336
337     # And finally we should be able to do it with the owner of the trace
338     get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
339     check_trace_show anon_trace_file
340   end
341
342   # Test showing a trace that doesn't exist
343   def test_show_not_found
344     deleted_trace_file = create(:trace, :deleted)
345
346     # First with a trace that has never existed
347     get :show, :params => { :display_name => create(:user).display_name, :id => 0 }
348     assert_response :redirect
349     assert_redirected_to :action => :index
350
351     # Now with a trace that has been deleted
352     get :show, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
353     assert_response :redirect
354     assert_redirected_to :action => :index
355   end
356
357   # Test downloading a trace
358   def test_data
359     public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
360
361     # First with no auth, which should work since the trace is public
362     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
363     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
364
365     # Now with some other user, which should work since the trace is public
366     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
367     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
368
369     # And finally we should be able to do it with the owner of the trace
370     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
371     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
372   end
373
374   # Test downloading a compressed trace
375   def test_data_compressed
376     identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
377
378     # First get the data as is
379     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id }
380     check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
381
382     # Now ask explicitly for XML format
383     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" }
384     check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
385
386     # Now ask explicitly for GPX format
387     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" }
388     check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
389   end
390
391   # Check an anonymous trace can't be downloaded by another user
392   def test_data_anon
393     anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
394
395     # First with no auth
396     get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
397     assert_response :not_found
398
399     # Now with some other user, which shouldn't work since the trace is anon
400     get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
401     assert_response :not_found
402
403     # And finally we should be able to do it with the owner of the trace
404     get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
405     check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
406   end
407
408   # Test downloading a trace that doesn't exist
409   def test_data_not_found
410     deleted_trace_file = create(:trace, :deleted)
411
412     # First with a trace that has never existed
413     get :data, :params => { :display_name => create(:user).display_name, :id => 0 }
414     assert_response :not_found
415
416     # Now with a trace that has been deleted
417     get :data, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
418     assert_response :not_found
419   end
420
421   # Test downloading the picture for a trace
422   def test_picture
423     public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
424
425     # First with no auth, which should work since the trace is public
426     get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
427     check_trace_picture public_trace_file
428
429     # Now with some other user, which should work since the trace is public
430     get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
431     check_trace_picture public_trace_file
432
433     # And finally we should be able to do it with the owner of the trace
434     get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
435     check_trace_picture public_trace_file
436   end
437
438   # Check the picture for an anonymous trace can't be downloaded by another user
439   def test_picture_anon
440     anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
441
442     # First with no auth
443     get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
444     assert_response :forbidden
445
446     # Now with some other user, which shouldn't work since the trace is anon
447     get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
448     assert_response :forbidden
449
450     # And finally we should be able to do it with the owner of the trace
451     get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
452     check_trace_picture anon_trace_file
453   end
454
455   # Test downloading the picture for a trace that doesn't exist
456   def test_picture_not_found
457     deleted_trace_file = create(:trace, :deleted)
458
459     # First with a trace that has never existed
460     get :picture, :params => { :display_name => create(:user).display_name, :id => 0 }
461     assert_response :not_found
462
463     # Now with a trace that has been deleted
464     get :picture, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
465     assert_response :not_found
466   end
467
468   # Test downloading the icon for a trace
469   def test_icon
470     public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
471
472     # First with no auth, which should work since the trace is public
473     get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
474     check_trace_icon public_trace_file
475
476     # Now with some other user, which should work since the trace is public
477     get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
478     check_trace_icon public_trace_file
479
480     # And finally we should be able to do it with the owner of the trace
481     get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
482     check_trace_icon public_trace_file
483   end
484
485   # Check the icon for an anonymous trace can't be downloaded by another user
486   def test_icon_anon
487     anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
488
489     # First with no auth
490     get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
491     assert_response :forbidden
492
493     # Now with some other user, which shouldn't work since the trace is anon
494     get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
495     assert_response :forbidden
496
497     # And finally we should be able to do it with the owner of the trace
498     get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
499     check_trace_icon anon_trace_file
500   end
501
502   # Test downloading the icon for a trace that doesn't exist
503   def test_icon_not_found
504     deleted_trace_file = create(:trace, :deleted)
505
506     # First with a trace that has never existed
507     get :icon, :params => { :display_name => create(:user).display_name, :id => 0 }
508     assert_response :not_found
509
510     # Now with a trace that has been deleted
511     get :icon, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
512     assert_response :not_found
513   end
514
515   # Test fetching the new trace page
516   def test_new_get
517     # First with no auth
518     get :new
519     assert_response :redirect
520     assert_redirected_to :controller => :users, :action => :login, :referer => new_trace_path
521
522     # Now authenticated as a user with gps.trace.visibility set
523     user = create(:user)
524     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
525     get :new, :session => { :user => user }
526     assert_response :success
527     assert_template :new
528     assert_select "select#trace_visibility option[value=identifiable][selected]", 1
529
530     # Now authenticated as a user with gps.trace.public set
531     second_user = create(:user)
532     create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
533     get :new, :session => { :user => second_user }
534     assert_response :success
535     assert_template :new
536     assert_select "select#trace_visibility option[value=public][selected]", 1
537
538     # Now authenticated as a user with no preferences
539     third_user = create(:user)
540     get :new, :session => { :user => third_user }
541     assert_response :success
542     assert_template :new
543     assert_select "select#trace_visibility option[value=private][selected]", 1
544   end
545
546   # Test creating a trace
547   def test_create_post
548     # Get file to use
549     fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
550     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
551     user = create(:user)
552
553     # First with no auth
554     post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
555     assert_response :forbidden
556
557     # Rewind the file
558     file.rewind
559
560     # Now authenticated
561     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
562     assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
563     post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
564     assert_response :redirect
565     assert_redirected_to :action => :index, :display_name => user.display_name
566     assert_match(/file has been uploaded/, flash[:notice])
567     trace = Trace.order(:id => :desc).first
568     assert_equal "a.gpx", trace.name
569     assert_equal "New Trace", trace.description
570     assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
571     assert_equal "trackable", trace.visibility
572     assert_equal false, trace.inserted
573     assert_equal File.new(fixture).read, File.new(trace.trace_name).read
574     trace.destroy
575     assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
576   end
577
578   # Test creating a trace with validation errors
579   def test_create_post_with_validation_errors
580     # Get file to use
581     fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
582     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
583     user = create(:user)
584
585     # Now authenticated
586     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
587     assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
588     post :create, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
589     assert_template :new
590     assert_match "Description is too short (minimum is 1 character)", response.body
591   end
592
593   # Test fetching the edit page for a trace using GET
594   def test_edit_get
595     public_trace_file = create(:trace, :visibility => "public")
596     deleted_trace_file = create(:trace, :deleted)
597
598     # First with no auth
599     get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
600     assert_response :redirect
601     assert_redirected_to :controller => :users, :action => :login, :referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)
602
603     # Now with some other user, which should fail
604     get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
605     assert_response :forbidden
606
607     # Now with a trace which doesn't exist
608     get :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
609     assert_response :not_found
610
611     # Now with a trace which has been deleted
612     get :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
613     assert_response :not_found
614
615     # Finally with a trace that we are allowed to edit
616     get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
617     assert_response :success
618   end
619
620   # Test saving edits to a trace
621   def test_update
622     public_trace_file = create(:trace, :visibility => "public")
623     deleted_trace_file = create(:trace, :deleted)
624
625     # New details
626     new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
627
628     # First with no auth
629     put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }
630     assert_response :forbidden
631
632     # Now with some other user, which should fail
633     put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => create(:user) }
634     assert_response :forbidden
635
636     # Now with a trace which doesn't exist
637     put :update, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user), :trace => new_details }
638     assert_response :not_found
639
640     # Now with a trace which has been deleted
641     put :update, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id, :trace => new_details }, :session => { :user => deleted_trace_file.user }
642     assert_response :not_found
643
644     # Finally with a trace that we are allowed to edit
645     put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => public_trace_file.user }
646     assert_response :redirect
647     assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
648     trace = Trace.find(public_trace_file.id)
649     assert_equal new_details[:description], trace.description
650     assert_equal new_details[:tagstring], trace.tagstring
651     assert_equal new_details[:visibility], trace.visibility
652   end
653
654   # Test deleting a trace
655   def test_delete
656     public_trace_file = create(:trace, :visibility => "public")
657     deleted_trace_file = create(:trace, :deleted)
658
659     # First with no auth
660     post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
661     assert_response :forbidden
662
663     # Now with some other user, which should fail
664     post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
665     assert_response :forbidden
666
667     # Now with a trace which doesn't exist
668     post :delete, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
669     assert_response :not_found
670
671     # Now with a trace has already been deleted
672     post :delete, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
673     assert_response :not_found
674
675     # Now with a trace that we are allowed to delete
676     post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
677     assert_response :redirect
678     assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
679     trace = Trace.find(public_trace_file.id)
680     assert_equal false, trace.visible
681
682     # Finally with a trace that is deleted by an admin
683     public_trace_file = create(:trace, :visibility => "public")
684     admin = create(:administrator_user)
685
686     post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => admin }
687     assert_response :redirect
688     assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
689     trace = Trace.find(public_trace_file.id)
690     assert_equal false, trace.visible
691   end
692
693   private
694
695   def check_trace_feed(traces)
696     assert_response :success
697     assert_template "georss"
698     assert_equal "application/rss+xml", @response.content_type
699     assert_select "rss", :count => 1 do
700       assert_select "channel", :count => 1 do
701         assert_select "title"
702         assert_select "description"
703         assert_select "link"
704         assert_select "image"
705         assert_select "item", :count => traces.length do |items|
706           traces.zip(items).each do |trace, item|
707             assert_select item, "title", trace.name
708             assert_select item, "link", "http://test.host/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
709             assert_select item, "guid", "http://test.host/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
710             assert_select item, "description"
711             # assert_select item, "dc:creator", trace.user.display_name
712             assert_select item, "pubDate", trace.timestamp.rfc822
713           end
714         end
715       end
716     end
717   end
718
719   def check_trace_index(traces)
720     assert_response :success
721     assert_template "index"
722
723     if !traces.empty?
724       assert_select "table#trace_list tbody", :count => 1 do
725         assert_select "tr", :count => traces.length do |rows|
726           traces.zip(rows).each do |trace, row|
727             assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
728             assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
729             assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
730             assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
731           end
732         end
733       end
734     else
735       assert_select "h4", /Nothing here yet/
736     end
737   end
738
739   def check_trace_show(trace)
740     assert_response :success
741     assert_template "show"
742
743     assert_select "table", :count => 1 do
744       assert_select "td", /^#{Regexp.quote(trace.name)} /
745       assert_select "td", trace.user.display_name
746       assert_select "td", trace.description
747     end
748   end
749
750   def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
751     assert_response :success
752     assert_equal digest, Digest::MD5.hexdigest(response.body)
753     assert_equal content_type, response.content_type
754     assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
755   end
756
757   def check_trace_picture(trace)
758     assert_response :success
759     assert_equal "image/gif", response.content_type
760     assert_equal trace.large_picture, response.body
761   end
762
763   def check_trace_icon(trace)
764     assert_response :success
765     assert_equal "image/gif", response.content_type
766     assert_equal trace.icon_picture, response.body
767   end
768 end