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