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