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