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