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