]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/nodes_controller_test.rb
Merge branch 'master' into tag-colour-preview-rebase
[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     # this tests deletion restrictions - basic deletion is tested in the unit
151     # tests for node!
152     def test_delete
153       private_user = create(:user, :data_public => false)
154       private_user_changeset = create(:changeset, :user => private_user)
155       private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
156       private_node = create(:node, :changeset => private_user_changeset)
157       private_deleted_node = create(:node, :deleted, :changeset => private_user_changeset)
158
159       ## first try to delete node without auth
160       delete :delete, :params => { :id => private_node.id }
161       assert_response :unauthorized
162
163       ## now set auth for the non-data public user
164       basic_authorization private_user.email, "test"
165
166       # try to delete with an invalid (closed) changeset
167       xml = update_changeset(private_node.to_xml, private_user_closed_changeset.id)
168       delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
169       assert_require_public_data("non-public user shouldn't be able to delete node")
170
171       # try to delete with an invalid (non-existent) changeset
172       xml = update_changeset(private_node.to_xml, 0)
173       delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
174       assert_require_public_data("shouldn't be able to delete node, when user's data is private")
175
176       # valid delete now takes a payload
177       xml = private_node.to_xml
178       delete :delete, :params => { :id => private_node.id }, :body => xml.to_s
179       assert_require_public_data("shouldn't be able to delete node when user's data isn't public'")
180
181       # this won't work since the node is already deleted
182       xml = private_deleted_node.to_xml
183       delete :delete, :params => { :id => private_deleted_node.id }, :body => xml.to_s
184       assert_require_public_data
185
186       # this won't work since the node never existed
187       delete :delete, :params => { :id => 0 }
188       assert_require_public_data
189
190       ## these test whether nodes which are in-use can be deleted:
191       # in a way...
192       private_used_node = create(:node, :changeset => private_user_changeset)
193       create(:way_node, :node => private_used_node)
194
195       xml = private_used_node.to_xml
196       delete :delete, :params => { :id => private_used_node.id }, :body => xml.to_s
197       assert_require_public_data "shouldn't be able to delete a node used in a way (#{@response.body})"
198
199       # in a relation...
200       private_used_node2 = create(:node, :changeset => private_user_changeset)
201       create(:relation_member, :member => private_used_node2)
202
203       xml = private_used_node2.to_xml
204       delete :delete, :params => { :id => private_used_node2.id }, :body => xml.to_s
205       assert_require_public_data "shouldn't be able to delete a node used in a relation (#{@response.body})"
206
207       ## now setup for the public data user
208       user = create(:user, :data_public => true)
209       changeset = create(:changeset, :user => user)
210       closed_changeset = create(:changeset, :closed, :user => user)
211       node = create(:node, :changeset => changeset)
212       basic_authorization user.email, "test"
213
214       # try to delete with an invalid (closed) changeset
215       xml = update_changeset(node.to_xml, closed_changeset.id)
216       delete :delete, :params => { :id => node.id }, :body => xml.to_s
217       assert_response :conflict
218
219       # try to delete with an invalid (non-existent) changeset
220       xml = update_changeset(node.to_xml, 0)
221       delete :delete, :params => { :id => node.id }, :body => xml.to_s
222       assert_response :conflict
223
224       # try to delete a node with a different ID
225       other_node = create(:node)
226       xml = other_node.to_xml
227       delete :delete, :params => { :id => node.id }, :body => xml.to_s
228       assert_response :bad_request,
229                       "should not be able to delete a node with a different ID from the XML"
230
231       # try to delete a node rubbish in the payloads
232       xml = "<delete/>"
233       delete :delete, :params => { :id => node.id }, :body => xml.to_s
234       assert_response :bad_request,
235                       "should not be able to delete a node without a valid XML payload"
236
237       # valid delete now takes a payload
238       xml = node.to_xml
239       delete :delete, :params => { :id => node.id }, :body => xml.to_s
240       assert_response :success
241
242       # valid delete should return the new version number, which should
243       # be greater than the old version number
244       assert @response.body.to_i > node.version,
245              "delete request should return a new version number for node"
246
247       # deleting the same node twice doesn't work
248       xml = node.to_xml
249       delete :delete, :params => { :id => node.id }, :body => xml.to_s
250       assert_response :gone
251
252       # this won't work since the node never existed
253       delete :delete, :params => { :id => 0 }
254       assert_response :not_found
255
256       ## these test whether nodes which are in-use can be deleted:
257       # in a way...
258       used_node = create(:node, :changeset => create(:changeset, :user => user))
259       way_node = create(:way_node, :node => used_node)
260       way_node2 = create(:way_node, :node => used_node)
261
262       xml = used_node.to_xml
263       delete :delete, :params => { :id => used_node.id }, :body => xml.to_s
264       assert_response :precondition_failed,
265                       "shouldn't be able to delete a node used in a way (#{@response.body})"
266       assert_equal "Precondition failed: Node #{used_node.id} is still used by ways #{way_node.way.id},#{way_node2.way.id}.", @response.body
267
268       # in a relation...
269       used_node2 = create(:node, :changeset => create(:changeset, :user => user))
270       relation_member = create(:relation_member, :member => used_node2)
271       relation_member2 = create(:relation_member, :member => used_node2)
272
273       xml = used_node2.to_xml
274       delete :delete, :params => { :id => used_node2.id }, :body => xml.to_s
275       assert_response :precondition_failed,
276                       "shouldn't be able to delete a node used in a relation (#{@response.body})"
277       assert_equal "Precondition failed: Node #{used_node2.id} is still used by relations #{relation_member.relation.id},#{relation_member2.relation.id}.", @response.body
278     end
279
280     ##
281     # tests whether the API works and prevents incorrect use while trying
282     # to update nodes.
283     def test_update
284       ## First test with no user credentials
285       # try and update a node without authorisation
286       # first try to delete node without auth
287       private_user = create(:user, :data_public => false)
288       private_node = create(:node, :changeset => create(:changeset, :user => private_user))
289       user = create(:user)
290       node = create(:node, :changeset => create(:changeset, :user => user))
291
292       xml = node.to_xml
293       put :update, :params => { :id => node.id }, :body => xml.to_s
294       assert_response :unauthorized
295
296       ## Second test with the private user
297
298       # setup auth
299       basic_authorization private_user.email, "test"
300
301       ## trying to break changesets
302
303       # try and update in someone else's changeset
304       xml = update_changeset(private_node.to_xml,
305                              create(:changeset).id)
306       put :update, :params => { :id => private_node.id }, :body => xml.to_s
307       assert_require_public_data "update with other user's changeset should be forbidden when data isn't public"
308
309       # try and update in a closed changeset
310       xml = update_changeset(private_node.to_xml,
311                              create(:changeset, :closed, :user => private_user).id)
312       put :update, :params => { :id => private_node.id }, :body => xml.to_s
313       assert_require_public_data "update with closed changeset should be forbidden, when data isn't public"
314
315       # try and update in a non-existant changeset
316       xml = update_changeset(private_node.to_xml, 0)
317       put :update, :params => { :id => private_node.id }, :body => xml.to_s
318       assert_require_public_data "update with changeset=0 should be forbidden, when data isn't public"
319
320       ## try and submit invalid updates
321       xml = xml_attr_rewrite(private_node.to_xml, "lat", 91.0)
322       put :update, :params => { :id => private_node.id }, :body => xml.to_s
323       assert_require_public_data "node at lat=91 should be forbidden, when data isn't public"
324
325       xml = xml_attr_rewrite(private_node.to_xml, "lat", -91.0)
326       put :update, :params => { :id => private_node.id }, :body => xml.to_s
327       assert_require_public_data "node at lat=-91 should be forbidden, when data isn't public"
328
329       xml = xml_attr_rewrite(private_node.to_xml, "lon", 181.0)
330       put :update, :params => { :id => private_node.id }, :body => xml.to_s
331       assert_require_public_data "node at lon=181 should be forbidden, when data isn't public"
332
333       xml = xml_attr_rewrite(private_node.to_xml, "lon", -181.0)
334       put :update, :params => { :id => private_node.id }, :body => xml.to_s
335       assert_require_public_data "node at lon=-181 should be forbidden, when data isn't public"
336
337       ## finally, produce a good request which still won't work
338       xml = private_node.to_xml
339       put :update, :params => { :id => private_node.id }, :body => xml.to_s
340       assert_require_public_data "should have failed with a forbidden when data isn't public"
341
342       ## Finally test with the public user
343
344       # try and update a node without authorisation
345       # first try to update node without auth
346       xml = node.to_xml
347       put :update, :params => { :id => node.id }, :body => xml.to_s
348       assert_response :forbidden
349
350       # setup auth
351       basic_authorization user.email, "test"
352
353       ## trying to break changesets
354
355       # try and update in someone else's changeset
356       xml = update_changeset(node.to_xml,
357                              create(:changeset).id)
358       put :update, :params => { :id => node.id }, :body => xml.to_s
359       assert_response :conflict, "update with other user's changeset should be rejected"
360
361       # try and update in a closed changeset
362       xml = update_changeset(node.to_xml,
363                              create(:changeset, :closed, :user => user).id)
364       put :update, :params => { :id => node.id }, :body => xml.to_s
365       assert_response :conflict, "update with closed changeset should be rejected"
366
367       # try and update in a non-existant changeset
368       xml = update_changeset(node.to_xml, 0)
369       put :update, :params => { :id => node.id }, :body => xml.to_s
370       assert_response :conflict, "update with changeset=0 should be rejected"
371
372       ## try and submit invalid updates
373       xml = xml_attr_rewrite(node.to_xml, "lat", 91.0)
374       put :update, :params => { :id => node.id }, :body => xml.to_s
375       assert_response :bad_request, "node at lat=91 should be rejected"
376
377       xml = xml_attr_rewrite(node.to_xml, "lat", -91.0)
378       put :update, :params => { :id => node.id }, :body => xml.to_s
379       assert_response :bad_request, "node at lat=-91 should be rejected"
380
381       xml = xml_attr_rewrite(node.to_xml, "lon", 181.0)
382       put :update, :params => { :id => node.id }, :body => xml.to_s
383       assert_response :bad_request, "node at lon=181 should be rejected"
384
385       xml = xml_attr_rewrite(node.to_xml, "lon", -181.0)
386       put :update, :params => { :id => node.id }, :body => xml.to_s
387       assert_response :bad_request, "node at lon=-181 should be rejected"
388
389       ## next, attack the versioning
390       current_node_version = node.version
391
392       # try and submit a version behind
393       xml = xml_attr_rewrite(node.to_xml,
394                              "version", current_node_version - 1)
395       put :update, :params => { :id => node.id }, :body => xml.to_s
396       assert_response :conflict, "should have failed on old version number"
397
398       # try and submit a version ahead
399       xml = xml_attr_rewrite(node.to_xml,
400                              "version", current_node_version + 1)
401       put :update, :params => { :id => node.id }, :body => xml.to_s
402       assert_response :conflict, "should have failed on skipped version number"
403
404       # try and submit total crap in the version field
405       xml = xml_attr_rewrite(node.to_xml,
406                              "version", "p1r4t3s!")
407       put :update, :params => { :id => node.id }, :body => xml.to_s
408       assert_response :conflict,
409                       "should not be able to put 'p1r4at3s!' in the version field"
410
411       ## try an update with the wrong ID
412       xml = create(:node).to_xml
413       put :update, :params => { :id => node.id }, :body => xml.to_s
414       assert_response :bad_request,
415                       "should not be able to update a node with a different ID from the XML"
416
417       ## try an update with a minimal valid XML doc which isn't a well-formed OSM doc.
418       xml = "<update/>"
419       put :update, :params => { :id => node.id }, :body => xml.to_s
420       assert_response :bad_request,
421                       "should not be able to update a node with non-OSM XML doc."
422
423       ## finally, produce a good request which should work
424       xml = node.to_xml
425       put :update, :params => { :id => node.id }, :body => xml.to_s
426       assert_response :success, "a valid update request failed"
427     end
428
429     ##
430     # test fetching multiple nodes
431     def test_index
432       node1 = create(:node)
433       node2 = create(:node, :deleted)
434       node3 = create(:node)
435       node4 = create(:node, :with_history, :version => 2)
436       node5 = create(:node, :deleted, :with_history, :version => 2)
437
438       # check error when no parameter provided
439       get :index
440       assert_response :bad_request
441
442       # check error when no parameter value provided
443       get :index, :params => { :nodes => "" }
444       assert_response :bad_request
445
446       # test a working call
447       get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}" }
448       assert_response :success
449       assert_select "osm" do
450         assert_select "node", :count => 5
451         assert_select "node[id='#{node1.id}'][visible='true']", :count => 1
452         assert_select "node[id='#{node2.id}'][visible='false']", :count => 1
453         assert_select "node[id='#{node3.id}'][visible='true']", :count => 1
454         assert_select "node[id='#{node4.id}'][visible='true']", :count => 1
455         assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
456       end
457
458       # check error when a non-existent node is included
459       get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0" }
460       assert_response :not_found
461     end
462
463     ##
464     # test adding tags to a node
465     def test_duplicate_tags
466       existing_tag = create(:node_tag)
467       assert_equal true, existing_tag.node.changeset.user.data_public
468       # setup auth
469       basic_authorization existing_tag.node.changeset.user.email, "test"
470
471       # add an identical tag to the node
472       tag_xml = XML::Node.new("tag")
473       tag_xml["k"] = existing_tag.k
474       tag_xml["v"] = existing_tag.v
475
476       # add the tag into the existing xml
477       node_xml = existing_tag.node.to_xml
478       node_xml.find("//osm/node").first << tag_xml
479
480       # try and upload it
481       put :update, :params => { :id => existing_tag.node.id }, :body => node_xml.to_s
482       assert_response :bad_request,
483                       "adding duplicate tags to a node should fail with 'bad request'"
484       assert_equal "Element node/#{existing_tag.node.id} has duplicate tags with key #{existing_tag.k}", @response.body
485     end
486
487     # test whether string injection is possible
488     def test_string_injection
489       private_user = create(:user, :data_public => false)
490       private_changeset = create(:changeset, :user => private_user)
491       user = create(:user)
492       changeset = create(:changeset, :user => user)
493
494       ## First try with the non-data public user
495       basic_authorization private_user.email, "test"
496
497       # try and put something into a string that the API might
498       # use unquoted and therefore allow code injection...
499       xml = "<osm><node lat='0' lon='0' changeset='#{private_changeset.id}'>" \
500             '<tag k="#{@user.inspect}" v="0"/>' \
501             "</node></osm>"
502       put :create, :body => xml
503       assert_require_public_data "Shouldn't be able to create with non-public user"
504
505       ## Then try with the public data user
506       basic_authorization user.email, "test"
507
508       # try and put something into a string that the API might
509       # use unquoted and therefore allow code injection...
510       xml = "<osm><node lat='0' lon='0' changeset='#{changeset.id}'>" \
511             '<tag k="#{@user.inspect}" v="0"/>' \
512             "</node></osm>"
513       put :create, :body => xml
514       assert_response :success
515       nodeid = @response.body
516
517       # find the node in the database
518       checknode = Node.find(nodeid)
519       assert_not_nil checknode, "node not found in data base after upload"
520
521       # and grab it using the api
522       get :show, :params => { :id => nodeid }
523       assert_response :success
524       apinode = Node.from_xml(@response.body)
525       assert_not_nil apinode, "downloaded node is nil, but shouldn't be"
526
527       # check the tags are not corrupted
528       assert_equal checknode.tags, apinode.tags
529       assert apinode.tags.include?("\#{@user.inspect}")
530     end
531
532     ##
533     # update the changeset_id of a node element
534     def update_changeset(xml, changeset_id)
535       xml_attr_rewrite(xml, "changeset", changeset_id)
536     end
537
538     ##
539     # update an attribute in the node element
540     def xml_attr_rewrite(xml, name, value)
541       xml.find("//osm/node").first[name] = value.to_s
542       xml
543     end
544
545     ##
546     # parse some xml
547     def xml_parse(xml)
548       parser = XML::Parser.string(xml)
549       parser.parse
550     end
551   end
552 end