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 # try and update a node without authorisation
131 # first try to delete node without auth
132 content current_nodes(:visible_node).to_xml
133 put :update, :id => current_nodes(:visible_node).id
134 assert_response :unauthorized
137 basic_authorization(users(:normal_user).email, "test")
139 ## trying to break changesets
141 # try and update in someone else's changeset
142 content update_changeset(current_nodes(:visible_node).to_xml,
143 changesets(:second_user_first_change).id)
144 put :update, :id => current_nodes(:visible_node).id
145 assert_response :conflict, "update with other user's changeset should be rejected"
147 # try and update in a closed changeset
148 content update_changeset(current_nodes(:visible_node).to_xml,
149 changesets(:normal_user_closed_change).id)
150 put :update, :id => current_nodes(:visible_node).id
151 assert_response :conflict, "update with closed changeset should be rejected"
153 # try and update in a non-existant changeset
154 content update_changeset(current_nodes(:visible_node).to_xml, 0)
155 put :update, :id => current_nodes(:visible_node).id
156 assert_response :conflict, "update with changeset=0 should be rejected"
158 ## try and submit invalid updates
159 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
160 put :update, :id => current_nodes(:visible_node).id
161 assert_response :bad_request, "node at lat=91 should be rejected"
163 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
164 put :update, :id => current_nodes(:visible_node).id
165 assert_response :bad_request, "node at lat=-91 should be rejected"
167 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
168 put :update, :id => current_nodes(:visible_node).id
169 assert_response :bad_request, "node at lon=181 should be rejected"
171 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
172 put :update, :id => current_nodes(:visible_node).id
173 assert_response :bad_request, "node at lon=-181 should be rejected"
175 ## next, attack the versioning
176 current_node_version = current_nodes(:visible_node).version
178 # try and submit a version behind
179 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
180 'version', current_node_version - 1);
181 put :update, :id => current_nodes(:visible_node).id
182 assert_response :conflict, "should have failed on old version number"
184 # try and submit a version ahead
185 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
186 'version', current_node_version + 1);
187 put :update, :id => current_nodes(:visible_node).id
188 assert_response :conflict, "should have failed on skipped version number"
190 # try and submit total crap in the version field
191 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
192 'version', 'p1r4t3s!');
193 put :update, :id => current_nodes(:visible_node).id
194 assert_response :conflict,
195 "should not be able to put 'p1r4at3s!' in the version field"
197 ## finally, produce a good request which should work
198 content current_nodes(:visible_node).to_xml
199 put :update, :id => current_nodes(:visible_node).id
200 assert_response :success, "a valid update request failed"
204 # test adding tags to a node
205 def test_duplicate_tags
207 basic_authorization(users(:normal_user).email, "test")
209 # add an identical tag to the node
210 tag_xml = XML::Node.new("tag")
211 tag_xml['k'] = current_node_tags(:t1).k
212 tag_xml['v'] = current_node_tags(:t1).v
214 # add the tag into the existing xml
215 node_xml = current_nodes(:visible_node).to_xml
216 node_xml.find("//osm/node").first << tag_xml
220 put :update, :id => current_nodes(:visible_node).id
221 assert_response :bad_request,
222 "adding duplicate tags to a node should fail with 'bad request'"
223 assert_equal "Element node/#{current_nodes(:visible_node).id} has duplicate tags with key #{current_node_tags(:t1).k}.", @response.body
226 # test whether string injection is possible
227 def test_string_injection
228 basic_authorization(users(:normal_user).email, "test")
229 changeset_id = changesets(:normal_user_first_change).id
231 # try and put something into a string that the API might
232 # use unquoted and therefore allow code injection...
233 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
234 '<tag k="#{@user.inspect}" v="0"/>' +
237 assert_response :success
238 nodeid = @response.body
240 # find the node in the database
241 checknode = Node.find(nodeid)
242 assert_not_nil checknode, "node not found in data base after upload"
244 # and grab it using the api
245 get :read, :id => nodeid
246 assert_response :success
247 apinode = Node.from_xml(@response.body)
248 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
250 # check the tags are not corrupted
251 assert_equal checknode.tags, apinode.tags
252 assert apinode.tags.include?('#{@user.inspect}')
255 def basic_authorization(user, pass)
256 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
260 @request.env["RAW_POST_DATA"] = c.to_s
264 # update the changeset_id of a node element
265 def update_changeset(xml, changeset_id)
266 xml_attr_rewrite(xml, 'changeset', changeset_id)
270 # update an attribute in the node element
271 def xml_attr_rewrite(xml, name, value)
272 xml.find("//osm/node").first[name] = value.to_s
279 parser = XML::Parser.new