]> git.openstreetmap.org Git - rails.git/blob - test/functional/node_controller_test.rb
Add a warning about whitelisting webmaster@openstreetmap.org in antispam
[rails.git] / test / functional / node_controller_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class NodeControllerTest < ActionController::TestCase
4   api_fixtures
5
6   def test_create
7     # cannot read password from fixture as it is stored as MD5 digest
8     ## First try with no auth
9     
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>")
17     assert_difference('OldNode.count', 0) do
18       put :create
19     end
20     # hope for unauthorized
21     assert_response :unauthorized, "node upload did not return unauthorized status"
22
23     
24     
25     ## Now try with the user which doesn't have their data public
26     basic_authorization(users(:normal_user).email, "test")
27     
28     # create a node with random lat/lon
29     lat = rand(100)-50 + rand
30     lon = rand(100)-50 + rand
31     # normal user has a changeset open, so we'll use that.
32     changeset = changesets(:normal_user_first_change)
33     # create a minimal xml file
34     content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
35     assert_difference('Node.count', 0) do
36       put :create
37     end
38     # hope for success
39     assert_require_public_data "node create did not return forbidden status"
40
41
42     
43     ## Now try with the user that has the public data
44     basic_authorization(users(:public_user).email, "test")
45     
46     # create a node with random lat/lon
47     lat = rand(100)-50 + rand
48     lon = rand(100)-50 + rand
49     # normal user has a changeset open, so we'll use that.
50     changeset = changesets(:public_user_first_change)
51     # create a minimal xml file
52     content("<osm><node lat='#{lat}' lon='#{lon}' changeset='#{changeset.id}'/></osm>")
53     put :create
54     # hope for success
55     assert_response :success, "node upload did not return success status"
56
57     # read id of created node and search for it
58     nodeid = @response.body
59     checknode = Node.find(nodeid)
60     assert_not_nil checknode, "uploaded node not found in data base after upload"
61     # compare values
62     assert_in_delta lat * 10000000, checknode.latitude, 1, "saved node does not match requested latitude"
63     assert_in_delta lon * 10000000, checknode.longitude, 1, "saved node does not match requested longitude"
64     assert_equal changesets(:public_user_first_change).id, checknode.changeset_id, "saved node does not belong to changeset that it was created in"
65     assert_equal true, checknode.visible, "saved node is not visible"
66   end
67
68   def test_create_invalid_xml
69     ## Only test public user here, as test_create should cover what's the forbiddens
70     ## that would occur here
71     # Initial setup
72     basic_authorization(users(:public_user).email, "test")
73     # normal user has a changeset open, so we'll use that.
74     changeset = changesets(:public_user_first_change)
75     lat = 3.434
76     lon = 3.23
77     
78     # test that the upload is rejected when no lat is supplied
79     # create a minimal xml file
80     content("<osm><node lon='#{lon}' changeset='#{changeset.id}'/></osm>")
81     put :create
82     # hope for success
83     assert_response :bad_request, "node upload did not return bad_request status"
84     assert_equal "Cannot parse valid node from xml string <node lon=\"3.23\" changeset=\"#{changeset.id}\"/>. lat missing", @response.body
85
86     # test that the upload is rejected when no lon is supplied
87     # create a minimal xml file
88     content("<osm><node lat='#{lat}' changeset='#{changeset.id}'/></osm>")
89     put :create
90     # hope for success
91     assert_response :bad_request, "node upload did not return bad_request status"
92     assert_equal "Cannot parse valid node from xml string <node lat=\"3.434\" changeset=\"#{changeset.id}\"/>. lon missing", @response.body
93
94   end
95
96   def test_read
97     # check that a visible node is returned properly
98     get :read, :id => current_nodes(:visible_node).id
99     assert_response :success
100
101     # check that an invisible node is not returned
102     get :read, :id => current_nodes(:invisible_node).id
103     assert_response :gone
104
105     # check chat a non-existent node is not returned
106     get :read, :id => 0
107     assert_response :not_found
108   end
109
110   # this tests deletion restrictions - basic deletion is tested in the unit
111   # tests for node!
112   def test_delete
113     ## first try to delete node without auth
114     delete :delete, :id => current_nodes(:visible_node).id
115     assert_response :unauthorized
116     
117     
118     ## now set auth for the non-data public user
119     basic_authorization(users(:normal_user).email, "test");  
120
121     # try to delete with an invalid (closed) changeset
122     content update_changeset(current_nodes(:visible_node).to_xml,
123                              changesets(:normal_user_closed_change).id)
124     delete :delete, :id => current_nodes(:visible_node).id
125     assert_require_public_data("non-public user shouldn't be able to delete node")
126
127     # try to delete with an invalid (non-existent) changeset
128     content update_changeset(current_nodes(:visible_node).to_xml,0)
129     delete :delete, :id => current_nodes(:visible_node).id
130     assert_require_public_data("shouldn't be able to delete node, when user's data is private")
131
132     # valid delete now takes a payload
133     content(nodes(:visible_node).to_xml)
134     delete :delete, :id => current_nodes(:visible_node).id
135     assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
136
137     # this won't work since the node is already deleted
138     content(nodes(:invisible_node).to_xml)
139     delete :delete, :id => current_nodes(:invisible_node).id
140     assert_require_public_data
141
142     # this won't work since the node never existed
143     delete :delete, :id => 0
144     assert_require_public_data
145
146     ## these test whether nodes which are in-use can be deleted:
147     # in a way...
148     content(nodes(:used_node_1).to_xml)
149     delete :delete, :id => current_nodes(:used_node_1).id
150     assert_require_public_data
151        "shouldn't be able to delete a node used in a way (#{@response.body})"
152
153     # in a relation...
154     content(nodes(:node_used_by_relationship).to_xml)
155     delete :delete, :id => current_nodes(:node_used_by_relationship).id
156     assert_require_public_data
157        "shouldn't be able to delete a node used in a relation (#{@response.body})"
158
159     
160
161     ## now set auth for the public data user
162     basic_authorization(users(:public_user).email, "test");  
163
164     # try to delete with an invalid (closed) changeset
165     content update_changeset(current_nodes(:visible_node).to_xml,
166                              changesets(:normal_user_closed_change).id)
167     delete :delete, :id => current_nodes(:visible_node).id
168     assert_response :conflict
169
170     # try to delete with an invalid (non-existent) changeset
171     content update_changeset(current_nodes(:visible_node).to_xml,0)
172     delete :delete, :id => current_nodes(:visible_node).id
173     assert_response :conflict
174
175     # valid delete now takes a payload
176     content(nodes(:public_visible_node).to_xml)
177     delete :delete, :id => current_nodes(:public_visible_node).id
178     assert_response :success
179
180     # valid delete should return the new version number, which should
181     # be greater than the old version number
182     assert @response.body.to_i > current_nodes(:public_visible_node).version,
183        "delete request should return a new version number for node"
184
185     # this won't work since the node is already deleted
186     content(nodes(:invisible_node).to_xml)
187     delete :delete, :id => current_nodes(:invisible_node).id
188     assert_response :gone
189
190     # this won't work since the node never existed
191     delete :delete, :id => 0
192     assert_response :not_found
193
194     ## these test whether nodes which are in-use can be deleted:
195     # in a way...
196     content(nodes(:used_node_1).to_xml)
197     delete :delete, :id => current_nodes(:used_node_1).id
198     assert_response :precondition_failed,
199        "shouldn't be able to delete a node used in a way (#{@response.body})"
200     assert_equal "Precondition failed: Node 3 is still used by way 1.", @response.body
201
202     # in a relation...
203     content(nodes(:node_used_by_relationship).to_xml)
204     delete :delete, :id => current_nodes(:node_used_by_relationship).id
205     assert_response :precondition_failed,
206        "shouldn't be able to delete a node used in a relation (#{@response.body})"
207     assert_equal "Precondition failed: Node 5 is still used by relation 3.", @response.body
208   end
209
210   ##
211   # tests whether the API works and prevents incorrect use while trying
212   # to update nodes.
213   def test_update
214     ## First test with no user credentials
215     # try and update a node without authorisation
216     # first try to delete node without auth
217     content current_nodes(:visible_node).to_xml
218     put :update, :id => current_nodes(:visible_node).id
219     assert_response :unauthorized
220     
221     
222     
223     ## Second test with the private user
224     
225     # setup auth
226     basic_authorization(users(:normal_user).email, "test")
227
228     ## trying to break changesets
229
230     # try and update in someone else's changeset
231     content update_changeset(current_nodes(:visible_node).to_xml,
232                              changesets(:public_user_first_change).id)
233     put :update, :id => current_nodes(:visible_node).id
234     assert_require_public_data "update with other user's changeset should be forbidden when date isn't public"
235
236     # try and update in a closed changeset
237     content update_changeset(current_nodes(:visible_node).to_xml,
238                              changesets(:normal_user_closed_change).id)
239     put :update, :id => current_nodes(:visible_node).id
240     assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
241
242     # try and update in a non-existant changeset
243     content update_changeset(current_nodes(:visible_node).to_xml, 0)
244     put :update, :id => current_nodes(:visible_node).id
245     assert_require_public_data("update with changeset=0 should be forbidden, when data isn't public")
246
247     ## try and submit invalid updates
248     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
249     put :update, :id => current_nodes(:visible_node).id
250     assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
251
252     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
253     put :update, :id => current_nodes(:visible_node).id
254     assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
255     
256     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
257     put :update, :id => current_nodes(:visible_node).id
258     assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
259
260     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
261     put :update, :id => current_nodes(:visible_node).id
262     assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
263     
264     ## finally, produce a good request which should work
265     content current_nodes(:visible_node).to_xml
266     put :update, :id => current_nodes(:visible_node).id
267     assert_require_public_data "should have failed with a forbidden when data isn't public"
268
269     
270     
271     
272     ## Finally test with the public user
273     
274     # try and update a node without authorisation
275     # first try to delete node without auth
276     content current_nodes(:visible_node).to_xml
277     put :update, :id => current_nodes(:visible_node).id
278     assert_response :forbidden
279     
280     # setup auth
281     basic_authorization(users(:public_user).email, "test")
282
283     ## trying to break changesets
284
285     # try and update in someone else's changeset
286     content update_changeset(current_nodes(:visible_node).to_xml,
287                               changesets(:normal_user_first_change).id)
288     put :update, :id => current_nodes(:visible_node).id
289     assert_response :conflict, "update with other user's changeset should be rejected"
290
291     # try and update in a closed changeset
292     content update_changeset(current_nodes(:visible_node).to_xml,
293                              changesets(:normal_user_closed_change).id)
294     put :update, :id => current_nodes(:visible_node).id
295     assert_response :conflict, "update with closed changeset should be rejected"
296
297     # try and update in a non-existant changeset
298     content update_changeset(current_nodes(:visible_node).to_xml, 0)
299     put :update, :id => current_nodes(:visible_node).id
300     assert_response :conflict, "update with changeset=0 should be rejected"
301
302     ## try and submit invalid updates
303     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', 91.0);
304     put :update, :id => current_nodes(:visible_node).id
305     assert_response :bad_request, "node at lat=91 should be rejected"
306
307     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lat', -91.0);
308     put :update, :id => current_nodes(:visible_node).id
309     assert_response :bad_request, "node at lat=-91 should be rejected"
310     
311     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', 181.0);
312     put :update, :id => current_nodes(:visible_node).id
313     assert_response :bad_request, "node at lon=181 should be rejected"
314
315     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 'lon', -181.0);
316     put :update, :id => current_nodes(:visible_node).id
317     assert_response :bad_request, "node at lon=-181 should be rejected"
318
319     ## next, attack the versioning
320     current_node_version = current_nodes(:visible_node).version
321
322     # try and submit a version behind
323     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 
324                              'version', current_node_version - 1);
325     put :update, :id => current_nodes(:visible_node).id
326     assert_response :conflict, "should have failed on old version number"
327     
328     # try and submit a version ahead
329     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 
330                              'version', current_node_version + 1);
331     put :update, :id => current_nodes(:visible_node).id
332     assert_response :conflict, "should have failed on skipped version number"
333
334     # try and submit total crap in the version field
335     content xml_attr_rewrite(current_nodes(:visible_node).to_xml, 
336                              'version', 'p1r4t3s!');
337     put :update, :id => current_nodes(:visible_node).id
338     assert_response :conflict, 
339        "should not be able to put 'p1r4at3s!' in the version field"
340     
341     ## finally, produce a good request which should work
342     content current_nodes(:public_visible_node).to_xml
343     put :update, :id => current_nodes(:public_visible_node).id
344     assert_response :success, "a valid update request failed"
345   end
346
347   ##
348   # test adding tags to a node
349   def test_duplicate_tags
350     # setup auth
351     basic_authorization(users(:public_user).email, "test")
352
353     # add an identical tag to the node
354     tag_xml = XML::Node.new("tag")
355     tag_xml['k'] = current_node_tags(:public_v_t1).k
356     tag_xml['v'] = current_node_tags(:public_v_t1).v
357
358     # add the tag into the existing xml
359     node_xml = current_nodes(:public_visible_node).to_xml
360     node_xml.find("//osm/node").first << tag_xml
361
362     # try and upload it
363     content node_xml
364     put :update, :id => current_nodes(:public_visible_node).id
365     assert_response :bad_request, 
366       "adding duplicate tags to a node should fail with 'bad request'"
367     assert_equal "Element node/#{current_nodes(:public_visible_node).id} has duplicate tags with key #{current_node_tags(:t1).k}.", @response.body
368   end
369
370   # test whether string injection is possible
371   def test_string_injection
372     ## First try with the non-data public user
373     basic_authorization(users(:normal_user).email, "test")
374     changeset_id = changesets(:normal_user_first_change).id
375
376     # try and put something into a string that the API might 
377     # use unquoted and therefore allow code injection...
378     content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
379       '<tag k="#{@user.inspect}" v="0"/>' +
380       '</node></osm>'
381     put :create
382     assert_require_public_data "Shouldn't be able to create with non-public user"
383     
384     
385     ## Then try with the public data user
386     basic_authorization(users(:public_user).email, "test")
387     changeset_id = changesets(:public_user_first_change).id
388
389     # try and put something into a string that the API might 
390     # use unquoted and therefore allow code injection...
391     content "<osm><node lat='0' lon='0' changeset='#{changeset_id}'>" +
392       '<tag k="#{@user.inspect}" v="0"/>' +
393       '</node></osm>'
394     put :create
395     assert_response :success
396     nodeid = @response.body
397
398     # find the node in the database
399     checknode = Node.find(nodeid)
400     assert_not_nil checknode, "node not found in data base after upload"
401     
402     # and grab it using the api
403     get :read, :id => nodeid
404     assert_response :success
405     apinode = Node.from_xml(@response.body)
406     assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
407     
408     # check the tags are not corrupted
409     assert_equal checknode.tags, apinode.tags
410     assert apinode.tags.include?('#{@user.inspect}')
411   end
412
413   def basic_authorization(user, pass)
414     @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
415   end
416
417   def content(c)
418     @request.env["RAW_POST_DATA"] = c.to_s
419   end
420
421   ##
422   # update the changeset_id of a node element
423   def update_changeset(xml, changeset_id)
424     xml_attr_rewrite(xml, 'changeset', changeset_id)
425   end
426
427   ##
428   # update an attribute in the node element
429   def xml_attr_rewrite(xml, name, value)
430     xml.find("//osm/node").first[name] = value.to_s
431     return xml
432   end
433
434   ##
435   # parse some xml
436   def xml_parse(xml)
437     parser = XML::Parser.string(xml)
438     parser.parse
439   end
440 end