]> git.openstreetmap.org Git - rails.git/blob - test/controllers/traces_controller_test.rb
Remove custom deny_access handlers
[rails.git] / test / controllers / traces_controller_test.rb
1 require "test_helper"
2 require "digest"
3 require "minitest/mock"
4
5 class TracesControllerTest < ActionController::TestCase
6   def setup
7     @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
8     Object.const_set("GPX_TRACE_DIR", Rails.root.join("test", "gpx", "traces"))
9
10     @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
11     Object.const_set("GPX_IMAGE_DIR", Rails.root.join("test", "gpx", "images"))
12   end
13
14   def teardown
15     File.unlink(*Dir.glob(File.join(GPX_TRACE_DIR, "*.gpx")))
16     File.unlink(*Dir.glob(File.join(GPX_IMAGE_DIR, "*.gif")))
17
18     Object.send("remove_const", "GPX_TRACE_DIR")
19     Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
20
21     Object.send("remove_const", "GPX_IMAGE_DIR")
22     Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
23   end
24
25   ##
26   # test all routes which lead to this controller
27   def test_routes
28     assert_routing(
29       { :path => "/api/0.6/gpx/create", :method => :post },
30       { :controller => "traces", :action => "api_create" }
31     )
32     assert_routing(
33       { :path => "/api/0.6/gpx/1", :method => :get },
34       { :controller => "traces", :action => "api_read", :id => "1" }
35     )
36     assert_routing(
37       { :path => "/api/0.6/gpx/1", :method => :put },
38       { :controller => "traces", :action => "api_update", :id => "1" }
39     )
40     assert_routing(
41       { :path => "/api/0.6/gpx/1", :method => :delete },
42       { :controller => "traces", :action => "api_delete", :id => "1" }
43     )
44     assert_recognizes(
45       { :controller => "traces", :action => "api_read", :id => "1" },
46       { :path => "/api/0.6/gpx/1/details", :method => :get }
47     )
48     assert_routing(
49       { :path => "/api/0.6/gpx/1/data", :method => :get },
50       { :controller => "traces", :action => "api_data", :id => "1" }
51     )
52     assert_routing(
53       { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
54       { :controller => "traces", :action => "api_data", :id => "1", :format => "xml" }
55     )
56
57     assert_routing(
58       { :path => "/traces", :method => :get },
59       { :controller => "traces", :action => "index" }
60     )
61     assert_routing(
62       { :path => "/traces/page/1", :method => :get },
63       { :controller => "traces", :action => "index", :page => "1" }
64     )
65     assert_routing(
66       { :path => "/traces/tag/tagname", :method => :get },
67       { :controller => "traces", :action => "index", :tag => "tagname" }
68     )
69     assert_routing(
70       { :path => "/traces/tag/tagname/page/1", :method => :get },
71       { :controller => "traces", :action => "index", :tag => "tagname", :page => "1" }
72     )
73     assert_routing(
74       { :path => "/user/username/traces", :method => :get },
75       { :controller => "traces", :action => "index", :display_name => "username" }
76     )
77     assert_routing(
78       { :path => "/user/username/traces/page/1", :method => :get },
79       { :controller => "traces", :action => "index", :display_name => "username", :page => "1" }
80     )
81     assert_routing(
82       { :path => "/user/username/traces/tag/tagname", :method => :get },
83       { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname" }
84     )
85     assert_routing(
86       { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
87       { :controller => "traces", :action => "index", :display_name => "username", :tag => "tagname", :page => "1" }
88     )
89
90     assert_routing(
91       { :path => "/traces/mine", :method => :get },
92       { :controller => "traces", :action => "mine" }
93     )
94     assert_routing(
95       { :path => "/traces/mine/page/1", :method => :get },
96       { :controller => "traces", :action => "mine", :page => "1" }
97     )
98     assert_routing(
99       { :path => "/traces/mine/tag/tagname", :method => :get },
100       { :controller => "traces", :action => "mine", :tag => "tagname" }
101     )
102     assert_routing(
103       { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
104       { :controller => "traces", :action => "mine", :tag => "tagname", :page => "1" }
105     )
106
107     assert_routing(
108       { :path => "/traces/rss", :method => :get },
109       { :controller => "traces", :action => "georss", :format => :rss }
110     )
111     assert_routing(
112       { :path => "/traces/tag/tagname/rss", :method => :get },
113       { :controller => "traces", :action => "georss", :tag => "tagname", :format => :rss }
114     )
115     assert_routing(
116       { :path => "/user/username/traces/rss", :method => :get },
117       { :controller => "traces", :action => "georss", :display_name => "username", :format => :rss }
118     )
119     assert_routing(
120       { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
121       { :controller => "traces", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
122     )
123
124     assert_routing(
125       { :path => "/user/username/traces/1", :method => :get },
126       { :controller => "traces", :action => "show", :display_name => "username", :id => "1" }
127     )
128     assert_routing(
129       { :path => "/user/username/traces/1/picture", :method => :get },
130       { :controller => "traces", :action => "picture", :display_name => "username", :id => "1" }
131     )
132     assert_routing(
133       { :path => "/user/username/traces/1/icon", :method => :get },
134       { :controller => "traces", :action => "icon", :display_name => "username", :id => "1" }
135     )
136
137     assert_routing(
138       { :path => "/traces/new", :method => :get },
139       { :controller => "traces", :action => "new" }
140     )
141     assert_routing(
142       { :path => "/traces", :method => :post },
143       { :controller => "traces", :action => "create" }
144     )
145     assert_routing(
146       { :path => "/trace/1/data", :method => :get },
147       { :controller => "traces", :action => "data", :id => "1" }
148     )
149     assert_routing(
150       { :path => "/trace/1/data.xml", :method => :get },
151       { :controller => "traces", :action => "data", :id => "1", :format => "xml" }
152     )
153     assert_routing(
154       { :path => "/traces/1/edit", :method => :get },
155       { :controller => "traces", :action => "edit", :id => "1" }
156     )
157     assert_routing(
158       { :path => "/traces/1", :method => :put },
159       { :controller => "traces", :action => "update", :id => "1" }
160     )
161     assert_routing(
162       { :path => "/trace/1/delete", :method => :post },
163       { :controller => "traces", :action => "delete", :id => "1" }
164     )
165   end
166
167   # Check that the index of traces is displayed
168   def test_index
169     user = create(:user)
170     # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
171     trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
172       create(:tracetag, :trace => trace, :tag => "London")
173     end
174     trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
175       create(:tracetag, :trace => trace, :tag => "Birmingham")
176     end
177     trace_c = create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
178       create(:tracetag, :trace => trace, :tag => "London")
179     end
180     trace_d = create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
181       create(:tracetag, :trace => trace, :tag => "Birmingham")
182     end
183
184     # First with the public index
185     get :index
186     check_trace_index [trace_b, trace_a]
187
188     # Restrict traces to those with a given tag
189     get :index, :params => { :tag => "London" }
190     check_trace_index [trace_a]
191
192     # Should see more when we are logged in
193     get :index, :session => { :user => user }
194     check_trace_index [trace_d, trace_c, trace_b, trace_a]
195
196     # Again, we should see more when we are logged in
197     get :index, :params => { :tag => "London" }, :session => { :user => user }
198     check_trace_index [trace_c, trace_a]
199   end
200
201   # Check that I can get mine
202   def test_index_mine
203     user = create(:user)
204     create(:trace, :visibility => "public") do |trace|
205       create(:tracetag, :trace => trace, :tag => "Birmingham")
206     end
207     trace_b = create(:trace, :visibility => "private", :user => user) do |trace|
208       create(:tracetag, :trace => trace, :tag => "London")
209     end
210
211     # First try to get it when not logged in
212     get :mine
213     assert_redirected_to :controller => "users", :action => "login", :referer => "/traces/mine"
214
215     # Now try when logged in
216     get :mine, :session => { :user => user }
217     assert_redirected_to :action => "index", :display_name => user.display_name
218
219     # Fetch the actual index
220     get :index, :params => { :display_name => user.display_name }, :session => { :user => user }
221     check_trace_index [trace_b]
222   end
223
224   # Check the index of traces for a specific user
225   def test_index_user
226     user = create(:user)
227     second_user = create(:user)
228     third_user = create(:user)
229     create(:trace)
230     trace_b = create(:trace, :visibility => "public", :user => user)
231     trace_c = create(:trace, :visibility => "private", :user => user) do |trace|
232       create(:tracetag, :trace => trace, :tag => "London")
233     end
234
235     # Test a user with no traces
236     get :index, :params => { :display_name => second_user.display_name }
237     check_trace_index []
238
239     # Test the user with the traces - should see only public ones
240     get :index, :params => { :display_name => user.display_name }
241     check_trace_index [trace_b]
242
243     # Should still see only public ones when authenticated as another user
244     get :index, :params => { :display_name => user.display_name }, :session => { :user => third_user }
245     check_trace_index [trace_b]
246
247     # Should see all traces when authenticated as the target user
248     get :index, :params => { :display_name => user.display_name }, :session => { :user => user }
249     check_trace_index [trace_c, trace_b]
250
251     # Should only see traces with the correct tag when a tag is specified
252     get :index, :params => { :display_name => user.display_name, :tag => "London" }, :session => { :user => user }
253     check_trace_index [trace_c]
254
255     # Should get an error if the user does not exist
256     get :index, :params => { :display_name => "UnknownUser" }
257     assert_response :not_found
258     assert_template "users/no_such_user"
259   end
260
261   # Check a multi-page index
262   def test_index_paged
263     # Create several pages worth of traces
264     create_list(:trace, 50)
265
266     # Try and get the index
267     get :index
268     assert_response :success
269     assert_select "table#trace_list tbody", :count => 1 do
270       assert_select "tr", :count => 20
271     end
272
273     # Try and get the second page
274     get :index, :params => { :page => 2 }
275     assert_response :success
276     assert_select "table#trace_list tbody", :count => 1 do
277       assert_select "tr", :count => 20
278     end
279   end
280
281   # Check the RSS feed
282   def test_rss
283     user = create(:user)
284     # The fourth test below is surpisingly sensitive to timestamp ordering when the timestamps are equal.
285     trace_a = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago) do |trace|
286       create(:tracetag, :trace => trace, :tag => "London")
287     end
288     trace_b = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago) do |trace|
289       create(:tracetag, :trace => trace, :tag => "Birmingham")
290     end
291     create(:trace, :visibility => "private", :user => user, :timestamp => 2.seconds.ago) do |trace|
292       create(:tracetag, :trace => trace, :tag => "London")
293     end
294     create(:trace, :visibility => "private", :user => user, :timestamp => 1.second.ago) do |trace|
295       create(:tracetag, :trace => trace, :tag => "Birmingham")
296     end
297
298     # First with the public feed
299     get :georss, :params => { :format => :rss }
300     check_trace_feed [trace_b, trace_a]
301
302     # Restrict traces to those with a given tag
303     get :georss, :params => { :tag => "London", :format => :rss }
304     check_trace_feed [trace_a]
305   end
306
307   # Check the RSS feed for a specific user
308   def test_rss_user
309     user = create(:user)
310     second_user = create(:user)
311     create(:user)
312     create(:trace)
313     trace_b = create(:trace, :visibility => "public", :timestamp => 4.seconds.ago, :user => user)
314     trace_c = create(:trace, :visibility => "public", :timestamp => 3.seconds.ago, :user => user) do |trace|
315       create(:tracetag, :trace => trace, :tag => "London")
316     end
317     create(:trace, :visibility => "private")
318
319     # Test a user with no traces
320     get :georss, :params => { :display_name => second_user.display_name, :format => :rss }
321     check_trace_feed []
322
323     # Test the user with the traces - should see only public ones
324     get :georss, :params => { :display_name => user.display_name, :format => :rss }
325     check_trace_feed [trace_c, trace_b]
326
327     # Should only see traces with the correct tag when a tag is specified
328     get :georss, :params => { :display_name => user.display_name, :tag => "London", :format => :rss }
329     check_trace_feed [trace_c]
330
331     # Should no traces if the user does not exist
332     get :georss, :params => { :display_name => "UnknownUser", :format => :rss }
333     check_trace_feed []
334   end
335
336   # Test showing a trace
337   def test_show
338     public_trace_file = create(:trace, :visibility => "public")
339
340     # First with no auth, which should work since the trace is public
341     get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
342     check_trace_show public_trace_file
343
344     # Now with some other user, which should work since the trace is public
345     get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
346     check_trace_show public_trace_file
347
348     # And finally we should be able to do it with the owner of the trace
349     get :show, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
350     check_trace_show public_trace_file
351   end
352
353   # Check an anonymous trace can't be viewed by another user
354   def test_show_anon
355     anon_trace_file = create(:trace, :visibility => "private")
356
357     # First with no auth
358     get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
359     assert_response :redirect
360     assert_redirected_to :action => :index
361
362     # Now with some other user, which should not work since the trace is anon
363     get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
364     assert_response :redirect
365     assert_redirected_to :action => :index
366
367     # And finally we should be able to do it with the owner of the trace
368     get :show, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
369     check_trace_show anon_trace_file
370   end
371
372   # Test showing a trace that doesn't exist
373   def test_show_not_found
374     deleted_trace_file = create(:trace, :deleted)
375
376     # First with a trace that has never existed
377     get :show, :params => { :display_name => create(:user).display_name, :id => 0 }
378     assert_response :redirect
379     assert_redirected_to :action => :index
380
381     # Now with a trace that has been deleted
382     get :show, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
383     assert_response :redirect
384     assert_redirected_to :action => :index
385   end
386
387   # Test downloading a trace
388   def test_data
389     public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
390
391     # First with no auth, which should work since the trace is public
392     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
393     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
394
395     # Now with some other user, which should work since the trace is public
396     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
397     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
398
399     # And finally we should be able to do it with the owner of the trace
400     get :data, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
401     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
402   end
403
404   # Test downloading a compressed trace
405   def test_data_compressed
406     identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
407
408     # First get the data as is
409     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id }
410     check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
411
412     # Now ask explicitly for XML format
413     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "xml" }
414     check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
415
416     # Now ask explicitly for GPX format
417     get :data, :params => { :display_name => identifiable_trace_file.user.display_name, :id => identifiable_trace_file.id, :format => "gpx" }
418     check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
419   end
420
421   # Check an anonymous trace can't be downloaded by another user
422   def test_data_anon
423     anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
424
425     # First with no auth
426     get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
427     assert_response :not_found
428
429     # Now with some other user, which shouldn't work since the trace is anon
430     get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
431     assert_response :not_found
432
433     # And finally we should be able to do it with the owner of the trace
434     get :data, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
435     check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
436   end
437
438   # Test downloading a trace that doesn't exist
439   def test_data_not_found
440     deleted_trace_file = create(:trace, :deleted)
441
442     # First with a trace that has never existed
443     get :data, :params => { :display_name => create(:user).display_name, :id => 0 }
444     assert_response :not_found
445
446     # Now with a trace that has been deleted
447     get :data, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
448     assert_response :not_found
449   end
450
451   # Test downloading the picture for a trace
452   def test_picture
453     public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
454
455     # First with no auth, which should work since the trace is public
456     get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
457     check_trace_picture public_trace_file
458
459     # Now with some other user, which should work since the trace is public
460     get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
461     check_trace_picture public_trace_file
462
463     # And finally we should be able to do it with the owner of the trace
464     get :picture, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
465     check_trace_picture public_trace_file
466   end
467
468   # Check the picture for an anonymous trace can't be downloaded by another user
469   def test_picture_anon
470     anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
471
472     # First with no auth
473     get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
474     assert_response :forbidden
475
476     # Now with some other user, which shouldn't work since the trace is anon
477     get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
478     assert_response :forbidden
479
480     # And finally we should be able to do it with the owner of the trace
481     get :picture, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
482     check_trace_picture anon_trace_file
483   end
484
485   # Test downloading the picture for a trace that doesn't exist
486   def test_picture_not_found
487     deleted_trace_file = create(:trace, :deleted)
488
489     # First with a trace that has never existed
490     get :picture, :params => { :display_name => create(:user).display_name, :id => 0 }
491     assert_response :not_found
492
493     # Now with a trace that has been deleted
494     get :picture, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
495     assert_response :not_found
496   end
497
498   # Test downloading the icon for a trace
499   def test_icon
500     public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
501
502     # First with no auth, which should work since the trace is public
503     get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
504     check_trace_icon public_trace_file
505
506     # Now with some other user, which should work since the trace is public
507     get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
508     check_trace_icon public_trace_file
509
510     # And finally we should be able to do it with the owner of the trace
511     get :icon, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
512     check_trace_icon public_trace_file
513   end
514
515   # Check the icon for an anonymous trace can't be downloaded by another user
516   def test_icon_anon
517     anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
518
519     # First with no auth
520     get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }
521     assert_response :forbidden
522
523     # Now with some other user, which shouldn't work since the trace is anon
524     get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => create(:user) }
525     assert_response :forbidden
526
527     # And finally we should be able to do it with the owner of the trace
528     get :icon, :params => { :display_name => anon_trace_file.user.display_name, :id => anon_trace_file.id }, :session => { :user => anon_trace_file.user }
529     check_trace_icon anon_trace_file
530   end
531
532   # Test downloading the icon for a trace that doesn't exist
533   def test_icon_not_found
534     deleted_trace_file = create(:trace, :deleted)
535
536     # First with a trace that has never existed
537     get :icon, :params => { :display_name => create(:user).display_name, :id => 0 }
538     assert_response :not_found
539
540     # Now with a trace that has been deleted
541     get :icon, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
542     assert_response :not_found
543   end
544
545   # Test fetching the new trace page
546   def test_new_get
547     # First with no auth
548     get :new
549     assert_response :redirect
550     assert_redirected_to :controller => :users, :action => :login, :referer => new_trace_path
551
552     # Now authenticated as a user with gps.trace.visibility set
553     user = create(:user)
554     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
555     get :new, :session => { :user => user }
556     assert_response :success
557     assert_template :new
558     assert_select "select#trace_visibility option[value=identifiable][selected]", 1
559
560     # Now authenticated as a user with gps.trace.public set
561     second_user = create(:user)
562     create(:user_preference, :user => second_user, :k => "gps.trace.public", :v => "default")
563     get :new, :session => { :user => second_user }
564     assert_response :success
565     assert_template :new
566     assert_select "select#trace_visibility option[value=public][selected]", 1
567
568     # Now authenticated as a user with no preferences
569     third_user = create(:user)
570     get :new, :session => { :user => third_user }
571     assert_response :success
572     assert_template :new
573     assert_select "select#trace_visibility option[value=private][selected]", 1
574   end
575
576   # Test creating a trace
577   def test_create_post
578     # Get file to use
579     fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
580     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
581     user = create(:user)
582
583     # First with no auth
584     post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }
585     assert_response :forbidden
586
587     # Rewind the file
588     file.rewind
589
590     # Now authenticated
591     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
592     assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
593     post :create, :params => { :trace => { :gpx_file => file, :description => "New Trace", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
594     assert_response :redirect
595     assert_redirected_to :action => :index, :display_name => user.display_name
596     assert_match(/file has been uploaded/, flash[:notice])
597     trace = Trace.order(:id => :desc).first
598     assert_equal "a.gpx", trace.name
599     assert_equal "New Trace", trace.description
600     assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
601     assert_equal "trackable", trace.visibility
602     assert_equal false, trace.inserted
603     assert_equal File.new(fixture).read, File.new(trace.trace_name).read
604     trace.destroy
605     assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
606   end
607
608   # Test creating a trace with validation errors
609   def test_create_post_with_validation_errors
610     # Get file to use
611     fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
612     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
613     user = create(:user)
614
615     # Now authenticated
616     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
617     assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
618     post :create, :params => { :trace => { :gpx_file => file, :description => "", :tagstring => "new,trace", :visibility => "trackable" } }, :session => { :user => user }
619     assert_template :new
620     assert_match "Description is too short (minimum is 1 character)", response.body
621   end
622
623   # Test fetching the edit page for a trace using GET
624   def test_edit_get
625     public_trace_file = create(:trace, :visibility => "public")
626     deleted_trace_file = create(:trace, :deleted)
627
628     # First with no auth
629     get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
630     assert_response :redirect
631     assert_redirected_to :controller => :users, :action => :login, :referer => edit_trace_path(:display_name => public_trace_file.user.display_name, :id => public_trace_file.id)
632
633     # Now with some other user, which should fail
634     get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
635     assert_response :forbidden
636
637     # Now with a trace which doesn't exist
638     get :edit, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
639     assert_response :not_found
640
641     # Now with a trace which has been deleted
642     get :edit, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
643     assert_response :not_found
644
645     # Finally with a trace that we are allowed to edit
646     get :edit, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
647     assert_response :success
648   end
649
650   # Test saving edits to a trace
651   def test_update
652     public_trace_file = create(:trace, :visibility => "public")
653     deleted_trace_file = create(:trace, :deleted)
654
655     # New details
656     new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
657
658     # First with no auth
659     put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }
660     assert_response :forbidden
661
662     # Now with some other user, which should fail
663     put :update, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id, :trace => new_details }, :session => { :user => create(:user) }
664     assert_response :forbidden
665
666     # Now with a trace which doesn't exist
667     put :update, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user), :trace => new_details }
668     assert_response :not_found
669
670     # Now with a trace which has been deleted
671     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 }
672     assert_response :not_found
673
674     # Finally with a trace that we are allowed to edit
675     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 }
676     assert_response :redirect
677     assert_redirected_to :action => :show, :display_name => public_trace_file.user.display_name
678     trace = Trace.find(public_trace_file.id)
679     assert_equal new_details[:description], trace.description
680     assert_equal new_details[:tagstring], trace.tagstring
681     assert_equal new_details[:visibility], trace.visibility
682   end
683
684   # Test deleting a trace
685   def test_delete
686     public_trace_file = create(:trace, :visibility => "public")
687     deleted_trace_file = create(:trace, :deleted)
688
689     # First with no auth
690     post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }
691     assert_response :forbidden
692
693     # Now with some other user, which should fail
694     post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => create(:user) }
695     assert_response :forbidden
696
697     # Now with a trace which doesn't exist
698     post :delete, :params => { :display_name => create(:user).display_name, :id => 0 }, :session => { :user => create(:user) }
699     assert_response :not_found
700
701     # Now with a trace has already been deleted
702     post :delete, :params => { :display_name => deleted_trace_file.user.display_name, :id => deleted_trace_file.id }, :session => { :user => deleted_trace_file.user }
703     assert_response :not_found
704
705     # Now with a trace that we are allowed to delete
706     post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => public_trace_file.user }
707     assert_response :redirect
708     assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
709     trace = Trace.find(public_trace_file.id)
710     assert_equal false, trace.visible
711
712     # Finally with a trace that is deleted by an admin
713     public_trace_file = create(:trace, :visibility => "public")
714     admin = create(:administrator_user)
715
716     post :delete, :params => { :display_name => public_trace_file.user.display_name, :id => public_trace_file.id }, :session => { :user => admin }
717     assert_response :redirect
718     assert_redirected_to :action => :index, :display_name => public_trace_file.user.display_name
719     trace = Trace.find(public_trace_file.id)
720     assert_equal false, trace.visible
721   end
722
723   # Check getting a specific trace through the api
724   def test_api_read
725     public_trace_file = create(:trace, :visibility => "public")
726
727     # First with no auth
728     get :api_read, :params => { :id => public_trace_file.id }
729     assert_response :unauthorized
730
731     # Now with some other user, which should work since the trace is public
732     basic_authorization create(:user).display_name, "test"
733     get :api_read, :params => { :id => public_trace_file.id }
734     assert_response :success
735
736     # And finally we should be able to do it with the owner of the trace
737     basic_authorization public_trace_file.user.display_name, "test"
738     get :api_read, :params => { :id => public_trace_file.id }
739     assert_response :success
740   end
741
742   # Check an anoymous trace can't be specifically fetched by another user
743   def test_api_read_anon
744     anon_trace_file = create(:trace, :visibility => "private")
745
746     # First with no auth
747     get :api_read, :params => { :id => anon_trace_file.id }
748     assert_response :unauthorized
749
750     # Now try with another user, which shouldn't work since the trace is anon
751     basic_authorization create(:user).display_name, "test"
752     get :api_read, :params => { :id => anon_trace_file.id }
753     assert_response :forbidden
754
755     # And finally we should be able to get the trace details with the trace owner
756     basic_authorization anon_trace_file.user.display_name, "test"
757     get :api_read, :params => { :id => anon_trace_file.id }
758     assert_response :success
759   end
760
761   # Check the api details for a trace that doesn't exist
762   def test_api_read_not_found
763     deleted_trace_file = create(:trace, :deleted)
764
765     # Try first with no auth, as it should require it
766     get :api_read, :params => { :id => 0 }
767     assert_response :unauthorized
768
769     # Login, and try again
770     basic_authorization deleted_trace_file.user.display_name, "test"
771     get :api_read, :params => { :id => 0 }
772     assert_response :not_found
773
774     # Now try a trace which did exist but has been deleted
775     basic_authorization deleted_trace_file.user.display_name, "test"
776     get :api_read, :params => { :id => deleted_trace_file.id }
777     assert_response :not_found
778   end
779
780   # Test downloading a trace through the api
781   def test_api_data
782     public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
783
784     # First with no auth
785     get :api_data, :params => { :id => public_trace_file.id }
786     assert_response :unauthorized
787
788     # Now with some other user, which should work since the trace is public
789     basic_authorization create(:user).display_name, "test"
790     get :api_data, :params => { :id => public_trace_file.id }
791     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
792
793     # And finally we should be able to do it with the owner of the trace
794     basic_authorization public_trace_file.user.display_name, "test"
795     get :api_data, :params => { :id => public_trace_file.id }
796     check_trace_data public_trace_file, "848caa72f2f456d1bd6a0fdf228aa1b9"
797   end
798
799   # Test downloading a compressed trace through the api
800   def test_api_data_compressed
801     identifiable_trace_file = create(:trace, :visibility => "identifiable", :fixture => "d")
802
803     # Authenticate as the owner of the trace we will be using
804     basic_authorization identifiable_trace_file.user.display_name, "test"
805
806     # First get the data as is
807     get :api_data, :params => { :id => identifiable_trace_file.id }
808     check_trace_data identifiable_trace_file, "c6422a3d8750faae49ed70e7e8a51b93", "application/x-gzip", "gpx.gz"
809
810     # Now ask explicitly for XML format
811     get :api_data, :params => { :id => identifiable_trace_file.id, :format => "xml" }
812     check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d", "application/xml", "xml"
813
814     # Now ask explicitly for GPX format
815     get :api_data, :params => { :id => identifiable_trace_file.id, :format => "gpx" }
816     check_trace_data identifiable_trace_file, "abd6675fdf3024a84fc0a1deac147c0d"
817   end
818
819   # Check an anonymous trace can't be downloaded by another user through the api
820   def test_api_data_anon
821     anon_trace_file = create(:trace, :visibility => "private", :fixture => "b")
822
823     # First with no auth
824     get :api_data, :params => { :id => anon_trace_file.id }
825     assert_response :unauthorized
826
827     # Now with some other user, which shouldn't work since the trace is anon
828     basic_authorization create(:user).display_name, "test"
829     get :api_data, :params => { :id => anon_trace_file.id }
830     assert_response :forbidden
831
832     # And finally we should be able to do it with the owner of the trace
833     basic_authorization anon_trace_file.user.display_name, "test"
834     get :api_data, :params => { :id => anon_trace_file.id }
835     check_trace_data anon_trace_file, "66179ca44f1e93d8df62e2b88cbea732"
836   end
837
838   # Test downloading a trace that doesn't exist through the api
839   def test_api_data_not_found
840     deleted_trace_file = create(:trace, :deleted)
841
842     # Try first with no auth, as it should require it
843     get :api_data, :params => { :id => 0 }
844     assert_response :unauthorized
845
846     # Login, and try again
847     basic_authorization create(:user).display_name, "test"
848     get :api_data, :params => { :id => 0 }
849     assert_response :not_found
850
851     # Now try a trace which did exist but has been deleted
852     basic_authorization deleted_trace_file.user.display_name, "test"
853     get :api_data, :params => { :id => deleted_trace_file.id }
854     assert_response :not_found
855   end
856
857   # Test creating a trace through the api
858   def test_api_create
859     # Get file to use
860     fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
861     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
862     user = create(:user)
863
864     # First with no auth
865     post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
866     assert_response :unauthorized
867
868     # Rewind the file
869     file.rewind
870
871     # Now authenticated
872     create(:user_preference, :user => user, :k => "gps.trace.visibility", :v => "identifiable")
873     assert_not_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
874     basic_authorization user.display_name, "test"
875     post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable" }
876     assert_response :success
877     trace = Trace.find(response.body.to_i)
878     assert_equal "a.gpx", trace.name
879     assert_equal "New Trace", trace.description
880     assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
881     assert_equal "trackable", trace.visibility
882     assert_equal false, trace.inserted
883     assert_equal File.new(fixture).read, File.new(trace.trace_name).read
884     trace.destroy
885     assert_equal "trackable", user.preferences.where(:k => "gps.trace.visibility").first.v
886
887     # Rewind the file
888     file.rewind
889
890     # Now authenticated, with the legacy public flag
891     assert_not_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
892     basic_authorization user.display_name, "test"
893     post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 1 }
894     assert_response :success
895     trace = Trace.find(response.body.to_i)
896     assert_equal "a.gpx", trace.name
897     assert_equal "New Trace", trace.description
898     assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
899     assert_equal "public", trace.visibility
900     assert_equal false, trace.inserted
901     assert_equal File.new(fixture).read, File.new(trace.trace_name).read
902     trace.destroy
903     assert_equal "public", user.preferences.where(:k => "gps.trace.visibility").first.v
904
905     # Rewind the file
906     file.rewind
907
908     # Now authenticated, with the legacy private flag
909     second_user = create(:user)
910     assert_nil second_user.preferences.where(:k => "gps.trace.visibility").first
911     basic_authorization second_user.display_name, "test"
912     post :api_create, :params => { :file => file, :description => "New Trace", :tags => "new,trace", :public => 0 }
913     assert_response :success
914     trace = Trace.find(response.body.to_i)
915     assert_equal "a.gpx", trace.name
916     assert_equal "New Trace", trace.description
917     assert_equal %w[new trace], trace.tags.order(:tag).collect(&:tag)
918     assert_equal "private", trace.visibility
919     assert_equal false, trace.inserted
920     assert_equal File.new(fixture).read, File.new(trace.trace_name).read
921     trace.destroy
922     assert_equal "private", second_user.preferences.where(:k => "gps.trace.visibility").first.v
923   end
924
925   # Check updating a trace through the api
926   def test_api_update
927     public_trace_file = create(:trace, :visibility => "public", :fixture => "a")
928     deleted_trace_file = create(:trace, :deleted)
929     anon_trace_file = create(:trace, :visibility => "private")
930
931     # First with no auth
932     content public_trace_file.to_xml
933     put :api_update, :params => { :id => public_trace_file.id }
934     assert_response :unauthorized
935
936     # Now with some other user, which should fail
937     basic_authorization create(:user).display_name, "test"
938     content public_trace_file.to_xml
939     put :api_update, :params => { :id => public_trace_file.id }
940     assert_response :forbidden
941
942     # Now with a trace which doesn't exist
943     basic_authorization create(:user).display_name, "test"
944     content public_trace_file.to_xml
945     put :api_update, :params => { :id => 0 }
946     assert_response :not_found
947
948     # Now with a trace which did exist but has been deleted
949     basic_authorization deleted_trace_file.user.display_name, "test"
950     content deleted_trace_file.to_xml
951     put :api_update, :params => { :id => deleted_trace_file.id }
952     assert_response :not_found
953
954     # Now try an update with the wrong ID
955     basic_authorization public_trace_file.user.display_name, "test"
956     content anon_trace_file.to_xml
957     put :api_update, :params => { :id => public_trace_file.id }
958     assert_response :bad_request,
959                     "should not be able to update a trace with a different ID from the XML"
960
961     # And finally try an update that should work
962     basic_authorization public_trace_file.user.display_name, "test"
963     t = public_trace_file
964     t.description = "Changed description"
965     t.visibility = "private"
966     content t.to_xml
967     put :api_update, :params => { :id => t.id }
968     assert_response :success
969     nt = Trace.find(t.id)
970     assert_equal nt.description, t.description
971     assert_equal nt.visibility, t.visibility
972   end
973
974   # Test that updating a trace doesn't duplicate the tags
975   def test_api_update_tags
976     tracetag = create(:tracetag)
977     trace = tracetag.trace
978     basic_authorization trace.user.display_name, "test"
979
980     content trace.to_xml
981     put :api_update, :params => { :id => trace.id }
982     assert_response :success
983
984     updated = Trace.find(trace.id)
985     # Ensure there's only one tag in the database after updating
986     assert_equal Tracetag.count, 1
987     # The new tag object might have a different id, so check the string representation
988     assert_equal trace.tagstring, updated.tagstring
989   end
990
991   # Check deleting a trace through the api
992   def test_api_delete
993     public_trace_file = create(:trace, :visibility => "public")
994
995     # First with no auth
996     delete :api_delete, :params => { :id => public_trace_file.id }
997     assert_response :unauthorized
998
999     # Now with some other user, which should fail
1000     basic_authorization create(:user).display_name, "test"
1001     delete :api_delete, :params => { :id => public_trace_file.id }
1002     assert_response :forbidden
1003
1004     # Now with a trace which doesn't exist
1005     basic_authorization create(:user).display_name, "test"
1006     delete :api_delete, :params => { :id => 0 }
1007     assert_response :not_found
1008
1009     # And finally we should be able to do it with the owner of the trace
1010     basic_authorization public_trace_file.user.display_name, "test"
1011     delete :api_delete, :params => { :id => public_trace_file.id }
1012     assert_response :success
1013
1014     # Try it a second time, which should fail
1015     basic_authorization public_trace_file.user.display_name, "test"
1016     delete :api_delete, :params => { :id => public_trace_file.id }
1017     assert_response :not_found
1018   end
1019
1020   private
1021
1022   def check_trace_feed(traces)
1023     assert_response :success
1024     assert_template "georss"
1025     assert_equal "application/rss+xml", @response.content_type
1026     assert_select "rss", :count => 1 do
1027       assert_select "channel", :count => 1 do
1028         assert_select "title"
1029         assert_select "description"
1030         assert_select "link"
1031         assert_select "image"
1032         assert_select "item", :count => traces.length do |items|
1033           traces.zip(items).each do |trace, item|
1034             assert_select item, "title", trace.name
1035             assert_select item, "link", "http://test.host/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
1036             assert_select item, "guid", "http://test.host/user/#{ERB::Util.u(trace.user.display_name)}/traces/#{trace.id}"
1037             assert_select item, "description"
1038             # assert_select item, "dc:creator", trace.user.display_name
1039             assert_select item, "pubDate", trace.timestamp.rfc822
1040           end
1041         end
1042       end
1043     end
1044   end
1045
1046   def check_trace_index(traces)
1047     assert_response :success
1048     assert_template "index"
1049
1050     if !traces.empty?
1051       assert_select "table#trace_list tbody", :count => 1 do
1052         assert_select "tr", :count => traces.length do |rows|
1053           traces.zip(rows).each do |trace, row|
1054             assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
1055             assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
1056             assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
1057             assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
1058           end
1059         end
1060       end
1061     else
1062       assert_select "h4", /Nothing here yet/
1063     end
1064   end
1065
1066   def check_trace_show(trace)
1067     assert_response :success
1068     assert_template "show"
1069
1070     assert_select "table", :count => 1 do
1071       assert_select "td", /^#{Regexp.quote(trace.name)} /
1072       assert_select "td", trace.user.display_name
1073       assert_select "td", trace.description
1074     end
1075   end
1076
1077   def check_trace_data(trace, digest, content_type = "application/gpx+xml", extension = "gpx")
1078     assert_response :success
1079     assert_equal digest, Digest::MD5.hexdigest(response.body)
1080     assert_equal content_type, response.content_type
1081     assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
1082   end
1083
1084   def check_trace_picture(trace)
1085     assert_response :success
1086     assert_equal "image/gif", response.content_type
1087     assert_equal trace.large_picture, response.body
1088   end
1089
1090   def check_trace_icon(trace)
1091     assert_response :success
1092     assert_equal "image/gif", response.content_type
1093     assert_equal trace.icon_picture, response.body
1094   end
1095 end