]> git.openstreetmap.org Git - rails.git/blob - test/controllers/trace_controller_test.rb
Removed italic styling on message body
[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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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).id }
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
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).id }
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).id }
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).id }
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).id }
584     assert_response :success
585   end
586
587   # Test saving edits to a trace
588   def test_edit_post
589     public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
590     deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
591     # New details
592     new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
593
594     # First with no auth
595     post :edit, :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details
596     assert_response :forbidden
597
598     # Now with some other user, which should fail
599     post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:public_user).id }
600     assert_response :forbidden
601
602     # Now with a trace which doesn't exist
603     post :edit, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id, :trace => new_details }
604     assert_response :not_found
605
606     # Now with a trace which has been deleted
607     post :edit, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id, :trace => new_details }, { :user => users(:public_user).id }
608     assert_response :not_found
609
610     # Finally with a trace that we are allowed to edit
611     post :edit, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id, :trace => new_details }, { :user => users(:normal_user).id }
612     assert_response :redirect
613     assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
614     trace = Trace.find(public_trace_file.id)
615     assert_equal new_details[:description], trace.description
616     assert_equal new_details[:tagstring], trace.tagstring
617     assert_equal new_details[:visibility], trace.visibility
618   end
619
620   # Test deleting a trace
621   def test_delete
622     public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
623     deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
624
625     # First with no auth
626     post :delete, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
627     assert_response :forbidden
628
629     # Now with some other user, which should fail
630     post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:public_user).id }
631     assert_response :forbidden
632
633     # Now with a trace which doesn't exist
634     post :delete, { :display_name => users(:public_user).display_name, :id => 0 }, { :user => users(:public_user).id }
635     assert_response :not_found
636
637     # Now with a trace has already been deleted
638     post :delete, { :display_name => users(:public_user).display_name, :id => deleted_trace_file.id }, { :user => users(:public_user).id }
639     assert_response :not_found
640
641     # Finally with a trace that we are allowed to delete
642     post :delete, { :display_name => users(:normal_user).display_name, :id => public_trace_file.id }, { :user => users(:normal_user).id }
643     assert_response :redirect
644     assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
645     trace = Trace.find(public_trace_file.id)
646     assert_equal false, trace.visible
647   end
648
649   # Check getting a specific trace through the api
650   def test_api_read
651     public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
652
653     # First with no auth
654     get :api_read, :id => public_trace_file.id
655     assert_response :unauthorized
656
657     # Now with some other user, which should work since the trace is public
658     basic_authorization(users(:public_user).display_name, "test")
659     get :api_read, :id => public_trace_file.id
660     assert_response :success
661
662     # And finally we should be able to do it with the owner of the trace
663     basic_authorization(users(:normal_user).display_name, "test")
664     get :api_read, :id => public_trace_file.id
665     assert_response :success
666   end
667
668   # Check an anoymous trace can't be specifically fetched by another user
669   def test_api_read_anon
670     anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
671
672     # First with no auth
673     get :api_read, :id => anon_trace_file.id
674     assert_response :unauthorized
675
676     # Now try with another user, which shouldn't work since the trace is anon
677     basic_authorization(users(:normal_user).display_name, "test")
678     get :api_read, :id => anon_trace_file.id
679     assert_response :forbidden
680
681     # And finally we should be able to get the trace details with the trace owner
682     basic_authorization(users(:public_user).display_name, "test")
683     get :api_read, :id => anon_trace_file.id
684     assert_response :success
685   end
686
687   # Check the api details for a trace that doesn't exist
688   def test_api_read_not_found
689     deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
690
691     # Try first with no auth, as it should require it
692     get :api_read, :id => 0
693     assert_response :unauthorized
694
695     # Login, and try again
696     basic_authorization(users(:public_user).display_name, "test")
697     get :api_read, :id => 0
698     assert_response :not_found
699
700     # Now try a trace which did exist but has been deleted
701     basic_authorization(users(:public_user).display_name, "test")
702     get :api_read, :id => deleted_trace_file.id
703     assert_response :not_found
704   end
705
706   # Test downloading a trace through the api
707   def test_api_data
708     public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user), :fixture => "a")
709
710     # First with no auth
711     get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
712     assert_response :unauthorized
713
714     # Now with some other user, which should work since the trace is public
715     basic_authorization(users(:public_user).display_name, "test")
716     get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
717     check_trace_data public_trace_file
718
719     # And finally we should be able to do it with the owner of the trace
720     basic_authorization(users(:normal_user).display_name, "test")
721     get :api_data, :display_name => users(:normal_user).display_name, :id => public_trace_file.id
722     check_trace_data public_trace_file
723   end
724
725   # Test downloading a compressed trace through the api
726   def test_api_data_compressed
727     identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => users(:public_user), :fixture => "d")
728
729     # Authenticate as the owner of the trace we will be using
730     basic_authorization(users(:public_user).display_name, "test")
731
732     # First get the data as is
733     get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id
734     check_trace_data identifiable_trace_file, "application/x-gzip", "gpx.gz"
735
736     # Now ask explicitly for XML format
737     get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "xml"
738     check_trace_data identifiable_trace_file, "application/xml", "xml"
739
740     # Now ask explicitly for GPX format
741     get :api_data, :display_name => users(:public_user).display_name, :id => identifiable_trace_file.id, :format => "gpx"
742     check_trace_data identifiable_trace_file
743   end
744
745   # Check an anonymous trace can't be downloaded by another user through the api
746   def test_api_data_anon
747     anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user), :fixture => "b")
748
749     # First with no auth
750     get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
751     assert_response :unauthorized
752
753     # Now with some other user, which shouldn't work since the trace is anon
754     basic_authorization(users(:normal_user).display_name, "test")
755     get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
756     assert_response :forbidden
757
758     # And finally we should be able to do it with the owner of the trace
759     basic_authorization(users(:public_user).display_name, "test")
760     get :api_data, :display_name => users(:public_user).display_name, :id => anon_trace_file.id
761     check_trace_data anon_trace_file
762   end
763
764   # Test downloading a trace that doesn't exist through the api
765   def test_api_data_not_found
766     # First with no auth
767     get :api_data, :display_name => users(:public_user).display_name, :id => 0
768     assert_response :unauthorized
769
770     # Now with a trace that has never existed
771     basic_authorization(users(:public_user).display_name, "test")
772     get :api_data, :display_name => users(:public_user).display_name, :id => 0
773     assert_response :not_found
774
775     # Now with a trace that has been deleted
776     deleted_trace_file = create(:trace, :deleted)
777     basic_authorization(users(:public_user).display_name, "test")
778     get :api_data, :display_name => users(:public_user).display_name, :id => deleted_trace_file.id
779     assert_response :not_found
780   end
781
782   # Test creating a trace through the api
783   def test_api_create
784     # Get file to use
785     fixture = Rails.root.join("test", "gpx", "fixtures", "a.gpx")
786     file = Rack::Test::UploadedFile.new(fixture, "application/gpx+xml")
787
788     # First with no auth
789     post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
790     assert_response :unauthorized
791
792     # Now authenticated
793     create(:user_preference, :user => users(:public_user), :k => "gps.trace.visibility", :v => "identifiable")
794     assert_not_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
795     basic_authorization(users(:public_user).display_name, "test")
796     post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :visibility => "trackable"
797     assert_response :success
798     trace = Trace.find(response.body.to_i)
799     assert_equal "a.gpx", trace.name
800     assert_equal "New Trace", trace.description
801     assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
802     assert_equal "trackable", trace.visibility
803     assert_equal false, trace.inserted
804     assert_equal File.new(fixture).read, File.new(trace.trace_name).read
805     trace.destroy
806     assert_equal "trackable", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
807
808     # Rewind the file
809     file.rewind
810
811     # Now authenticated, with the legacy public flag
812     assert_not_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
813     basic_authorization(users(:public_user).display_name, "test")
814     post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 1
815     assert_response :success
816     trace = Trace.find(response.body.to_i)
817     assert_equal "a.gpx", trace.name
818     assert_equal "New Trace", trace.description
819     assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
820     assert_equal "public", trace.visibility
821     assert_equal false, trace.inserted
822     assert_equal File.new(fixture).read, File.new(trace.trace_name).read
823     trace.destroy
824     assert_equal "public", users(:public_user).preferences.where(:k => "gps.trace.visibility").first.v
825
826     # Rewind the file
827     file.rewind
828
829     # Now authenticated, with the legacy private flag
830     assert_nil users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first
831     basic_authorization(users(:second_public_user).display_name, "test")
832     post :api_create, :file => file, :description => "New Trace", :tags => "new,trace", :public => 0
833     assert_response :success
834     trace = Trace.find(response.body.to_i)
835     assert_equal "a.gpx", trace.name
836     assert_equal "New Trace", trace.description
837     assert_equal %w(new trace), trace.tags.order(:tag).collect(&:tag)
838     assert_equal "private", trace.visibility
839     assert_equal false, trace.inserted
840     assert_equal File.new(fixture).read, File.new(trace.trace_name).read
841     trace.destroy
842     assert_equal "private", users(:second_public_user).preferences.where(:k => "gps.trace.visibility").first.v
843   end
844
845   # Check updating a trace through the api
846   def test_api_update
847     public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user), :fixture => "a")
848     deleted_trace_file = create(:trace, :deleted, :user => users(:public_user))
849     anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
850
851     # First with no auth
852     content public_trace_file.to_xml
853     put :api_update, :id => public_trace_file.id
854     assert_response :unauthorized
855
856     # Now with some other user, which should fail
857     basic_authorization(users(:public_user).display_name, "test")
858     content public_trace_file.to_xml
859     put :api_update, :id => public_trace_file.id
860     assert_response :forbidden
861
862     # Now with a trace which doesn't exist
863     basic_authorization(users(:public_user).display_name, "test")
864     content public_trace_file.to_xml
865     put :api_update, :id => 0
866     assert_response :not_found
867
868     # Now with a trace which did exist but has been deleted
869     basic_authorization(users(:public_user).display_name, "test")
870     content deleted_trace_file.to_xml
871     put :api_update, :id => deleted_trace_file.id
872     assert_response :not_found
873
874     # Now try an update with the wrong ID
875     basic_authorization(users(:normal_user).display_name, "test")
876     content anon_trace_file.to_xml
877     put :api_update, :id => public_trace_file.id
878     assert_response :bad_request,
879                     "should not be able to update a trace with a different ID from the XML"
880
881     # And finally try an update that should work
882     basic_authorization(users(:normal_user).display_name, "test")
883     t = public_trace_file
884     t.description = "Changed description"
885     t.visibility = "private"
886     content t.to_xml
887     put :api_update, :id => t.id
888     assert_response :success
889     nt = Trace.find(t.id)
890     assert_equal nt.description, t.description
891     assert_equal nt.visibility, t.visibility
892   end
893
894   # Check deleting a trace through the api
895   def test_api_delete
896     public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
897
898     # First with no auth
899     delete :api_delete, :id => public_trace_file.id
900     assert_response :unauthorized
901
902     # Now with some other user, which should fail
903     basic_authorization(users(:public_user).display_name, "test")
904     delete :api_delete, :id => public_trace_file.id
905     assert_response :forbidden
906
907     # Now with a trace which doesn't exist
908     basic_authorization(users(:public_user).display_name, "test")
909     delete :api_delete, :id => 0
910     assert_response :not_found
911
912     # And finally we should be able to do it with the owner of the trace
913     basic_authorization(users(:normal_user).display_name, "test")
914     delete :api_delete, :id => public_trace_file.id
915     assert_response :success
916
917     # Try it a second time, which should fail
918     basic_authorization(users(:normal_user).display_name, "test")
919     delete :api_delete, :id => public_trace_file.id
920     assert_response :not_found
921   end
922
923   private
924
925   def check_trace_feed(traces)
926     assert_response :success
927     assert_template "georss"
928     assert_equal "application/rss+xml", @response.content_type
929     assert_select "rss", :count => 1 do
930       assert_select "channel", :count => 1 do
931         assert_select "title"
932         assert_select "description"
933         assert_select "link"
934         assert_select "image"
935         assert_select "item", :count => traces.visible.count do |items|
936           traces.visible.order("timestamp DESC").zip(items).each do |trace, item|
937             assert_select item, "title", trace.name
938             assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
939             assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
940             assert_select item, "description"
941             # assert_select item, "dc:creator", trace.user.display_name
942             assert_select item, "pubDate", trace.timestamp.rfc822
943           end
944         end
945       end
946     end
947   end
948
949   def check_trace_list(traces)
950     assert_response :success
951     assert_template "list"
952
953     if !traces.empty?
954       assert_select "table#trace_list tbody", :count => 1 do
955         assert_select "tr", :count => traces.length do |rows|
956           traces.zip(rows).each do |trace, row|
957             assert_select row, "a", Regexp.new(Regexp.escape(trace.name))
958             assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)")) if trace.inserted?
959             assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
960             assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
961           end
962         end
963       end
964     else
965       assert_select "h4", /Nothing here yet/
966     end
967   end
968
969   def check_trace_view(trace)
970     assert_response :success
971     assert_template "view"
972
973     assert_select "table", :count => 1 do
974       assert_select "td", /^#{Regexp.quote(trace.name)} /
975       assert_select "td", trace.user.display_name
976       assert_select "td", trace.description
977     end
978   end
979
980   def check_trace_data(trace, content_type = "application/gpx+xml", extension = "gpx")
981     assert_response :success
982     assert_equal content_type, response.content_type
983     assert_equal "attachment; filename=\"#{trace.id}.#{extension}\"", @response.header["Content-Disposition"]
984   end
985
986   def check_trace_picture(trace)
987     assert_response :success
988     assert_equal "image/gif", response.content_type
989     assert_equal trace.large_picture, response.body
990   end
991
992   def check_trace_icon(trace)
993     assert_response :success
994     assert_equal "image/gif", response.content_type
995     assert_equal trace.icon_picture, response.body
996   end
997 end