1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'node_controller'
4 class NodeControllerTest < ActionController::TestCase
8 # cannot read password from fixture as it is stored as MD5 digest
9 basic_authorization(users(:normal_user).email, "test")
11 # create a node with random lat/lon
12 lat = rand(100)-50 + rand
13 lon = rand(100)-50 + rand
14 # normal user has a changeset open, so we'll use that.
15 changeset = changesets(:normal_user_first_change)
16 # create a minimal xml file
17 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
20 assert_response :success, "node upload did not return success status"
22 # read id of created node and search for it
23 nodeid = @response.body
24 checknode = Node.find(nodeid)
25 assert_not_nil checknode, "uploaded node not found in data base after upload"
27 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
28 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
29 assert_equal changesets(:normal_user_first_change).id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
30 assert_equal true, checknode.visible, "saved node is not visible"
33 def test_create_invalid_xml
35 basic_authorization(users(:normal_user).email, "test")
36 # normal user has a changeset open, so we'll use that.
37 changeset = changesets(:normal_user_first_change)
41 # test that the upload is rejected when no lat is supplied
42 # create a minimal xml file
43 content("<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>")
46 assert_response :bad_request, "node upload did not return bad_request status"
47 assert_equal 'Cannot parse valid node from xml string <node lon="3.23" changeset="1"/>. lat missing', @response.body
49 # test that the upload is rejected when no lon is supplied
50 # create a minimal xml file
51 content("<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>")
54 assert_response :bad_request, "node upload did not return bad_request status"
55 assert_equal 'Cannot parse valid node from xml string <node lat="3.434" changeset="1"/>. lon missing', @response.body
60 # check that a visible node is returned properly
61 get :read, :id => current_nodes(:visible_node).id
62 assert_response :success
64 # check that an invisible node is not returned
65 get :read, :id => current_nodes(:invisible_node).id
68 # check chat a non-existent node is not returned
70 assert_response :not_found
73 # this tests deletion restrictions - basic deletion is tested in the unit
76 # first try to delete node without auth
77 delete :delete, :id => current_nodes(:visible_node).id
78 assert_response :unauthorized
81 basic_authorization(users(:normal_user).email, "test");
83 # try to delete with an invalid (closed) changeset
84 content update_changeset(current_nodes(:visible_node).to_xml,
85 changesets(:normal_user_closed_change).id)
86 delete :delete, :id => current_nodes(:visible_node).id
87 assert_response :conflict
89 # try to delete with an invalid (non-existent) changeset
90 content update_changeset(current_nodes(:visible_node).to_xml,0)
91 delete :delete, :id => current_nodes(:visible_node).id
92 assert_response :conflict
94 # valid delete now takes a payload
95 content(nodes(:visible_node).to_xml)
96 delete :delete, :id => current_nodes(:visible_node).id
97 assert_response :success
99 # valid delete should return the new version number, which should
100 # be greater than the old version number
101 assert @response.body.to_i > current_nodes(:visible_node).version,
102 "delete request should return a new version number for node"
104 # this won't work since the node is already deleted
105 content(nodes(:invisible_node).to_xml)
106 delete :delete, :id => current_nodes(:invisible_node).id
107 assert_response :gone
109 # this won't work since the node never existed
110 delete :delete, :id => 0
111 assert_response :not_found
113 ## these test whether nodes which are in-use can be deleted:
115 content(nodes(:used_node_1).to_xml)
116 delete :delete, :id => current_nodes(:used_node_1).id
117 assert_response :precondition_failed,
118 "shouldn't be able to delete a node used in a way (#{@response.body})"
121 content(nodes(:node_used_by_relationship).to_xml)
122 delete :delete, :id => current_nodes(:node_used_by_relationship).id
123 assert_response :precondition_failed,
124 "shouldn't be able to delete a node used in a relation (#{@response.body})"
128 # tests whether the API works and prevents incorrect use while trying
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
138 basic_authorization(users(:normal_user).email, "test")
140 ## trying to break changesets
142 # try and update in someone else's changeset
143 content update_changeset(current_nodes(:visible_node).to_xml,
144 changesets(:second_user_first_change).id)
145 put :update, :id => current_nodes(:visible_node).id
146 assert_response :conflict, "update with other user's changeset should be rejected"
148 # try and update in a closed changeset
149 content update_changeset(current_nodes(:visible_node).to_xml,
150 changesets(:normal_user_closed_change).id)
151 put :update, :id => current_nodes(:visible_node).id
152 assert_response :conflict, "update with closed changeset should be rejected"
154 # try and update in a non-existant changeset
155 content update_changeset(current_nodes(:visible_node).to_xml, 0)
156 put :update, :id => current_nodes(:visible_node).id
157 assert_response :conflict, "update with changeset=0 should be rejected"
159 ## try and submit invalid updates
160 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
161 put :update, :id => current_nodes(:visible_node).id
162 assert_response :bad_request, "node at lat=91 should be rejected"
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_response :bad_request, "node at lat=-91 should be rejected"
168 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
169 put :update, :id => current_nodes(:visible_node).id
170 assert_response :bad_request, "node at lon=181 should be rejected"
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_response :bad_request, "node at lon=-181 should be rejected"
176 ## next, attack the versioning
177 current_node_version = current_nodes(:visible_node).version
179 # try and submit a version behind
180 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
181 'version', current_node_version - 1);
182 put :update, :id => current_nodes(:visible_node).id
183 assert_response :conflict, "should have failed on old version number"
185 # try and submit a version ahead
186 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
187 'version', current_node_version + 1);
188 put :update, :id => current_nodes(:visible_node).id
189 assert_response :conflict, "should have failed on skipped version number"
191 # try and submit total crap in the version field
192 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
193 'version', 'p1r4t3s!');
194 put :update, :id => current_nodes(:visible_node).id
195 assert_response :conflict,
196 "should not be able to put 'p1r4at3s!' in the version field"
198 ## finally, produce a good request which should work
199 content current_nodes(:visible_node).to_xml
200 put :update, :id => current_nodes(:visible_node).id
201 assert_response :success, "a valid update request failed"
205 # test adding tags to a node
206 def test_duplicate_tags
208 basic_authorization(users(:normal_user).email, "test")
210 # add an identical tag to the node
211 tag_xml = XML::Node.new("tag")
212 tag_xml['k'] = current_node_tags(:t1).k
213 tag_xml['v'] = current_node_tags(:t1).v
215 # add the tag into the existing xml
216 node_xml = current_nodes(:visible_node).to_xml
217 node_xml.find("//osm/node").first << tag_xml
221 put :update, :id => current_nodes(:visible_node).id
222 assert_response :bad_request,
223 "adding duplicate tags to a node should fail with 'bad request'"
224 assert_equal "Element node/#{current_nodes(:visible_node).id} has duplicate tags with key #{current_node_tags(:t1).k}.", @response.body
227 # test whether string injection is possible
228 def test_string_injection
229 basic_authorization(users(:normal_user).email, "test")
230 changeset_id = changesets(:normal_user_first_change).id
232 # try and put something into a string that the API might
233 # use unquoted and therefore allow code injection...
234 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
235 '<tag k="#{@user.inspect}" v="0"/>' +
238 assert_response :success
239 nodeid = @response.body
241 # find the node in the database
242 checknode = Node.find(nodeid)
243 assert_not_nil checknode, "node not found in data base after upload"
245 # and grab it using the api
246 get :read, :id => nodeid
247 assert_response :success
248 apinode = Node.from_xml(@response.body)
249 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
251 # check the tags are not corrupted
252 assert_equal checknode.tags, apinode.tags
253 assert apinode.tags.include?('#{@user.inspect}')
256 def basic_authorization(user, pass)
257 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
261 @request.env["RAW_POST_DATA"] = c.to_s
265 # update the changeset_id of a node element
266 def update_changeset(xml, changeset_id)
267 xml_attr_rewrite(xml, 'changeset', changeset_id)
271 # update an attribute in the node element
272 def xml_attr_rewrite(xml, name, value)
273 xml.find("//osm/node").first[name] = value.to_s
280 parser = XML::Parser.new