1 require File.dirname(__FILE__) + '/../test_helper'
3 class NodeControllerTest < ActionController::TestCase
7 # cannot read password from fixture as it is stored as MD5 digest
8 ## First try with no auth
10 # create a node with random lat/lon
11 lat = rand(100)-50 + rand
12 lon = rand(100)-50 + rand
13 # normal user has a changeset open, so we'll use that.
14 changeset = changesets(:normal_user_first_change)
15 # create a minimal xml file
16 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
17 assert_difference('OldNode.count', 0) do
20 # hope for unauthorized
21 assert_response :unauthorized, "node upload did not return unauthorized status"
25 ## Now try with the user which doesn't have their data public
26 basic_authorization(users(:normal_user).email, "test")
28 # create a node with random lat/lon
29 lat = rand(100)-50 + rand
30 lon = rand(100)-50 + rand
31 # normal user has a changeset open, so we'll use that.
32 changeset = changesets(:normal_user_first_change)
33 # create a minimal xml file
34 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
35 assert_difference('Node.count', 0) do
39 assert_require_public_data "node create did not return forbidden status"
43 ## Now try with the user that has the public data
44 basic_authorization(users(:public_user).email, "test")
46 # create a node with random lat/lon
47 lat = rand(100)-50 + rand
48 lon = rand(100)-50 + rand
49 # normal user has a changeset open, so we'll use that.
50 changeset = changesets(:public_user_first_change)
51 # create a minimal xml file
52 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
55 assert_response :success, "node upload did not return success status"
57 # read id of created node and search for it
58 nodeid = @response.body
59 checknode = Node.find(nodeid)
60 assert_not_nil checknode, "uploaded node not found in data base after upload"
62 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
63 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
64 assert_equal changesets(:public_user_first_change).id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
65 assert_equal true, checknode.visible, "saved node is not visible"
68 def test_create_invalid_xml
69 ## Only test public user here, as test_create should cover what's the forbiddens
70 ## that would occur here
72 basic_authorization(users(:public_user).email, "test")
73 # normal user has a changeset open, so we'll use that.
74 changeset = changesets(:public_user_first_change)
78 # test that the upload is rejected when no lat is supplied
79 # create a minimal xml file
80 content("<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>")
83 assert_response :bad_request, "node upload did not return bad_request status"
84 assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
86 # test that the upload is rejected when no lon is supplied
87 # create a minimal xml file
88 content("<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>")
91 assert_response :bad_request, "node upload did not return bad_request status"
92 assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
94 # test that the upload is rejected when we have a tag which is too long
95 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'><tag k='foo' v='#{'x'*256}'/></node></osm>")
97 assert_response :bad_request, "node upload did not return bad_request status"
98 assert_equal ["NodeTag ", " v: is too long (maximum is 255 characters) (\"#{'x'*256}\")"], @response.body.split(/[0-9]+:/)
103 # check that a visible node is returned properly
104 get :read, :id => current_nodes(:visible_node).id
105 assert_response :success
107 # check that an invisible node is not returned
108 get :read, :id => current_nodes(:invisible_node).id
109 assert_response :gone
111 # check chat a non-existent node is not returned
113 assert_response :not_found
116 # this tests deletion restrictions - basic deletion is tested in the unit
119 ## first try to delete node without auth
120 delete :delete, :id => current_nodes(:visible_node).id
121 assert_response :unauthorized
124 ## now set auth for the non-data public user
125 basic_authorization(users(:normal_user).email, "test");
127 # try to delete with an invalid (closed) changeset
128 content update_changeset(current_nodes(:visible_node).to_xml,
129 changesets(:normal_user_closed_change).id)
130 delete :delete, :id => current_nodes(:visible_node).id
131 assert_require_public_data("non-public user shouldn't be able to delete node")
133 # try to delete with an invalid (non-existent) changeset
134 content update_changeset(current_nodes(:visible_node).to_xml,0)
135 delete :delete, :id => current_nodes(:visible_node).id
136 assert_require_public_data("shouldn't be able to delete node, when user's data is private")
138 # valid delete now takes a payload
139 content(nodes(:visible_node).to_xml)
140 delete :delete, :id => current_nodes(:visible_node).id
141 assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
143 # this won't work since the node is already deleted
144 content(nodes(:invisible_node).to_xml)
145 delete :delete, :id => current_nodes(:invisible_node).id
146 assert_require_public_data
148 # this won't work since the node never existed
149 delete :delete, :id => 0
150 assert_require_public_data
152 ## these test whether nodes which are in-use can be deleted:
154 content(nodes(:used_node_1).to_xml)
155 delete :delete, :id => current_nodes(:used_node_1).id
156 assert_require_public_data
157 "shouldn't be able to delete a node used in a way (#{@response.body})"
160 content(nodes(:node_used_by_relationship).to_xml)
161 delete :delete, :id => current_nodes(:node_used_by_relationship).id
162 assert_require_public_data
163 "shouldn't be able to delete a node used in a relation (#{@response.body})"
167 ## now set auth for the public data user
168 basic_authorization(users(:public_user).email, "test");
170 # try to delete with an invalid (closed) changeset
171 content update_changeset(current_nodes(:visible_node).to_xml,
172 changesets(:normal_user_closed_change).id)
173 delete :delete, :id => current_nodes(:visible_node).id
174 assert_response :conflict
176 # try to delete with an invalid (non-existent) changeset
177 content update_changeset(current_nodes(:visible_node).to_xml,0)
178 delete :delete, :id => current_nodes(:visible_node).id
179 assert_response :conflict
181 # valid delete now takes a payload
182 content(nodes(:public_visible_node).to_xml)
183 delete :delete, :id => current_nodes(:public_visible_node).id
184 assert_response :success
186 # valid delete should return the new version number, which should
187 # be greater than the old version number
188 assert @response.body.to_i > current_nodes(:public_visible_node).version,
189 "delete request should return a new version number for node"
191 # this won't work since the node is already deleted
192 content(nodes(:invisible_node).to_xml)
193 delete :delete, :id => current_nodes(:invisible_node).id
194 assert_response :gone
196 # this won't work since the node never existed
197 delete :delete, :id => 0
198 assert_response :not_found
200 ## these test whether nodes which are in-use can be deleted:
202 content(nodes(:used_node_1).to_xml)
203 delete :delete, :id => current_nodes(:used_node_1).id
204 assert_response :precondition_failed,
205 "shouldn't be able to delete a node used in a way (#{@response.body})"
206 assert_equal "Precondition failed: Node 3 is still used by way 1.", @response.body
209 content(nodes(:node_used_by_relationship).to_xml)
210 delete :delete, :id => current_nodes(:node_used_by_relationship).id
211 assert_response :precondition_failed,
212 "shouldn't be able to delete a node used in a relation (#{@response.body})"
213 assert_match /Precondition failed: Node 5 is still used by relation [13]./, @response.body
217 # tests whether the API works and prevents incorrect use while trying
220 ## First test with no user credentials
221 # try and update a node without authorisation
222 # first try to delete node without auth
223 content current_nodes(:visible_node).to_xml
224 put :update, :id => current_nodes(:visible_node).id
225 assert_response :unauthorized
229 ## Second test with the private user
232 basic_authorization(users(:normal_user).email, "test")
234 ## trying to break changesets
236 # try and update in someone else's changeset
237 content update_changeset(current_nodes(:visible_node).to_xml,
238 changesets(:public_user_first_change).id)
239 put :update, :id => current_nodes(:visible_node).id
240 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
242 # try and update in a closed changeset
243 content update_changeset(current_nodes(:visible_node).to_xml,
244 changesets(:normal_user_closed_change).id)
245 put :update, :id => current_nodes(:visible_node).id
246 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
248 # try and update in a non-existant changeset
249 content update_changeset(current_nodes(:visible_node).to_xml, 0)
250 put :update, :id => current_nodes(:visible_node).id
251 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
253 ## try and submit invalid updates
254 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
255 put :update, :id => current_nodes(:visible_node).id
256 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
258 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
259 put :update, :id => current_nodes(:visible_node).id
260 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
262 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
263 put :update, :id => current_nodes(:visible_node).id
264 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
266 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
267 put :update, :id => current_nodes(:visible_node).id
268 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
270 ## finally, produce a good request which should work
271 content current_nodes(:visible_node).to_xml
272 put :update, :id => current_nodes(:visible_node).id
273 assert_require_public_data "should have failed with a forbidden when data isn't public"
278 ## Finally test with the public user
280 # try and update a node without authorisation
281 # first try to delete node without auth
282 content current_nodes(:visible_node).to_xml
283 put :update, :id => current_nodes(:visible_node).id
284 assert_response :forbidden
287 basic_authorization(users(:public_user).email, "test")
289 ## trying to break changesets
291 # try and update in someone else's changeset
292 content update_changeset(current_nodes(:visible_node).to_xml,
293 changesets(:normal_user_first_change).id)
294 put :update, :id => current_nodes(:visible_node).id
295 assert_response :conflict, "update with other user's changeset should be rejected"
297 # try and update in a closed changeset
298 content update_changeset(current_nodes(:visible_node).to_xml,
299 changesets(:normal_user_closed_change).id)
300 put :update, :id => current_nodes(:visible_node).id
301 assert_response :conflict, "update with closed changeset should be rejected"
303 # try and update in a non-existant changeset
304 content update_changeset(current_nodes(:visible_node).to_xml, 0)
305 put :update, :id => current_nodes(:visible_node).id
306 assert_response :conflict, "update with changeset=0 should be rejected"
308 ## try and submit invalid updates
309 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
310 put :update, :id => current_nodes(:visible_node).id
311 assert_response :bad_request, "node at lat=91 should be rejected"
313 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
314 put :update, :id => current_nodes(:visible_node).id
315 assert_response :bad_request, "node at lat=-91 should be rejected"
317 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
318 put :update, :id => current_nodes(:visible_node).id
319 assert_response :bad_request, "node at lon=181 should be rejected"
321 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
322 put :update, :id => current_nodes(:visible_node).id
323 assert_response :bad_request, "node at lon=-181 should be rejected"
325 ## next, attack the versioning
326 current_node_version = current_nodes(:visible_node).version
328 # try and submit a version behind
329 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
330 'version', current_node_version - 1);
331 put :update, :id => current_nodes(:visible_node).id
332 assert_response :conflict, "should have failed on old version number"
334 # try and submit a version ahead
335 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
336 'version', current_node_version + 1);
337 put :update, :id => current_nodes(:visible_node).id
338 assert_response :conflict, "should have failed on skipped version number"
340 # try and submit total crap in the version field
341 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
342 'version', 'p1r4t3s!');
343 put :update, :id => current_nodes(:visible_node).id
344 assert_response :conflict,
345 "should not be able to put 'p1r4at3s!' in the version field"
347 ## finally, produce a good request which should work
348 content current_nodes(:public_visible_node).to_xml
349 put :update, :id => current_nodes(:public_visible_node).id
350 assert_response :success, "a valid update request failed"
354 # test adding tags to a node
355 def test_duplicate_tags
357 basic_authorization(users(:public_user).email, "test")
359 # add an identical tag to the node
360 tag_xml = XML::Node.new("tag")
361 tag_xml['k'] = current_node_tags(:public_v_t1).k
362 tag_xml['v'] = current_node_tags(:public_v_t1).v
364 # add the tag into the existing xml
365 node_xml = current_nodes(:public_visible_node).to_xml
366 node_xml.find("//osm/node").first << tag_xml
370 put :update, :id => current_nodes(:public_visible_node).id
371 assert_response :bad_request,
372 "adding duplicate tags to a node should fail with 'bad request'"
373 assert_equal "Element node/#{current_nodes(:public_visible_node).id} has duplicate tags with key #{current_node_tags(:t1).k}", @response.body
376 # test whether string injection is possible
377 def test_string_injection
378 ## First try with the non-data public user
379 basic_authorization(users(:normal_user).email, "test")
380 changeset_id = changesets(:normal_user_first_change).id
382 # try and put something into a string that the API might
383 # use unquoted and therefore allow code injection...
384 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
385 '<tag k="#{@user.inspect}" v="0"/>' +
388 assert_require_public_data "Shouldn't be able to create with non-public user"
391 ## Then try with the public data user
392 basic_authorization(users(:public_user).email, "test")
393 changeset_id = changesets(:public_user_first_change).id
395 # try and put something into a string that the API might
396 # use unquoted and therefore allow code injection...
397 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
398 '<tag k="#{@user.inspect}" v="0"/>' +
401 assert_response :success
402 nodeid = @response.body
404 # find the node in the database
405 checknode = Node.find(nodeid)
406 assert_not_nil checknode, "node not found in data base after upload"
408 # and grab it using the api
409 get :read, :id => nodeid
410 assert_response :success
411 apinode = Node.from_xml(@response.body)
412 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
414 # check the tags are not corrupted
415 assert_equal checknode.tags, apinode.tags
416 assert apinode.tags.include?('#{@user.inspect}')
419 def basic_authorization(user, pass)
420 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
424 @request.env["RAW_POST_DATA"] = c.to_s
428 # update the changeset_id of a node element
429 def update_changeset(xml, changeset_id)
430 xml_attr_rewrite(xml, 'changeset', changeset_id)
434 # update an attribute in the node element
435 def xml_attr_rewrite(xml, name, value)
436 xml.find("//osm/node").first[name] = value.to_s
443 parser = XML::Parser.string(xml)