]> git.openstreetmap.org Git - rails.git/blob - test/functional/trace_controller_test.rb
Test user#set_status
[rails.git] / test / functional / trace_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class TraceControllerTest < ActionController::TestCase
4   fixtures :users, :gpx_files
5   set_fixture_class :gpx_files => 'Trace'
6
7   ##
8   # test all routes which lead to this controller
9   def test_routes
10     assert_routing(
11       { :path => "/api/0.6/gpx/create", :method => :post },
12       { :controller => "trace", :action => "api_create" }
13     )
14     assert_routing(
15       { :path => "/api/0.6/gpx/1", :method => :get },
16       { :controller => "trace", :action => "api_read", :id => "1" }
17     )
18     assert_routing(
19       { :path => "/api/0.6/gpx/1", :method => :put },
20       { :controller => "trace", :action => "api_update", :id => "1" }
21     )
22     assert_routing(
23       { :path => "/api/0.6/gpx/1", :method => :delete },
24       { :controller => "trace", :action => "api_delete", :id => "1" }
25     )
26     assert_recognizes(
27       { :controller => "trace", :action => "api_read", :id => "1" },
28       { :path => "/api/0.6/gpx/1/details", :method => :get }
29     )
30     assert_routing(
31       { :path => "/api/0.6/gpx/1/data", :method => :get },
32       { :controller => "trace", :action => "api_data", :id => "1" }
33     )
34     assert_routing(
35       { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
36       { :controller => "trace", :action => "api_data", :id => "1", :format => "xml" }
37     )
38
39     assert_routing(
40       { :path => "/traces", :method => :get },
41       { :controller => "trace", :action => "list" }
42     )
43     assert_routing(
44       { :path => "/traces/page/1", :method => :get },
45       { :controller => "trace", :action => "list", :page => "1" }
46     )
47     assert_routing(
48       { :path => "/traces/tag/tagname", :method => :get },
49       { :controller => "trace", :action => "list", :tag => "tagname" }
50     )
51     assert_routing(
52       { :path => "/traces/tag/tagname/page/1", :method => :get },
53       { :controller => "trace", :action => "list", :tag => "tagname", :page => "1" }
54     )
55     assert_routing(
56       { :path => "/user/username/traces", :method => :get },
57       { :controller => "trace", :action => "list", :display_name => "username" }
58     )
59     assert_routing(
60       { :path => "/user/username/traces/page/1", :method => :get },
61       { :controller => "trace", :action => "list", :display_name => "username", :page => "1" }
62     )
63     assert_routing(
64       { :path => "/user/username/traces/tag/tagname", :method => :get },
65       { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname" }
66     )
67     assert_routing(
68       { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
69       { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname", :page => "1" }
70     )
71
72     assert_routing(
73       { :path => "/traces/mine", :method => :get },
74       { :controller => "trace", :action => "mine" }
75     )
76     assert_routing(
77       { :path => "/traces/mine/page/1", :method => :get },
78       { :controller => "trace", :action => "mine", :page => "1" }
79     )
80     assert_routing(
81       { :path => "/traces/mine/tag/tagname", :method => :get },
82       { :controller => "trace", :action => "mine", :tag => "tagname" }
83     )
84     assert_routing(
85       { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
86       { :controller => "trace", :action => "mine", :tag => "tagname", :page => "1" }
87     )
88
89     assert_routing(
90       { :path => "/traces/rss", :method => :get },
91       { :controller => "trace", :action => "georss", :format => :rss }
92     )
93     assert_routing(
94       { :path => "/traces/tag/tagname/rss", :method => :get },
95       { :controller => "trace", :action => "georss", :tag => "tagname", :format => :rss }
96     )
97     assert_routing(
98       { :path => "/user/username/traces/rss", :method => :get },
99       { :controller => "trace", :action => "georss", :display_name => "username", :format => :rss }
100     )
101     assert_routing(
102       { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
103       { :controller => "trace", :action => "georss", :display_name => "username", :tag => "tagname", :format => :rss }
104     )
105
106     assert_routing(
107       { :path => "/user/username/traces/1", :method => :get },
108       { :controller => "trace", :action => "view", :display_name => "username", :id => "1" }
109     )
110     assert_routing(
111       { :path => "/user/username/traces/1/picture", :method => :get },
112       { :controller => "trace", :action => "picture", :display_name => "username", :id => "1" }
113     )
114     assert_routing(
115       { :path => "/user/username/traces/1/icon", :method => :get },
116       { :controller => "trace", :action => "icon", :display_name => "username", :id => "1" }
117     )
118
119     assert_routing(
120       { :path => "/trace/create", :method => :get },
121       { :controller => "trace", :action => "create" }
122     )
123     assert_routing(
124       { :path => "/trace/create", :method => :post },
125       { :controller => "trace", :action => "create" }
126     )
127     assert_routing(
128       { :path => "/trace/1/data", :method => :get },
129       { :controller => "trace", :action => "data", :id => "1" }
130     )
131     assert_routing(
132       { :path => "/trace/1/data.xml", :method => :get },
133       { :controller => "trace", :action => "data", :id => "1", :format => "xml" }
134     )
135     assert_routing(
136       { :path => "/trace/1/edit", :method => :get },
137       { :controller => "trace", :action => "edit", :id => "1" }
138     )
139     assert_routing(
140       { :path => "/trace/1/edit", :method => :post },
141       { :controller => "trace", :action => "edit", :id => "1" }
142     )
143     assert_routing(
144       { :path => "/trace/1/edit", :method => :patch },
145       { :controller => "trace", :action => "edit", :id => "1" }
146     )
147     assert_routing(
148       { :path => "/trace/1/delete", :method => :post },
149       { :controller => "trace", :action => "delete", :id => "1" }
150     )
151   end
152
153   # Check that the list of changesets is displayed
154   def test_list
155     get :list
156     check_trace_list Trace.public
157
158     get :list, :tag => "London"
159     check_trace_list Trace.tagged("London").public
160   end
161
162   # Check that I can get mine
163   def test_list_mine
164     @request.cookies["_osm_username"] = users(:public_user).display_name
165
166     # First try to get it when not logged in
167     get :mine
168     assert_redirected_to :controller => 'user', :action => 'login', :referer => '/traces/mine'
169
170     # Now try when logged in
171     get :mine, {}, {:user => users(:public_user).id}
172     assert_redirected_to :controller => 'trace', :action => 'list', :display_name => users(:public_user).display_name
173
174     # Fetch the actual list
175     get :list, {:display_name => users(:public_user).display_name}, {:user => users(:public_user).id}
176     check_trace_list users(:public_user).traces
177   end
178
179   # Check the list of changesets for a specific user
180   def test_list_user
181     # Test a user with no traces
182     get :list, :display_name => users(:second_public_user).display_name
183     check_trace_list users(:second_public_user).traces.public
184
185     # Test a user with some traces - should see only public ones
186     get :list, :display_name => users(:public_user).display_name
187     check_trace_list users(:public_user).traces.public
188
189     @request.cookies["_osm_username"] = users(:normal_user).display_name
190
191     # Should still see only public ones when authenticated as another user
192     get :list, {:display_name => users(:public_user).display_name}, {:user => users(:normal_user).id}
193     check_trace_list users(:public_user).traces.public
194
195     @request.cookies["_osm_username"] = users(:public_user).display_name
196
197     # Should see all traces when authenticated as the target user
198     get :list, {:display_name => users(:public_user).display_name}, {:user => users(:public_user).id}
199     check_trace_list users(:public_user).traces
200
201     # Should only see traces with the correct tag when a tag is specified
202     get :list, {:display_name => users(:public_user).display_name, :tag => "London"}, {:user => users(:public_user).id}
203     check_trace_list users(:public_user).traces.tagged("London")
204   end
205
206   # Check that the rss loads
207   def test_rss
208     get :georss, :format => :rss
209     check_trace_feed Trace.public
210
211     get :georss, :tag => "London", :format => :rss
212     check_trace_feed Trace.tagged("London").public
213
214     get :georss, :display_name => users(:public_user).display_name, :format => :rss
215     check_trace_feed users(:public_user).traces.public
216
217     get :georss, :display_name => users(:public_user).display_name, :tag => "Birmingham", :format => :rss
218     check_trace_feed users(:public_user).traces.tagged("Birmingham").public
219   end
220
221   # Test viewing a trace
222   def test_view
223     # First with no auth, which should work since the trace is public
224     get :view, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}
225     check_trace_view gpx_files(:public_trace_file)
226
227     @request.cookies["_osm_username"] = users(:public_user).display_name
228
229     # Now with some other user, which should work since the trace is public
230     get :view, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}, {:user => users(:public_user).id}
231     check_trace_view gpx_files(:public_trace_file)
232
233     @request.cookies["_osm_username"] = users(:normal_user).display_name
234
235     # And finally we should be able to do it with the owner of the trace
236     get :view, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}, {:user => users(:normal_user).id}
237     check_trace_view gpx_files(:public_trace_file)
238   end
239
240   # Check an anonymous trace can't be viewed by another user
241   def test_view_anon
242     # First with no auth
243     get :view, {:display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id}
244     assert_response :redirect
245     assert_redirected_to :action => :list
246
247     @request.cookies["_osm_username"] = users(:normal_user).display_name
248
249     # Now with some other user, which should work since the trace is anon
250     get :view, {:display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id}, {:user => users(:normal_user).id}
251     assert_response :redirect
252     assert_redirected_to :action => :list
253
254     @request.cookies["_osm_username"] = users(:public_user).display_name
255
256     # And finally we should be able to do it with the owner of the trace
257     get :view, {:display_name => users(:public_user).display_name, :id => gpx_files(:anon_trace_file).id}, {:user => users(:public_user).id}
258     check_trace_view gpx_files(:anon_trace_file)
259   end
260
261   # Test viewing a trace that doesn't exist
262   def test_view_not_found
263     # First with no auth, which should work since the trace is public
264     get :view, {:display_name => users(:public_user).display_name, :id => 0}
265     assert_response :redirect
266     assert_redirected_to :action => :list
267
268     @request.cookies["_osm_username"] = users(:public_user).display_name
269
270     # Now with some other user, which should work since the trace is public
271     get :view, {:display_name => users(:public_user).display_name, :id => 0}, {:user => users(:public_user).id}
272     assert_response :redirect
273     assert_redirected_to :action => :list
274
275     # And finally we should be able to do it with the owner of the trace
276     get :view, {:display_name => users(:public_user).display_name, :id => 5}, {:user => users(:public_user).id}
277     assert_response :redirect
278     assert_redirected_to :action => :list
279   end
280
281   # Test fetching the edit page for a trace
282   def test_edit_get
283     # First with no auth
284     get :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}
285     assert_response :redirect
286     assert_redirected_to :controller => :user, :action => :login, :referer => trace_edit_path(:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id)
287
288     @request.cookies["_osm_username"] = users(:public_user).display_name
289
290     # Now with some other user, which should fail
291     get :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}, {:user => users(:public_user).id}
292     assert_response :forbidden
293
294     # Now with a trace which doesn't exist
295     get :edit, {:display_name => users(:public_user).display_name, :id => 0}, {:user => users(:public_user).id}
296     assert_response :not_found
297
298     # Now with a trace which has been deleted
299     get :edit, {:display_name => users(:public_user).display_name, :id => gpx_files(:deleted_trace_file).id}, {:user => users(:public_user).id}
300     assert_response :not_found
301
302     @request.cookies["_osm_username"] = users(:normal_user).display_name
303
304     # Finally with a trace that we are allowed to edit
305     get :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}, {:user => users(:normal_user).id}
306     assert_response :success
307   end
308
309   # Test saving edits to a trace
310   def test_edit_post
311     # New details
312     new_details = { :description => "Changed description", :tagstring => "new_tag", :visibility => "private" }
313
314     # First with no auth
315     post :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id, :trace => new_details}
316     assert_response :forbidden
317
318     @request.cookies["_osm_username"] = users(:public_user).display_name
319
320     # Now with some other user, which should fail
321     post :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id, :trace => new_details}, {:user => users(:public_user).id}
322     assert_response :forbidden
323
324     # Now with a trace which doesn't exist
325     post :edit, {:display_name => users(:public_user).display_name, :id => 0}, {:user => users(:public_user).id, :trace => new_details}
326     assert_response :not_found
327
328     # Now with a trace which has been deleted
329     post :edit, {:display_name => users(:public_user).display_name, :id => gpx_files(:deleted_trace_file).id, :trace => new_details}, {:user => users(:public_user).id}
330     assert_response :not_found
331
332     @request.cookies["_osm_username"] = users(:normal_user).display_name
333
334     # Finally with a trace that we are allowed to edit
335     post :edit, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id, :trace => new_details}, {:user => users(:normal_user).id}
336     assert_response :redirect
337     assert_redirected_to :action => :view, :display_name => users(:normal_user).display_name
338     trace = Trace.find(gpx_files(:public_trace_file).id)
339     assert_equal new_details[:description], trace.description
340     assert_equal new_details[:tagstring], trace.tagstring
341     assert_equal new_details[:visibility], trace.visibility
342   end
343
344   # Test deleting a trace
345   def test_delete
346     # First with no auth
347     post :delete, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id,}
348     assert_response :forbidden
349
350     @request.cookies["_osm_username"] = users(:public_user).display_name
351
352     # Now with some other user, which should fail
353     post :delete, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}, {:user => users(:public_user).id}
354     assert_response :forbidden
355
356     # Now with a trace which doesn't exist
357     post :delete, {:display_name => users(:public_user).display_name, :id => 0}, {:user => users(:public_user).id}
358     assert_response :not_found
359
360     # Now with a trace has already been deleted
361     post :delete, {:display_name => users(:public_user).display_name, :id => gpx_files(:deleted_trace_file).id}, {:user => users(:public_user).id}
362     assert_response :not_found
363
364     @request.cookies["_osm_username"] = users(:normal_user).display_name
365
366     # Finally with a trace that we are allowed to delete
367     post :delete, {:display_name => users(:normal_user).display_name, :id => gpx_files(:public_trace_file).id}, {:user => users(:normal_user).id}
368     assert_response :redirect
369     assert_redirected_to :action => :list, :display_name => users(:normal_user).display_name
370     trace = Trace.find(gpx_files(:public_trace_file).id)
371     assert_equal false, trace.visible
372   end
373
374   # Check getting a specific trace through the api
375   def test_api_read
376     # First with no auth
377     get :api_read, :id => gpx_files(:public_trace_file).id
378     assert_response :unauthorized
379
380     # Now with some other user, which should work since the trace is public
381     basic_authorization(users(:public_user).display_name, "test")
382     get :api_read, :id => gpx_files(:public_trace_file).id
383     assert_response :success
384
385     # And finally we should be able to do it with the owner of the trace
386     basic_authorization(users(:normal_user).display_name, "test")
387     get :api_read, :id => gpx_files(:public_trace_file).id
388     assert_response :success
389   end
390
391   # Check an anoymous trace can't be specifically fetched by another user
392   def test_api_read_anon
393     # Furst with no auth
394     get :api_read, :id => gpx_files(:anon_trace_file).id
395     assert_response :unauthorized
396
397     # Now try with another user, which shouldn't work since the trace is anon
398     basic_authorization(users(:normal_user).display_name, "test")
399     get :api_read, :id => gpx_files(:anon_trace_file).id
400     assert_response :forbidden
401
402     # And finally we should be able to get the trace details with the trace owner
403     basic_authorization(users(:public_user).display_name, "test")
404     get :api_read, :id => gpx_files(:anon_trace_file).id
405     assert_response :success
406   end
407
408   # Check the api details for a trace that doesn't exist
409   def test_api_read_not_found
410     # Try first with no auth, as it should requure it
411     get :api_read, :id => 0
412     assert_response :unauthorized
413
414     # Login, and try again
415     basic_authorization(users(:public_user).display_name, "test")
416     get :api_read, :id => 0
417     assert_response :not_found
418
419     # Now try a trace which did exist but has been deleted
420     basic_authorization(users(:public_user).display_name, "test")
421     get :api_read, :id => 5
422     assert_response :not_found
423   end
424
425   # Check updating a trace through the api
426   def test_api_update
427     # First with no auth
428     content gpx_files(:public_trace_file).to_xml
429     put :api_update, :id => gpx_files(:public_trace_file).id
430     assert_response :unauthorized
431
432     # Now with some other user, which should fail
433     basic_authorization(users(:public_user).display_name, "test")
434     content gpx_files(:public_trace_file).to_xml
435     put :api_update, :id => gpx_files(:public_trace_file).id
436     assert_response :forbidden
437
438     # Now with a trace which doesn't exist
439     basic_authorization(users(:public_user).display_name, "test")
440     content gpx_files(:public_trace_file).to_xml
441     put :api_update, :id => 0
442     assert_response :not_found
443
444     # Now with a trace which did exist but has been deleted
445     basic_authorization(users(:public_user).display_name, "test")
446     content gpx_files(:deleted_trace_file).to_xml
447     put :api_update, :id => gpx_files(:deleted_trace_file).id
448     assert_response :not_found
449
450     # Now try an update with the wrong ID
451     basic_authorization(users(:normal_user).display_name, "test")
452     content gpx_files(:anon_trace_file).to_xml
453     put :api_update, :id => gpx_files(:public_trace_file).id
454     assert_response :bad_request, 
455        "should not be able to update a trace with a different ID from the XML"
456
457     # And finally try an update that should work
458     basic_authorization(users(:normal_user).display_name, "test")
459     t = gpx_files(:public_trace_file)
460     t.description = "Changed description"
461     t.visibility = "private"
462     content t.to_xml
463     put :api_update, :id => t.id
464     assert_response :success
465     nt = Trace.find(t.id)
466     assert_equal nt.description, t.description
467     assert_equal nt.visibility, t.visibility
468   end
469
470   # Check deleting a trace through the api
471   def test_api_delete
472     # First with no auth
473     delete :api_delete, :id => gpx_files(:public_trace_file).id
474     assert_response :unauthorized
475
476     # Now with some other user, which should fail
477     basic_authorization(users(:public_user).display_name, "test")
478     delete :api_delete, :id => gpx_files(:public_trace_file).id
479     assert_response :forbidden
480
481     # Now with a trace which doesn't exist
482     basic_authorization(users(:public_user).display_name, "test")
483     delete :api_delete, :id => 0
484     assert_response :not_found
485
486     # And finally we should be able to do it with the owner of the trace
487     basic_authorization(users(:normal_user).display_name, "test")
488     delete :api_delete, :id => gpx_files(:public_trace_file).id
489     assert_response :success
490
491     # Try it a second time, which should fail
492     basic_authorization(users(:normal_user).display_name, "test")
493     delete :api_delete, :id => gpx_files(:public_trace_file).id
494     assert_response :not_found
495   end
496
497 private
498
499   def check_trace_feed(traces)
500     assert_response :success
501     assert_template "georss"
502     assert_equal "application/rss+xml", @response.content_type
503     assert_select "rss", :count => 1 do
504       assert_select "channel", :count => 1 do
505         assert_select "title"
506         assert_select "description"
507         assert_select "link"
508         assert_select "image"
509         assert_select "item", :count => traces.visible.count do |items|
510           traces.visible.order("timestamp DESC").zip(items).each do |trace,item|
511             assert_select item, "title", trace.name
512             assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
513             assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
514             assert_select item, "description"
515 #            assert_select item, "dc:creator", trace.user.display_name
516             assert_select item, "pubDate", trace.timestamp.rfc822
517           end
518         end
519       end
520     end
521   end
522
523   def check_trace_list(traces)
524     assert_response :success
525     assert_template "list"
526
527     if traces.count > 0
528       assert_select "table#trace_list tbody", :count => 1 do
529         assert_select "tr", :count => traces.visible.count do |rows|
530           traces.visible.order("timestamp DESC").zip(rows).each do |trace,row|
531             assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)"))
532             assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
533             assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
534           end
535         end
536       end
537     else
538       assert_select "h4", /Nothing here yet/
539     end
540   end
541
542   def check_trace_view(trace)
543     assert_response :success
544     assert_template "view"
545
546     assert_select "table", :count => 1 do
547       assert_select "td", /^#{Regexp.quote(trace.name)} /
548       assert_select "td", trace.user.display_name
549       assert_select "td", trace.description
550     end
551   end
552 end