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