]> git.openstreetmap.org Git - rails.git/blob - test/functional/node_controller_test.rb
Prepare for Rails 2.1+, as we will no longer be able to use a symbol in the set_fixtu...
[rails.git] / test / functional / node_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2 require 'node_controller'
3
4 # Re-raise errors caught by the controller.
5 class NodeController; def rescue_action(e) raise e end; end
6
7 class NodeControllerTest < Test::Unit::TestCase
8   api_fixtures
9
10   def setup
11     @controller = NodeController.new
12     @request    = ActionController::TestRequest.new
13     @response   = ActionController::TestResponse.new
14   end
15
16   def test_create
17     # cannot read password from fixture as it is stored as MD5 digest
18     basic_authorization(users(:normal_user).email, "test");
19     
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>")
27     put :create
28     # hope for success
29     assert_response :success, "node upload did not return success status"
30
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"
35     # compare values
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"
40   end
41
42   def test_read
43     # check that a visible node is returned properly
44     get :read, :id => current_nodes(:visible_node).id
45     assert_response :success
46
47     # check that an invisible node is not returned
48     get :read, :id => current_nodes(:invisible_node).id
49     assert_response :gone
50
51     # check chat a non-existent node is not returned
52     get :read, :id => 0
53     assert_response :not_found
54   end
55
56   # this tests deletion restrictions - basic deletion is tested in the unit
57   # tests for node!
58   def test_delete
59     # first try to delete node without auth
60     delete :delete, :id => current_nodes(:visible_node).id
61     assert_response :unauthorized
62
63     # now set auth
64     basic_authorization(users(:normal_user).email, "test");  
65
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
71
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
76
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
81
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"
86
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
90     assert_response :gone
91
92     # this won't work since the node never existed
93     delete :delete, :id => 0
94     assert_response :not_found
95
96     ## these test whether nodes which are in-use can be deleted:
97     # in a way...
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})"
102
103     # in a relation...
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})"
108   end
109
110   ##
111   # tests whether the API works and prevents incorrect use while trying
112   # to update nodes.
113   def test_update
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
119     
120     # setup auth
121     basic_authorization(users(:normal_user).email, "test")
122
123     ## trying to break changesets
124
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"
130
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"
136
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"
141
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"
146
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"
150     
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"
154
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"
158
159     ## next, attack the versioning
160     current_node_version = current_nodes(:visible_node).version
161
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"
167     
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"
173
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"
180     
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"
185   end
186
187   ##
188   # test adding tags to a node
189   def test_duplicate_tags
190     # setup auth
191     basic_authorization(users(:normal_user).email, "test")
192
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
197
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
201
202     # try and upload it
203     content node_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'"
207   end
208
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
213
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"/>' +
218       '</node></osm>'
219     put :create
220     assert_response :success
221     nodeid = @response.body
222
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"
226     
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"
232     
233     # check the tags are not corrupted
234     assert_equal checknode.tags, apinode.tags
235     assert apinode.tags.include?('#{@user.inspect}')
236   end
237
238   def basic_authorization(user, pass)
239     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
240   end
241
242   def content(c)
243     @request.env["RAW_POST_DATA"] = c.to_s
244   end
245
246   ##
247   # update the changeset_id of a node element
248   def update_changeset(xml, changeset_id)
249     xml_attr_rewrite(xml, 'changeset', changeset_id)
250   end
251
252   ##
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
256     return xml
257   end
258
259   ##
260   # parse some xml
261   def xml_parse(xml)
262     parser = XML::Parser.new
263     parser.string = xml
264     parser.parse
265   end
266 end