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