1 require File.dirname(__FILE__) + '/../test_helper'
3 class TraceControllerTest < ActionController::TestCase
4 fixtures :users, :gpx_files
5 set_fixture_class :gpx_files => 'Trace'
8 # test all routes which lead to this controller
11 { :path => "/api/0.6/gpx/create", :method => :post },
12 { :controller => "trace", :action => "api_create" }
15 { :path => "/api/0.6/gpx/1", :method => :get },
16 { :controller => "trace", :action => "api_read", :id => "1" }
19 { :path => "/api/0.6/gpx/1", :method => :put },
20 { :controller => "trace", :action => "api_update", :id => "1" }
23 { :path => "/api/0.6/gpx/1", :method => :delete },
24 { :controller => "trace", :action => "api_delete", :id => "1" }
27 { :controller => "trace", :action => "api_read", :id => "1" },
28 { :path => "/api/0.6/gpx/1/details", :method => :get }
31 { :path => "/api/0.6/gpx/1/data", :method => :get },
32 { :controller => "trace", :action => "api_data", :id => "1" }
35 { :path => "/api/0.6/gpx/1/data.xml", :method => :get },
36 { :controller => "trace", :action => "api_data", :id => "1", :format => "xml" }
40 { :path => "/traces", :method => :get },
41 { :controller => "trace", :action => "list" }
44 { :path => "/traces/page/1", :method => :get },
45 { :controller => "trace", :action => "list", :page => "1" }
48 { :path => "/traces/tag/tagname", :method => :get },
49 { :controller => "trace", :action => "list", :tag => "tagname" }
52 { :path => "/traces/tag/tagname/page/1", :method => :get },
53 { :controller => "trace", :action => "list", :tag => "tagname", :page => "1" }
56 { :path => "/user/username/traces", :method => :get },
57 { :controller => "trace", :action => "list", :display_name => "username" }
60 { :path => "/user/username/traces/page/1", :method => :get },
61 { :controller => "trace", :action => "list", :display_name => "username", :page => "1" }
64 { :path => "/user/username/traces/tag/tagname", :method => :get },
65 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname" }
68 { :path => "/user/username/traces/tag/tagname/page/1", :method => :get },
69 { :controller => "trace", :action => "list", :display_name => "username", :tag => "tagname", :page => "1" }
73 { :path => "/traces/mine", :method => :get },
74 { :controller => "trace", :action => "mine" }
77 { :path => "/traces/mine/page/1", :method => :get },
78 { :controller => "trace", :action => "mine", :page => "1" }
81 { :path => "/traces/mine/tag/tagname", :method => :get },
82 { :controller => "trace", :action => "mine", :tag => "tagname" }
85 { :path => "/traces/mine/tag/tagname/page/1", :method => :get },
86 { :controller => "trace", :action => "mine", :tag => "tagname", :page => "1" }
90 { :path => "/traces/rss", :method => :get },
91 { :controller => "trace", :action => "georss" }
94 { :path => "/traces/tag/tagname/rss", :method => :get },
95 { :controller => "trace", :action => "georss", :tag => "tagname" }
98 { :path => "/user/username/traces/rss", :method => :get },
99 { :controller => "trace", :action => "georss", :display_name => "username" }
102 { :path => "/user/username/traces/tag/tagname/rss", :method => :get },
103 { :controller => "trace", :action => "georss", :display_name => "username", :tag => "tagname" }
107 { :path => "/user/username/traces/1", :method => :get },
108 { :controller => "trace", :action => "view", :display_name => "username", :id => "1" }
111 { :path => "/user/username/traces/1/picture", :method => :get },
112 { :controller => "trace", :action => "picture", :display_name => "username", :id => "1" }
115 { :path => "/user/username/traces/1/icon", :method => :get },
116 { :controller => "trace", :action => "icon", :display_name => "username", :id => "1" }
120 { :path => "/trace/create", :method => :get },
121 { :controller => "trace", :action => "create" }
124 { :path => "/trace/create", :method => :post },
125 { :controller => "trace", :action => "create" }
128 { :path => "/trace/1/data", :method => :get },
129 { :controller => "trace", :action => "data", :id => "1" }
132 { :path => "/trace/1/data.xml", :method => :get },
133 { :controller => "trace", :action => "data", :id => "1", :format => "xml" }
136 { :path => "/trace/1/edit", :method => :get },
137 { :controller => "trace", :action => "edit", :id => "1" }
140 { :path => "/trace/1/edit", :method => :post },
141 { :controller => "trace", :action => "edit", :id => "1" }
144 { :path => "/trace/1/delete", :method => :post },
145 { :controller => "trace", :action => "delete", :id => "1" }
149 # Check that the list of changesets is displayed
152 assert_response :success
153 assert_template 'list'
156 # Check that I can get mine
158 @request.cookies["_osm_username"] = users(:public_user).display_name
160 # First try to get it when not logged in
162 assert_redirected_to :controller => 'user', :action => 'login', :referer => '/traces/mine'
164 # Now try when logged in
165 get :mine, {}, {:user => users(:public_user).id}
166 assert_redirected_to :controller => 'trace', :action => 'list', :display_name => users(:public_user).display_name
169 # Check that the rss loads
174 get :georss, :display_name => users(:normal_user).display_name
178 def assert_rss_success
179 assert_response :success
181 assert_equal "application/rss+xml", @response.content_type
184 # Check getting a specific trace through the api
187 get :api_read, :id => gpx_files(:public_trace_file).id
188 assert_response :unauthorized
190 # Now with some other user, which should work since the trace is public
191 basic_authorization(users(:public_user).display_name, "test")
192 get :api_read, :id => gpx_files(:public_trace_file).id
193 assert_response :success
195 # And finally we should be able to do it with the owner of the trace
196 basic_authorization(users(:normal_user).display_name, "test")
197 get :api_read, :id => gpx_files(:public_trace_file).id
198 assert_response :success
201 # Check an anoymous trace can't be specifically fetched by another user
202 def test_api_read_anon
204 get :api_read, :id => gpx_files(:anon_trace_file).id
205 assert_response :unauthorized
207 # Now try with another user, which shouldn't work since the trace is anon
208 basic_authorization(users(:normal_user).display_name, "test")
209 get :api_read, :id => gpx_files(:anon_trace_file).id
210 assert_response :forbidden
212 # And finally we should be able to get the trace details with the trace owner
213 basic_authorization(users(:public_user).display_name, "test")
214 get :api_read, :id => gpx_files(:anon_trace_file).id
215 assert_response :success
218 # Check the api details for a trace that doesn't exist
219 def test_api_read_not_found
220 # Try first with no auth, as it should requure it
221 get :api_read, :id => 0
222 assert_response :unauthorized
224 # Login, and try again
225 basic_authorization(users(:public_user).display_name, "test")
226 get :api_read, :id => 0
227 assert_response :not_found
229 # Now try a trace which did exist but has been deleted
230 basic_authorization(users(:public_user).display_name, "test")
231 get :api_read, :id => 5
232 assert_response :not_found
235 # Check updating a trace through the api
238 content gpx_files(:public_trace_file).to_xml
239 put :api_update, :id => gpx_files(:public_trace_file).id
240 assert_response :unauthorized
242 # Now with some other user, which should fail
243 basic_authorization(users(:public_user).display_name, "test")
244 content gpx_files(:public_trace_file).to_xml
245 put :api_update, :id => gpx_files(:public_trace_file).id
246 assert_response :forbidden
248 # Now with a trace which doesn't exist
249 basic_authorization(users(:public_user).display_name, "test")
250 content gpx_files(:public_trace_file).to_xml
251 put :api_update, :id => 0
252 assert_response :not_found
254 # Now with a trace which did exist but has been deleted
255 basic_authorization(users(:public_user).display_name, "test")
256 content gpx_files(:deleted_trace_file).to_xml
257 put :api_update, :id => gpx_files(:deleted_trace_file).id
258 assert_response :not_found
260 # Now try an update with the wrong ID
261 basic_authorization(users(:normal_user).display_name, "test")
262 content gpx_files(:anon_trace_file).to_xml
263 put :api_update, :id => gpx_files(:public_trace_file).id
264 assert_response :bad_request,
265 "should not be able to update a trace with a different ID from the XML"
267 # And finally try an update that should work
268 basic_authorization(users(:normal_user).display_name, "test")
269 t = gpx_files(:public_trace_file)
270 t.description = "Changed description"
271 t.visibility = "private"
273 put :api_update, :id => t.id
274 assert_response :success
275 nt = Trace.find(t.id)
276 assert_equal nt.description, t.description
277 assert_equal nt.visibility, t.visibility
280 # Check deleting a trace through the api
283 delete :api_delete, :id => gpx_files(:public_trace_file).id
284 assert_response :unauthorized
286 # Now with some other user, which should fail
287 basic_authorization(users(:public_user).display_name, "test")
288 delete :api_delete, :id => gpx_files(:public_trace_file).id
289 assert_response :forbidden
291 # Now with a trace which doesn't exist
292 basic_authorization(users(:public_user).display_name, "test")
293 delete :api_delete, :id => 0
294 assert_response :not_found
296 # And finally we should be able to do it with the owner of the trace
297 basic_authorization(users(:normal_user).display_name, "test")
298 delete :api_delete, :id => gpx_files(:public_trace_file).id
299 assert_response :success
301 # Try it a second time, which should fail
302 basic_authorization(users(:normal_user).display_name, "test")
303 delete :api_delete, :id => gpx_files(:public_trace_file).id
304 assert_response :not_found