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 basic_authorization(users(:normal_user).email, "test")
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>")
19 assert_response :success, "node upload did not return success status"
21 # read id of created node and search for it
22 nodeid = @response.body
23 checknode = Node.find(nodeid)
24 assert_not_nil checknode, "uploaded node not found in data base after upload"
26 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
27 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
28 assert_equal changesets(:normal_user_first_change).id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
29 assert_equal true, checknode.visible, "saved node is not visible"
32 def test_create_invalid_xml
34 basic_authorization(users(:normal_user).email, "test")
35 # normal user has a changeset open, so we'll use that.
36 changeset = changesets(:normal_user_first_change)
40 # test that the upload is rejected when no lat is supplied
41 # create a minimal xml file
42 content("<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>")
45 assert_response :bad_request, "node upload did not return bad_request status"
46 assert_equal 'Cannot parse valid node from xml string <node lon="3.23" changeset="1"/>. lat missing', @response.body
48 # test that the upload is rejected when no lon is supplied
49 # create a minimal xml file
50 content("<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>")
53 assert_response :bad_request, "node upload did not return bad_request status"
54 assert_equal 'Cannot parse valid node from xml string <node lat="3.434" changeset="1"/>. lon missing', @response.body
59 # check that a visible node is returned properly
60 get :read, :id => current_nodes(:visible_node).id
61 assert_response :success
63 # check that an invisible node is not returned
64 get :read, :id => current_nodes(:invisible_node).id
67 # check chat a non-existent node is not returned
69 assert_response :not_found
72 # this tests deletion restrictions - basic deletion is tested in the unit
75 # first try to delete node without auth
76 delete :delete, :id => current_nodes(:visible_node).id
77 assert_response :unauthorized
80 basic_authorization(users(:normal_user).email, "test");
82 # try to delete with an invalid (closed) changeset
83 content update_changeset(current_nodes(:visible_node).to_xml,
84 changesets(:normal_user_closed_change).id)
85 delete :delete, :id => current_nodes(:visible_node).id
86 assert_response :conflict
88 # try to delete with an invalid (non-existent) changeset
89 content update_changeset(current_nodes(:visible_node).to_xml,0)
90 delete :delete, :id => current_nodes(:visible_node).id
91 assert_response :conflict
93 # valid delete now takes a payload
94 content(nodes(:visible_node).to_xml)
95 delete :delete, :id => current_nodes(:visible_node).id
96 assert_response :success
98 # valid delete should return the new version number, which should
99 # be greater than the old version number
100 assert @response.body.to_i > current_nodes(:visible_node).version,
101 "delete request should return a new version number for node"
103 # this won't work since the node is already deleted
104 content(nodes(:invisible_node).to_xml)
105 delete :delete, :id => current_nodes(:invisible_node).id
106 assert_response :gone
108 # this won't work since the node never existed
109 delete :delete, :id => 0
110 assert_response :not_found
112 ## these test whether nodes which are in-use can be deleted:
114 content(nodes(:used_node_1).to_xml)
115 delete :delete, :id => current_nodes(:used_node_1).id
116 assert_response :precondition_failed,
117 "shouldn't be able to delete a node used in a way (#{@response.body})"
120 content(nodes(:node_used_by_relationship).to_xml)
121 delete :delete, :id => current_nodes(:node_used_by_relationship).id
122 assert_response :precondition_failed,
123 "shouldn't be able to delete a node used in a relation (#{@response.body})"
127 # tests whether the API works and prevents incorrect use while trying
130 ## First test with no user credentials
131 # try and update a node without authorisation
132 # first try to delete node without auth
133 content current_nodes(:visible_node).to_xml
134 put :update, :id => current_nodes(:visible_node).id
135 assert_response :unauthorized
139 ## Second test with the private user
142 basic_authorization(users(:normal_user).email, "test")
144 ## trying to break changesets
146 # try and update in someone else's changeset
147 content update_changeset(current_nodes(:visible_node).to_xml,
148 changesets(:public_user_first_change).id)
149 put :update, :id => current_nodes(:visible_node).id
150 assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
152 # try and update in a closed changeset
153 content update_changeset(current_nodes(:visible_node).to_xml,
154 changesets(:normal_user_closed_change).id)
155 put :update, :id => current_nodes(:visible_node).id
156 assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
158 # try and update in a non-existant changeset
159 content update_changeset(current_nodes(:visible_node).to_xml, 0)
160 put :update, :id => current_nodes(:visible_node).id
161 assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
163 ## try and submit invalid updates
164 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
165 put :update, :id => current_nodes(:visible_node).id
166 assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
168 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
169 put :update, :id => current_nodes(:visible_node).id
170 assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
172 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
173 put :update, :id => current_nodes(:visible_node).id
174 assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
176 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
177 put :update, :id => current_nodes(:visible_node).id
178 assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
180 ## finally, produce a good request which should work
181 content current_nodes(:visible_node).to_xml
182 put :update, :id => current_nodes(:visible_node).id
183 assert_require_public_data "should have failed with a forbidden when data isn't public"
188 ## Finally test with the public user
190 # try and update a node without authorisation
191 # first try to delete node without auth
192 content current_nodes(:visible_node).to_xml
193 put :update, :id => current_nodes(:visible_node).id
194 assert_response :forbidden
197 basic_authorization(users(:public_user).email, "test")
199 ## trying to break changesets
201 # try and update in someone else's changeset
202 content update_changeset(current_nodes(:visible_node).to_xml,
203 changesets(:normal_user_first_change).id)
204 put :update, :id => current_nodes(:visible_node).id
205 assert_response :conflict, "update with other user's changeset should be rejected"
207 # try and update in a closed changeset
208 content update_changeset(current_nodes(:visible_node).to_xml,
209 changesets(:normal_user_closed_change).id)
210 put :update, :id => current_nodes(:visible_node).id
211 assert_response :conflict, "update with closed changeset should be rejected"
213 # try and update in a non-existant changeset
214 content update_changeset(current_nodes(:visible_node).to_xml, 0)
215 put :update, :id => current_nodes(:visible_node).id
216 assert_response :conflict, "update with changeset=0 should be rejected"
218 ## try and submit invalid updates
219 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
220 put :update, :id => current_nodes(:visible_node).id
221 assert_response :bad_request, "node at lat=91 should be rejected"
223 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
224 put :update, :id => current_nodes(:visible_node).id
225 assert_response :bad_request, "node at lat=-91 should be rejected"
227 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
228 put :update, :id => current_nodes(:visible_node).id
229 assert_response :bad_request, "node at lon=181 should be rejected"
231 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
232 put :update, :id => current_nodes(:visible_node).id
233 assert_response :bad_request, "node at lon=-181 should be rejected"
235 ## next, attack the versioning
236 current_node_version = current_nodes(:visible_node).version
238 # try and submit a version behind
239 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
240 'version', current_node_version - 1);
241 put :update, :id => current_nodes(:visible_node).id
242 assert_response :conflict, "should have failed on old version number"
244 # try and submit a version ahead
245 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
246 'version', current_node_version + 1);
247 put :update, :id => current_nodes(:visible_node).id
248 assert_response :conflict, "should have failed on skipped version number"
250 # try and submit total crap in the version field
251 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
252 'version', 'p1r4t3s!');
253 put :update, :id => current_nodes(:visible_node).id
254 assert_response :conflict,
255 "should not be able to put 'p1r4at3s!' in the version field"
257 ## finally, produce a good request which should work
258 content current_nodes(:public_visible_node).to_xml
259 put :update, :id => current_nodes(:public_visible_node).id
260 assert_response :success, "a valid update request failed"
264 # test adding tags to a node
265 def test_duplicate_tags
267 basic_authorization(users(:normal_user).email, "test")
269 # add an identical tag to the node
270 tag_xml = XML::Node.new("tag")
271 tag_xml['k'] = current_node_tags(:t1).k
272 tag_xml['v'] = current_node_tags(:t1).v
274 # add the tag into the existing xml
275 node_xml = current_nodes(:visible_node).to_xml
276 node_xml.find("//osm/node").first << tag_xml
280 put :update, :id => current_nodes(:visible_node).id
281 assert_response :bad_request,
282 "adding duplicate tags to a node should fail with 'bad request'"
283 assert_equal "Element node/#{current_nodes(:visible_node).id} has duplicate tags with key #{current_node_tags(:t1).k}.", @response.body
286 # test whether string injection is possible
287 def test_string_injection
288 basic_authorization(users(:normal_user).email, "test")
289 changeset_id = changesets(:normal_user_first_change).id
291 # try and put something into a string that the API might
292 # use unquoted and therefore allow code injection...
293 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
294 '<tag k="#{@user.inspect}" v="0"/>' +
297 assert_response :success
298 nodeid = @response.body
300 # find the node in the database
301 checknode = Node.find(nodeid)
302 assert_not_nil checknode, "node not found in data base after upload"
304 # and grab it using the api
305 get :read, :id => nodeid
306 assert_response :success
307 apinode = Node.from_xml(@response.body)
308 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
310 # check the tags are not corrupted
311 assert_equal checknode.tags, apinode.tags
312 assert apinode.tags.include?('#{@user.inspect}')
315 def basic_authorization(user, pass)
316 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
320 @request.env["RAW_POST_DATA"] = c.to_s
324 # update the changeset_id of a node element
325 def update_changeset(xml, changeset_id)
326 xml_attr_rewrite(xml, 'changeset', changeset_id)
330 # update an attribute in the node element
331 def xml_attr_rewrite(xml, name, value)
332 xml.find("//osm/node").first[name] = value.to_s
339 parser = XML::Parser.string(xml)