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