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"
34 # check that a visible node is returned properly
35 get :read, :id => current_nodes(:visible_node).id
36 assert_response :success
38 # check that an invisible node is not returned
39 get :read, :id => current_nodes(:invisible_node).id
42 # check chat a non-existent node is not returned
44 assert_response :not_found
47 # this tests deletion restrictions - basic deletion is tested in the unit
50 # first try to delete node without auth
51 delete :delete, :id => current_nodes(:visible_node).id
52 assert_response :unauthorized
55 basic_authorization(users(:normal_user).email, "test");
57 # try to delete with an invalid (closed) changeset
58 content update_changeset(current_nodes(:visible_node).to_xml,
59 changesets(:normal_user_closed_change).id)
60 delete :delete, :id => current_nodes(:visible_node).id
61 assert_response :conflict
63 # try to delete with an invalid (non-existent) changeset
64 content update_changeset(current_nodes(:visible_node).to_xml,0)
65 delete :delete, :id => current_nodes(:visible_node).id
66 assert_response :conflict
68 # valid delete now takes a payload
69 content(nodes(:visible_node).to_xml)
70 delete :delete, :id => current_nodes(:visible_node).id
71 assert_response :success
73 # valid delete should return the new version number, which should
74 # be greater than the old version number
75 assert @response.body.to_i > current_nodes(:visible_node).version,
76 "delete request should return a new version number for node"
78 # this won't work since the node is already deleted
79 content(nodes(:invisible_node).to_xml)
80 delete :delete, :id => current_nodes(:invisible_node).id
83 # this won't work since the node never existed
84 delete :delete, :id => 0
85 assert_response :not_found
87 ## these test whether nodes which are in-use can be deleted:
89 content(nodes(:used_node_1).to_xml)
90 delete :delete, :id => current_nodes(:used_node_1).id
91 assert_response :precondition_failed,
92 "shouldn't be able to delete a node used in a way (#{@response.body})"
95 content(nodes(:node_used_by_relationship).to_xml)
96 delete :delete, :id => current_nodes(:node_used_by_relationship).id
97 assert_response :precondition_failed,
98 "shouldn't be able to delete a node used in a relation (#{@response.body})"
102 # tests whether the API works and prevents incorrect use while trying
105 # try and update a node without authorisation
106 # first try to delete node without auth
107 content current_nodes(:visible_node).to_xml
108 put :update, :id => current_nodes(:visible_node).id
109 assert_response :unauthorized
112 basic_authorization(users(:normal_user).email, "test")
114 ## trying to break changesets
116 # try and update in someone else's changeset
117 content update_changeset(current_nodes(:visible_node).to_xml,
118 changesets(:second_user_first_change).id)
119 put :update, :id => current_nodes(:visible_node).id
120 assert_response :conflict, "update with other user's changeset should be rejected"
122 # try and update in a closed changeset
123 content update_changeset(current_nodes(:visible_node).to_xml,
124 changesets(:normal_user_closed_change).id)
125 put :update, :id => current_nodes(:visible_node).id
126 assert_response :conflict, "update with closed changeset should be rejected"
128 # try and update in a non-existant changeset
129 content update_changeset(current_nodes(:visible_node).to_xml, 0)
130 put :update, :id => current_nodes(:visible_node).id
131 assert_response :conflict, "update with changeset=0 should be rejected"
133 ## try and submit invalid updates
134 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
135 put :update, :id => current_nodes(:visible_node).id
136 assert_response :bad_request, "node at lat=91 should be rejected"
138 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
139 put :update, :id => current_nodes(:visible_node).id
140 assert_response :bad_request, "node at lat=-91 should be rejected"
142 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
143 put :update, :id => current_nodes(:visible_node).id
144 assert_response :bad_request, "node at lon=181 should be rejected"
146 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
147 put :update, :id => current_nodes(:visible_node).id
148 assert_response :bad_request, "node at lon=-181 should be rejected"
150 ## next, attack the versioning
151 current_node_version = current_nodes(:visible_node).version
153 # try and submit a version behind
154 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
155 'version', current_node_version - 1);
156 put :update, :id => current_nodes(:visible_node).id
157 assert_response :conflict, "should have failed on old version number"
159 # try and submit a version ahead
160 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
161 'version', current_node_version + 1);
162 put :update, :id => current_nodes(:visible_node).id
163 assert_response :conflict, "should have failed on skipped version number"
165 # try and submit total crap in the version field
166 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
167 'version', 'p1r4t3s!');
168 put :update, :id => current_nodes(:visible_node).id
169 assert_response :conflict,
170 "should not be able to put 'p1r4at3s!' in the version field"
172 ## finally, produce a good request which should work
173 content current_nodes(:visible_node).to_xml
174 put :update, :id => current_nodes(:visible_node).id
175 assert_response :success, "a valid update request failed"
179 # test adding tags to a node
180 def test_duplicate_tags
182 basic_authorization(users(:normal_user).email, "test")
184 # add an identical tag to the node
185 tag_xml = XML::Node.new("tag")
186 tag_xml['k'] = current_node_tags(:t1).k
187 tag_xml['v'] = current_node_tags(:t1).v
189 # add the tag into the existing xml
190 node_xml = current_nodes(:visible_node).to_xml
191 node_xml.find("//osm/node").first << tag_xml
195 put :update, :id => current_nodes(:visible_node).id
196 assert_response :bad_request,
197 "adding duplicate tags to a node should fail with 'bad request'"
200 # test whether string injection is possible
201 def test_string_injection
202 basic_authorization(users(:normal_user).email, "test")
203 changeset_id = changesets(:normal_user_first_change).id
205 # try and put something into a string that the API might
206 # use unquoted and therefore allow code injection...
207 content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
208 '<tag k="#{@user.inspect}" v="0"/>' +
211 assert_response :success
212 nodeid = @response.body
214 # find the node in the database
215 checknode = Node.find(nodeid)
216 assert_not_nil checknode, "node not found in data base after upload"
218 # and grab it using the api
219 get :read, :id => nodeid
220 assert_response :success
221 apinode = Node.from_xml(@response.body)
222 assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
224 # check the tags are not corrupted
225 assert_equal checknode.tags, apinode.tags
226 assert apinode.tags.include?('#{@user.inspect}')
229 def basic_authorization(user, pass)
230 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
234 @request.env["RAW_POST_DATA"] = c.to_s
238 # update the changeset_id of a node element
239 def update_changeset(xml, changeset_id)
240 xml_attr_rewrite(xml, 'changeset', changeset_id)
244 # update an attribute in the node element
245 def xml_attr_rewrite(xml, name, value)
246 xml.find("//osm/node").first[name] = value.to_s
253 parser = XML::Parser.new