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