]> git.openstreetmap.org Git - rails.git/blob - test/functional/trace_controller_test.rb
Fix trace lists to correctly respect tag restrictions
[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" }
92     )
93     assert_routing(
94       { :path => "/traces/tag/tagname/rss", :method => :get },
95       { :controller => "trace", :action => "georss", :tag => "tagname" }
96     )
97     assert_routing(
98       { :path => "/user/username/traces/rss", :method => :get },
99       { :controller => "trace", :action => "georss", :display_name => "username" }
100     )
101     assert_routing(
102       { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
103       { :controller => "trace", :action => "georss", :display_name => "username", :tag => "tagname" }
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 => :put },
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     assert_response :success
157     assert_template "list"
158     check_trace_list Trace.public
159   end
160
161   # Check that I can get mine
162   def test_list_mine
163     @request.cookies["_osm_username"] = users(:public_user).display_name
164
165     # First try to get it when not logged in
166     get :mine
167     assert_redirected_to :controller => 'user', :action => 'login', :referer => '/traces/mine'
168
169     # Now try when logged in
170     get :mine, {}, {:user => users(:public_user).id}
171     assert_redirected_to :controller => 'trace', :action => 'list', :display_name => users(:public_user).display_name
172
173     # Fetch the actual list
174     get :list, {:display_name => users(:public_user).display_name}, {:user => users(:public_user).id}
175     assert_response :success
176     assert_template "list"
177     check_trace_list users(:public_user).traces
178   end
179
180   # Check the list of changesets for a specific user
181   def test_list_user
182     # Test a user with no traces
183     get :list, :display_name => users(:second_public_user).display_name
184     assert_response :success
185     assert_template "list"
186     check_trace_list users(:second_public_user).traces.public
187
188     # Test a user with some traces - should see only public ones
189     get :list, :display_name => users(:public_user).display_name
190     assert_response :success
191     assert_template "list"
192     check_trace_list users(:public_user).traces.public
193
194     @request.cookies["_osm_username"] = users(:normal_user).display_name
195
196     # Should still see only public ones when authenticated as another user
197     get :list, {:display_name => users(:public_user).display_name}, {:user => users(:normal_user).id}
198     assert_response :success
199     assert_template "list"
200     check_trace_list users(:public_user).traces.public
201
202     @request.cookies["_osm_username"] = users(:public_user).display_name
203
204     # Should see all traces when authenticated as the target user
205     get :list, {:display_name => users(:public_user).display_name}, {:user => users(:public_user).id}
206     assert_response :success
207     assert_template "list"
208     check_trace_list users(:public_user).traces
209   end
210
211   # Check that the rss loads
212   def test_rss
213     get :georss
214     check_trace_feed Trace.public
215
216     get :georss, :tag => "London"
217     check_trace_feed Trace.tagged("London").public
218
219     get :georss, :display_name => users(:public_user).display_name
220     check_trace_feed users(:public_user).traces.public
221
222     get :georss, :display_name => users(:public_user).display_name, :tag => "Birmingham"
223     check_trace_feed users(:public_user).traces.tagged("Birmingham").public
224   end
225
226   # Check getting a specific trace through the api
227   def test_api_read
228     # First with no auth
229     get :api_read, :id => gpx_files(:public_trace_file).id
230     assert_response :unauthorized
231
232     # Now with some other user, which should work since the trace is public
233     basic_authorization(users(:public_user).display_name, "test")
234     get :api_read, :id => gpx_files(:public_trace_file).id
235     assert_response :success
236
237     # And finally we should be able to do it with the owner of the trace
238     basic_authorization(users(:normal_user).display_name, "test")
239     get :api_read, :id => gpx_files(:public_trace_file).id
240     assert_response :success
241   end
242
243   # Check an anoymous trace can't be specifically fetched by another user
244   def test_api_read_anon
245     # Furst with no auth
246     get :api_read, :id => gpx_files(:anon_trace_file).id
247     assert_response :unauthorized
248
249     # Now try with another user, which shouldn't work since the trace is anon
250     basic_authorization(users(:normal_user).display_name, "test")
251     get :api_read, :id => gpx_files(:anon_trace_file).id
252     assert_response :forbidden
253
254     # And finally we should be able to get the trace details with the trace owner
255     basic_authorization(users(:public_user).display_name, "test")
256     get :api_read, :id => gpx_files(:anon_trace_file).id
257     assert_response :success
258   end
259
260   # Check the api details for a trace that doesn't exist
261   def test_api_read_not_found
262     # Try first with no auth, as it should requure it
263     get :api_read, :id => 0
264     assert_response :unauthorized
265
266     # Login, and try again
267     basic_authorization(users(:public_user).display_name, "test")
268     get :api_read, :id => 0
269     assert_response :not_found
270
271     # Now try a trace which did exist but has been deleted
272     basic_authorization(users(:public_user).display_name, "test")
273     get :api_read, :id => 5
274     assert_response :not_found
275   end
276
277   # Check updating a trace through the api
278   def test_api_update
279     # First with no auth
280     content gpx_files(:public_trace_file).to_xml
281     put :api_update, :id => gpx_files(:public_trace_file).id
282     assert_response :unauthorized
283
284     # Now with some other user, which should fail
285     basic_authorization(users(:public_user).display_name, "test")
286     content gpx_files(:public_trace_file).to_xml
287     put :api_update, :id => gpx_files(:public_trace_file).id
288     assert_response :forbidden
289
290     # Now with a trace which doesn't exist
291     basic_authorization(users(:public_user).display_name, "test")
292     content gpx_files(:public_trace_file).to_xml
293     put :api_update, :id => 0
294     assert_response :not_found
295
296     # Now with a trace which did exist but has been deleted
297     basic_authorization(users(:public_user).display_name, "test")
298     content gpx_files(:deleted_trace_file).to_xml
299     put :api_update, :id => gpx_files(:deleted_trace_file).id
300     assert_response :not_found
301
302     # Now try an update with the wrong ID
303     basic_authorization(users(:normal_user).display_name, "test")
304     content gpx_files(:anon_trace_file).to_xml
305     put :api_update, :id => gpx_files(:public_trace_file).id
306     assert_response :bad_request, 
307        "should not be able to update a trace with a different ID from the XML"
308
309     # And finally try an update that should work
310     basic_authorization(users(:normal_user).display_name, "test")
311     t = gpx_files(:public_trace_file)
312     t.description = "Changed description"
313     t.visibility = "private"
314     content t.to_xml
315     put :api_update, :id => t.id
316     assert_response :success
317     nt = Trace.find(t.id)
318     assert_equal nt.description, t.description
319     assert_equal nt.visibility, t.visibility
320   end
321
322   # Check deleting a trace through the api
323   def test_api_delete
324     # First with no auth
325     delete :api_delete, :id => gpx_files(:public_trace_file).id
326     assert_response :unauthorized
327
328     # Now with some other user, which should fail
329     basic_authorization(users(:public_user).display_name, "test")
330     delete :api_delete, :id => gpx_files(:public_trace_file).id
331     assert_response :forbidden
332
333     # Now with a trace which doesn't exist
334     basic_authorization(users(:public_user).display_name, "test")
335     delete :api_delete, :id => 0
336     assert_response :not_found
337
338     # And finally we should be able to do it with the owner of the trace
339     basic_authorization(users(:normal_user).display_name, "test")
340     delete :api_delete, :id => gpx_files(:public_trace_file).id
341     assert_response :success
342
343     # Try it a second time, which should fail
344     basic_authorization(users(:normal_user).display_name, "test")
345     delete :api_delete, :id => gpx_files(:public_trace_file).id
346     assert_response :not_found
347   end
348
349 private
350
351   def check_trace_feed(traces)
352     assert_response :success
353     assert_template nil
354     assert_equal "application/rss+xml", @response.content_type
355     assert_select "rss", :count => 1 do
356       assert_select "channel", :count => 1 do
357         assert_select "title"
358         assert_select "description"
359         assert_select "link"
360         assert_select "image"
361         assert_select "item", :count => traces.visible.count do |items|
362           traces.visible.order("timestamp DESC").zip(items).each do |trace,item|
363             assert_select item, "title", trace.name
364             assert_select item, "link", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
365             assert_select item, "guid", "http://test.host/user/#{trace.user.display_name}/traces/#{trace.id}"
366             assert_select item, "description"
367             assert_select item, "author", trace.user.display_name
368             assert_select item, "pubDate", trace.timestamp.rfc822
369           end
370         end
371       end
372     end
373   end
374
375   def check_trace_list(traces)
376     traces = traces.visible.order("timestamp DESC")
377     
378     if traces.count > 0
379       assert_select "table#trace_list tbody", :count => 1 do
380         assert_select "tr", :count => traces.count do |rows|
381           traces.zip(rows).each do |trace,row|
382             assert_select row, "span.trace_summary", Regexp.new(Regexp.escape("(#{trace.size} points)"))
383             assert_select row, "td", Regexp.new(Regexp.escape(trace.description))
384             assert_select row, "td", Regexp.new(Regexp.escape("by #{trace.user.display_name}"))
385           end
386         end
387       end
388     else
389       assert_select "h4", /Nothing here yet/
390     end
391   end
392 end