1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'node_controller'
4 # Re-raise errors caught by the controller.
5 class NodeController; def rescue_action(e) raise e end; end
7 class NodeControllerTest < Test::Unit::TestCase
11 @controller = NodeController.new
12 @request = ActionController::TestRequest.new
13 @response = ActionController::TestResponse.new
17 # cannot read password from fixture as it is stored as MD5 digest
18 basic_authorization(users(:normal_user).email, "test");
20 # create a node with random lat/lon
21 lat = rand(100)-50 + rand
22 lon = rand(100)-50 + rand
23 # normal user has a changeset open, so we'll use that.
24 changeset = changesets(:normal_user_first_change)
25 # create a minimal xml file
26 content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
29 assert_response :success, "node upload did not return success status"
31 # read id of created node and search for it
32 nodeid = @response.body
33 checknode = Node.find(nodeid)
34 assert_not_nil checknode, "uploaded node not found in data base after upload"
36 assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
37 assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
38 assert_equal changesets(:normal_user_first_change).id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
39 assert_equal true, checknode.visible, "saved node is not visible"
43 # check that a visible node is returned properly
44 get :read, :id => current_nodes(:visible_node).id
45 assert_response :success
47 # check that an invisible node is not returned
48 get :read, :id => current_nodes(:invisible_node).id
51 # check chat a non-existent node is not returned
53 assert_response :not_found
56 # this tests deletion restrictions - basic deletion is tested in the unit
59 # first try to delete node without auth
60 delete :delete, :id => current_nodes(:visible_node).id
61 assert_response :unauthorized
64 basic_authorization(users(:normal_user).email, "test");
66 # try to delete with an invalid (closed) changeset
67 content update_changeset(current_nodes(:visible_node).to_xml,
68 changesets(:normal_user_closed_change).id)
69 delete :delete, :id => current_nodes(:visible_node).id
70 assert_response :conflict
72 # try to delete with an invalid (non-existent) changeset
73 content update_changeset(current_nodes(:visible_node).to_xml,0)
74 delete :delete, :id => current_nodes(:visible_node).id
75 assert_response :conflict
77 # valid delete now takes a payload
78 content(nodes(:visible_node).to_xml)
79 delete :delete, :id => current_nodes(:visible_node).id
80 assert_response :success
82 # valid delete should return the new version number, which should
83 # be greater than the old version number
84 assert @response.body.to_i > current_nodes(:visible_node).version,
85 "delete request should return a new version number for node"
87 # this won't work since the node is already deleted
88 content(nodes(:invisible_node).to_xml)
89 delete :delete, :id => current_nodes(:invisible_node).id
92 # this won't work since the node never existed
93 delete :delete, :id => 0
94 assert_response :not_found
96 ## these test whether nodes which are in-use can be deleted:
98 content(nodes(:used_node_1).to_xml)
99 delete :delete, :id => current_nodes(:used_node_1).id
100 assert_response :precondition_failed,
101 "shouldn't be able to delete a node used in a way (#{@response.body})"
104 content(nodes(:node_used_by_relationship).to_xml)
105 delete :delete, :id => current_nodes(:node_used_by_relationship).id
106 assert_response :precondition_failed,
107 "shouldn't be able to delete a node used in a relation (#{@response.body})"
111 # tests whether the API works and prevents incorrect use while trying
114 # try and update a node without authorisation
115 # first try to delete node without auth
116 content current_nodes(:visible_node).to_xml
117 put :update, :id => current_nodes(:visible_node).id
118 assert_response :unauthorized
121 basic_authorization(users(:normal_user).email, "test")
123 ## trying to break changesets
125 # try and update in someone else's changeset
126 content update_changeset(current_nodes(:visible_node).to_xml,
127 changesets(:second_user_first_change).id)
128 put :update, :id => current_nodes(:visible_node).id
129 assert_response :conflict, "update with other user's changeset should be rejected"
131 # try and update in a closed changeset
132 content update_changeset(current_nodes(:visible_node).to_xml,
133 changesets(:normal_user_closed_change).id)
134 put :update, :id => current_nodes(:visible_node).id
135 assert_response :conflict, "update with closed changeset should be rejected"
137 # try and update in a non-existant changeset
138 content update_changeset(current_nodes(:visible_node).to_xml, 0)
139 put :update, :id => current_nodes(:visible_node).id
140 assert_response :conflict, "update with changeset=0 should be rejected"
142 ## try and submit invalid updates
143 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
144 put :update, :id => current_nodes(:visible_node).id
145 assert_response :bad_request, "node at lat=91 should be rejected"
147 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
148 put :update, :id => current_nodes(:visible_node).id
149 assert_response :bad_request, "node at lat=-91 should be rejected"
151 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
152 put :update, :id => current_nodes(:visible_node).id
153 assert_response :bad_request, "node at lon=181 should be rejected"
155 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
156 put :update, :id => current_nodes(:visible_node).id
157 assert_response :bad_request, "node at lon=-181 should be rejected"
159 ## next, attack the versioning
160 current_node_version = current_nodes(:visible_node).version
162 # try and submit a version behind
163 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
164 'version', current_node_version - 1);
165 put :update, :id => current_nodes(:visible_node).id
166 assert_response :conflict, "should have failed on old version number"
168 # try and submit a version ahead
169 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
170 'version', current_node_version + 1);
171 put :update, :id => current_nodes(:visible_node).id
172 assert_response :conflict, "should have failed on skipped version number"
174 # try and submit total crap in the version field
175 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
176 'version', 'p1r4t3s!');
177 put :update, :id => current_nodes(:visible_node).id
178 assert_response :conflict,
179 "should not be able to put 'p1r4at3s!' in the version field"
181 ## finally, produce a good request which should work
182 content current_nodes(:visible_node).to_xml
183 put :update, :id => current_nodes(:visible_node).id
184 assert_response :success, "a valid update request failed"
188 # test adding tags to a node
189 def test_duplicate_tags
191 basic_authorization(users(:normal_user).email, "test")
193 # add an identical tag to the node
194 tag_xml = XML::Node.new("tag")
195 tag_xml['k'] = current_node_tags(:t1).k
196 tag_xml['v'] = current_node_tags(:t1).v
198 # add the tag into the existing xml
199 node_xml = current_nodes(:visible_node).to_xml
200 node_xml.find("//osm/node").first << tag_xml
204 put :update, :id => current_nodes(:visible_node).id
205 assert_response :bad_request,
206 "adding duplicate tags to a node should fail with 'bad request'"
209 # test whether string injection is possible
210 def test_string_injection
211 basic_authorization(users(:normal_user).email, "test")
212 changeset_id = changesets(:normal_user_first_change).id
214 # try and put something into a string that the API might
215 # use unquoted and therefore allow code injection...
216 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
217 '<tag k="#{@user.inspect}" v="0"/>' +
220 assert_response :success
221 nodeid = @response.body
223 # find the node in the database
224 checknode = Node.find(nodeid)
225 assert_not_nil checknode, "node not found in data base after upload"
227 # and grab it using the api
228 get :read, :id => nodeid
229 assert_response :success
230 apinode = Node.from_xml(@response.body)
231 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
233 # check the tags are not corrupted
234 assert_equal checknode.tags, apinode.tags
235 assert apinode.tags.include?('#{@user.inspect}')
238 def basic_authorization(user, pass)
239 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
243 @request.env["RAW_POST_DATA"] = c.to_s
247 # update the changeset_id of a node element
248 def update_changeset(xml, changeset_id)
249 xml_attr_rewrite(xml, 'changeset', changeset_id)
253 # update an attribute in the node element
254 def xml_attr_rewrite(xml, name, value)
255 xml.find("//osm/node").first[name] = value.to_s
262 parser = XML::Parser.new