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