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 # this won't work since the node is already deleted
83 content(nodes(:invisible_node).to_xml)
84 delete :delete, :id => current_nodes(:invisible_node).id
87 # this won't work since the node never existed
88 delete :delete, :id => 0
89 assert_response :not_found
91 ## these test whether nodes which are in-use can be deleted:
93 content(nodes(:used_node_1).to_xml)
94 delete :delete, :id => current_nodes(:used_node_1).id
95 assert_response :precondition_failed
98 content(nodes(:node_used_by_relationship).to_xml)
99 delete :delete, :id => current_nodes(:node_used_by_relationship).id
100 assert_response :precondition_failed
104 # tests whether the API works and prevents incorrect use while trying
107 # try and update a node without authorisation
108 # first try to delete node without auth
109 content current_nodes(:visible_node).to_xml
110 put :update, :id => current_nodes(:visible_node).id
111 assert_response :unauthorized
114 basic_authorization(users(:normal_user).email, "test")
116 ## trying to break changesets
118 # try and update in someone else's changeset
119 content update_changeset(current_nodes(:visible_node).to_xml,
120 changesets(:second_user_first_change).id)
121 put :update, :id => current_nodes(:visible_node).id
122 assert_response :conflict, "update with other user's changeset should be rejected"
124 # try and update in a closed changeset
125 content update_changeset(current_nodes(:visible_node).to_xml,
126 changesets(:normal_user_closed_change).id)
127 put :update, :id => current_nodes(:visible_node).id
128 assert_response :conflict, "update with closed changeset should be rejected"
130 # try and update in a non-existant changeset
131 content update_changeset(current_nodes(:visible_node).to_xml, 0)
132 put :update, :id => current_nodes(:visible_node).id
133 assert_response :conflict, "update with changeset=0 should be rejected"
135 ## try and submit invalid updates
136 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
137 put :update, :id => current_nodes(:visible_node).id
138 assert_response :bad_request, "node at lat=91 should be rejected"
140 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
141 put :update, :id => current_nodes(:visible_node).id
142 assert_response :bad_request, "node at lat=-91 should be rejected"
144 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
145 put :update, :id => current_nodes(:visible_node).id
146 assert_response :bad_request, "node at lon=181 should be rejected"
148 content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
149 put :update, :id => current_nodes(:visible_node).id
150 assert_response :bad_request, "node at lon=-181 should be rejected"
152 ## next, attack the versioning
153 current_node_version = current_nodes(:visible_node).version
155 # try and submit a version behind
156 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
157 'version', current_node_version - 1);
158 put :update, :id => current_nodes(:visible_node).id
159 assert_response :conflict, "should have failed on old version number"
161 # try and submit a version ahead
162 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
163 'version', current_node_version + 1);
164 put :update, :id => current_nodes(:visible_node).id
165 assert_response :conflict, "should have failed on skipped version number"
167 # try and submit total crap in the version field
168 content xml_attr_rewrite(current_nodes(:visible_node).to_xml,
169 'version', 'p1r4t3s!');
170 put :update, :id => current_nodes(:visible_node).id
171 assert_response :conflict,
172 "should not be able to put 'p1r4at3s!' in the version field"
174 ## finally, produce a good request which should work
175 content current_nodes(:visible_node).to_xml
176 put :update, :id => current_nodes(:visible_node).id
177 assert_response :success, "a valid update request failed"
181 # test adding tags to a node
182 def test_duplicate_tags
184 basic_authorization(users(:normal_user).email, "test")
186 # add an identical tag to the node
187 tag_xml = XML::Node.new("tag")
188 tag_xml['k'] = current_node_tags(:t1).k
189 tag_xml['v'] = current_node_tags(:t1).v
191 # add the tag into the existing xml
192 node_xml = current_nodes(:visible_node).to_xml
193 node_xml.find("//osm/node").first << tag_xml
197 put :update, :id => current_nodes(:visible_node).id
198 assert_response :bad_request,
199 "adding duplicate tags to a node should fail with 'bad request'"
202 def basic_authorization(user, pass)
203 @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
207 @request.env["RAW_POST_DATA"] = c.to_s
211 # update the changeset_id of a node element
212 def update_changeset(xml, changeset_id)
213 xml_attr_rewrite(xml, 'changeset', changeset_id)
217 # update an attribute in the node element
218 def xml_attr_rewrite(xml, name, value)
219 xml.find("//osm/node").first[name] = value.to_s
226 parser = XML::Parser.new