]> git.openstreetmap.org Git - rails.git/blob - test/functional/trace_controller_test.rb
Use ejs templates in browse JS
[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   end
159
160   # Check that I can get mine
161   def test_list_mine
162     @request.cookies["_osm_username"] = users(:public_user).display_name
163
164     # First try to get it when not logged in
165     get :mine
166     assert_redirected_to :controller => 'user', :action => 'login', :referer => '/traces/mine'
167
168     # Now try when logged in
169     get :mine, {}, {:user => users(:public_user).id}
170     assert_redirected_to :controller => 'trace', :action => 'list', :display_name => users(:public_user).display_name
171   end
172
173   # Check that the rss loads
174   def test_rss
175     get :georss
176     assert_rss_success
177
178     get :georss, :display_name => users(:normal_user).display_name
179     assert_rss_success
180   end
181
182   def assert_rss_success
183     assert_response :success
184     assert_template nil
185     assert_equal "application/rss+xml", @response.content_type
186   end
187
188   # Check getting a specific trace through the api
189   def test_api_read
190     # First with no auth
191     get :api_read, :id => gpx_files(:public_trace_file).id
192     assert_response :unauthorized
193
194     # Now with some other user, which should work since the trace is public
195     basic_authorization(users(:public_user).display_name, "test")
196     get :api_read, :id => gpx_files(:public_trace_file).id
197     assert_response :success
198
199     # And finally we should be able to do it with the owner of the trace
200     basic_authorization(users(:normal_user).display_name, "test")
201     get :api_read, :id => gpx_files(:public_trace_file).id
202     assert_response :success
203   end
204
205   # Check an anoymous trace can't be specifically fetched by another user
206   def test_api_read_anon
207     # Furst with no auth
208     get :api_read, :id => gpx_files(:anon_trace_file).id
209     assert_response :unauthorized
210
211     # Now try with another user, which shouldn't work since the trace is anon
212     basic_authorization(users(:normal_user).display_name, "test")
213     get :api_read, :id => gpx_files(:anon_trace_file).id
214     assert_response :forbidden
215
216     # And finally we should be able to get the trace details with the trace owner
217     basic_authorization(users(:public_user).display_name, "test")
218     get :api_read, :id => gpx_files(:anon_trace_file).id
219     assert_response :success
220   end
221
222   # Check the api details for a trace that doesn't exist
223   def test_api_read_not_found
224     # Try first with no auth, as it should requure it
225     get :api_read, :id => 0
226     assert_response :unauthorized
227
228     # Login, and try again
229     basic_authorization(users(:public_user).display_name, "test")
230     get :api_read, :id => 0
231     assert_response :not_found
232
233     # Now try a trace which did exist but has been deleted
234     basic_authorization(users(:public_user).display_name, "test")
235     get :api_read, :id => 5
236     assert_response :not_found
237   end
238
239   # Check updating a trace through the api
240   def test_api_update
241     # First with no auth
242     content gpx_files(:public_trace_file).to_xml
243     put :api_update, :id => gpx_files(:public_trace_file).id
244     assert_response :unauthorized
245
246     # Now with some other user, which should fail
247     basic_authorization(users(:public_user).display_name, "test")
248     content gpx_files(:public_trace_file).to_xml
249     put :api_update, :id => gpx_files(:public_trace_file).id
250     assert_response :forbidden
251
252     # Now with a trace which doesn't exist
253     basic_authorization(users(:public_user).display_name, "test")
254     content gpx_files(:public_trace_file).to_xml
255     put :api_update, :id => 0
256     assert_response :not_found
257
258     # Now with a trace which did exist but has been deleted
259     basic_authorization(users(:public_user).display_name, "test")
260     content gpx_files(:deleted_trace_file).to_xml
261     put :api_update, :id => gpx_files(:deleted_trace_file).id
262     assert_response :not_found
263
264     # Now try an update with the wrong ID
265     basic_authorization(users(:normal_user).display_name, "test")
266     content gpx_files(:anon_trace_file).to_xml
267     put :api_update, :id => gpx_files(:public_trace_file).id
268     assert_response :bad_request, 
269        "should not be able to update a trace with a different ID from the XML"
270
271     # And finally try an update that should work
272     basic_authorization(users(:normal_user).display_name, "test")
273     t = gpx_files(:public_trace_file)
274     t.description = "Changed description"
275     t.visibility = "private"
276     content t.to_xml
277     put :api_update, :id => t.id
278     assert_response :success
279     nt = Trace.find(t.id)
280     assert_equal nt.description, t.description
281     assert_equal nt.visibility, t.visibility
282   end
283
284   # Check deleting a trace through the api
285   def test_api_delete
286     # First with no auth
287     delete :api_delete, :id => gpx_files(:public_trace_file).id
288     assert_response :unauthorized
289
290     # Now with some other user, which should fail
291     basic_authorization(users(:public_user).display_name, "test")
292     delete :api_delete, :id => gpx_files(:public_trace_file).id
293     assert_response :forbidden
294
295     # Now with a trace which doesn't exist
296     basic_authorization(users(:public_user).display_name, "test")
297     delete :api_delete, :id => 0
298     assert_response :not_found
299
300     # And finally we should be able to do it with the owner of the trace
301     basic_authorization(users(:normal_user).display_name, "test")
302     delete :api_delete, :id => gpx_files(:public_trace_file).id
303     assert_response :success
304
305     # Try it a second time, which should fail
306     basic_authorization(users(:normal_user).display_name, "test")
307     delete :api_delete, :id => gpx_files(:public_trace_file).id
308     assert_response :not_found
309   end
310 end